nodeview: attach new nodes to mouse for easier placement
This commit is contained in:
@@ -20,13 +20,16 @@
|
||||
|
||||
#include "nodeview.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
|
||||
#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_) {
|
||||
|
||||
@@ -88,6 +88,11 @@ signals:
|
||||
*/
|
||||
void SelectionChanged(QList<Node*> selected_nodes);
|
||||
|
||||
protected:
|
||||
virtual void mousePressEvent(QMouseEvent *event) override;
|
||||
|
||||
virtual void mouseMoveEvent(QMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
QList<Node*> GetNodeDirectDescendants(Node* n, const QList<Node*> connected_nodes, QList<Node*>& 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_;
|
||||
|
||||
Reference in New Issue
Block a user