From 186973ff2094d00e5b463680f22793d9c2e67ff9 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 13 Dec 2019 03:06:12 +1100 Subject: [PATCH] implemented final connections to allow creating new nodes in the nodeview --- app/node/factory.cpp | 17 ++++++++++++++++- app/node/factory.h | 2 ++ app/widget/nodeview/nodeview.cpp | 23 +++++++++++++++++++++-- app/widget/nodeview/nodeview.h | 5 +++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/node/factory.cpp b/app/node/factory.cpp index 97114b992..cbe4b7b6b 100644 --- a/app/node/factory.cpp +++ b/app/node/factory.cpp @@ -37,7 +37,10 @@ Menu *NodeFactory::CreateMenu() { Menu* menu = new Menu(); - foreach (Node* n, library_) { + for (int i=0;iRetranslate(); QStringList path = n->Category().split('/'); @@ -72,12 +75,24 @@ Menu *NodeFactory::CreateMenu() // Add entry to menu QAction* a = destination->InsertAlphabetically(n->Name()); + a->setData(i); a->setToolTip(n->Description()); } return menu; } +Node* NodeFactory::CreateFromMenuAction(QAction *action) +{ + int library_index = action->data().toInt(); + + if (library_index >= 0 && library_index < library_.size()) { + return library_.at(library_index)->copy(); + } + + return nullptr; +} + Node *NodeFactory::CreateInternal(const NodeFactory::InternalID &id) { switch (id) { diff --git a/app/node/factory.h b/app/node/factory.h index a1f2acfcc..f592ce0a1 100644 --- a/app/node/factory.h +++ b/app/node/factory.h @@ -35,6 +35,8 @@ public: static Menu* CreateMenu(); + static Node *CreateFromMenuAction(QAction* action); + private: static Node* CreateInternal(const InternalID& id); diff --git a/app/widget/nodeview/nodeview.cpp b/app/widget/nodeview/nodeview.cpp index e5e85fc16..78840e8ba 100644 --- a/app/widget/nodeview/nodeview.cpp +++ b/app/widget/nodeview/nodeview.cpp @@ -199,6 +199,25 @@ void NodeView::SceneSelectionChangedSlot() void NodeView::ShowContextMenu(const QPoint &pos) { - Menu* m = NodeFactory::CreateMenu(); - m->exec(pos); + if (!graph_) { + return; + } + + Menu* m = new Menu(); + + Menu* add_menu = NodeFactory::CreateMenu(); + add_menu->setTitle(tr("Add")); + connect(add_menu, SIGNAL(triggered(QAction*)), this, SLOT(CreateNodeSlot(QAction*))); + m->addMenu(add_menu); + + m->exec(mapToGlobal(pos)); +} + +void NodeView::CreateNodeSlot(QAction *action) +{ + Node* new_node = NodeFactory::CreateFromMenuAction(action); + + if (new_node) { + graph_->AddNode(new_node); + } } diff --git a/app/widget/nodeview/nodeview.h b/app/widget/nodeview/nodeview.h index c5acb7beb..c3a719694 100644 --- a/app/widget/nodeview/nodeview.h +++ b/app/widget/nodeview/nodeview.h @@ -137,6 +137,11 @@ private slots: */ void ShowContextMenu(const QPoint &pos); + /** + * @brief Receiver for when the user requests a new node from the add menu + */ + void CreateNodeSlot(QAction* action); + }; #endif // NODEVIEW_H