Skip to content

Commit b147ba0

Browse files
zmothpaceholder
authored andcommitted
Add CreateCommand
1 parent 8a41b1c commit b147ba0

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

src/DataFlowGraphicsScene.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "GraphicsView.hpp"
55
#include "NodeDelegateModelRegistry.hpp"
66
#include "NodeGraphicsObject.hpp"
7+
#include "UndoCommands.hpp"
78

89
#include <QtWidgets/QFileDialog>
910
#include <QtWidgets/QGraphicsSceneMoveEvent>
@@ -130,14 +131,7 @@ createSceneMenu(QPointF const scenePos)
130131
return;
131132
}
132133

133-
NodeId nodeId = this->_graphModel.addNode(item->text(0));
134-
135-
if (nodeId != InvalidNodeId)
136-
{
137-
_graphModel.setNodeData(nodeId,
138-
NodeRole::Position,
139-
scenePos);
140-
}
134+
this->undoStack().push(new CreateCommand(this, item->text(0), scenePos));
141135

142136
modelMenu->close();
143137
});

src/UndoCommands.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,53 @@ computeAverageNodePosition(QJsonObject const & sceneJson)
149149
}
150150

151151

152+
//-------------------------------------
153+
154+
155+
CreateCommand::
156+
CreateCommand(BasicGraphicsScene* scene,
157+
QString const name,
158+
QPointF const & mouseScenePos)
159+
: _scene(scene)
160+
, _sceneJson(QJsonObject())
161+
{
162+
_nodeId = _scene->graphModel().addNode(name);
163+
if (_nodeId != InvalidNodeId)
164+
{
165+
_scene->graphModel().setNodeData(_nodeId, NodeRole::Position, mouseScenePos);
166+
}
167+
else
168+
{
169+
setObsolete(true);
170+
}
171+
}
172+
173+
174+
void
175+
CreateCommand::
176+
undo()
177+
{
178+
QJsonArray nodesJsonArray;
179+
nodesJsonArray.append(_scene->graphModel().saveNode(_nodeId));
180+
_sceneJson["nodes"] = nodesJsonArray;
181+
182+
_scene->graphModel().deleteNode(_nodeId);
183+
}
184+
185+
186+
void
187+
CreateCommand::
188+
redo()
189+
{
190+
if(_sceneJson.empty() || _sceneJson["nodes"].toArray().empty())
191+
return;
192+
193+
insertSerializedItems(_sceneJson, _scene);
194+
}
195+
196+
197+
//-------------------------------------
198+
152199

153200
DeleteCommand::
154201
DeleteCommand(BasicGraphicsScene* scene)

src/UndoCommands.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ namespace QtNodes
1414
class BasicGraphicsScene;
1515

1616

17+
class CreateCommand : public QUndoCommand
18+
{
19+
public:
20+
CreateCommand(BasicGraphicsScene* scene,
21+
QString const name,
22+
QPointF const & mouseScenePos);
23+
24+
void undo() override;
25+
void redo() override;
26+
27+
private:
28+
BasicGraphicsScene* _scene;
29+
NodeId _nodeId;
30+
QJsonObject _sceneJson;
31+
};
32+
33+
1734
/**
1835
* Selected scene objects are serialized and then removed from the scene.
1936
* The deleted elements could be restored in `undo`.

0 commit comments

Comments
 (0)