Skip to content

Commit 35b11b6

Browse files
g-abiliotatatupiGabrielnmds
authored
Adds freeze and unfreeze functionality (#507)
* include freeze and unfreeze tools to nodeeditor * add frozen dynamic to node delegate model * create freeze and unfreeze menu * add freeze and unfreeze logic to freeze menu * structure to sync delegatemodel and connectionstate * sync delegatemodel and connectionstate, as well as freeze example creation * refactor freeze code and remove comments * fix build errors in mac-os * fix build errors in mac-os * fix cross os build error * add frozen menu flag * remove example implementation code * remove unnecessary changes --------- Co-authored-by: Taiguara Tupinambás <tatatupi@gmail.com> Co-authored-by: Gabrielnmds <gabrielnmds21@gmail.com>
1 parent 0d28130 commit 35b11b6

File tree

10 files changed

+81
-10
lines changed

10 files changed

+81
-10
lines changed

examples/calculator/MathOperationDataModel.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "MathOperationDataModel.hpp"
2-
32
#include "DecimalData.hpp"
43

54
unsigned int MathOperationDataModel::nPorts(PortType portType) const

include/QtNodes/internal/BasicGraphicsScene.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#include "Export.hpp"
88
#include "GroupGraphicsObject.hpp"
99
#include "NodeGroup.hpp"
10+
#include "QUuidStdHash.hpp"
1011
#include "UndoCommands.hpp"
11-
1212
#include <QtCore/QJsonObject>
13+
#include <QtCore/QUuid>
1314
#include <QtWidgets/QGraphicsScene>
1415
#include <QtWidgets/QMenu>
1516

@@ -34,7 +35,7 @@ class NodeGroup;
3435
class GroupGraphicsObject;
3536
struct ConnectionId;
3637

37-
/// An instance of QGraphicsScene, holds connections and nodes.
38+
/// An instance of QGraphicsScene , holds connections and nodes.
3839
class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
3940
{
4041
Q_OBJECT
@@ -205,6 +206,12 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
205206
*/
206207
virtual QMenu *createSceneMenu(QPointF const scenePos);
207208

209+
/**
210+
* @brief Freezes and unfreezes the model and connections of the selected nodes.
211+
* @param isFreeze reference for freezing or unfreezing the model and connections of the selected nodes.
212+
*/
213+
void freezeModelAndConnections(bool isFreeze);
214+
208215
/**
209216
* @brief Creates the default menu when a node is selected.
210217
*/

include/QtNodes/internal/ConnectionState.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class NODE_EDITOR_PUBLIC ConnectionState
2525
ConnectionState(ConnectionGraphicsObject &cgo)
2626
: _cgo(cgo)
2727
, _hovered(false)
28+
, _frozen(false)
2829
{}
2930

3031
ConnectionState(ConnectionState const &) = delete;
@@ -42,6 +43,9 @@ class NODE_EDITOR_PUBLIC ConnectionState
4243
bool hovered() const;
4344
void setHovered(bool hovered);
4445

46+
bool frozen() const { return _frozen; }
47+
void setFrozen(bool frozen) { _frozen = frozen; }
48+
4549
public:
4650
/// Caches NodeId for further interaction.
4751
void setLastHoveredNode(NodeId const nodeId);
@@ -56,5 +60,7 @@ class NODE_EDITOR_PUBLIC ConnectionState
5660
bool _hovered;
5761

5862
NodeId _lastHoveredNode{InvalidNodeId};
63+
64+
bool _frozen;
5965
};
6066
} // namespace QtNodes

include/QtNodes/internal/DataFlowGraphicsScene.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#pragma once
22

33
#include "BasicGraphicsScene.hpp"
4+
#include "ConnectionGraphicsObject.hpp"
45
#include "DataFlowGraphModel.hpp"
56
#include "Export.hpp"
7+
#include "NodeConnectionInteraction.hpp"
68

79
namespace QtNodes {
810

@@ -22,6 +24,7 @@ class NODE_EDITOR_PUBLIC DataFlowGraphicsScene : public BasicGraphicsScene
2224
public:
2325
std::vector<NodeId> selectedNodes() const;
2426
QMenu *createSceneMenu(QPointF const scenePos) override;
27+
void updateConnectionGraphics(const std::unordered_set<ConnectionId> &connections, bool state);
2528

2629
public Q_SLOTS:
2730
bool save() const;

include/QtNodes/internal/NodeDelegateModel.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "NodeData.hpp"
1313
#include "NodeStyle.hpp"
1414
#include "Serializable.hpp"
15+
#include <QtGui/QColor>
1516

1617
namespace QtNodes {
1718

@@ -133,6 +134,10 @@ class NODE_EDITOR_PUBLIC NodeDelegateModel
133134

134135
virtual bool resizable() const { return false; }
135136

137+
bool frozen() const { return _frozen; }
138+
139+
void setFrozenState(bool state) { _frozen = state; }
140+
136141
public Q_SLOTS:
137142
virtual void inputConnectionCreated(ConnectionId const &) {}
138143
virtual void inputConnectionDeleted(ConnectionId const &) {}
@@ -186,6 +191,8 @@ public Q_SLOTS:
186191
private:
187192
NodeStyle _nodeStyle;
188193

194+
bool _frozen{false};
195+
189196
NodeValidationState _nodeValidationState;
190197

191198
NodeProcessingStatus _processingStatus{NodeProcessingStatus::NoStatus};

src/BasicGraphicsScene.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#include "AbstractNodeGeometry.hpp"
44
#include "ConnectionGraphicsObject.hpp"
55
#include "ConnectionIdUtils.hpp"
6+
#include "DataFlowGraphModel.hpp"
67
#include "DefaultConnectionPainter.hpp"
78
#include "DefaultHorizontalNodeGeometry.hpp"
89
#include "DefaultNodePainter.hpp"
910
#include "DefaultVerticalNodeGeometry.hpp"
1011
#include "GraphicsView.hpp"
12+
#include "NodeDelegateModel.hpp"
1113
#include "NodeGraphicsObject.hpp"
1214

1315
#include <QUndoStack>
@@ -425,6 +427,33 @@ void BasicGraphicsScene::onModelReset()
425427
traverseGraphAndPopulateGraphicsObjects();
426428
}
427429

430+
void BasicGraphicsScene::freezeModelAndConnections(bool isFreeze)
431+
{
432+
for (QGraphicsItem *item : selectedItems()) {
433+
if (auto n = qgraphicsitem_cast<NodeGraphicsObject *>(item)) {
434+
int portCount = graphModel().nodeData(n->nodeId(), NodeRole::OutPortCount).toInt();
435+
for (int i = 0; i < portCount; i++) {
436+
auto graphConnections = graphModel().connections(n->nodeId(),
437+
QtNodes::PortType::Out,
438+
QtNodes::PortIndex(i));
439+
440+
for (auto const &c : graphConnections) {
441+
if (auto *cgo = connectionGraphicsObject(c)) {
442+
cgo->connectionState().setFrozen(isFreeze);
443+
cgo->update();
444+
}
445+
}
446+
}
447+
448+
if (auto *dfModel = dynamic_cast<DataFlowGraphModel *>(&graphModel())) {
449+
if (auto *delegate = dfModel->delegateModel<NodeDelegateModel>(n->nodeId())) {
450+
delegate->setFrozenState(isFreeze);
451+
}
452+
}
453+
}
454+
}
455+
}
456+
428457
std::weak_ptr<NodeGroup> BasicGraphicsScene::createGroup(std::vector<NodeGraphicsObject *> &nodes,
429458
QString groupName,
430459
GroupId groupId)

src/DataFlowGraphModel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ bool DataFlowGraphModel::setPortData(
444444
switch (role) {
445445
case PortRole::Data:
446446
if (portType == PortType::In) {
447+
if (model->frozen())
448+
return false;
449+
447450
model->setInData(value.value<std::shared_ptr<NodeData>>(), portIndex);
448451

449452
// Triggers repainting on the scene.

src/DataFlowGraphicsScene.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,15 @@ bool DataFlowGraphicsScene::load()
294294
return true;
295295
}
296296

297+
void DataFlowGraphicsScene::updateConnectionGraphics(
298+
const std::unordered_set<ConnectionId> &connections, bool state)
299+
{
300+
for (auto const &c : connections) {
301+
if (auto *cgo = connectionGraphicsObject(c)) {
302+
cgo->connectionState().setFrozen(state);
303+
cgo->update();
304+
}
305+
}
306+
}
307+
297308
} // namespace QtNodes

src/DefaultConnectionPainter.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <QtGui/QIcon>
1111

12-
1312
namespace QtNodes {
1413

1514
QPainterPath DefaultConnectionPainter::cubicPath(ConnectionGraphicsObject const &connection) const
@@ -27,11 +26,12 @@ QPainterPath DefaultConnectionPainter::cubicPath(ConnectionGraphicsObject const
2726
return cubic;
2827
}
2928

30-
void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const
29+
void DefaultConnectionPainter::drawSketchLine(QPainter *painter,
30+
ConnectionGraphicsObject const &cgo) const
3131
{
3232
ConnectionState const &state = cgo.connectionState();
3333

34-
if (state.requiresPort()) {
34+
if (state.requiresPort() || state.frozen()) {
3535
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();
3636

3737
QPen pen;
@@ -49,7 +49,8 @@ void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraph
4949
}
5050
}
5151

52-
void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo) const
52+
void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter,
53+
ConnectionGraphicsObject const &cgo) const
5354
{
5455
bool const hovered = cgo.connectionState().hovered();
5556
bool const selected = cgo.isSelected();
@@ -74,11 +75,12 @@ void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, Connecti
7475
}
7576
}
7677

77-
void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const
78+
void DefaultConnectionPainter::drawNormalLine(QPainter *painter,
79+
ConnectionGraphicsObject const &cgo) const
7880
{
7981
ConnectionState const &state = cgo.connectionState();
8082

81-
if (state.requiresPort())
83+
if (state.requiresPort() || state.frozen())
8284
return;
8385

8486
// colors
@@ -132,6 +134,7 @@ void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraph
132134
painter->setBrush(Qt::NoBrush);
133135

134136
QColor cOut = normalColorOut;
137+
135138
if (selected)
136139
cOut = cOut.darker(200);
137140
p.setColor(cOut);
@@ -200,7 +203,8 @@ void DefaultConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject
200203
painter->drawEllipse(cgo.in(), pointRadius, pointRadius);
201204
}
202205

203-
QPainterPath DefaultConnectionPainter::getPainterStroke(ConnectionGraphicsObject const &connection) const
206+
QPainterPath DefaultConnectionPainter::getPainterStroke(
207+
ConnectionGraphicsObject const &connection) const
204208
{
205209
auto cubic = cubicPath(connection);
206210

src/GraphicsView.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "DataFlowGraphModel.hpp"
66
#include "Definitions.hpp"
77
#include "GroupGraphicsObject.hpp"
8+
#include "NodeDelegateModel.hpp"
89
#include "NodeGraphicsObject.hpp"
910
#include "StyleCollection.hpp"
1011
#include "UndoCommands.hpp"
@@ -28,6 +29,7 @@
2829
using QtNodes::BasicGraphicsScene;
2930
using QtNodes::DataFlowGraphModel;
3031
using QtNodes::GraphicsView;
32+
using QtNodes::NodeDelegateModel;
3133
using QtNodes::NodeGraphicsObject;
3234

3335
GraphicsView::GraphicsView(QWidget *parent)

0 commit comments

Comments
 (0)