diff --git a/include/QtNodes/internal/GroupGraphicsObject.hpp b/include/QtNodes/internal/GroupGraphicsObject.hpp index 9a2e918f..474c3f4d 100644 --- a/include/QtNodes/internal/GroupGraphicsObject.hpp +++ b/include/QtNodes/internal/GroupGraphicsObject.hpp @@ -180,6 +180,9 @@ class NODE_EDITOR_PUBLIC GroupGraphicsObject /** @copydoc QGraphicsItem::hoverLeaveEvent() */ void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override; + /** @copydoc QGraphicsItem::mousePressEvent() */ + void mousePressEvent(QGraphicsSceneMouseEvent *event) override; + /** @copydoc QGraphicsItem::mouseMoveEvent() */ void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; diff --git a/include/QtNodes/internal/UndoCommands.hpp b/include/QtNodes/internal/UndoCommands.hpp index 913e0d10..fbe15405 100644 --- a/include/QtNodes/internal/UndoCommands.hpp +++ b/include/QtNodes/internal/UndoCommands.hpp @@ -99,6 +99,10 @@ class NODE_EDITOR_PUBLIC ConnectCommand : public QUndoCommand class NODE_EDITOR_PUBLIC MoveNodeCommand : public QUndoCommand { public: + /// Starts a new move, for example when a drag begins. Every command that is created + /// afterwards belongs to this move. It does not merge with the commands of earlier moves. + static void beginMove(); + /// Moves the currently selected nodes. MoveNodeCommand(BasicGraphicsScene *scene, QPointF const &diff); @@ -117,7 +121,8 @@ class NODE_EDITOR_PUBLIC MoveNodeCommand : public QUndoCommand int id() const override; /** - * Several sequential movements could be merged into one command. + * Several sequential movements of one move are merged into one command. + * Movements from different moves, see beginMove(), are not merged. */ bool mergeWith(QUndoCommand const *c) override; @@ -125,6 +130,10 @@ class NODE_EDITOR_PUBLIC MoveNodeCommand : public QUndoCommand BasicGraphicsScene *_scene; std::unordered_set _nodeIds; QPointF _diff; + /// QUndoStack merges commands that follow each other and have the same id(). It does not + /// know where a drag ends. Without this id, two drags of the same nodes merged into one + /// undo step. The commands of one move have the same _moveId. See beginMove(). + unsigned int _moveId; }; } // namespace QtNodes diff --git a/src/GroupGraphicsObject.cpp b/src/GroupGraphicsObject.cpp index 4c626209..1aac8e61 100644 --- a/src/GroupGraphicsObject.cpp +++ b/src/GroupGraphicsObject.cpp @@ -190,6 +190,14 @@ void GroupGraphicsObject::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) setHovered(false); } +void GroupGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + // A new press starts a new drag. Its moves must not merge into an earlier undo step. + MoveNodeCommand::beginMove(); + + QGraphicsRectItem::mousePressEvent(event); +} + void GroupGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (event->lastPos() != event->pos()) { diff --git a/src/NodeGraphicsObject.cpp b/src/NodeGraphicsObject.cpp index 2d2ddb21..03f5f696 100644 --- a/src/NodeGraphicsObject.cpp +++ b/src/NodeGraphicsObject.cpp @@ -201,6 +201,9 @@ void NodeGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *event) // Position updates from setup or undo must not count as a drag. nodeScene()->_nodeDrag = false; + // A new press starts a new drag. Its moves must not merge into an earlier undo step. + MoveNodeCommand::beginMove(); + if (_graphModel.nodeFlags(_nodeId) & NodeFlag::Locked) { return; } diff --git a/src/UndoCommands.cpp b/src/UndoCommands.cpp index 6c32a92a..452c2e1c 100644 --- a/src/UndoCommands.cpp +++ b/src/UndoCommands.cpp @@ -568,9 +568,22 @@ void ConnectCommand::redo() //------ +namespace { + +// Only the equality of two ids matters, so one counter for all scenes is enough. +unsigned int currentMoveId = 0; + +} // namespace + +void MoveNodeCommand::beginMove() +{ + ++currentMoveId; +} + MoveNodeCommand::MoveNodeCommand(BasicGraphicsScene *scene, QPointF const &diff) : _scene(scene) , _diff(diff) + , _moveId(currentMoveId) { for (QGraphicsItem *item : _scene->selectedItems()) { if (auto n = qgraphicsitem_cast(item)) { @@ -585,6 +598,7 @@ MoveNodeCommand::MoveNodeCommand(BasicGraphicsScene *scene, : _scene(scene) , _nodeIds(nodeIds) , _diff(diff) + , _moveId(currentMoveId) {} void MoveNodeCommand::undo() @@ -626,7 +640,7 @@ bool MoveNodeCommand::mergeWith(QUndoCommand const *c) { auto mc = static_cast(c); - if (_nodeIds == mc->_nodeIds) { + if (_nodeIds == mc->_nodeIds && _moveId == mc->_moveId) { _diff += mc->_diff; return true; } diff --git a/test/src/TestNodeDrag.cpp b/test/src/TestNodeDrag.cpp index a9f3227f..71bad1d8 100644 --- a/test/src/TestNodeDrag.cpp +++ b/test/src/TestNodeDrag.cpp @@ -68,6 +68,38 @@ TEST_CASE("Interactive node drag syncs the model", "[ui][visual][drag]") CHECK(item->pos() == delta); } + SECTION("Two separate drags of the same node are two undo steps") + { + UITestHelper::simulateMouseDrag(&view, start, start + delta); + QPointF const second = start + delta; + UITestHelper::simulateMouseDrag(&view, second, second + delta); + + REQUIRE(scene.undoStack().count() == 2); + CHECK(item->pos() == delta + delta); + + scene.undoStack().undo(); + CHECK(item->pos() == delta); + CHECK(model->nodeData(nodeId, NodeRole::Position).value() == delta); + + scene.undoStack().undo(); + CHECK(item->pos() == QPointF(0, 0)); + } + + SECTION("One drag with many mouse moves is one undo step") + { + UITestHelper::simulateMousePress(&view, start); + for (int i = 1; i <= 10; ++i) { + UITestHelper::simulateMouseMove(&view, start + delta * (i / 10.0)); + } + UITestHelper::simulateMouseRelease(&view, start + delta); + + CHECK(scene.undoStack().count() == 1); + CHECK(item->pos() == delta); + + scene.undoStack().undo(); + CHECK(item->pos() == QPointF(0, 0)); + } + SECTION("A click without movement leaves model and undo stack untouched") { UITestHelper::simulateMousePress(&view, start);