Repository navigation
Expand file tree
/
Copy pathVertex.cpp
More file actions
144 lines (129 loc) · 3.69 KB
/
Copy pathVertex.cpp
File metadata and controls
144 lines (129 loc) · 3.69 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
133
134
135
136
137
138
139
140
141
142
143
144
#include "Vertex.h"
#include <vector>
#include <QDebug>
#include "Edge.h"
#include "GraphArea.h"
/**
* @brief Vertex constructor.
* @param graphArea Passed to QLabel() constructor.
*/
Vertex::Vertex(GraphArea *graphArea)
: graphArea(graphArea)
{
setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
}
/**
* @brief Make pair and add to vertexEdgeList.
* @param vertex
* @param edge
*/
void Vertex::addPair(Vertex *vertex, Edge *edge)
{
vertexEdgeList.push_back(std::make_pair(vertex, edge));
edge->adjust();
}
/**
* @brief Removes pair from vertexEdgeList
* @param vertex
* @param edge
*/
void Vertex::removePair(Vertex *vertex, Edge *edge)
{
list<pair<Vertex*, Edge*>>::iterator it;
it = std::find(vertexEdgeList.begin(), vertexEdgeList.end(), std::make_pair(vertex, edge));
if (it != vertexEdgeList.end()) {
it = vertexEdgeList.erase(it);
}
}
/**
* @brief Gets a list of pairs.
* @return STL list of Vertex* Edge* pairs
*/
list<pair<Vertex *, Edge *> >& Vertex::pairs()
{
return vertexEdgeList;
}
bool Vertex::advancePosition()
{
if (newPos == pos())
return false;
setPos(newPos);
return true;
}
QRectF Vertex::boundingRect() const
{
qreal adjust = 2;
return QRectF(-20 - adjust, -20 - adjust, 40 + adjust, 40 + adjust);
}
QPainterPath Vertex::shape() const
{
QPainterPath path;
path.addEllipse(-18, -18, 36, 36);
return path;
}
void Vertex::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->setPen(vertexColor);
painter->setBrush(vertexColor);
painter->drawEllipse(-18, -18, 36, 36);
painter->setBrush(Qt::white);
painter->drawEllipse(-16, -16, 32, 32);
if (distanceWhenVis != std::numeric_limits<int>::max()) {
// painter->drawText(-5, 5, QString::number(distance));
painter->drawText(-5, 5, QString::number(distanceWhenVis));
}
}
QVariant Vertex::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change) {
case ItemPositionHasChanged:
for (pair<Vertex*, Edge*> e : qAsConst(vertexEdgeList))
e.second->adjust();
break;
default:
break;
};
return QGraphicsItem::itemChange(change, value);
}
void Vertex::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton) {
setFlag(ItemIsMovable, true);
if (graphArea->getCursorMode() == GraphArea::Cursor::EDGE) {
setFlag(ItemIsSelectable);
setFlag(ItemIsMovable, false);
}
emit vertexClicked(this);
}
else if ((event->buttons() == Qt::RightButton && graphArea->getCursorMode() == GraphArea::Cursor::POINTER)
|| graphArea->getCursorMode() == GraphArea::Cursor::START)
{
std::vector<list<pair<Vertex*, Edge*>>::iterator> iteratorsToRemove;
for (list<pair<Vertex*, Edge*>>::iterator it = vertexEdgeList.begin(); it != vertexEdgeList.end(); ++it) {
iteratorsToRemove.push_back(it);
}
while (!iteratorsToRemove.empty()) {
Edge* temp = iteratorsToRemove[iteratorsToRemove.size() - 1]->second;
iteratorsToRemove[iteratorsToRemove.size() - 1]->second->detachFromIncidentVertices();
delete temp;
iteratorsToRemove.pop_back();
}
emit destroyVertex(this);
delete this;
return;
}
update();
QGraphicsItem::mousePressEvent(event);
}
void Vertex::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
update();
QGraphicsItem::mouseReleaseEvent(event);
if (candidatePairFound) {
emit promptCreatePair(this);
}
candidatePairFound = false;
}