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
9 changes: 9 additions & 0 deletions docs/guide/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ inputs like "Add N Numbers" or "Merge Arrays".

// 3. Complete: library restores shifted connections
portsInserted();

// 4. The port count changed: let the scene recompute the node size
emit nodeUpdated(nodeId);
}

void MyModel::removePortFromNode(NodeId nodeId, PortIndex portIndex)
Expand All @@ -51,6 +54,9 @@ inputs like "Add N Numbers" or "Merge Arrays".

// 3. Complete
portsDeleted();

// 4. The port count changed: let the scene recompute the node size
emit nodeUpdated(nodeId);
}

**In NodeDelegateModel:**
Expand All @@ -66,6 +72,9 @@ inputs like "Add N Numbers" or "Merge Arrays".
emit portsInserted();
}

``DataFlowGraphModel`` updates the node after ``portsInserted()`` and ``portsDeleted()``.
A delegate does not need to emit ``requestNodeUpdate()`` for the port change.

.. warning::

Always use the two-phase approach. Modifying ports without the
Expand Down
23 changes: 21 additions & 2 deletions docs/guide/data-flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,27 @@ Implementing Data Flow

**Signals to emit:**

- ``dataUpdated(portIndex)`` -- Output data changed, propagate downstream
- ``dataInvalidated(portIndex)`` -- Output is no longer valid
- ``dataUpdated(portIndex)`` -- Output data changed. ``DataFlowGraphModel`` calls
``outData()`` and sends the value to all connected inputs. To clear the inputs
downstream, return ``nullptr`` from ``outData()`` and emit ``dataUpdated``.
- ``dataInvalidated(portIndex)`` -- Output is no longer valid. ``DataFlowGraphModel`` sends
empty data (``nullptr``) to all connected inputs. ``dataUpdated`` with a ``nullptr``
output has the same effect.
- ``requestNodeUpdate()`` -- Something visible changed that has no signal of its own,
for example after ``setNodeStyle()``, ``setNodeProcessingStatus()``,
``setValidationState()`` or ``setProgressValue()``. The model emits ``nodeUpdated``
and the scene recomputes the size and repaints the node.
- ``embeddedWidgetSizeUpdated()`` -- The embedded widget changed its size. The model
emits ``nodeUpdated``, like for ``requestNodeUpdate()``.
- ``portsAboutToBeInserted()`` / ``portsInserted()`` and ``portsAboutToBeDeleted()`` /
``portsDeleted()`` -- The port count changes. See :doc:`advanced`. The model fixes the
connections, emits its port signals and then ``nodeUpdated``.

**Signals for the application:**

``computingStarted()`` and ``computingFinished()`` have no listener in the library. Emitting
them changes nothing in the model or on the scene. Your application can connect to them, for
example to show a busy state, as the ``calculator`` example does.

Data Flow Diagram
-----------------
Expand Down
22 changes: 22 additions & 0 deletions docs/guide/graph-models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,28 @@ Your model **must** emit these signals at the appropriate times:
// After removing a connection
emit connectionDeleted(connectionId);

// 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``.

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.

When the port count of a node changes, call the connection fix-up methods around the
change and emit ``nodeUpdated`` at the end, so that the scene recomputes the node size:

.. code-block:: cpp

portsAboutToBeInserted(nodeId, PortType::In, first, last); // fixes connections
// ... change the port count ...
portsInserted(); // restores connections
emit nodeUpdated(nodeId);

The deletion is the same with ``portsAboutToBeDeleted()`` and ``portsDeleted()``.
``DataFlowGraphModel`` does all of this when a delegate changes its ports.

.. warning::

Forgetting to emit signals will cause the view to become out of sync
Expand Down
18 changes: 14 additions & 4 deletions docs/guide/undo-redo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ automatically push commands to this stack:
- Creating connections
- Deleting connections
- Moving nodes
- Duplicating nodes
- Pasting and duplicating nodes

Accessing the Undo Stack
------------------------
Expand Down Expand Up @@ -47,17 +47,23 @@ QtNodes provides these ``QUndoCommand`` implementations in ``UndoCommands.cpp``:

* - Command
- Description
* - ``CreateCommand``
- Creates a node
* - ``DeleteCommand``
- Removes nodes and their connections
* - ``DuplicateCommand``
- Duplicates selected nodes
* - ``CopyCommand``
- Copies the selection to the clipboard. It does not stay on the stack
* - ``PasteCommand``
- Inserts the nodes and connections from the clipboard
* - ``DisconnectCommand``
- Removes a connection
* - ``ConnectCommand``
- Creates a connection
* - ``MoveNodeCommand``
- Moves a node to a new position

Duplicate has no command of its own. It pushes a ``CopyCommand`` and a ``PasteCommand``.

Serialization Requirement
-------------------------

Expand Down Expand Up @@ -168,6 +174,10 @@ Use macros to group multiple commands:
Clearing History
----------------

``BasicGraphicsScene::clearScene()`` deletes the nodes without an undo step, so it clears the
stack. A ``modelReset`` and ``DataFlowGraphicsScene::load()`` clear it too. The commands on
the stack refer to nodes by id, and these nodes are gone.

.. code-block:: cpp

// Clear all undo history
Expand All @@ -189,7 +199,7 @@ Default shortcuts (handled by ``GraphicsView``):
- ``Ctrl+Z`` -- Undo
- ``Ctrl+Shift+Z`` or ``Ctrl+Y`` -- Redo
- ``Delete`` -- Delete selection (creates ``DeleteCommand``)
- ``Ctrl+D`` -- Duplicate (creates ``DuplicateCommand``)
- ``Ctrl+D`` -- Duplicate (creates ``CopyCommand`` and ``PasteCommand``)

.. seealso::

Expand Down
9 changes: 9 additions & 0 deletions docs/guide/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ Connect to user interactions:
// Show connection info
});

// Connection interactions
connect(&scene, &BasicGraphicsScene::connectionSelected, [](ConnectionId id) {
// Show the connection in a side panel
});

connect(&scene, &BasicGraphicsScene::connectionDoubleClicked, [](ConnectionId id) {
// Open an editor for the connection
});

// Context menus
connect(&scene, &BasicGraphicsScene::nodeContextMenu,
[](NodeId id, QPointF scenePos) {
Expand Down
9 changes: 6 additions & 3 deletions include/QtNodes/internal/AbstractGraphModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ class NODE_EDITOR_PUBLIC AbstractGraphModel : public QObject
PortIndex const last);

/**
* Signal emitted when model no longer has the old data associated with the
* given port indices and when the node must be repainted.
* Function re-creates the connections that were shifted during the port
* deletion. Call it when the model no longer has the old data of the
* deleted ports. It does not update the node: the model must emit
* `nodeUpdated` after this call, so that the scene recomputes the size.
*/
void portsDeleted();

Expand All @@ -236,7 +238,8 @@ class NODE_EDITOR_PUBLIC AbstractGraphModel : public QObject

/**
* Function re-creates the connections that were shifted during the port
* insertion. After that the node is updated.
* insertion. It does not update the node: the model must emit
* `nodeUpdated` after this call, so that the scene recomputes the size.
*/
void portsInserted();

Expand Down
7 changes: 7 additions & 0 deletions include/QtNodes/internal/BasicGraphicsScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
void resetDraftConnection();

/// Deletes all the nodes. Connections are removed automatically.
/// The deletion is not an undo step, so the function also clears the undo stack.
void clearScene();

/**
Expand Down Expand Up @@ -280,6 +281,12 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
void zoomFitAllClicked();
void zoomFitSelectedClicked();

/// A press on a connection left it selected. Like `nodeSelected` for nodes.
void connectionSelected(ConnectionId const connectionId);

/// A double click on a connection. Like `nodeDoubleClicked` for nodes.
void connectionDoubleClicked(ConnectionId const connectionId);

private:
/**
* @brief Creates Node and Connection graphics objects.
Expand Down
2 changes: 2 additions & 0 deletions include/QtNodes/internal/ConnectionGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class NODE_EDITOR_PUBLIC ConnectionGraphicsObject : public QGraphicsObject

void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;

void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;

private:
void initializePosition();

Expand Down
5 changes: 5 additions & 0 deletions include/QtNodes/internal/DataFlowGraphModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class NODE_EDITOR_PUBLIC DataFlowGraphModel

void sendConnectionDeletion(ConnectionId const connectionId);

/// Connects the signals of a new or restored delegate to this model.
void connectDelegateModel(NodeId const nodeId, NodeDelegateModel *model);

private Q_SLOTS:
/**
* Fuction is called in three cases:
Expand All @@ -126,6 +129,8 @@ private Q_SLOTS:
*/
void onOutPortDataUpdated(NodeId const nodeId, PortIndex const portIndex);

void onOutPortDataInvalidated(NodeId const nodeId, PortIndex const portIndex);

/// Function is called after detaching a connection.
void propagateEmptyDataTo(NodeId const nodeId, PortIndex const portIndex);

Expand Down
6 changes: 6 additions & 0 deletions include/QtNodes/internal/GroupGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class NODE_EDITOR_PUBLIC GroupGraphicsObject
*/
QRectF boundingRect() const override;

/**
* @brief Recomputes the group area from the areas of its nodes. It is called
* when a node of the group moves, changes its size, joins or leaves.
*/
void updateGeometry();

enum { Type = UserType + 3 };

/**
Expand Down
16 changes: 14 additions & 2 deletions include/QtNodes/internal/NodeDelegateModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,27 @@ public Q_SLOTS:
virtual void outputConnectionDeleted(ConnectionId const &) {}

Q_SIGNALS:
/// Triggers the updates in the nodes downstream.
/// Triggers the updates in the nodes downstream. `DataFlowGraphModel` calls
/// `outData(index)` and sends the result to all connected inputs. A `nullptr`
/// output clears the inputs downstream.
void dataUpdated(PortIndex const index);

/// Triggers the propagation of the empty data downstream.
/**
* Tells the model that the output at `index` is no longer valid.
* `DataFlowGraphModel` sends empty data (`nullptr`) to the inputs that are
* connected to this output.
*/
void dataInvalidated(PortIndex const index);

/// For the application. The library does not listen to these signals.
void computingStarted();
void computingFinished();

/**
* Emit this signal after the embedded widget changed its size.
* `DataFlowGraphModel` emits `nodeUpdated` for the node, like for
* `requestNodeUpdate`, so that the scene recomputes the node size.
*/
void embeddedWidgetSizeUpdated();

/// Request an update of the node's UI.
Expand Down
1 change: 1 addition & 0 deletions include/QtNodes/internal/NodeGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class NODE_EDITOR_PUBLIC NodeGraphicsObject : public QGraphicsObject
private:
void embedQWidget();
void setLockedState();
void updateGroupGeometry() const;

private:
NodeId _nodeId;
Expand Down
12 changes: 11 additions & 1 deletion src/BasicGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ void BasicGraphicsScene::clearScene()
for (auto nodeId : allNodeIds) {
graphModel().deleteNode(nodeId);
}

// The deletion is not an undo step.
_undoStack->clear();
}

std::vector<std::shared_ptr<ConnectionId>> BasicGraphicsScene::connectionsWithinGroup(GroupId groupID)
Expand Down Expand Up @@ -417,6 +420,9 @@ void BasicGraphicsScene::onNodeUpdated(NodeId const nodeId)
node->updateQWidgetEmbedPos();
node->update();
node->moveConnections();

if (auto group = node->nodeGroup().lock())
group->groupGraphicsObject().updateGeometry();
}
}

Expand All @@ -439,6 +445,8 @@ void BasicGraphicsScene::onModelReset()

clear();

_undoStack->clear();

traverseGraphAndPopulateGraphicsObjects();
}

Expand Down Expand Up @@ -675,7 +683,9 @@ std::unordered_map<QUuid, QUuid> BasicGraphicsScene::loadItems(const QByteArray
offsetInitialized = true;
}
if (usePastePos) {
nodeRef.moveBy(offset.x(), offset.y());
// Move through the model, so that the model and the item agree.
QPointF const pos = _graphModel.nodeData<QPointF>(newNodeId, NodeRole::Position);
_graphModel.setNodeData(newNodeId, NodeRole::Position, pos + offset);
}
nodeRef.moveConnections();
nodeRef.setSelected(true);
Expand Down
12 changes: 12 additions & 0 deletions src/ConnectionGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <QtWidgets/QGraphicsBlurEffect>
#include <QtWidgets/QGraphicsDropShadowEffect>
#include <QtWidgets/QGraphicsSceneContextMenuEvent>
#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtWidgets/QGraphicsView>
#include <QtWidgets/QStyleOptionGraphicsItem>
Expand Down Expand Up @@ -204,6 +205,17 @@ void ConnectionGraphicsObject::paint(QPainter *painter,
void ConnectionGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);

if (isSelected()) {
Q_EMIT nodeScene()->connectionSelected(connectionId());
}
}

void ConnectionGraphicsObject::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseDoubleClickEvent(event);

Q_EMIT nodeScene()->connectionDoubleClicked(connectionId());
}

void ConnectionGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Expand Down
Loading
Loading