-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElaGraphicsScene.cpp
556 lines (524 loc) · 14.2 KB
/
ElaGraphicsScene.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "ElaGraphicsScene.h"
#include <QDebug>
#include <QFile>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QKeyEvent>
#include "ElaGraphicsItem.h"
#include "ElaGraphicsLineItem.h"
#include "private/ElaGraphicsScenePrivate.h"
Q_PROPERTY_CREATE_Q_CPP(ElaGraphicsScene, bool, IsCheckLinkPort)
Q_PROPERTY_CREATE_Q_CPP(ElaGraphicsScene, QString, SerializePath)
ElaGraphicsScene::ElaGraphicsScene(QObject* parent)
: QGraphicsScene(parent), d_ptr(new ElaGraphicsScenePrivate())
{
Q_D(ElaGraphicsScene);
d->q_ptr = this;
setItemIndexMethod(QGraphicsScene::NoIndex);
d->_pIsCheckLinkPort = false;
d->_sceneMode = ElaGraphicsSceneType::SceneMode::Default;
d->_pSerializePath = "./scene.bin";
}
ElaGraphicsScene::~ElaGraphicsScene()
{
}
void ElaGraphicsScene::addItem(ElaGraphicsItem* item)
{
Q_D(ElaGraphicsScene);
if (!item)
{
return;
}
for (const auto& pair : d->_items.toStdMap())
{
if (pair.second == item)
{
return;
}
}
item->setParent(this);
item->setZValue(d->_currentZ);
if (item->getItemName().isEmpty())
{
item->setItemName(QString("ElaItem%1").arg(d->_currentZ));
}
item->setPos(sceneRect().width() / 2, sceneRect().height() / 2);
QGraphicsScene::addItem(item);
d->_currentZ++;
d->_items.insert(item->getItemUID(), item);
}
void ElaGraphicsScene::removeItem(ElaGraphicsItem* item)
{
Q_D(ElaGraphicsScene);
if (!item)
{
return;
}
d->_items.remove(d->_items.key(item));
removeItemLink(item);
QGraphicsScene::removeItem(item);
delete item;
update();
}
void ElaGraphicsScene::removeSelectedItems()
{
QList<ElaGraphicsItem*> selectedItemList = getSelectedElaItems();
if (selectedItemList.count() == 0)
{
return;
}
for (auto item : selectedItemList)
{
removeItem(item);
}
}
void ElaGraphicsScene::clear()
{
Q_D(ElaGraphicsScene);
d->_itemsLink.clear();
for (const auto& pair : d->_items.toStdMap())
{
delete pair.second;
}
d->_items.clear();
update();
}
QList<ElaGraphicsItem*> ElaGraphicsScene::createAndAddItem(int width, int height, int count)
{
if (count <= 0)
{
return QList<ElaGraphicsItem*>();
}
QList<ElaGraphicsItem*> createItemList;
for (int i = 0; i < count; i++)
{
ElaGraphicsItem* item = new ElaGraphicsItem();
item->setWidth(width);
item->setHeight(height);
createItemList.append(item);
addItem(item);
}
return createItemList;
}
QList<ElaGraphicsItem*> ElaGraphicsScene::getSelectedElaItems() const
{
QList<QGraphicsItem*> selectedItemList = selectedItems();
QList<ElaGraphicsItem*> selectedElaItemList;
for (auto item : selectedItemList)
{
ElaGraphicsItem* itemCast = dynamic_cast<ElaGraphicsItem*>(item);
if (itemCast)
{
selectedElaItemList.append(itemCast);
}
}
return selectedElaItemList;
}
QList<ElaGraphicsItem*> ElaGraphicsScene::getElaItems()
{
Q_D(ElaGraphicsScene);
return d->_items.values();
}
QList<ElaGraphicsItem*> ElaGraphicsScene::getElaItems(QPoint pos)
{
QList<QGraphicsItem*> itemList = items(pos);
QList<ElaGraphicsItem*> elaItemList;
for (auto item : itemList)
{
ElaGraphicsItem* elaItem = dynamic_cast<ElaGraphicsItem*>(item);
if (elaItem)
{
elaItemList.append(elaItem);
}
}
return elaItemList;
}
QList<ElaGraphicsItem*> ElaGraphicsScene::getElaItems(QPointF pos)
{
QList<QGraphicsItem*> itemList = items(pos);
QList<ElaGraphicsItem*> elaItemList;
for (auto item : itemList)
{
ElaGraphicsItem* elaItem = dynamic_cast<ElaGraphicsItem*>(item);
if (elaItem)
{
elaItemList.append(elaItem);
}
}
return elaItemList;
}
QList<ElaGraphicsItem*> ElaGraphicsScene::getElaItems(QRect rect)
{
QList<QGraphicsItem*> itemList = items(rect);
QList<ElaGraphicsItem*> elaItemList;
for (auto item : itemList)
{
ElaGraphicsItem* elaItem = dynamic_cast<ElaGraphicsItem*>(item);
if (elaItem)
{
elaItemList.append(elaItem);
}
}
return elaItemList;
}
QList<ElaGraphicsItem*> ElaGraphicsScene::getElaItems(QRectF rect)
{
QList<QGraphicsItem*> itemList = items(rect);
QList<ElaGraphicsItem*> elaItemList;
for (auto item : itemList)
{
ElaGraphicsItem* elaItem = dynamic_cast<ElaGraphicsItem*>(item);
if (elaItem)
{
elaItemList.append(elaItem);
}
}
return elaItemList;
}
void ElaGraphicsScene::setSceneMode(ElaGraphicsSceneType::SceneMode mode)
{
Q_D(ElaGraphicsScene);
d->_sceneMode = mode;
if (mode == ElaGraphicsSceneType::SceneMode::DragMove)
{
views().at(0)->setDragMode(QGraphicsView::ScrollHandDrag);
}
else
{
views().at(0)->setDragMode(QGraphicsView::RubberBandDrag);
}
}
ElaGraphicsSceneType::SceneMode ElaGraphicsScene::getSceneMode() const
{
return d_ptr->_sceneMode;
}
void ElaGraphicsScene::selectAllItems()
{
Q_D(ElaGraphicsScene);
for (const auto& pair : d->_items.toStdMap())
{
ElaGraphicsItem* item = pair.second;
item->setSelected(true);
}
}
QList<QVariantMap> ElaGraphicsScene::getItemLinkList() const
{
return d_ptr->_itemsLink;
}
bool ElaGraphicsScene::addItemLink(ElaGraphicsItem* item1, ElaGraphicsItem* item2, int port1, int port2)
{
Q_D(ElaGraphicsScene);
if (!item1 || !item2 || (item1 == item2) || port1 < 0 || port2 < 0 || item1->getMaxLinkPortCount() <= port1 || item2->getMaxLinkPortCount() <= port2)
{
return false;
}
if (d->_pIsCheckLinkPort)
{
if (!item1->getLinkPortState(port1) && !item2->getLinkPortState(port2))
{
item1->setLinkPortState(true, port1);
item2->setLinkPortState(true, port2);
}
else
{
return false;
}
}
QVariantMap linkObject;
linkObject.insert(item1->getItemUID(), port1);
linkObject.insert(item2->getItemUID(), port2);
d->_itemsLink.append(linkObject);
ElaGraphicsLineItem* lineItem = new ElaGraphicsLineItem(item1, item2, port1, port2);
QGraphicsScene::addItem(lineItem);
d->_lineItemsList.append(lineItem);
update();
return true;
}
bool ElaGraphicsScene::removeItemLink(ElaGraphicsItem* item1)
{
Q_D(ElaGraphicsScene);
if (!item1)
{
return false;
}
if (d->_pIsCheckLinkPort)
{
item1->setLinkPortState(false);
}
// 处理与该Item有关的连接
foreach (auto& link, d->_itemsLink)
{
if (link.contains(item1->getItemUID()))
{
if (d->_pIsCheckLinkPort)
{
// 解除otherItem端口占用
QStringList keys = link.keys();
keys.removeOne(item1->getItemUID());
ElaGraphicsItem* otherItem = d->_items.value(keys.at(0));
otherItem->setLinkPortState(false, link.value(keys.at(0)).toInt());
}
d->_itemsLink.removeOne(link);
}
}
// 处理连接图元
foreach (auto lineItem, d->_lineItemsList)
{
if (lineItem->isTargetLink(item1))
{
d->_lineItemsList.removeOne(lineItem);
QGraphicsScene::removeItem(lineItem);
delete lineItem;
}
}
update();
return true;
}
bool ElaGraphicsScene::removeItemLink(ElaGraphicsItem* item1, ElaGraphicsItem* item2, int port1, int port2)
{
Q_D(ElaGraphicsScene);
if (!item1 || !item2)
{
return false;
}
bool isLinkExist = false;
foreach (auto& link, d->_itemsLink)
{
QVariant portVariant1 = link.value(item1->getItemUID());
QVariant portVariant2 = link.value(item2->getItemUID());
if (portVariant1.isValid() && portVariant2.isValid() && portVariant1.toUInt() == port1 && portVariant2.toUInt() == port2)
{
d->_itemsLink.removeOne(link);
// 这里处理连线图元
isLinkExist = true;
break;
}
}
foreach (auto lineItem, d->_lineItemsList)
{
if (lineItem->isTargetLink(item1, item2, port1, port2))
{
d->_lineItemsList.removeOne(lineItem);
QGraphicsScene::removeItem(lineItem);
delete lineItem;
break;
}
}
if (isLinkExist)
{
if (d->_pIsCheckLinkPort)
{
item1->setLinkPortState(false, port1);
item2->setLinkPortState(false, port2);
}
update();
return true;
}
else
{
return false;
}
}
QVector<QVariantMap> ElaGraphicsScene::getItemsDataRoute() const
{
QVector<QVariantMap> dataRouteVector;
for (const auto& pair : d_ptr->_items.toStdMap())
{
ElaGraphicsItem* item = pair.second;
dataRouteVector.append(item->getDataRoutes());
}
return dataRouteVector;
}
void ElaGraphicsScene::serialize()
{
Q_D(ElaGraphicsScene);
QFile file(d->_pSerializePath);
if (!file.open(QIODevice::WriteOnly))
{
qDebug() << "serialize Error";
return;
}
QDataStream serialStream(&file);
serialStream << d;
file.flush();
file.close();
}
void ElaGraphicsScene::deserialize()
{
Q_D(ElaGraphicsScene);
QFile file(d->_pSerializePath);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "deserialize Error";
return;
}
QDataStream deserialStream(&file);
deserialStream >> d;
file.close();
update();
}
void ElaGraphicsScene::focusOutEvent(QFocusEvent* event)
{
Q_D(ElaGraphicsScene);
d->_sceneMode = ElaGraphicsSceneType::SceneMode::Default;
d->_removeLinkLineItem();
QGraphicsScene::focusOutEvent(event);
}
void ElaGraphicsScene::keyPressEvent(QKeyEvent* event)
{
Q_D(ElaGraphicsScene);
switch (event->key())
{
case Qt::Key_Control:
{
d->_sceneMode = ElaGraphicsSceneType::SceneMode::MultiSelect;
break;
}
case Qt::Key_Shift:
{
d->_sceneMode = ElaGraphicsSceneType::SceneMode::ItemLink;
clearSelection();
break;
}
case Qt::Key_Delete:
{
removeSelectedItems();
break;
}
}
QGraphicsScene::keyPressEvent(event);
}
void ElaGraphicsScene::keyReleaseEvent(QKeyEvent* event)
{
Q_D(ElaGraphicsScene);
d->_sceneMode = ElaGraphicsSceneType::SceneMode::Default;
d->_removeLinkLineItem();
QGraphicsScene::keyReleaseEvent(event);
}
void ElaGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
Q_D(ElaGraphicsScene);
if (event->button() == Qt::LeftButton)
{
if (d->_sceneMode == ElaGraphicsSceneType::SceneMode::Default)
{
d->_isLeftButtonPress = true;
d->_lastPos = event->pos();
}
}
QList<QGraphicsItem*> selectedItemList = selectedItems();
QGraphicsScene::mousePressEvent(event);
if (d->_sceneMode == ElaGraphicsSceneType::SceneMode::ItemLink)
{
for (auto item : selectedItemList)
{
item->setSelected(true);
}
d->_lastPos = event->pos();
}
}
void ElaGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
Q_D(ElaGraphicsScene);
QList<ElaGraphicsItem*> itemList = getElaItems(event->scenePos());
if (event->button() == Qt::LeftButton)
{
d->_isLeftButtonPress = false;
switch (d->_sceneMode)
{
case ElaGraphicsSceneType::SceneMode::Default:
{
if (itemList.count() > 0)
{
Q_EMIT mouseLeftClickedItem(itemList[0]);
}
break;
}
case ElaGraphicsSceneType::SceneMode::ItemLink:
{
QList<ElaGraphicsItem*> selectedItemList = getSelectedElaItems();
if (selectedItemList.count() == 1 && !d->_linkLineItem)
{
d->_linkLineItem = new ElaGraphicsLineItem(selectedItemList.at(0)->pos(), selectedItemList.at(0)->pos());
QGraphicsScene::addItem(d->_linkLineItem);
}
else if (selectedItemList.count() == 2)
{
if (d->_pIsCheckLinkPort)
{
Q_EMIT showItemLink();
}
else
{
QVariantMap linkObject;
for (auto item : selectedItemList)
{
linkObject.insert(item->getItemUID(), 0);
}
d->_itemsLink.append(linkObject);
QGraphicsScene::mouseReleaseEvent(event);
d->_removeLinkLineItem();
addItemLink(selectedItemList.at(0), selectedItemList.at(1));
clearSelection();
return;
}
}
else
{
d->_removeLinkLineItem();
update();
}
break;
}
default:
{
break;
}
}
}
else if (event->button() == Qt::RightButton)
{
if (itemList.count() > 0)
{
clearSelection();
itemList[0]->setSelected(true);
Q_EMIT mouseRightClickedItem(itemList[0]);
}
}
QGraphicsScene::mouseReleaseEvent(event);
}
void ElaGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
Q_D(ElaGraphicsScene);
if (d->_sceneMode == ElaGraphicsSceneType::SceneMode::ItemLink)
{
if (getSelectedElaItems().count() == 1)
{
if (d->_linkLineItem)
{
d->_linkLineItem->setEndPoint(event->scenePos());
d->_linkLineItem->update();
}
}
}
else
{
if (d->_isLeftButtonPress)
{
for (auto lineItem : d->_lineItemsList)
{
lineItem->update();
}
}
}
QGraphicsScene::mouseMoveEvent(event);
}
void ElaGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
QList<ElaGraphicsItem*> itemList = getElaItems(event->scenePos());
if (itemList.count() > 0)
{
Q_EMIT mouseDoubleClickedItem(itemList[0]);
}
QGraphicsScene::mouseDoubleClickEvent(event);
}