Skip to content

Commit

Permalink
compelete Aggregation and Composition Relation of classdiagram
Browse files Browse the repository at this point in the history
  • Loading branch information
parisa-hr committed Nov 26, 2021
1 parent 6cf0859 commit 940031f
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 12 deletions.
22 changes: 20 additions & 2 deletions src/Base/diagramscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QGraphicsSceneMouseEvent>
#include <QPolygonF>
#include <QPointF>
#include <src/Diagrams/Relations/aggregation.h>
#include <src/Diagrams/Relations/composition.h>
#include <src/Diagrams/Relations/directassosiation.h>
#include <src/commonItems/arrow.h>
#include <src/commonItems/communicationpath.h>
Expand Down Expand Up @@ -140,14 +142,30 @@ void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)

break;
case _Aggregation:
{
Aggregation *_aggregation = new Aggregation(_p2, _p1);
cmd->setItem(_aggregation);

break;
ObjectKeeper::instance()->createCommand(cmd);
addItem(_aggregation);
update();
}

break;
case _Assosiation:

break;
case _Composition:
{
Composition *_composition = new Composition(_p2, _p1);
cmd->setItem(_composition);

break;
ObjectKeeper::instance()->createCommand(cmd);
addItem(_composition);
update();
}

break;
case _Dependency:

break;
Expand Down
11 changes: 11 additions & 0 deletions src/Diagrams/Class/classdiagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ ClassDiagram::ClassDiagram()
"<html><head/><body><p style=\"background-color:white\"><font face=\"Times New Roman\" color=\"dark blue\">Composition</font></p></body></html>",
nullptr));

connect(act7, &QAction::triggered, this, []()
{
DiagramScene::instance()->setRelation(DiagramScene::_Composition);
});

menuBar()->addToolButton(act7);


Expand All @@ -149,6 +154,12 @@ ClassDiagram::ClassDiagram()
"<html><head/><body><p style=\"background-color:white\"><font face=\"Times New Roman\" color=\"dark blue\">Aggregation</font></p></body></html>",
nullptr));

connect(act8, &QAction::triggered, this, []()
{
DiagramScene::instance()->setRelation(DiagramScene::_Aggregation);
});


menuBar()->addToolButton(act8);

cmd = new ShapeCommand();
Expand Down
71 changes: 70 additions & 1 deletion src/Diagrams/Relations/aggregation.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,75 @@
#include "aggregation.h"

Aggregation::Aggregation(QObject *parent) : QObject(parent)
#include <QPainter>
#include <QPen>
#include <QtMath>
#include <QDebug>

Aggregation::Aggregation(QPointF startItem, QPointF endItem, QGraphicsItem *parent):
myStartItem(startItem), myEndItem(endItem)
{
setAcceptHoverEvents(true);
setZValue(105);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsFocusable);
}

QRectF Aggregation::boundingRect() const
{
return QRectF(myStartItem, myEndItem);
}

QPainterPath Aggregation::shape() const
{
QPainterPath path;

path.addRect(boundingRect());

return path;
}

void Aggregation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);

QPen myPen;

myPen.setColor(myColor);
myPen.setWidth(2);
myPen.setCosmetic(true);
painter->setPen(myPen);
painter->setBrush(myColor);

painter->save();

painter->setRenderHints(QPainter::Antialiasing);

painter->setBrush(Qt::NoBrush);

painter->drawLine(myStartItem, myEndItem);
painter->restore();

double angle = std::atan2(-(myEndItem.y() - myStartItem.y()), (myEndItem.x() - myStartItem.x()));
QPointF arrowP1 = myStartItem + QPointF(sin(angle + M_PI / 3) * 20,
cos(angle + M_PI / 3) * 20);
QPointF arrowP2 = myStartItem + QPointF(sin(angle + M_PI - M_PI / 3) * 20,
cos(angle + M_PI - M_PI / 3) * 20);

arrowHead.clear();

painter->save();
painter->setBrush(Qt::white);
QLineF tmp(myStartItem, myEndItem);
tmp.setLength(35);
auto test_point = tmp.p2();

arrowHead << myStartItem << arrowP1 << test_point << arrowP2;
painter->drawPolygon(arrowHead);
painter->restore();
}

void Aggregation::setMyColor(const QColor &value)
{
myColor = value;
}
20 changes: 16 additions & 4 deletions src/Diagrams/Relations/aggregation.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
#define AGGREGATION_H

#include <QObject>
#include <QGraphicsItem>

class Aggregation : public QObject
class Aggregation: public QGraphicsItem
{
Q_OBJECT
public:
explicit Aggregation(QObject *parent = nullptr);
explicit Aggregation(QPointF startItem, QPointF endItem,
QGraphicsItem *parent = nullptr);

signals:
QRectF boundingRect() const;

QPainterPath shape() const override;

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

void setMyColor(const QColor &value);

private:
QPointF myStartItem;
QPointF myEndItem;
QPolygonF arrowHead;
QColor myColor = Qt::black;
};

#endif // AGGREGATION_H
67 changes: 66 additions & 1 deletion src/Diagrams/Relations/composition.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,71 @@
#include "composition.h"
#include <QPainter>
#include <QPen>
#include <QtMath>
#include <QDebug>

Composition::Composition(QObject *parent) : QObject(parent)
Composition::Composition(QPointF startItem, QPointF endItem, QGraphicsItem *parent):
myStartItem(startItem), myEndItem(endItem)
{
setAcceptHoverEvents(true);
setZValue(105);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsFocusable);
}

QRectF Composition::boundingRect() const
{
return QRectF(myStartItem, myEndItem);
}

QPainterPath Composition::shape() const
{
QPainterPath path;

path.addRect(boundingRect());

return path;
}

void Composition::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);

QPen myPen;

myPen.setColor(myColor);
myPen.setWidth(2);
myPen.setCosmetic(true);
painter->setPen(myPen);
painter->setBrush(myColor);

painter->save();

painter->setRenderHints(QPainter::Antialiasing);

painter->setBrush(Qt::NoBrush);

painter->drawLine(myStartItem, myEndItem);
painter->restore();

double angle = std::atan2(-(myEndItem.y() - myStartItem.y()), (myEndItem.x() - myStartItem.x()));
QPointF arrowP1 = myStartItem + QPointF(sin(angle + M_PI / 3) * 20,
cos(angle + M_PI / 3) * 20);
QPointF arrowP2 = myStartItem + QPointF(sin(angle + M_PI - M_PI / 3) * 20,
cos(angle + M_PI - M_PI / 3) * 20);

arrowHead.clear();

QLineF tmp(myStartItem, myEndItem);
tmp.setLength(35);
auto test_point = tmp.p2();

arrowHead << myStartItem << arrowP1 << test_point << arrowP2;
painter->drawPolygon(arrowHead);
}

void Composition::setMyColor(const QColor &value)
{
myColor = value;
}
20 changes: 16 additions & 4 deletions src/Diagrams/Relations/composition.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
#define COMPOSITION_H

#include <QObject>
#include <QGraphicsItem>

class Composition : public QObject
class Composition: public QGraphicsItem
{
Q_OBJECT
public:
explicit Composition(QObject *parent = nullptr);
explicit Composition(QPointF startItem, QPointF endItem,
QGraphicsItem *parent = nullptr);

signals:
QRectF boundingRect() const;

QPainterPath shape() const override;

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

void setMyColor(const QColor &value);

private:
QPointF myStartItem;
QPointF myEndItem;
QPolygonF arrowHead;
QColor myColor = Qt::black;
};

#endif // COMPOSITION_H

0 comments on commit 940031f

Please sign in to comment.