forked from liamnegron3/COP3530_Project3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeciesnode.cpp
More file actions
132 lines (106 loc) · 3.98 KB
/
Copy pathspeciesnode.cpp
File metadata and controls
132 lines (106 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "speciesnode.h"
SpeciesNode::SpeciesNode()
{
pressed = false;
setFlag(ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
commonName = "";
scientificName = "";
}
SpeciesNode::SpeciesNode(QString _commonName, QString _scientificName)
{
pressed = false;
setFlag(ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
commonName = _commonName;
scientificName = _scientificName;
}
void SpeciesNode::addEdge(QGraphicsLineItem *line,QGraphicsPolygonItem *arrowItem, bool _isParent)
{
//this->line = line;
lines.push_back(line);
arrowItems.push_back(arrowItem);
isParent.push_back(_isParent);
}
QVariant SpeciesNode::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPointF newPosition = value.toPointF();
for(unsigned int i = 0; i < lines.size(); i++)
moveLineToCenter(newPosition,lines[i],arrowItems[i],isParent[i]);
}
return QGraphicsItem::itemChange(change, value);
}
void SpeciesNode::moveLineToCenter(QPointF newPosition,QGraphicsLineItem *line,QGraphicsPolygonItem *arrowItem,bool isParent) {
// to its center position
int dx = boundingRect().x() + boundingRect().width()/2;
int dy = boundingRect().y() + boundingRect().width()/2;
QPointF newEllipsePosition = QPointF(newPosition.x() + dx, newPosition.y() + boundingRect().y());
// Move the required point of the line to the center of the elipse
QPointF parent,child;
//checks if the ellipse is the parent or the child and
//changes the point accordingly
if(isParent)
{
parent = newEllipsePosition;
}
else
{
parent = line->line().p1();
}
if(isParent)
{
child = line->line().p2();
}
else
{
child = QPointF(newPosition.x() + dx, newPosition.y() + boundingRect().y() + boundingRect().height());
//if you want center position QPointF(newPosition.x() + dx, newPosition.y() + dy);
}
line->setLine(QLineF(parent,child));
//from Qt docs: https://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html#arrow-class-definition
qreal arrowSize = 20; // size of head
double angle = std::atan2(-line->line().dy(), line->line().dx());
QPointF arrowP1 = line->line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = line->line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
QPolygonF arrowHead;
arrowHead.clear();
arrowHead << line->line().p1() << arrowP1 << arrowP2;
//generate arrowhead
arrowItem->setPolygon(arrowHead);
//fill arrowhead
QBrush brush(Qt::lightGray);
brush.setStyle(Qt::SolidPattern);
arrowItem->setBrush(brush);
}
QRectF SpeciesNode::boundingRect() const
{
return QRectF (0,0,175,100);
}
void SpeciesNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QGraphicsEllipseItem ellipse = QGraphicsEllipseItem(boundingRect());
QBrush brush(Qt::lightGray);
brush.setStyle(Qt::SolidPattern);
painter->setBrush(brush);
painter->drawEllipse(ellipse.rect());
QString name = commonName + QString("\n") + scientificName;
painter->drawText(ellipse.rect(),Qt::AlignCenter, name);
}
void SpeciesNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
pressed = true;
update();
QGraphicsItem::mousePressEvent(event);
}
void SpeciesNode::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
pressed = true;
update();
QGraphicsItem::mouseReleaseEvent(event);
}