From aba479853499ff014c6de1e656010ea83e26e783 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Wed, 2 Dec 2026 12:51:55 +0100 Subject: [PATCH 1/3] Decide the drag into a group from the current node position A node dragged over a group left the group only when a later move still collided with the group. A move that took the node far away in one step kept the old state, and the node joined the group at the release. The node now checks on each move whether it still touches the group, and the release always clears the drag state. The group is held in a QPointer, so a group deleted during the drag is not used at the release. See #537 --- docs/migration/v3-to-v4.rst | 29 ++-- .../QtNodes/internal/NodeGraphicsObject.hpp | 5 +- src/NodeGraphicsObject.cpp | 48 +++---- test/CMakeLists.txt | 1 + test/src/TestGroupDrag.cpp | 134 ++++++++++++++++++ 5 files changed, 167 insertions(+), 50 deletions(-) create mode 100644 test/src/TestGroupDrag.cpp diff --git a/docs/migration/v3-to-v4.rst b/docs/migration/v3-to-v4.rst index 8cb16bf9..231b0f4f 100644 --- a/docs/migration/v3-to-v4.rst +++ b/docs/migration/v3-to-v4.rst @@ -29,27 +29,12 @@ The model has no group data, so: that was deleted and restored loses its name and its id. - Copy and paste of a group loses its name and lock state. -The graphics items point to each other, so: - -- A node that is dragged over a group keeps a raw pointer to it. If the group - is deleted during the drag, the pointer dangles. -- The drag-into-group state is reset only while the node still touches the - group. If you move a node away from a group in one big step and release it, - the node joins the group. -- The group item changes its own geometry inside ``paint()``. That schedules - the next paint, so a view with a group repaints all the time, also when - idle. This costs CPU. - Other problems: -- ``BasicGraphicsScene::loadItems()`` with a paste position moves plain nodes - in the graphics only, and the model keeps the old position. The offset of a - group is wrong, because the group item is always at (0,0). The built-in - paste does not use this path, so only code that calls ``loadItems()`` itself - is affected. -- Two separate drags of the same node are merged into one undo step. This is - the behavior of the upstream code. A fix needs a decision on what one undo - step is. +- ``BasicGraphicsScene::loadItems()`` with a paste position computes the + offset of a group wrongly, because the group item is always at (0,0). The + built-in paste does not use this path, so only code that calls + ``loadItems()`` itself is affected. Why these are not fixed in 3.x ------------------------------ @@ -68,6 +53,12 @@ users today: (#536). - Crashes in the group lifetime are fixed (#540): a model reset with a group, the double delete of the group item, and a node in more than one group. +- A node that is dragged over a group joins it only if it touches the group + at the release. A group that is deleted during the drag is not used (#537). +- A visible group does not repaint all the time (#537). +- Two separate drags of the same node are two undo steps (#538). +- ``BasicGraphicsScene::loadItems()`` with a paste position moves plain nodes + through the model, so the model and the item agree. - ``GroupGraphicsObject`` is exported. On a Windows shared build, code that called its methods did not link. diff --git a/include/QtNodes/internal/NodeGraphicsObject.hpp b/include/QtNodes/internal/NodeGraphicsObject.hpp index 2e8c88ff..a04e1ccc 100644 --- a/include/QtNodes/internal/NodeGraphicsObject.hpp +++ b/include/QtNodes/internal/NodeGraphicsObject.hpp @@ -4,6 +4,7 @@ #include "NodeGroup.hpp" #include "NodeState.hpp" #include +#include #include #include #include @@ -108,8 +109,8 @@ class NODE_EDITOR_PUBLIC NodeGraphicsObject : public QGraphicsObject bool _locked; - bool _draggingIntoGroup; - GroupGraphicsObject *_possibleGroup; + // The group that the node joins at the release. + QPointer _possibleGroup; QRectF _originalGroupSize; // either nullptr or owned by parent QGraphicsItem diff --git a/src/NodeGraphicsObject.cpp b/src/NodeGraphicsObject.cpp index b40e969a..f2f8a97c 100644 --- a/src/NodeGraphicsObject.cpp +++ b/src/NodeGraphicsObject.cpp @@ -24,7 +24,6 @@ NodeGraphicsObject::NodeGraphicsObject(BasicGraphicsScene &scene, NodeId nodeId) , _graphModel(scene.graphModel()) , _nodeState(*this) , _locked(false) - , _draggingIntoGroup(false) , _possibleGroup(nullptr) , _originalGroupSize() , _proxyWidget(nullptr) @@ -334,29 +333,21 @@ void NodeGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event) if (auto nodeGroup = _nodeGroup.lock(); nodeGroup) { nodeGroup->groupGraphicsObject().moveConnections(); } else { - // if it intersects with a group, expand group - QList overlapItems = collidingItems(); - for (auto &item : overlapItems) { - auto ggo = qgraphicsitem_cast(item); - if (ggo != nullptr) { - if (!ggo->locked()) { - if (!_draggingIntoGroup) { - _draggingIntoGroup = true; - _possibleGroup = ggo; - _originalGroupSize = _possibleGroup->mapRectToScene(ggo->rect()); - _possibleGroup->setPossibleChild(this); - break; - } else { - if (ggo == _possibleGroup) { - if (!boundingRect().intersects( - mapRectFromScene(_originalGroupSize))) { - _draggingIntoGroup = false; - _originalGroupSize = QRectF(); - _possibleGroup->unsetPossibleChild(); - _possibleGroup = nullptr; - } - } - } + // _originalGroupSize is the box before it grew to show this node. + if (_possibleGroup) { + if (!boundingRect().intersects(mapRectFromScene(_originalGroupSize))) { + _possibleGroup->unsetPossibleChild(); + _possibleGroup = nullptr; + _originalGroupSize = QRectF(); + } + } else { + for (QGraphicsItem *item : collidingItems()) { + auto ggo = qgraphicsitem_cast(item); + if (ggo && !ggo->locked()) { + _possibleGroup = ggo; + _originalGroupSize = ggo->mapRectToScene(ggo->rect()); + ggo->setPossibleChild(this); + break; } } } @@ -382,13 +373,12 @@ void NodeGraphicsObject::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) // position connections precisely after fast node move moveConnections(); - if (nodeScene()->groupingEnabled() && _draggingIntoGroup && _possibleGroup - && _nodeGroup.expired()) { - nodeScene()->addNodeToGroup(_nodeId, _possibleGroup->group().id()); + if (_possibleGroup) { + if (nodeScene()->groupingEnabled() && _nodeGroup.expired()) + nodeScene()->addNodeToGroup(_nodeId, _possibleGroup->group().id()); _possibleGroup->unsetPossibleChild(); - _draggingIntoGroup = false; - _originalGroupSize = QRectF(); _possibleGroup = nullptr; + _originalGroupSize = QRectF(); } nodeScene()->nodeClicked(_nodeId); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 22e5db74..16c57185 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -38,6 +38,7 @@ add_executable(test_nodes src/TestSceneSetup.cpp src/TestLockAndPaste.cpp src/TestGroupRepaint.cpp + src/TestGroupDrag.cpp src/TestNotifications.cpp src/TestDataInvalidated.cpp src/TestNodeHover.cpp diff --git a/test/src/TestGroupDrag.cpp b/test/src/TestGroupDrag.cpp new file mode 100644 index 00000000..3b167a33 --- /dev/null +++ b/test/src/TestGroupDrag.cpp @@ -0,0 +1,134 @@ +#include "ApplicationSetup.hpp" +#include "TestGraphModel.hpp" +#include "UITestHelper.hpp" + +#include +#include +#include +#include +#include + +#include + +#include + +using QtNodes::BasicGraphicsScene; +using QtNodes::GraphicsView; +using QtNodes::NodeGraphicsObject; +using QtNodes::NodeGroup; +using QtNodes::NodeId; +using QtNodes::NodeRole; + +namespace { + +/// A shown view with one unlocked group that holds node `member`, and node `outsider` +/// below the group. +struct GroupDragFixture +{ + TestGraphModel model; + BasicGraphicsScene scene{model}; + GraphicsView view{&scene}; + NodeId member = QtNodes::InvalidNodeId; + NodeId outsider = QtNodes::InvalidNodeId; + std::shared_ptr group; + + GroupDragFixture() + { + view.resize(800, 600); + view.show(); + REQUIRE(QTest::qWaitForWindowExposed(&view)); + + member = model.addNode("TestNode"); + outsider = model.addNode("TestNode"); + model.setNodeData(outsider, NodeRole::Position, QPointF(0, 250)); + + // No style is loaded in tests, so the default opacity is 0 and Qt would + // skip the nodes when hit-testing the simulated mouse events. + scene.nodeGraphicsObject(member)->setOpacity(1.0); + scene.nodeGraphicsObject(outsider)->setOpacity(1.0); + + std::vector nodes{scene.nodeGraphicsObject(member)}; + group = scene.createGroup(nodes, "Group").lock(); + REQUIRE(group); + // A locked group does not take new nodes. + group->groupGraphicsObject().lock(false); + UITestHelper::waitForUI(); + } + + NodeGraphicsObject &outsiderObject() { return *scene.nodeGraphicsObject(outsider); } + + QPointF outsiderCenter() { return outsiderObject().sceneBoundingRect().center(); } + + /// Presses on the outsider and moves it so that it overlaps the member. + QPointF pressAndMoveOntoGroup() + { + QPointF const start = outsiderCenter(); + QPointF const memberCenter = scene.nodeGraphicsObject(member)->sceneBoundingRect().center(); + QPointF const overGroup = memberCenter + QPointF(30, 30); + + UITestHelper::simulateMousePress(&view, start); + UITestHelper::simulateMouseMove(&view, overGroup); + + REQUIRE(outsiderObject().sceneBoundingRect().intersects( + group->groupGraphicsObject().sceneBoundingRect())); + return overGroup; + } +}; + +} // namespace + +TEST_CASE("A node dragged over a group and released away from it stays out of the group", + "[node-group][drag]") +{ + auto app = applicationSetup(); + GroupDragFixture f; + + QRectF const boxBefore = f.group->groupGraphicsObject().sceneBoundingRect(); + + QPointF const overGroup = f.pressAndMoveOntoGroup(); + + // One move takes the node far from the group. + QPointF const away = overGroup + QPointF(450, 0); + UITestHelper::simulateMouseMove(&f.view, away); + REQUIRE_FALSE(f.outsiderObject().sceneBoundingRect().intersects(boxBefore)); + + UITestHelper::simulateMouseRelease(&f.view, away); + UITestHelper::waitForUI(); + + CHECK(f.outsiderObject().nodeGroup().expired()); + CHECK(f.group->childNodes().size() == 1); + CHECK(f.group->groupGraphicsObject().sceneBoundingRect() == boxBefore); +} + +TEST_CASE("A node dragged over a group and released on it joins the group", "[node-group][drag]") +{ + auto app = applicationSetup(); + GroupDragFixture f; + + QPointF const overGroup = f.pressAndMoveOntoGroup(); + UITestHelper::simulateMouseRelease(&f.view, overGroup); + UITestHelper::waitForUI(); + + CHECK(f.outsiderObject().nodeGroup().lock() == f.group); +} + +TEST_CASE("A group deleted while a node is dragged over it is not used at the release", + "[node-group][drag][lifetime]") +{ + auto app = applicationSetup(); + GroupDragFixture f; + + QPointF const overGroup = f.pressAndMoveOntoGroup(); + + // Removing the last node deletes the group. + f.scene.removeNodeFromGroup(f.member); + f.group.reset(); + REQUIRE(f.scene.groups().empty()); + + UITestHelper::simulateMouseMove(&f.view, overGroup + QPointF(5, 5)); + UITestHelper::simulateMouseRelease(&f.view, overGroup + QPointF(5, 5)); + UITestHelper::waitForUI(); + + CHECK(f.outsiderObject().nodeGroup().expired()); + CHECK(f.scene.groups().empty()); +} From 5e79ce239147337f1ab1bcadc47ff9848941fb29 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Wed, 2 Dec 2026 13:00:35 +0100 Subject: [PATCH 2/3] Test that the group box follows a node resized with its handle The test drags the resize handle of a resizable node with an embedded widget. The node belongs to a group. After the drag the box of the group must still contain the larger node. This covers the updateGroupGeometry() call in the resize branch of NodeGraphicsObject::mouseMoveEvent(). --- test/src/TestGroupDrag.cpp | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/test/src/TestGroupDrag.cpp b/test/src/TestGroupDrag.cpp index 3b167a33..ecd308c1 100644 --- a/test/src/TestGroupDrag.cpp +++ b/test/src/TestGroupDrag.cpp @@ -1,19 +1,27 @@ #include "ApplicationSetup.hpp" +#include "TestDelegateModels.hpp" #include "TestGraphModel.hpp" #include "UITestHelper.hpp" #include +#include #include +#include +#include #include #include #include #include +#include #include +#include using QtNodes::BasicGraphicsScene; +using QtNodes::DataFlowGraphModel; using QtNodes::GraphicsView; +using QtNodes::NodeDelegateModelRegistry; using QtNodes::NodeGraphicsObject; using QtNodes::NodeGroup; using QtNodes::NodeId; @@ -75,6 +83,29 @@ struct GroupDragFixture } }; +/// A resizable node with an embedded widget. The resize handle changes the widget size. +class ResizableWidgetNode : public StubDelegateModel +{ +public: + ResizableWidgetNode() + : StubDelegateModel(QStringLiteral("ResizableWidgetNode"), QStringLiteral("Resizable")) + {} + + bool resizable() const override { return true; } + + QWidget *embeddedWidget() override + { + if (!_widget) { + _widget = new QWidget; + _widget->resize(80, 60); + } + return _widget; + } + +private: + QPointer _widget; +}; + } // namespace TEST_CASE("A node dragged over a group and released away from it stays out of the group", @@ -132,3 +163,48 @@ TEST_CASE("A group deleted while a node is dragged over it is not used at the re CHECK(f.outsiderObject().nodeGroup().expired()); CHECK(f.scene.groups().empty()); } + +TEST_CASE("The box of a group follows a node resized with its handle", "[node-group][drag]") +{ + auto app = applicationSetup(); + + auto registry = std::make_shared(); + registry->registerModel([] { return std::make_unique(); }, "Test"); + DataFlowGraphModel model(registry); + BasicGraphicsScene scene(model); + GraphicsView view(&scene); + view.resize(800, 600); + view.show(); + REQUIRE(QTest::qWaitForWindowExposed(&view)); + + NodeId const nodeId = model.addNode(QStringLiteral("ResizableWidgetNode")); + auto *node = scene.nodeGraphicsObject(nodeId); + REQUIRE(node != nullptr); + node->setOpacity(1.0); + + std::vector nodes{node}; + auto group = scene.createGroup(nodes, "Group").lock(); + REQUIRE(group); + // The nodes of a locked group do not take mouse presses. + group->groupGraphicsObject().lock(false); + UITestHelper::waitForUI(); + + auto &box = group->groupGraphicsObject(); + QRectF const nodeBefore = node->sceneBoundingRect(); + REQUIRE(box.sceneBoundingRect().contains(nodeBefore)); + + QRect const handle = scene.nodeGeometry().resizeHandleRect(nodeId); + QPointF const start = node->mapToScene(QRectF(handle).center()); + QPointF const delta(120, 90); + + UITestHelper::simulateMousePress(&view, start); + UITestHelper::simulateMouseMove(&view, start + delta); + UITestHelper::simulateMouseRelease(&view, start + delta); + UITestHelper::waitForUI(); + + QRectF const nodeAfter = node->sceneBoundingRect(); + REQUIRE(nodeAfter.topLeft() == nodeBefore.topLeft()); + REQUIRE(nodeAfter.width() > nodeBefore.width()); + REQUIRE(nodeAfter.height() > nodeBefore.height()); + CHECK(box.sceneBoundingRect().contains(nodeAfter)); +} From de3b3e0bf55cb7161ebf1676cca9b6b38ad81608 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Wed, 2 Dec 2026 21:18:47 +0100 Subject: [PATCH 3/3] Document how the moves of a drag merge into one undo step MoveNodeCommand::beginMove() was added without a mention in the undo guide. The guide now says that the moves of one drag merge, that a mouse press starts a new move, and that code which pushes the command itself must call beginMove(). The migration page narrows the warning about loadItems() with a paste position to pasted groups. --- docs/guide/undo-redo.rst | 8 +++++++- docs/migration/v3-to-v4.rst | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/guide/undo-redo.rst b/docs/guide/undo-redo.rst index 0068a189..96e20311 100644 --- a/docs/guide/undo-redo.rst +++ b/docs/guide/undo-redo.rst @@ -60,10 +60,16 @@ QtNodes provides these ``QUndoCommand`` implementations in ``UndoCommands.cpp``: * - ``ConnectCommand`` - Creates a connection * - ``MoveNodeCommand`` - - Moves a node to a new position + - Moves nodes by an offset. The moves of one drag merge into one undo step Duplicate has no command of its own. It pushes a ``CopyCommand`` and a ``PasteCommand``. +``MoveNodeCommand`` merges with the command before it when both move the same nodes and belong +to the same move. A mouse press on a node or on a group starts a new move, so two separate drags +of the same node are two undo steps. If you push ``MoveNodeCommand`` yourself, for example from +your own view, call ``MoveNodeCommand::beginMove()`` when a new drag starts. Without it, the +moves of the new drag merge with the previous drag of the same nodes. + Serialization Requirement ------------------------- diff --git a/docs/migration/v3-to-v4.rst b/docs/migration/v3-to-v4.rst index 231b0f4f..d033e54e 100644 --- a/docs/migration/v3-to-v4.rst +++ b/docs/migration/v3-to-v4.rst @@ -68,7 +68,8 @@ Use of groups in 3.x Groups are usable. Until v4: - Do not rely on undo for group changes. -- Do not call ``BasicGraphicsScene::loadItems()`` with a paste position. +- Do not call ``BasicGraphicsScene::loadItems()`` with a paste position for data that has + groups. The offset of a group is wrong. - Keep the group lock on, if you do not want the drag-into-group behavior. What changes in v4