-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbaseState.cpp
180 lines (157 loc) · 4.48 KB
/
baseState.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
#include <QtGui>
#include "baseState.h"
#include "Transition.h"
void BaseState::setName(QString newName)
{
stateName=newName;
updateSize();
}
void BaseState::updateSize()
{
QString x;
QString y;
int fontSize = 15;
do
{
y = (QString(this->stateName).append(QString("\n")).append(QString().fromStdString(STATE_TYPE_TABLE[this->stateType])));
nameTextItem->setPlainText(y);
QFont tmp = nameTextItem->font();
tmp.setPixelSize(--fontSize);
nameTextItem->setFont(tmp);
while(nameTextItem->boundingRect().width()>100)
{
x=y;
int size = x.size();
while (nameTextItem->boundingRect().width()>100)
{
size--;
x.truncate(size);
nameTextItem->setPlainText(x);
}
y.insert(size, QString("\n"));
nameTextItem->setPlainText(y);
}
}
while ((nameTextItem->boundingRect().height()>100));
}
BaseState::BaseState()
{
myPolygon << QPointF(-50, -50) << QPointF(50, -50)
<< QPointF(50, 50) << QPointF(-50, 50)
<< QPointF(-50, -50);
setPolygon(myPolygon);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
nameTextItem = new QGraphicsTextItem();
this->stateType=STATE_TYPES_NUMBER;
}
BaseState & BaseState::operator=(const BaseState &other)
{
if (this->stateName==other.stateName) return *this;
this->argument=other.argument;
this->stateType=other.stateType;
this->setName(other.stateName);
this->parameters=other.parameters;
this->Transitions=other.Transitions;
this->myContextMenu=other.myContextMenu;
this->myPolygon=other.myPolygon;
nameTextItem = new QGraphicsTextItem();
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
return *this;
}
BaseState::BaseState(BaseState& old)
{
nameTextItem = new QGraphicsTextItem();
this->argument=old.argument;
this->stateType=old.stateType;
this->setName(old.stateName);
this->parameters=old.parameters;
myPolygon << QPointF(-50, -50) << QPointF(50, -50)
<< QPointF(50, 50) << QPointF(-50, 50)
<< QPointF(-50, -50);
setPolygon(myPolygon);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
BaseState::~BaseState()
{
delete nameTextItem;
foreach(Transition * tr, subtaskTransitions)
{
tr->removeSubtask();
}
}
void BaseState::addSubtaskTrans(Transition * tr)
{
subtaskTransitions.push_back(tr);
}
void BaseState::removeSubtaskTrans(Transition * tr)
{
subtaskTransitions.removeOne(tr);
}
void BaseState::setMenu( QMenu *contextMenu)
{
myContextMenu = contextMenu;
}
void BaseState::removeTransition(Transition *Transition)
{
int index = Transitions.indexOf(Transition);
if (index != -1)
Transitions.removeAt(index);
}
void BaseState::removeTransitions()
{
foreach (Transition *Transition, Transitions)
{
Transition->startItem()->removeTransition(Transition);
Transition->endItem()->removeTransition(Transition);
scene()->removeItem(Transition);
delete Transition;
}
}
void BaseState::addTransition(Transition *Transition)
{
Transitions.append(Transition);
}
QPixmap BaseState::image() const
{
QPixmap pixmap(250, 250);
pixmap.fill(Qt::transparent);
if(isSelected())pixmap.fill(Qt::red);
QPainter painter(&pixmap);
painter.setPen(QPen(Qt::black, 8));
if(isSelected())painter.setPen(QPen(Qt::red, 8));
painter.translate(125, 125);
painter.drawPolyline(myPolygon);
return pixmap;
}
void BaseState::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
scene()->clearSelection();
setSelected(true);
myContextMenu->exec(event->screenPos());
}
QVariant BaseState::itemChange(GraphicsItemChange change,
const QVariant &value)
{
if (change == QGraphicsItem::ItemPositionChange) {
foreach (Transition *Transition, Transitions) {
Transition->updatePosition();
}
}
return value;
}
int BaseState::outTransitionsCount()
{
int i=0;
foreach(Transition* tr, Transitions)
{
if(tr->startItem()==this)
i++;
}
return i;
}