forked from twiniars/RESpecTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transition.cpp
199 lines (163 loc) · 6.64 KB
/
Transition.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <QtGui>
#include "Transition.h"
#include <math.h>
const qreal Pi = 3.14;
Transition::Transition(BaseState *startItem, BaseState *endItem,
QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsLineItem(parent, scene)
{
subtaskItem = new QGraphicsTextItem();
CondType=INIFILE;
subtask=NULL;
myStartItem = startItem;
myEndItem = endItem;
setFlag(QGraphicsItem::ItemIsSelectable, true);
setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
lines.push_back(new QGraphicsLineItem(QLineF(0,0,0,0)));
lines.push_back(new QGraphicsLineItem(QLineF(0,0,0,0)));
lines.push_back(new QGraphicsLineItem(QLineF(0,0,0,0)));
}
Transition::~Transition()
{
if(subtask) subtask->removeSubtaskTrans(this);
scene->removeItem(lines[0]);
scene->removeItem(lines[1]);
scene->removeItem(lines[2]);
scene->removeItem(subtaskItem);
delete lines[0];
delete lines[1];
delete lines[2];
delete subtaskItem;
}
void Transition::setScene(QGraphicsScene *sc)
{
scene=sc;
scene->addItem(lines[0]);
scene->addItem(lines[1]);
scene->addItem(lines[2]);
scene->addItem(subtaskItem);
}
QRectF Transition::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 Transition::shape() const
{
QPainterPath path = QGraphicsLineItem::shape();
path.addPolygon(TransitionHead);
return path;
}
void Transition::updatePosition()
{
subtaskItem->setPos((myStartItem->x()+myEndItem->x())/2 , (myStartItem->y()+myEndItem->y())/2);
QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0));
setLine(line);
}
void Transition::setZValue(qreal z)
{
lines[0]->setZValue(z);
lines[0]->setZValue(z);
lines[0]->setZValue(z);
QGraphicsLineItem::setZValue(z);
}
void Transition::paint(QPainter *painter, const QStyleOptionGraphicsItem * StyleOptions,
QWidget * widget)
{
double angle;
qreal TransitionSize = 20;
QPointF point1;
if(myStartItem==myEndItem)
{
QPen myPen = pen();
myPen.setColor(Qt::black);
if(isSelected())myPen.setColor(Qt::red);
painter->setPen(myPen);
painter->setBrush(Qt::black);
if(isSelected())painter->setBrush(Qt::red);
QVector<QPoint> pointsVector;
pointsVector.push_back((startItem()->pos()+QPoint(0,-50)).toPoint());
pointsVector.push_back((startItem()->pos()+QPoint(0,-100)).toPoint());
pointsVector.push_back((startItem()->pos()+QPoint(100,-100)).toPoint());
pointsVector.push_back((startItem()->pos()+QPoint(100,0)).toPoint());
pointsVector.push_back((startItem()->pos()+QPoint(50,0)).toPoint());
painter->drawLines(pointsVector);
//TODO:create transition view
setLine(QLineF(startItem()->pos()+QPoint(0,-50), startItem()->pos()+QPoint(0,-100)));
/*scene->removeItem(lines[0]);
scene->removeItem(lines[1]);
scene->removeItem(lines[2]);
//delete lines[0];
//delete lines[1];
//delete lines[2];
lines.clear();*/
scene->removeItem(lines[0]);
scene->removeItem(lines[1]);
scene->removeItem(lines[2]);
delete lines[0];
delete lines[1];
delete lines[2];
lines[0]=(new QGraphicsLineItem(QLineF(startItem()->pos()+QPoint(0,-100),startItem()->pos()+QPoint(100,-100))));
lines[1]=(new QGraphicsLineItem(QLineF(startItem()->pos()+QPoint(100,-100),startItem()->pos()+QPoint(100,0))));
lines[2]=(new QGraphicsLineItem(QLineF(startItem()->pos()+QPoint(100,0),startItem()->pos()+QPoint(50,0))));
lines[0]->setZValue(zValue());
lines[1]->setZValue(zValue());
lines[2]->setZValue(zValue());
scene->addItem(lines[0]);
scene->addItem(lines[1]);
scene->addItem(lines[2]);
//lines[0]->setLine((startItem()->pos()+QPoint(0,-100)).x(),(startItem()->pos()+QPoint(0,-100)).y(),(startItem()->pos()+QPoint(100,-100)).x(),(startItem()->pos()+QPoint(100,-100)).y());
//lines[1]->setLine((startItem()->pos()+QPoint(100,-100)).x(),(startItem()->pos()+QPoint(100,-100)).y(),(startItem()->pos()+QPoint(100,0)).x(),(startItem()->pos()+QPoint(100,0)).y());
//lines[2]->setLine((startItem()->pos()+QPoint(100,0)).x(),(startItem()->pos()+QPoint(100,0)).y(),(startItem()->pos()+QPoint(50,0)).x(),(startItem()->pos()+QPoint(50,0)).y());
lines[0]->paint(painter, StyleOptions, widget);
lines[1]->paint(painter, StyleOptions, widget);
lines[2]->paint(painter, StyleOptions, widget);
point1=startItem()->pos()+QPoint(0,-50);
angle = Pi/2;
}
else if (myStartItem->collidesWithItem(myEndItem))
return;
else
{
//scene->removeItem(lines[0]);//TODO czy moznato zrobic w taki sposob, czy nie trzebausunac i dodac czegos pustego.
//scene->removeItem(lines[1]);
//scene->removeItem(lines[2]);
QPen myPen = pen();
myPen.setColor(Qt::black);
if(isSelected())myPen.setColor(Qt::red);
painter->setPen(myPen);
painter->setBrush(Qt::black);
if(isSelected())painter->setBrush(Qt::red);
QLineF centerLine(myStartItem->pos(), myEndItem->pos());
QPolygonF endPolygon = myEndItem->polygon();
QPointF p1 = endPolygon.first() + myEndItem->pos();
QPointF p2;
QPointF intersectPoint;
QLineF polyLine;
for (int i = 1; i < endPolygon.count(); ++i) {
p2 = endPolygon.at(i) + myEndItem->pos();
polyLine = QLineF(p1, p2);
QLineF::IntersectType intersectType =
polyLine.intersect(centerLine, &intersectPoint);
if (intersectType == QLineF::BoundedIntersection)
break;
p1 = p2;
}
setLine(QLineF(intersectPoint, myStartItem->pos()));
angle = ::acos(line().dx() / line().length());
if (line().dy() >= 0)
angle = (Pi * 2) - angle;
painter->drawLine(line());
point1=line().p1();
}
QPointF TransitionP1 = point1 + QPointF(sin(angle + Pi / 3) * TransitionSize,
cos(angle + Pi / 3) * TransitionSize);
QPointF TransitionP2 = point1 + QPointF(sin(angle + Pi - Pi / 3) * TransitionSize,
cos(angle + Pi - Pi / 3) * TransitionSize);
TransitionHead.clear();
TransitionHead << point1 << TransitionP1 << TransitionP2;
painter->drawPolygon(TransitionHead);
}