/
graphicstransition.cpp
142 lines (92 loc) · 3.57 KB
/
graphicstransition.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
138
139
140
141
#include "graphicstransition.h"
#include <math.h>
#include <QPen>
#include <QPainter>
const qreal PI = 3.14;
GraphicsTransition::GraphicsTransition(GraphicsStateItem *from, GraphicsStateItem *to, QGraphicsItem *parent)
: QGraphicsLineItem(parent){
mCurrentState = from;
mNextState = to;
setFlag(QGraphicsItem::ItemIsSelectable, true);
setPen(QPen(Qt::white, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
}
QRectF GraphicsTransition::boundingRect(){
prepareGeometryChange();
qreal extra = (pen().width() + 60) / 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 GraphicsTransition::shape(){
QPainterPath path = QGraphicsLineItem::shape();
path.addPolygon(mArrowHead);
return path;
}
void GraphicsTransition::setNextState(GraphicsStateItem *state){
mNextState = state;
}
void GraphicsTransition::setCurrentState(GraphicsStateItem *state){
mCurrentState = state;
}
void GraphicsTransition::updatePosition(){
if(mCurrentState != nullptr && mNextState != nullptr){
QLineF line(mapFromItem(mCurrentState, 20, 0), mapFromItem(mNextState, 0, 0));
setLine(line);
}
}
void GraphicsTransition::writeXml(QXmlStreamWriter &writer){
writer.writeStartElement("transition");
writer.writeAttribute("target", mNextState->getId());
//TODO for Each condition
writer.writeStartElement("conditions");
mCondition->writeXml(writer);
writer.writeEndElement();
writer.writeEndElement();
}
void GraphicsTransition::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
//If the collide no need to draw an arrow
if (mCurrentState->collidesWithItem(mNextState))
return;
QPen myPen = pen();
myPen.setColor(Qt::white);
qreal arrowSize = 20;
painter->setPen(myPen);
painter->setBrush(Qt::white);
QLineF centerLine(mCurrentState->getCenterPoint(), mNextState->getCenterPoint());
QPolygonF endPolygon = mNextState->boundingRect();
QPointF p1 = endPolygon.first() + mNextState->pos();
QPointF p2;
QPointF intersectPoint;
QLineF polyLine;
for (int i = 1; i < endPolygon.count(); ++i) {
p2 = endPolygon.at(i) + mNextState->pos();
polyLine = QLineF(p1, p2);
QLineF::IntersectType intersectType = polyLine.intersect(centerLine, &intersectPoint);
if (intersectType == QLineF::BoundedIntersection)
break;
p1 = p2;
}
setLine(QLineF(intersectPoint, mCurrentState->getCenterPoint()));
double angle = ::acos(line().dx() / line().length());
if (line().dy() >= 0)
angle = (PI * 2) - angle;
QPointF arrowP1 = line().p1() + QPointF(sin(angle + PI / 3) * arrowSize,
cos(angle + PI / 3) * arrowSize);
QPointF arrowP2 = line().p1() + QPointF(sin(angle + PI - PI / 3) * arrowSize,
cos(angle + PI - PI / 3) * arrowSize);
mArrowHead.clear();
mArrowHead << line().p1() << arrowP1 << arrowP2;
painter->drawLine(line());
painter->drawPolygon(mArrowHead);
//painter->drawEllipse(line().p1(), 10,10);
//Selection square
if (isSelected()) {
painter->setPen(QPen(Qt::yellow, 3, Qt::DashLine));
QLineF myLine = line();
myLine.translate(0, 4.0);
painter->drawLine(myLine);
myLine.translate(0,-8.0);
painter->drawLine(myLine);
}
}