From b444058d26dfd839fdd5157eba812765cf524dc0 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Wed, 2 Dec 2026 13:27:51 +0100 Subject: [PATCH] Write the node size only when it changed The default geometries wrote the size on every recompute. A model that emitted nodeUpdated from setNodeData(Size), as the guide advised, then recursed without end. AbstractNodeGeometry::storeSize() now writes a size only when it differs from the stored one, so a loop ends after one pass. Both default geometries use it, and a custom geometry can call it. The guide said that such an emit starts an endless recursion. It now says only that the emit is not needed, and tells a custom geometry to store the size with storeSize(). See #568 --- docs/guide/advanced.rst | 6 +- docs/guide/change-notifications.rst | 9 ++- docs/guide/graph-models.rst | 4 +- .../QtNodes/internal/AbstractNodeGeometry.hpp | 5 ++ src/AbstractNodeGeometry.cpp | 6 ++ src/DefaultHorizontalNodeGeometry.cpp | 2 +- src/DefaultVerticalNodeGeometry.cpp | 2 +- test/CMakeLists.txt | 1 + test/src/TestSizeWrite.cpp | 81 +++++++++++++++++++ 9 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 test/src/TestSizeWrite.cpp diff --git a/docs/guide/advanced.rst b/docs/guide/advanced.rst index 0c75bd178..3fd37916f 100644 --- a/docs/guide/advanced.rst +++ b/docs/guide/advanced.rst @@ -152,9 +152,9 @@ Store the size: // ... } -Do not emit ``nodeUpdated`` when ``Size`` is set. The scene sets ``Size`` while it handles -``nodeUpdated``, so the signal would start an endless recursion. See -:doc:`change-notifications` for the signals that a model must emit. +You do not need to emit ``nodeUpdated`` when ``Size`` is set. The scene sets ``Size`` itself, +while it handles ``nodeUpdated``. See :doc:`change-notifications` for the signals that a model +must emit. Non-Detachable Connections -------------------------- diff --git a/docs/guide/change-notifications.rst b/docs/guide/change-notifications.rst index ed6b993dc..dbda30f54 100644 --- a/docs/guide/change-notifications.rst +++ b/docs/guide/change-notifications.rst @@ -296,8 +296,8 @@ What Your Code Must Call - Emit ``nodePositionUpdated`` when ``Position`` changes. - Emit ``nodeUpdated`` when anything changes that affects the look or the size of a node: caption, label, style, port count, port captions, validation state, processing status. -- Do **not** emit ``nodeUpdated`` when ``Size`` is set. The scene sets ``Size`` while it - handles ``nodeUpdated``. A signal there starts an endless recursion. +- Do not emit ``nodeUpdated`` when ``Size`` is set. It is not needed: the scene sets ``Size`` + while it handles ``nodeUpdated``. - After ``portsInserted()`` or ``portsDeleted()``, emit ``nodeUpdated`` for the node. These functions restore the shifted connections, but they do not update the node. - Emit ``nodeFlagsUpdated`` when ``nodeFlags()`` changes. @@ -322,8 +322,9 @@ What Your Code Must Call **A custom** ``AbstractNodeGeometry``: -- ``recomputeSize()`` must store the size with ``setNodeData(nodeId, NodeRole::Size, size)``. - ``size()`` and ``boundingRect()`` read it back from the model. +- ``recomputeSize()`` must store the size in the model with ``storeSize(nodeId, size)``. + ``size()`` and ``boundingRect()`` read it back from the model. ``storeSize()`` writes only a + size that changed, so a model that reacts to the write cannot start a loop. - The scene calls ``recomputeSize()`` when a node is created and in each ``onNodeUpdated``. Nothing else calls it, so a size that depends on other state needs a ``nodeUpdated``. - ``setOrientation()`` replaces the geometry with a default one. diff --git a/docs/guide/graph-models.rst b/docs/guide/graph-models.rst index 7ae34c000..860b49b22 100644 --- a/docs/guide/graph-models.rst +++ b/docs/guide/graph-models.rst @@ -283,8 +283,8 @@ Your model **must** emit these signals at the appropriate times: // After nodeFlags() changed, for example a node was locked emit nodeFlagsUpdated(nodeId); -Do not emit ``nodeUpdated`` when ``NodeRole::Size`` is set. The scene sets ``Size`` -while it handles ``nodeUpdated``. +You do not need to emit ``nodeUpdated`` when ``NodeRole::Size`` is set. The scene sets +``Size`` itself, while it handles ``nodeUpdated``. Emit ``modelReset()`` only when you replace the whole graph. The scene deletes all items, groups and embedded widgets, builds them again and clears the undo stack. diff --git a/include/QtNodes/internal/AbstractNodeGeometry.hpp b/include/QtNodes/internal/AbstractNodeGeometry.hpp index c3be96301..7400cf18b 100644 --- a/include/QtNodes/internal/AbstractNodeGeometry.hpp +++ b/include/QtNodes/internal/AbstractNodeGeometry.hpp @@ -79,6 +79,11 @@ class NODE_EDITOR_PUBLIC AbstractNodeGeometry virtual int getPortSpacing() = 0; protected: + /// Stores the layout size of the node in the model. Call it at the end of recomputeSize(). + /// It writes only a size that differs from the stored one, so a model that reacts to the + /// write cannot start a loop. + void storeSize(NodeId const nodeId, QSize const &size) const; + AbstractGraphModel &_graphModel; }; diff --git a/src/AbstractNodeGeometry.cpp b/src/AbstractNodeGeometry.cpp index f6c893df7..82cf69f4d 100644 --- a/src/AbstractNodeGeometry.cpp +++ b/src/AbstractNodeGeometry.cpp @@ -25,6 +25,12 @@ QPointF AbstractNodeGeometry::portScenePosition(NodeId const nodeId, return t.map(result); } +void AbstractNodeGeometry::storeSize(NodeId const nodeId, QSize const &size) const +{ + if (_graphModel.nodeData(nodeId, NodeRole::Size) != size) + _graphModel.setNodeData(nodeId, NodeRole::Size, size); +} + PortIndex AbstractNodeGeometry::checkPortHit(NodeId const nodeId, PortType const portType, QPointF const nodePoint) const diff --git a/src/DefaultHorizontalNodeGeometry.cpp b/src/DefaultHorizontalNodeGeometry.cpp index ff2e89558..d2acf283a 100644 --- a/src/DefaultHorizontalNodeGeometry.cpp +++ b/src/DefaultHorizontalNodeGeometry.cpp @@ -84,7 +84,7 @@ void DefaultHorizontalNodeGeometry::recomputeSize(NodeId const nodeId) const QSize size(width, height); - _graphModel.setNodeData(nodeId, NodeRole::Size, size); + storeSize(nodeId, size); } QPointF DefaultHorizontalNodeGeometry::portPosition(NodeId const nodeId, diff --git a/src/DefaultVerticalNodeGeometry.cpp b/src/DefaultVerticalNodeGeometry.cpp index 8af3466ea..2e3a1e08a 100644 --- a/src/DefaultVerticalNodeGeometry.cpp +++ b/src/DefaultVerticalNodeGeometry.cpp @@ -96,7 +96,7 @@ void DefaultVerticalNodeGeometry::recomputeSize(NodeId const nodeId) const QSize size(width, height); - _graphModel.setNodeData(nodeId, NodeRole::Size, size); + storeSize(nodeId, size); } QPointF DefaultVerticalNodeGeometry::portPosition(NodeId const nodeId, diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 16c571856..bfa45dddc 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable(test_nodes src/TestZoomFeatures.cpp src/TestLoopDetection.cpp src/TestEmbeddedWidgetKeys.cpp + src/TestSizeWrite.cpp src/TestWidgetPosition.cpp src/TestOrientationWidgets.cpp src/TestViewDragMode.cpp diff --git a/test/src/TestSizeWrite.cpp b/test/src/TestSizeWrite.cpp new file mode 100644 index 000000000..bfc2af38a --- /dev/null +++ b/test/src/TestSizeWrite.cpp @@ -0,0 +1,81 @@ +#include "ApplicationSetup.hpp" +#include "TestGraphModel.hpp" + +#include +#include + +#include + +#include + +using QtNodes::BasicGraphicsScene; +using QtNodes::NodeId; +using QtNodes::NodeRole; + +namespace { + +/// Counts the writes of the Size role. It can emit nodeUpdated on such a write, with a depth cap +/// so that a loop ends the test with a failed check and not with a stack overflow. +class SizeWritingModel : public TestGraphModel +{ +public: + bool setNodeData(NodeId nodeId, NodeRole role, QVariant value) override + { + bool const result = TestGraphModel::setNodeData(nodeId, role, value); + + if (role != NodeRole::Size) + return result; + + ++sizeWrites; + + if (emitOnSizeWrite && depth < maxDepth) { + ++depth; + deepest = std::max(deepest, depth); + Q_EMIT nodeUpdated(nodeId); + --depth; + } + + return result; + } + + bool emitOnSizeWrite = false; + int sizeWrites = 0; + int deepest = 0; + +private: + int depth = 0; + int const maxDepth = 50; +}; + +} // namespace + +TEST_CASE("A recompute that does not change the size writes no size", "[graphics][size]") +{ + auto app = applicationSetup(); + SizeWritingModel model; + BasicGraphicsScene scene(model); + + NodeId const id = model.addNode("TestNode"); + CHECK(model.sizeWrites == 1); + + model.sizeWrites = 0; + Q_EMIT model.nodeUpdated(id); + Q_EMIT model.nodeUpdated(id); + + CHECK(model.sizeWrites == 0); +} + +TEST_CASE("A model that emits nodeUpdated when the size is written does not start a loop", + "[graphics][size]") +{ + auto app = applicationSetup(); + SizeWritingModel model; + model.emitOnSizeWrite = true; + BasicGraphicsScene scene(model); + + NodeId const id = model.addNode("TestNode"); + model.setNodeData(id, NodeRole::Caption, QString(40, 'W')); + + CHECK(model.deepest < 5); + CHECK(scene.nodeGeometry().size(id).width() > 0); +}