-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphnode.cpp
134 lines (108 loc) · 2.45 KB
/
graphnode.cpp
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
133
134
#include "graphnode.h"
#include <QGraphicsScene>
GraphNode::GraphNode(unsigned _id, qreal x, qreal y, qreal _r)
:
QGraphicsEllipseItem(x - _r, y - _r, _r + _r, _r + _r),
id(_id), label(QString::number(_id)), subscript(""), cx(x), cy(y), r(_r)
{
}
GraphNode::~GraphNode()
{
}
unsigned GraphNode::getId()
{
return id;
}
QPointF GraphNode::getCenter()
{
return QPointF(cx, cy);
}
qreal GraphNode::getRadius()
{
return r;
}
QString GraphNode::getLabel()
{
return label;
}
QString GraphNode::getSubscript()
{
return subscript;
}
void GraphNode::setCenter(const QPointF& pt)
{
cx = pt.x();
cy = pt.y();
QRectF newRect = rect();
newRect.moveLeft(cx - r);
newRect.moveTop(cy - r);
setRect(newRect);
}
void GraphNode::setLabel(QString _label)
{
label = _label;
}
void GraphNode::setSubscript(QString s)
{
subscript = s;
}
void GraphNode::setRadius(qreal _r)
{
r = _r;
qreal d = r + r;
setRect(cx - r, cy - r, d, d);
update();
}
void GraphNode::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
{
QPen pen(Qt::black, 2);
QBrush brush(Qt::red);
painter->setPen(pen);
painter->setBrush(brush);
painter->drawEllipse(rect());
painter->drawText(rect(), Qt::AlignCenter, label);
if (!subscript.isEmpty()) {
QRectF subRect;
subRect.setRect(cx - 1.65*r, cy + 1.15*r, 15, 15);
painter->drawText(subRect, Qt::AlignCenter, subscript);
scene()->update(subRect);
}
}
void GraphNode::moveTo(const QPointF& pt)
{
setCenter(pt);
QListIterator<GraphEdge*> it(destinationEdges);
while (it.hasNext())
it.next()->destinationUpdated();
it = QListIterator<GraphEdge*>(sourceEdges);
while (it.hasNext()) {
GraphEdge* edge = it.next();
// The undirected edges were already updated because the node is both source and destination
if (!edge->isUndirected())
edge->sourceUpdated();
}
}
QList<GraphEdge*>& GraphNode::getSourceEdges()
{
return sourceEdges;
}
QList<GraphEdge*>& GraphNode::getDestinationEdges()
{
return destinationEdges;
}
void GraphNode::addSourceEdge(GraphEdge* e)
{
sourceEdges.append(e);
}
void GraphNode::addDestinationEdge(GraphEdge* e)
{
destinationEdges.append(e);
}
void GraphNode::removeSourceEdge(GraphEdge* e)
{
sourceEdges.removeOne(e);
}
void GraphNode::removeDestinationEdge(GraphEdge* e)
{
destinationEdges.removeOne(e);
}