Skip to content

Commit 6cc9127

Browse files
committed
Added actions in context menu to add components at mouse position
1 parent 4c9f2db commit 6cc9127

File tree

2 files changed

+61
-131
lines changed

2 files changed

+61
-131
lines changed

UI/nodalview.cpp

Lines changed: 51 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ NodalView::NodalView(QWidget* parent)
3232
: QGraphicsView (parent),
3333
m_dragging(false),
3434
m_rightClickPressed(false),
35+
m_contextMenu(false),
3536
m_zoom(1.0),
3637
m_nextCreationPosition(0, 0)
3738
{
39+
setAcceptDrops(true);
3840
setContextMenuPolicy(Qt::CustomContextMenu);
3941
this->connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showCustomContextMenu(const QPoint&)));
4042

@@ -52,6 +54,8 @@ NodalView::NodalView(QWidget* parent)
5254

5355

5456
setZoom(1.0);
57+
58+
m_menuAdd = nullptr;
5559
}
5660

5761
void NodalView::init()
@@ -74,55 +78,13 @@ void NodalView::setZoom(qreal zoom)
7478

7579
void NodalView::save(QString fileName, const qreal duration)
7680
{
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-
11781
QJsonArray componentArray = NodeItem::NodeArrayToJson(m_nodeList);
11882

119-
12083
QJsonObject root;
12184
root["duration"] = duration;
12285
root["version"] = 1;
12386
root["components"] = componentArray;
12487

125-
12688
QJsonDocument json(root);
12789
QByteArray data = json.toJson();
12890

@@ -240,6 +202,7 @@ NodeItem* NodalView::createComponent(QString componentName, qreal width)
240202
{
241203
setDirty();
242204
m_nextCreationPosition += QPoint(20, 20);
205+
qDebug() << "Create Component";
243206
return createNode(componentName, width);
244207
}
245208

@@ -259,11 +222,6 @@ int NodalView::clearItems(int from)
259222
nbRemoved++;
260223
}
261224

262-
// if(nbRemoved > 0)
263-
// {
264-
// setDirty();
265-
// }
266-
267225
return nbRemoved;
268226
}
269227

@@ -322,22 +280,6 @@ void NodalView::mousePressEvent(QMouseEvent* event)
322280
setDragMode(QGraphicsView::NoDrag);
323281
QGraphicsView::mousePressEvent(event);
324282
}
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-
}*/
341283
}
342284

343285
void NodalView::mouseReleaseEvent(QMouseEvent *event)
@@ -358,23 +300,6 @@ void NodalView::mouseReleaseEvent(QMouseEvent *event)
358300

359301
QGraphicsView::mouseReleaseEvent(event);
360302
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-
}*/
378303
}
379304

380305
void NodalView::mouseMoveEvent(QMouseEvent* event)
@@ -396,24 +321,27 @@ void NodalView::mouseMoveEvent(QMouseEvent* event)
396321
QGraphicsView::mouseMoveEvent(event);
397322
}
398323

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+
}
403326

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+
}
407331

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();
409338
}
410339

411340
void NodalView::drawBackground(QPainter *painter, const QRectF &rect)
412341
{
413342
QVector<QLine> lines;
414343
QVector<QLine> mainLines;
415344

416-
//int unit = 10;
417345
int nbLineWidth = qCeil(rect.width() / gridUnit);
418346
int nbLineHeight = qCeil(rect.height() / gridUnit);
419347
int startX = qCeil(rect.x() / gridUnit) * gridUnit;
@@ -548,45 +476,6 @@ void NodalView::copyComponents()
548476

549477
// create a json array with all selected components
550478
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-
590479

591480
QJsonObject root;
592481
root["components"] = componentArray;
@@ -762,8 +651,11 @@ void NodalView::updateZoomView()
762651

763652
void NodalView::showCustomContextMenu(const QPoint& pos)
764653
{
654+
m_contextMenu = true;
655+
765656
// for most widgets
766657
QPoint globalPos = mapToGlobal(pos);
658+
m_ContextPosition = mapToScene(pos);
767659
// for QAbstractScrollArea and derived classes you would use:
768660
// QPoint globalPos = myWidget->viewport()->mapToGlobal(pos);
769661

@@ -772,6 +664,12 @@ void NodalView::showCustomContextMenu(const QPoint& pos)
772664

773665
QMenu myMenu;
774666

667+
if(m_menuAdd != nullptr)
668+
{
669+
myMenu.addMenu(m_menuAdd);
670+
qDebug() << "Add menu in context";
671+
}
672+
775673
QGraphicsItem* item = scene()->itemAt(mapToScene(pos), QTransform());
776674
PinItem* pin = dynamic_cast<PinItem*>(item);
777675
LinkItem* link = dynamic_cast<LinkItem*>(item);
@@ -806,8 +704,22 @@ void NodalView::showCustomContextMenu(const QPoint& pos)
806704
}
807705
else
808706
{
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+
809720
// nothing was chosen
810721
}
722+
m_contextMenu = false;
811723
}
812724

813725

@@ -829,10 +741,20 @@ void NodalView::autocomputeSceneSize()
829741

830742
NodeItem *NodalView::createNode(QString componentName, qreal width)
831743
{
744+
QPointF position;
745+
if(m_contextMenu)
746+
{
747+
position = m_ContextPosition;
748+
}
749+
else
750+
{
751+
position = m_nextCreationPosition;
752+
}
753+
832754
Component* component = ComponentFactory::CreateComponent(componentName);
833755
NodeItem* item = new NodeItem();
834756
item->setComponent(component);
835-
item->setPos(m_nextCreationPosition);
757+
item->setPos(position);
836758
item->setWidth(width);
837759

838760
addNodeItem(item);

UI/nodalview.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,16 @@ class NodalView : public QGraphicsView
5050

5151
NodeItem* getOutput() { return m_nodeList[0]; }
5252

53+
void setMenuAdd(QMenu* menuAdd) { m_menuAdd = menuAdd; }
54+
5355
protected:
5456
virtual void keyPressEvent(QKeyEvent * event) override;
5557
virtual void wheelEvent(QWheelEvent* event) override;
5658
virtual void mousePressEvent(QMouseEvent* event) override;
5759
virtual void mouseReleaseEvent(QMouseEvent *event) override;
5860
virtual void mouseMoveEvent(QMouseEvent* event) override;
61+
virtual void dragEnterEvent(QDragEnterEvent* event) override;
62+
virtual void dropEvent(QDropEvent* event) override;
5963

6064
virtual void drawBackground(QPainter * painter, const QRectF & rect) override;
6165

@@ -83,6 +87,7 @@ protected slots:
8387
private:
8488
bool m_dragging;
8589
bool m_rightClickPressed;
90+
bool m_contextMenu;
8691
QPoint m_lastMousePos;
8792
QPoint m_startMousePos;
8893
QPointF m_startScenePos;
@@ -93,10 +98,13 @@ protected slots:
9398
qreal m_zoom;
9499
QPoint m_translation;
95100

96-
QPoint m_nextCreationPosition;
97-
QPoint m_nextPastePosition;
101+
QPointF m_nextCreationPosition;
102+
QPointF m_nextPastePosition;
103+
QPointF m_ContextPosition;
98104
QVector<NodeItem*> m_nodeList;
99105

106+
QMenu* m_menuAdd;
107+
100108

101109
public:
102110
static const int gridUnit;

0 commit comments

Comments
 (0)