/
edge.cpp
118 lines (100 loc) · 3.26 KB
/
edge.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
#include "edge.h"
#include <math.h>
#include <QDebug>
#include <iostream>
#include <QPen>
#include <QPainter>
Edge::Edge(Node *sourceNode, Node *destinationNode, QGraphicsItem *parent)
: QGraphicsLineItem(parent)
{
destination = destinationNode;
source = sourceNode;
setFlag(QGraphicsItem::ItemIsSelectable, true);
color = Qt::black;
setZValue(1000.0);
setPen(QPen(color, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
}
QRectF Edge::boundingRect() const
{
qreal extra = (pen().width() + 20) / 2.0;
return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),
line().p2().y() - line().p1().y()))
.normalized()
.adjusted(-extra, -extra, extra, extra);
}
QPainterPath Edge::shape() const
{
QPainterPath path = QGraphicsLineItem::shape();
path.addPolygon(arrowHead);
return path;
}
void Edge::updatePosition()
{
QLineF line(mapFromItem(source, 0, 0), mapFromItem(destination, 0, 0));
this->setLine(line);
textV->setPos((mapFromItem(source, 0, 0).x()+mapFromItem(destination, 0, 0).x())/2 + 2, (mapFromItem(source, 0, 0).y()+mapFromItem(destination, 0, 0).y())/2 + 10);
//qDebug() << textV->pos();
if (collidesWithItem(textV, Qt::IntersectsItemShape))
{
//qDebug() << "collission";
//textV->setPos((mapFromItem(source, 0, 0).x()+mapFromItem(destination, 0, 0).x())/2 + 2, (mapFromItem(source, 0, 0).y()+mapFromItem(destination, 0, 0).y())/2 -10);
}
//qDebug() << textV->pos();
}
void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *)
{
if (source->collidesWithItem(destination))
return;
QPen myPen = pen();
myPen.setColor(color);
qreal arrowSize = 20;
painter->setPen(myPen);
painter->setBrush(color);
QLineF centerLine(source->pos(), destination->pos());
centerLine.setLength(centerLine.length()-20.0);
setLine(QLineF(centerLine.p2(), source->pos()));
double angle = ::acos(line().dx() / line().length());
if (line().dy() >= 0)
angle = (3.14 * 2) - angle;
QPointF arrowP1 = line().p1() + QPointF(sin(angle + 3.14 / 3) * arrowSize,
cos(angle + 3.14 / 3) * arrowSize);
QPointF arrowP2 = line().p1() + QPointF(sin(angle + 3.14 - 3.14 / 3) * arrowSize,
cos(angle + 3.14 - 3.14 / 3) * arrowSize);
arrowHead.clear();
arrowHead << line().p1() << arrowP1 << arrowP2;
painter->drawLine(line());
painter->drawPolygon(arrowHead);
if (isSelected()) {
painter->setPen(QPen(color, 1, Qt::DashLine));
QLineF myLine = line();
myLine.translate(0, 4.0);
painter->drawLine(myLine);
myLine.translate(0,-8.0);
painter->drawLine(myLine);
}
}
void Edge::setTextObject(TextValue *object)
{
textV = object;
//connect(object, SIGNAL(textChange(int)), this, SLOT(setCap(int)));
}
void Edge::setFlow(int flow)
{
textV->setFlowValue(flow);
textV->updateText();
}
void Edge::setCapacite(int capacite)
{
textV->setCapacityValue(capacite);
textV->updateText();
//std::cout << capacite << std::endl;
}
int Edge::getFlow()
{
return textV->getFlowValue();
}
int Edge::getCapacite()
{
return textV->getCapacityValue();
}