-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_diagram_scene.cpp
450 lines (393 loc) · 16.7 KB
/
my_diagram_scene.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <QtGui>
#include "my_diagram_scene.h"
#include "my_diagram_arrow.h"
My_diagram_scene::My_diagram_scene(QMenu *itemMenu, QObject *parent): QGraphicsScene(parent)
{
myItemMenu = itemMenu;
myMode = MoveItem;
myItemType = My_diagram_item::Class_; // Ýňî çíŕ÷čň ěű âńňŕâë˙ĺě ëčáî ňđĺóăîëüíčę ęâŕäđŕňčę č ň.ä.
line = 0;
textItem = 0;
myItemColor = Qt::white;
myTextColor = Qt::black;
myLineColor = Qt::black;
ismodified = false;
}
void My_diagram_scene::setLineColor(const QColor &color)
{
myLineColor = color;
if (isItemChange(My_diagram_arrow::Type))
{
My_diagram_arrow *item =
qgraphicsitem_cast<My_diagram_arrow *>(selectedItems().first());
item->setColor(myLineColor);
update();
}
}
void My_diagram_scene::setTextColor(const QColor &color)
{
myTextColor = color;
if (isItemChange(My_diagram_item::Type))
{
My_diagram_text_item *item =
qgraphicsitem_cast<My_diagram_text_item *>(selectedItems().first());
item->setDefaultTextColor(myTextColor);
}
}
void My_diagram_scene::setItemColor(const QColor &color)
{
myItemColor = color;
if (isItemChange(My_diagram_item::Type))
{
My_diagram_item *item =
qgraphicsitem_cast<My_diagram_item *>(selectedItems().first());
item->setBrush(myItemColor);
}
}
void My_diagram_scene::setFont(const QFont &font)
{
myFont = font;
if (isItemChange(My_diagram_text_item::Type))
{
QGraphicsTextItem *item =
qgraphicsitem_cast<My_diagram_text_item *>(selectedItems().first());
if(item!=0 && item->parentItem()!=0)
return;
if (item)
item->setFont(myFont);
}
}
void My_diagram_scene::setMode(Mode mode)
{
myMode = mode;
}
void My_diagram_scene::setArrows(Arrows arrows)
{
myArrow = arrows;
}
void My_diagram_scene::setItemType(My_diagram_item::DiagramType type)
{
myItemType = type;
}
void My_diagram_scene::editorLostFocus(My_diagram_text_item *item)
{
QTextCursor cursor = item->textCursor();
cursor.clearSelection(); //î÷čůŕĺě ňĺęóůčé ýëĺěĺíň ęîăäŕ îí ňĺđ˙ĺň ôîęóń
item->setTextCursor(cursor);
if (item->toPlainText().isEmpty())
{
removeItem(item);
item->deleteLater();
}
}
void My_diagram_scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
// My_diagram_item * my = new My_diagram_item(myItemType,myItemMenu);
// addItem(my);
My_diagram_item* item;
My_diagram_text_item* One;
My_diagram_text_item* Two;
My_diagram_text_item* Three;
switch (myMode)
{
case InsertItem:
{
if(myItemType == My_diagram_item::Class_)
{
item = new My_diagram_item(myItemType, myItemMenu);
item->setBrush(myItemColor);
One = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::Class);
One->moveBy(-100,-100);
connect(One, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(One, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(One, SIGNAL(items_changed()),
this, SLOT(isModified()));
addItem(item);
item->setPos(mouseEvent->scenePos());
//==================================================================
Two = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::Attribute);
Two->moveBy(-100,-65);
connect(Two, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(Two, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(Two, SIGNAL(items_changed()),
this, SLOT(isModified()));
//==================================================================
Three = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::Operation);
Three->moveBy(-100,25);
connect(Three, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(Three, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(Three, SIGNAL(items_changed()),
this, SLOT(isModified()));
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::Interface)
{
item = new My_diagram_item(myItemType, myItemMenu);
item->setBrush(myItemColor);
One = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::Interface);
One->moveBy(-115,-100);
connect(One, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(One, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(One, SIGNAL(items_changed()),
this, SLOT(isModified()));
addItem(item);
item->setPos(mouseEvent->scenePos());
Three = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::Operation);
Three->setTextWidth(230);
Three->moveBy(-115,10);
connect(Three, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(Three, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(Three, SIGNAL(items_changed()),
this, SLOT(isModified()));
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::Option)
{
item = new My_diagram_item(myItemType, myItemMenu);
item->setBrush(myItemColor);
One = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::Option_disc);
One->moveBy(-115,-60);
connect(One, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(One, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(One, SIGNAL(items_changed()),
this, SLOT(isModified()));
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::State)
{
item = new My_diagram_item(myItemType, myItemMenu);
item->setBrush(myItemColor);
One = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::State_name);
One->moveBy(-115,-80);
connect(One, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(One, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(One, SIGNAL(items_changed()),
this, SLOT(isModified()));
Two = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::State_disc);
Two->moveBy(-115,-60);
connect(Two, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(Two, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(One, SIGNAL(items_changed()),
this, SLOT(isModified()));
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::BeginState)
{
item = new My_diagram_item(myItemType, myItemMenu);
item->setBrush(QBrush(Qt::SolidPattern));
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::EndState)
{
item = new My_diagram_item(myItemType, myItemMenu);
//item->setBrush(Qt::white);
addItem(item);
item->setPos(mouseEvent->scenePos());
update();
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::SyncState)
{
item = new My_diagram_item(myItemType, myItemMenu);
//item->setBrush(QBrush(Qt::SolidPattern));
// item->update();
// item->setBrush(Qt::white);
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::Shallow_history_state)
{
item = new My_diagram_item(myItemType, myItemMenu);
// item->setBrush(QBrush(Qt::SolidPattern));
//item->setBrush(Qt::white);
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::Deep_history_state)
{
item = new My_diagram_item(myItemType, myItemMenu);
// item->setBrush(QBrush(Qt::SolidPattern));
//item->setBrush(Qt::white);
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::Transition)
{
item = new My_diagram_item(myItemType, myItemMenu);
item->setBrush(QBrush(Qt::SolidPattern));
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
if(myItemType == My_diagram_item::Actor)
{
item = new My_diagram_item(myItemType, myItemMenu);
//item->setBrush(QBrush(Qt::SolidPattern));
One = new My_diagram_text_item(item, 0,My_diagram_text_item::Margin, My_diagram_text_item::Actor_name);
One->moveBy(-45,100);
connect(One, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(One, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(One, SIGNAL(items_changed()),
this, SLOT(isModified()));
item->setBrush(Qt::white);
addItem(item);
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
}
}
case InsertLine:
line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),mouseEvent->scenePos()));
line->setPen(QPen(myLineColor, 2));
addItem(line);
break;
case InsertText:
textItem = new My_diagram_text_item();
textItem->setFont(myFont);
textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
textItem->setZValue(1000.0);
connect(textItem, SIGNAL(lostFocus(My_diagram_text_item*)),
this, SLOT(editorLostFocus(My_diagram_text_item*)));
connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
this, SIGNAL(itemSelected(QGraphicsItem*)));
connect(textItem, SIGNAL(items_changed()),
this, SLOT(isModified()));
addItem(textItem);
textItem->setDefaultTextColor(myTextColor);
textItem->setPos(mouseEvent->scenePos());
emit textInserted(textItem);
default:
;
}
QGraphicsScene::mousePressEvent(mouseEvent);
QList<QGraphicsItem*> ASA = this->items();
for(int i=0;i!=ASA.count();i++)
{
if(qgraphicsitem_cast<My_diagram_text_item *>(ASA.at(i))!=0)
normal_text_without_focus(qgraphicsitem_cast<My_diagram_text_item *>(ASA.at(i)));
}
}
void My_diagram_scene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)//âűçűâŕĺňń˙ ňîëüęî â ňîě ńëó÷ŕĺ ĺńëč ňîëüęî îäíŕ čç ęíîďîę ěűřč íŕćŕňŕ
{
if (myMode == InsertLine && line != 0)
{
QLineF newLine(line->line().p1(), mouseEvent->scenePos());
line->setLine(newLine);
} else if (myMode == MoveItem)
{
QGraphicsScene::update();
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
}
void My_diagram_scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (line != 0 && myMode == InsertLine)
{
QList<QGraphicsItem *> startItems = items(line->line().p1());
if (startItems.count() && startItems.first() == line)
startItems.removeFirst();
QList<QGraphicsItem *> endItems = items(line->line().p2());
if (endItems.count() && endItems.first() == line)
endItems.removeFirst();
removeItem(line);
delete line;
//Ńíŕ÷ŕëŕ íŕě íŕäî ďîëó÷čňü ýëĺěĺíňű (ĺńëč ňŕęčĺ ĺńňü) ďîä íŕ÷ŕëüíîé č ęîíĺ÷íîé ňî÷ęŕěč.
//Ńŕěŕ ëčíč˙ ýňî ďĺđâűé ýëĺěĺíň íŕ ýňčő ęîîđäčíŕňŕő, ďîýňîěó ěű óäŕë˙ĺě ĺĺ čç ńďčńęŕ.
// ęŕ÷ĺńňâĺ ďđĺäîńňîđîćíîńňč ěű ďđîâĺđ˙ĺě íĺ ďóńňîé ëč ńďčńîę, íî ýňî íĺ äîëćíî íčęîăäŕ ďđîčçîéňč.
if (startItems.count() > 0 && endItems.count() > 0 &&
startItems.first()->type() == My_diagram_item::Type &&
endItems.first()->type() == My_diagram_item::Type &&
startItems.first() != endItems.first())
{
My_diagram_item *startItem =
qgraphicsitem_cast<My_diagram_item *>(startItems.first());
My_diagram_item *endItem =
qgraphicsitem_cast<My_diagram_item *>(endItems.first());
My_diagram_arrow *arrow = 0;
if(myArrow == Dependency)
arrow = new My_diagram_arrow(My_diagram_arrow::Dependency, startItem, endItem);
if(myArrow == Association)
arrow = new My_diagram_arrow(My_diagram_arrow::Association, startItem, endItem);
if(myArrow == Aggregation)
arrow = new My_diagram_arrow(My_diagram_arrow::Aggregation, startItem, endItem);
if(myArrow == Composition)
arrow = new My_diagram_arrow(My_diagram_arrow::Composition, startItem, endItem);
if(myArrow == Generalization)
arrow = new My_diagram_arrow(My_diagram_arrow::Generalization, startItem, endItem);
if(myArrow == Include)
arrow = new My_diagram_arrow(My_diagram_arrow::Include, startItem, endItem);
if(myArrow == Extend)
arrow = new My_diagram_arrow(My_diagram_arrow::Extend, startItem, endItem);
if(myArrow == Transaction)
arrow = new My_diagram_arrow(My_diagram_arrow::Transaction, startItem, endItem);
arrow->setColor(myLineColor);
startItem->addArrow(arrow);
endItem->addArrow(arrow);
arrow->setZValue(-1000.0);
addItem(arrow);
arrow->updatePosition();
}
}
line = 0;
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
bool My_diagram_scene::isItemChange(int type)
{
foreach (QGraphicsItem *item, selectedItems())
{
if (item->type() == type)
return true;
}
return false;
}
void My_diagram_scene::normal_text_without_focus(My_diagram_text_item* item)
{
QTextCursor cursor = item->textCursor();
cursor.clearSelection();
item->setTextCursor(cursor);
if(!item->isSelected())
item->setTextInteractionFlags(Qt::NoTextInteraction);
}
void My_diagram_scene::isModified()
{
ismodified = true;
}