-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicsedge.cpp
94 lines (76 loc) · 2.36 KB
/
graphicsedge.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
#include <QPainter>
#include "graphicsedge.h"
template <class GNode>
GraphicsEdge<GNode>::GraphicsEdge(GNode *startNode, GNode *endNode)
: Edge<GNode>(startNode,endNode), arrowSize(10) // nism sigurn ce je arrow size potreben? !!!! zgleda da ni
{
setAcceptedMouseButtons(0);
setZValue(-2);
this->startN->addEdge(this);
this->endN->addEdge(this);
adjust();
}
template <class GNode>
GraphicsEdge<GNode>::GraphicsEdge(GNode *startNode, GNode *endNode, int Price)
: Edge<GNode>(startNode,endNode, Price), arrowSize(20)
{
setAcceptedMouseButtons(0);
setZValue(-2);
this->startN->addEdge(this);
this->endN->addEdge(this);
adjust();
}
template <class GNode>
void GraphicsEdge<GNode>::adjust()
{
if (!this->startN || !this->endN)
return;
QLineF line(mapFromItem(this->startN, 0, 0), mapFromItem(this->endN, 0, 0));
qreal length = line.length();
prepareGeometryChange();
if (length > qreal(20.)) {
QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
startPoint = line.p1() + edgeOffset;
endPoint = line.p2() - edgeOffset;
} else {
startPoint = endPoint = line.p1();
}
}
template <class GNode>
QRectF GraphicsEdge<GNode>::boundingRect() const
{
if (!this->startN || !this->endN)
return QRectF();
return QRectF(startPoint, QSizeF(endPoint.x() - startPoint.x(),
endPoint.y() - startPoint.y()));
}
template <class GNode>
void GraphicsEdge<GNode>::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
if (!this->startN || !this->endN)
return;
QLineF line(startPoint, endPoint);
if (qFuzzyCompare(line.length(), qreal(0.)))
return;
// Draw the line itself
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter->drawLine(line);
}
template <class GNode>
void GraphicsEdge<GNode>::setNodes(GNode *startNode, GNode *endNode)
{
this->startN = startNode;
this->endN = endNode;
this->startN->addEdge(this);
this->endN->addEdge(this);
adjust();
}
template <class GNode>
bool GraphicsEdge<GNode>::unlink()
{
if (!this->startN || !this->endN)
return false;
bool returnSuccess = this->startN->removeEdge(this) && this->endN->removeEdge(this);
this->startN = this->endN = 0;
return returnSuccess;
}