diff --git a/app/widget/nodeview/nodeview.cpp b/app/widget/nodeview/nodeview.cpp index 39e0c7c5e..d4396a340 100644 --- a/app/widget/nodeview/nodeview.cpp +++ b/app/widget/nodeview/nodeview.cpp @@ -20,13 +20,16 @@ #include "nodeview.h" +#include + #include "core.h" #include "nodeviewundo.h" #include "node/factory.h" NodeView::NodeView(QWidget *parent) : QGraphicsView(parent), - graph_(nullptr) + graph_(nullptr), + attached_item_(nullptr) { setScene(&scene_); setDragMode(RubberBandDrag); @@ -37,6 +40,8 @@ NodeView::NodeView(QWidget *parent) : connect(this, &NodeView::customContextMenuRequested, this, &NodeView::ShowContextMenu); connect(&reorganize_timer_, &QTimer::timeout, &reorganize_timer_, &QTimer::stop); connect(&reorganize_timer_, &QTimer::timeout, this, &NodeView::Reorganize); + + setMouseTracking(true); } NodeView::~NodeView() @@ -210,6 +215,24 @@ void NodeView::ItemsChanged() } } +void NodeView::mousePressEvent(QMouseEvent *event) +{ + if (attached_item_) { + DetachItemFromCursor(); + } + + QGraphicsView::mousePressEvent(event); +} + +void NodeView::mouseMoveEvent(QMouseEvent *event) +{ + QGraphicsView::mouseMoveEvent(event); + + if (attached_item_) { + attached_item_->setPos(mapToScene(event->pos())); + } +} + void NodeView::SceneSelectionChangedSlot() { // Get the scene's selected items and convert it into a list of selected nodes @@ -251,6 +274,9 @@ void NodeView::CreateNodeSlot(QAction *action) if (new_node) { Core::instance()->undo_stack()->push(new NodeAddCommand(graph_, new_node)); + + NodeViewItem* item = NodeToUIObject(new_node); + AttachItemToCursor(item); } } @@ -497,6 +523,18 @@ void NodeView::QueueReorganize() reorganize_timer_.start(20); } +void NodeView::AttachItemToCursor(NodeViewItem *item) +{ + attached_item_ = item; + + setMouseTracking(attached_item_); +} + +void NodeView::DetachItemFromCursor() +{ + AttachItemToCursor(nullptr); +} + void NodeView::Reorganize() { if (!graph_) { diff --git a/app/widget/nodeview/nodeview.h b/app/widget/nodeview/nodeview.h index bd40881b3..8b19e0684 100644 --- a/app/widget/nodeview/nodeview.h +++ b/app/widget/nodeview/nodeview.h @@ -88,6 +88,11 @@ signals: */ void SelectionChanged(QList selected_nodes); +protected: + virtual void mousePressEvent(QMouseEvent *event) override; + + virtual void mouseMoveEvent(QMouseEvent *event) override; + private: QList GetNodeDirectDescendants(Node* n, const QList connected_nodes, QList& processed_nodes); @@ -99,8 +104,14 @@ private: void QueueReorganize(); + void AttachItemToCursor(NodeViewItem* item); + + void DetachItemFromCursor(); + NodeGraph* graph_; + NodeViewItem* attached_item_; + QGraphicsScene scene_; QTimer reorganize_timer_;