Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/guide/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------
Expand Down
9 changes: 5 additions & 4 deletions docs/guide/change-notifications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/graph-models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions include/QtNodes/internal/AbstractNodeGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
6 changes: 6 additions & 0 deletions src/AbstractNodeGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QSize>(nodeId, NodeRole::Size) != size)
_graphModel.setNodeData(nodeId, NodeRole::Size, size);
}

PortIndex AbstractNodeGeometry::checkPortHit(NodeId const nodeId,
PortType const portType,
QPointF const nodePoint) const
Expand Down
2 changes: 1 addition & 1 deletion src/DefaultHorizontalNodeGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/DefaultVerticalNodeGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions test/src/TestSizeWrite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "ApplicationSetup.hpp"
#include "TestGraphModel.hpp"

#include <QtNodes/BasicGraphicsScene>
#include <QtNodes/internal/AbstractNodeGeometry.hpp>

#include <catch2/catch.hpp>

#include <algorithm>

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);
}
Loading