-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.cpp
137 lines (121 loc) · 3.3 KB
/
node.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
135
136
137
#include "edge.h"
#include "node.h"
#include "graphwidget.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOption>
#include <QFont>
Node::Node(GraphWidget *graphWidget, int i)
: graph(graphWidget), tag(i), active(-1), explored(false)
{
setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
}
void Node::addEdge(Edge *edge)
{
edgeList << edge;
edge->adjust();
}
void Node::addIncoming(Edge *edge)
{
incomingEdgesList << edge;
edge->adjust();
}
QList<Edge *> Node::edges() const
{
return edgeList;
}
QList<Node *> Node::connected()
{
foreach(Edge *e, edgeList) connectedNodes<<e->destNode();
foreach(Edge *e, incomingEdgesList)
{
if (! connectedNodes.contains(e->sourceNode())) connectedNodes<<e->sourceNode();
}
std::sort(connectedNodes.begin(),connectedNodes.end());
return connectedNodes;
}
bool Node::advance()
{
if (newPos == pos())
return false;
setPos(newPos);
return true;
}
QRectF Node::boundingRect() const
{
qreal adjust = 2;
return QRectF( -15 - adjust, -15 - adjust, 35 + adjust, 35 + adjust);
}
QPainterPath Node::shape() const
{
QPainterPath path;
path.addEllipse(-15, -15, 30, 30);
return path;
}
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(Qt::NoPen);
/*painter->setBrush(Qt::darkGray);
painter->drawEllipse(-10, -10, 30, 30);
*/
QRadialGradient gradient(-3, -3, 15);
gradient.setCenter(3, 3);
gradient.setFocalPoint(3, 3);
if (option->state & QStyle::State_Sunken) {
gradient.setColorAt(1, QColor(Qt::yellow).light(120));
gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
}
else if (active==1)
{
gradient.setColorAt(1, QColor(Qt::green));
gradient.setColorAt(0, QColor(Qt::darkGreen));
}
else if (active==0)
{
gradient.setColorAt(0, Qt::gray);
gradient.setColorAt(1, Qt::darkGray);
}
else
{
gradient.setColorAt(0, Qt::yellow);
gradient.setColorAt(1, Qt::darkYellow);
}
painter->setBrush(gradient);
painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-15, -15, 30, 30);
QFont font=painter->font() ;
font.setPointSize ( 12 );
font.setWeight(QFont::DemiBold);
painter->setFont(font);
if(tag>9) painter->drawText(-9,6,QString::number(tag));
else painter->drawText(-5,6,QString::number(tag));
}
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change) {
case ItemPositionHasChanged:
foreach (Edge *edge, edgeList)
edge->adjust();
foreach (Edge *edge, incomingEdgesList)
edge->adjust();
graph->itemMoved();
break;
default:
break;
};
return QGraphicsItem::itemChange(change, value);
}
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
update();
QGraphicsItem::mousePressEvent(event);
}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
update();
QGraphicsItem::mouseReleaseEvent(event);
}