@@ -32,9 +32,11 @@ NodalView::NodalView(QWidget* parent)
32
32
: QGraphicsView (parent),
33
33
m_dragging(false ),
34
34
m_rightClickPressed(false ),
35
+ m_contextMenu(false ),
35
36
m_zoom(1.0 ),
36
37
m_nextCreationPosition(0 , 0 )
37
38
{
39
+ setAcceptDrops (true );
38
40
setContextMenuPolicy (Qt::CustomContextMenu);
39
41
this ->connect (this , SIGNAL (customContextMenuRequested (const QPoint&)), SLOT (showCustomContextMenu (const QPoint&)));
40
42
@@ -52,6 +54,8 @@ NodalView::NodalView(QWidget* parent)
52
54
53
55
54
56
setZoom (1.0 );
57
+
58
+ m_menuAdd = nullptr ;
55
59
}
56
60
57
61
void NodalView::init ()
@@ -74,55 +78,13 @@ void NodalView::setZoom(qreal zoom)
74
78
75
79
void NodalView::save (QString fileName, const qreal duration)
76
80
{
77
- /* QJsonArray componentArray;
78
- // save data
79
- for(int i = 0; i < m_nodeList.size(); i++)
80
- {
81
- QJsonArray inputArray;
82
- for(int k = 0; k < m_nodeList[i]->getInputCount(); k++)
83
- {
84
- PinInputItem* pin = m_nodeList[i]->getInput(k);
85
-
86
- Component* link = pin->input()->getComponent();
87
- int linkIndex = -1;
88
- if(link != nullptr)
89
- {
90
- for(int p = 0; p < m_nodeList.size() && linkIndex < 0; p++)
91
- {
92
- if(m_nodeList[p]->component() == link)
93
- {
94
- linkIndex = p;
95
- }
96
- }
97
- }
98
-
99
- QJsonObject input;
100
- input["name"] = pin->input()->getName();
101
- input["value"] = pin->input()->getDefaultValue();
102
- input["link"] = linkIndex;
103
-
104
- inputArray.append(input);
105
- }
106
-
107
- QJsonObject component;
108
- component["id"] = i;
109
- component["x"] = m_nodeList[i]->x();
110
- component["y"] = m_nodeList[i]->y();
111
- component["name"] = m_nodeList[i]->component()->getName();
112
- component["inputs"] = inputArray;
113
-
114
- componentArray.append(component);
115
- }*/
116
-
117
81
QJsonArray componentArray = NodeItem::NodeArrayToJson (m_nodeList);
118
82
119
-
120
83
QJsonObject root;
121
84
root[" duration" ] = duration;
122
85
root[" version" ] = 1 ;
123
86
root[" components" ] = componentArray;
124
87
125
-
126
88
QJsonDocument json (root);
127
89
QByteArray data = json.toJson ();
128
90
@@ -240,6 +202,7 @@ NodeItem* NodalView::createComponent(QString componentName, qreal width)
240
202
{
241
203
setDirty ();
242
204
m_nextCreationPosition += QPoint (20 , 20 );
205
+ qDebug () << " Create Component" ;
243
206
return createNode (componentName, width);
244
207
}
245
208
@@ -259,11 +222,6 @@ int NodalView::clearItems(int from)
259
222
nbRemoved++;
260
223
}
261
224
262
- // if(nbRemoved > 0)
263
- // {
264
- // setDirty();
265
- // }
266
-
267
225
return nbRemoved;
268
226
}
269
227
@@ -322,22 +280,6 @@ void NodalView::mousePressEvent(QMouseEvent* event)
322
280
setDragMode (QGraphicsView::NoDrag);
323
281
QGraphicsView::mousePressEvent (event);
324
282
}
325
-
326
- /* if(event->button() == Qt::MidButton)
327
- {
328
- m_lastMousePos = event->pos();
329
- m_dragging = true;
330
- m_startScenePos = mapToScene(event->pos());
331
- m_startMousePos = event->pos();
332
- }
333
- else if(event->button() == Qt::LeftButton)
334
- {
335
- m_rubberDrag = true;
336
- m_startMousePos = event->pos();
337
- m_rubberBand.move(event->pos());
338
- m_rubberBand.resize(0, 0);
339
- m_rubberBand.show();
340
- }*/
341
283
}
342
284
343
285
void NodalView::mouseReleaseEvent (QMouseEvent *event)
@@ -358,23 +300,6 @@ void NodalView::mouseReleaseEvent(QMouseEvent *event)
358
300
359
301
QGraphicsView::mouseReleaseEvent (event);
360
302
setDragMode (QGraphicsView::NoDrag);
361
- /* if(event->button() == Qt::MidButton)
362
- {
363
- m_dragging = false;
364
- }
365
- else if(event->button() == Qt::LeftButton)
366
- {
367
- m_rubberDrag = false;
368
- m_rubberBand.hide();
369
- //scene()->clearSelection();
370
-
371
-
372
- QPointF topLeft = mapToScene(qMin(event->pos().x(), m_startMousePos.x()), qMin(event->pos().y(), m_startMousePos.y()));
373
- QPointF bottomRight = mapToScene(qMax(event->pos().x(), m_startMousePos.x()), qMax(event->pos().y(), m_startMousePos.y()));
374
- QPainterPath shape;
375
- shape.addRect(topLeft.x(), topLeft.y(), bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y());
376
- scene()->setSelectionArea(shape);
377
- }*/
378
303
}
379
304
380
305
void NodalView::mouseMoveEvent (QMouseEvent* event)
@@ -396,24 +321,27 @@ void NodalView::mouseMoveEvent(QMouseEvent* event)
396
321
QGraphicsView::mouseMoveEvent (event);
397
322
}
398
323
399
- /* if(m_rubberDrag)
400
- {
401
- QPoint topLeft(qMin(event->pos().x(), m_startMousePos.x()), qMin(event->pos().y(), m_startMousePos.y()));
402
- QPoint bottomRight(qMax(event->pos().x(), m_startMousePos.x()), qMax(event->pos().y(), m_startMousePos.y()));
324
+ m_lastMousePos = event->pos ();
325
+ }
403
326
404
- m_rubberBand.move(topLeft);
405
- m_rubberBand.resize(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y());
406
- }*/
327
+ void NodalView::dragEnterEvent (QDragEnterEvent *event)
328
+ {
329
+ Q_UNUSED (event);
330
+ }
407
331
408
- m_lastMousePos = event->pos ();
332
+ void NodalView::dropEvent (QDropEvent *event)
333
+ {
334
+ Q_UNUSED (event);
335
+ // m_nextCreationPosition = mapToScene(event->pos());
336
+
337
+ // const QMimeData* data = event->mimeData();
409
338
}
410
339
411
340
void NodalView::drawBackground (QPainter *painter, const QRectF &rect)
412
341
{
413
342
QVector<QLine> lines;
414
343
QVector<QLine> mainLines;
415
344
416
- // int unit = 10;
417
345
int nbLineWidth = qCeil (rect.width () / gridUnit);
418
346
int nbLineHeight = qCeil (rect.height () / gridUnit);
419
347
int startX = qCeil (rect.x () / gridUnit) * gridUnit;
@@ -548,45 +476,6 @@ void NodalView::copyComponents()
548
476
549
477
// create a json array with all selected components
550
478
QJsonArray componentArray = NodeItem::NodeArrayToJson (selectedNodes);
551
- /* QJsonArray componentArray;
552
- for(int i = 0; i < selectedNodes.size(); i++)
553
- {
554
- QJsonArray inputArray;
555
- for(int k = 0; k < selectedNodes[i]->getInputCount(); k++)
556
- {
557
- PinInputItem* pin = selectedNodes[i]->getInput(k);
558
-
559
- Component* link = pin->input()->getComponent();
560
- int linkIndex = -1;
561
- if(link != nullptr)
562
- {
563
- for(int p = 0; p < selectedNodes.size() && linkIndex < 0; p++)
564
- {
565
- if(selectedNodes[p]->component() == link)
566
- {
567
- linkIndex = p;
568
- }
569
- }
570
- }
571
-
572
- QJsonObject input;
573
- input["name"] = pin->input()->getName();
574
- input["value"] = pin->input()->getDefaultValue();
575
- input["link"] = linkIndex;
576
-
577
- inputArray.append(input);
578
- }
579
-
580
- QJsonObject component;
581
- component["id"] = i;
582
- component["x"] = selectedNodes[i]->x();
583
- component["y"] = selectedNodes[i]->y();
584
- component["name"] = selectedNodes[i]->component()->getName();
585
- component["inputs"] = inputArray;
586
-
587
- componentArray.append(component);
588
- }*/
589
-
590
479
591
480
QJsonObject root;
592
481
root[" components" ] = componentArray;
@@ -762,8 +651,11 @@ void NodalView::updateZoomView()
762
651
763
652
void NodalView::showCustomContextMenu (const QPoint& pos)
764
653
{
654
+ m_contextMenu = true ;
655
+
765
656
// for most widgets
766
657
QPoint globalPos = mapToGlobal (pos);
658
+ m_ContextPosition = mapToScene (pos);
767
659
// for QAbstractScrollArea and derived classes you would use:
768
660
// QPoint globalPos = myWidget->viewport()->mapToGlobal(pos);
769
661
@@ -772,6 +664,12 @@ void NodalView::showCustomContextMenu(const QPoint& pos)
772
664
773
665
QMenu myMenu;
774
666
667
+ if (m_menuAdd != nullptr )
668
+ {
669
+ myMenu.addMenu (m_menuAdd);
670
+ qDebug () << " Add menu in context" ;
671
+ }
672
+
775
673
QGraphicsItem* item = scene ()->itemAt (mapToScene (pos), QTransform ());
776
674
PinItem* pin = dynamic_cast <PinItem*>(item);
777
675
LinkItem* link = dynamic_cast <LinkItem*>(item);
@@ -806,8 +704,22 @@ void NodalView::showCustomContextMenu(const QPoint& pos)
806
704
}
807
705
else
808
706
{
707
+ if (m_menuAdd != nullptr )
708
+ {
709
+ bool addAction = false ;
710
+ for (QAction* act : m_menuAdd->actions ())
711
+ {
712
+ addAction |= (act == selectedAction);
713
+ }
714
+ if (addAction)
715
+ {
716
+ m_nextCreationPosition = m_ContextPosition;
717
+ }
718
+ }
719
+
809
720
// nothing was chosen
810
721
}
722
+ m_contextMenu = false ;
811
723
}
812
724
813
725
@@ -829,10 +741,20 @@ void NodalView::autocomputeSceneSize()
829
741
830
742
NodeItem *NodalView::createNode (QString componentName, qreal width)
831
743
{
744
+ QPointF position;
745
+ if (m_contextMenu)
746
+ {
747
+ position = m_ContextPosition;
748
+ }
749
+ else
750
+ {
751
+ position = m_nextCreationPosition;
752
+ }
753
+
832
754
Component* component = ComponentFactory::CreateComponent (componentName);
833
755
NodeItem* item = new NodeItem ();
834
756
item->setComponent (component);
835
- item->setPos (m_nextCreationPosition );
757
+ item->setPos (position );
836
758
item->setWidth (width);
837
759
838
760
addNodeItem (item);
0 commit comments