Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ build*/
qt-build

tags

# OpenSpec working directory (local planning)
openspec/
.claude/
96 changes: 96 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# AGENTS.md

This file provides guidance to coding agents when working with code in this repository.

## Writing style

Write short, simple technical English. One idea per sentence. No filler or marketing words.
This applies to answers, code comments, commit messages and docs.

## Project overview

QtNodes is a Qt-based C++ library for building node graph editors. It separates graph data
(`AbstractGraphModel`) from visualization (`BasicGraphicsScene`/`GraphicsView`). It supports
generic graph editing and a data-flow paradigm with automatic propagation. It works with Qt5
(5.15+) and Qt6 (6.3+).

## Build and test

```bash
cmake -S . -Bbuild -DCMAKE_BUILD_TYPE=Release -DUSE_QT6=ON # Qt6 (default); -DUSE_QT6=OFF for Qt5
cmake --build build
cmake --build build --target test_nodes # tests only
./build/bin/test_nodes # all tests
./build/bin/test_nodes "[core]" # by Catch2 tag, e.g. [core] [graphics]
cd build && ctest --output-on-failure # via ctest (Linux headless: xvfb-run -a ctest ...)
```

CMake options: `BUILD_TESTING`, `BUILD_EXAMPLES`, `BUILD_DOCS`, `BUILD_SHARED_LIBS`, `USE_QT6`.

Tests use Catch2 v3 (downloaded if not found) in one executable, `test_nodes`. Helpers are in
`test/include/`: `TestGraphModel`, `UITestHelper`, `TestDataFlowNodes`, `ApplicationSetup`.

CI (`.github/workflows/cmake_build.yml`): Linux/gcc/Qt5, macOS/clang/Qt6, Windows/MSVC/Qt5 and
Qt6. Apple Clang builds with `-Werror`.

## Architecture

Model-View. The model is the single source of truth. The scene and view only observe model
signals.

- **Model state changes go through the model.** Change the graph with model calls or undo
commands (`UndoCommands.hpp`). The graphics layer follows the model signals.
- `AbstractGraphModel`: pure virtual graph. Node and connection CRUD, data queries by
`NodeRole`/`PortRole`. It can run headless, without a scene.
- `DataFlowGraphModel`: concrete model for the data-flow paradigm. It owns one
`NodeDelegateModel` per node and propagates data through connections. Loops are disabled.
- `NodeDelegateModel`: node logic for `DataFlowGraphModel`. It defines ports, data types and
embedded widgets, receives data in `setInData()` and signals `dataUpdated(PortIndex)`.
Subclasses are registered in `NodeDelegateModelRegistry`.
- Graphics: `BasicGraphicsScene` (owns the `QUndoStack`), `DataFlowGraphicsScene` (JSON
save/load, context menu), `GraphicsView` (zoom, pan, copy/paste, shortcuts).
- Customization by inheritance: `AbstractNodeGeometry`, `AbstractNodePainter`,
`AbstractConnectionPainter`.
- Styling: JSON styles via `NodeStyle`, `ConnectionStyle`, `GraphicsViewStyle`, managed by the
`StyleCollection` singleton.
- Serialization: the `Serializable` interface, `save()`/`load()` with `QJsonObject`.

Core types: `NodeId` and `PortIndex` are `unsigned int` (`InvalidNodeId` is the maximum
value). `PortType` is `In`, `Out` or `None`. `ConnectionId` is
`{outNodeId, outPortIndex, inNodeId, inPortIndex}`.

Directories: `include/QtNodes/internal/` (public headers), `src/`, `test/src/` and
`test/include/`, `examples/`.

## Code style

Enforced by `.clang-format`.

- 4 spaces, no tabs, 100 columns.
- Brace on a new line after class, struct and function. Same line after control statements.
- Pointer alignment right (`int *p`). No namespace indentation.
- `#pragma once` in all headers. Everything is in `namespace QtNodes`.
- Members are prefixed with `_` (`_value`).
- `QT_NO_KEYWORDS` is defined. Use `Q_EMIT`, `Q_SIGNALS`, `Q_SLOTS`, `Q_FOREACH`.

## C++ standard

The sources use C++17, and Qt 6 requires it. The standard is declared only in the
`QT_NODES_DEVELOPER_DEFAULTS` block of `CMakeLists.txt`. Do not set it for consumers. Projects
that include QtNodes choose their own standard.

## Tests and docs

- Test titles state the rule they prove in words.
- A bug fix comes with a regression test that fails without the fix.
- Update `docs/` and `README.rst` when a change alters what they describe.
- Do not silently change recorded behavior or settle an open design decision. Ask.

## Commits and releases

- One logical change per commit.
- No `Co-Authored-By` lines or other attribution trailers in commit messages.
- Do not push, tag or delete remote branches unless asked.
- Versions follow semver. A tag on a later commit must have a higher version than the tags
before it. Never move or delete a published tag.
- Branches: `master` (latest dev state) and `v3`. Older versions are tags (`1.0`, `2.*`).
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@AGENTS.md
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ if(NOT "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
endif()

if(QT_NODES_DEVELOPER_DEFAULTS)
target_compile_features(QtNodes PUBLIC cxx_std_14)
target_compile_features(QtNodes PUBLIC cxx_std_17)
set_target_properties(QtNodes PROPERTIES CXX_EXTENSIONS OFF)
endif()

Expand Down
12 changes: 10 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ Warning
Branches
--------

There are branches ``v2`` and ``v3`` for versions ``2.x.x`` and ``3.x``
respectively. The branch ``master`` contains the latest dev state.
There is a branch ``v3`` for version ``3.x``. The branch ``master`` contains the latest
dev state. Older versions are available as tags: ``1.0`` and ``2.*`` (the last ``2.x.x``
release is ``2.2.4``).


.. contents:: Navigation
Expand Down Expand Up @@ -104,6 +105,13 @@ Current State (v3)
Building
========

C++ standard
------------

The sources use C++17 and Qt 6 requires it, so build with C++17 or newer. QtNodes does not
impose a standard on projects that include it. Your project sets it, for example with
``set(CMAKE_CXX_STANDARD 17)``, so that all of its targets stay consistent.

Set this option to false if you want to build with Qt5 version instead of Qt6:

::
Expand Down
3 changes: 2 additions & 1 deletion docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ For ports on all sides, create a custom ``AbstractNodeGeometry``.

**Can I group nodes?**

Node grouping is planned but not yet implemented. Track progress on GitHub.
Yes, since 3.1.4. Call ``BasicGraphicsScene::setGroupingEnabled(true)``. Groups have known
limits in 3.x. They are explained in :doc:`migration/v3-to-v4`, together with the plan for v4.

**Can I have curved connections?**

Expand Down
8 changes: 4 additions & 4 deletions docs/guide/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ Build graphs in code (useful for testing or loading custom formats):
}
}

Node Groups (Future)
--------------------
Node Groups
-----------

.. note::

Node grouping is planned but not yet implemented. See the
`GitHub issues <https://github.com/paceholder/nodeeditor/issues>`_ for status.
Call ``BasicGraphicsScene::setGroupingEnabled(true)`` to enable groups. Groups have known
limits in 3.x, for example group changes cannot be undone. See :doc:`../migration/v3-to-v4`.

.. seealso::

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Other Resources
:maxdepth: 1

migration/v2-to-v3
migration/v3-to-v4
faq
testing
license_link
Expand Down
94 changes: 94 additions & 0 deletions docs/migration/v3-to-v4.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Planned changes for v4: node groups
===================================

Node groups were added in 3.1.4. They work, but their design has limits.
Version 4 will rebuild groups so that the model owns the group data, like
it does for nodes and connections. This page lists the known problems of
groups in 3.x and says why they are not fixed there.

.. note::

The v4 design is not final. Progress is tracked in
`issue #539 <https://github.com/paceholder/nodeeditor/issues/539>`_.

What is wrong with groups in 3.x
--------------------------------

The scene owns the groups. The model does not know about them. A group is a
``NodeGroup`` that holds raw pointers to node graphics items, and the scene
changes node positions and group state directly. This is against the rule of
the library: the model is the source of truth, and the scene follows it.

The model has no group data, so:

- A group is not saved by ``DataFlowGraphModel::save()``. It is saved by the
scene.
- Creating a group, adding a node to it and removing a node from it cannot be
undone.
- Undo of a node deletion brings the node back outside its group. A group
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.

Why these are not fixed in 3.x
------------------------------

Each of these problems comes from the same cause: the group state is in the
scene. A correct fix moves it to the model. That changes the public API
(``NodeGroup``, ``childNodes()`` and the group functions of the scene), so it
needs a new major version.

A local fix in 3.x would rewrite code that v4 removes. It would also add more
code to a design we want to leave. So 3.x gets only the fixes that matter to
users today:

- Node moves go through the model again (#516).
- A locked node, or a node of a locked group, can no longer start connections
(#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.
- ``GroupGraphicsObject`` is exported. On a Windows shared build, code that
called its methods did not link.

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.
- Keep the group lock on, if you do not want the drag-into-group behavior.

What changes in v4
------------------

- The model holds the group data: a ``GroupId`` per node, and name and lock
per group. A small helper class, which a model keeps as a member, does the
bookkeeping, so each model does not need its own.
- ``NodeGroup`` and the pointers between graphics items are removed.
``GroupGraphicsObject`` becomes a view that draws a rectangle around the
nodes of the group.
- Group changes are undo commands.
- Groups do not nest and cannot be folded.
- Saved scenes keep the ``"groups"`` array, so files of 3.x load in v4.
1 change: 1 addition & 0 deletions include/QtNodes/internal/BasicGraphicsScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ public Q_SLOTS:
std::unique_ptr<AbstractNodePainter> _nodePainter;
std::unique_ptr<AbstractConnectionPainter> _connectionPainter;
bool _nodeDrag;
friend class NodeGraphicsObject;
QUndoStack *_undoStack;
Qt::Orientation _orientation;
bool _groupingEnabled;
Expand Down
3 changes: 2 additions & 1 deletion include/QtNodes/internal/GroupGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "BasicGraphicsScene.hpp"
#include "Definitions.hpp"
#include "Export.hpp"
#include "NodeGroup.hpp"
#include <QPen>
#include <QtWidgets/QGraphicsRectItem>
Expand Down Expand Up @@ -45,7 +46,7 @@ class NodeGraphicsObject;
* @brief The GroupGraphicsObject class handles the graphical part of a node group.
* Each node group is associated with a unique GroupGraphicsObject.
*/
class GroupGraphicsObject
class NODE_EDITOR_PUBLIC GroupGraphicsObject
: public QObject
, public QGraphicsRectItem
{
Expand Down
8 changes: 7 additions & 1 deletion include/QtNodes/internal/UndoCommands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ class NODE_EDITOR_PUBLIC ConnectCommand : public QUndoCommand
class NODE_EDITOR_PUBLIC MoveNodeCommand : public QUndoCommand
{
public:
/// Moves the currently selected nodes.
MoveNodeCommand(BasicGraphicsScene *scene, QPointF const &diff);

/// Moves the given nodes, regardless of the selection.
MoveNodeCommand(BasicGraphicsScene *scene,
std::unordered_set<NodeId> const &nodeIds,
QPointF const &diff);

void undo() override;
void redo() override;

Expand All @@ -117,7 +123,7 @@ class NODE_EDITOR_PUBLIC MoveNodeCommand : public QUndoCommand

private:
BasicGraphicsScene *_scene;
std::unordered_set<NodeId> _selectedNodes;
std::unordered_set<NodeId> _nodeIds;
QPointF _diff;
};

Expand Down
23 changes: 21 additions & 2 deletions src/BasicGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,15 @@ BasicGraphicsScene::BasicGraphicsScene(AbstractGraphModel &graphModel, QObject *
traverseGraphAndPopulateGraphicsObjects();
}

BasicGraphicsScene::~BasicGraphicsScene() = default;
BasicGraphicsScene::~BasicGraphicsScene()
{
// A group that is still referenced elsewhere must not delete its item after the scene
// is gone. Delete the items now, while the scene is alive.
for (auto &entry : _groups) {
if (entry.second)
entry.second->setGraphicsObject(nullptr);
}
}

AbstractGraphModel const &BasicGraphicsScene::graphModel() const
{
Expand Down Expand Up @@ -419,6 +427,9 @@ void BasicGraphicsScene::onNodeClicked(NodeId const nodeId)

void BasicGraphicsScene::onModelReset()
{
// The groups own graphics items that clear() would delete a second time.
_groups.clear();

_connectionGraphicsObjects.clear();
_nodeGraphicsObjects.clear();

Expand Down Expand Up @@ -485,7 +496,7 @@ std::weak_ptr<NodeGroup> BasicGraphicsScene::createGroup(std::vector<NodeGraphic
}
}

auto group = std::make_shared<NodeGroup>(nodes, groupId, groupName, this);
auto group = std::make_shared<NodeGroup>(nodes, groupId, groupName);
auto ggo = std::make_unique<GroupGraphicsObject>(*this, *group);

group->setGraphicsObject(std::move(ggo));
Expand Down Expand Up @@ -554,6 +565,14 @@ void BasicGraphicsScene::addNodeToGroup(NodeId nodeId, GroupId groupId)

auto group = groupIt->second;
auto node = nodeIt->second.get();

if (auto currentGroup = node->nodeGroup().lock()) {
if (currentGroup == group)
return;

removeNodeFromGroup(nodeId);
}

group->addNode(node);
node->setNodeGroup(group);
}
Expand Down
Loading
Loading