diff --git a/docs/faq.rst b/docs/faq.rst index 25ec08a0..681fa8a9 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -72,6 +72,9 @@ Implement ``saveNode()`` and ``loadNode()`` in your model: // Recreate node from saved data } +The library writes the node id itself. With the default ``saveNode()`` and +``loadNode()``, undo of a delete cannot restore the node. + **Embedded widget doesn't show** Ensure your ``embeddedWidget()`` returns a valid widget: diff --git a/docs/guide/graph-models.rst b/docs/guide/graph-models.rst index 3ce60bbc..fed51af6 100644 --- a/docs/guide/graph-models.rst +++ b/docs/guide/graph-models.rst @@ -305,6 +305,10 @@ Override ``saveNode()`` and ``loadNode()`` to support save/load: return json; } +The default ``saveNode()`` returns an empty object and the default +``loadNode()`` does nothing. Without both functions, undo of a delete cannot +restore the node. See :doc:`undo-redo`. + See :doc:`serialization` for complete save/load implementation. Complete Example diff --git a/docs/guide/undo-redo.rst b/docs/guide/undo-redo.rst index 4912863e..5e2ea36a 100644 --- a/docs/guide/undo-redo.rst +++ b/docs/guide/undo-redo.rst @@ -99,10 +99,16 @@ model implements ``saveNode()`` and ``loadNode()``: restoreNodeInternalData(nodeId, json["internal-data"].toObject()); } +The library writes the ``"id"`` key itself after it calls ``saveNode()``. +Your ``saveNode()`` does not need to save the node id for the undo commands. +It still needs to save the data that ``loadNode()`` reads back. + .. warning:: - Without proper ``saveNode()``/``loadNode()``, deleted nodes cannot be - restored by undo. + The default ``saveNode()`` returns an empty object and the default + ``loadNode()`` does nothing. With the defaults, the delete command still + deletes the right node, but undo cannot restore it. The node stays deleted. + To restore a deleted node by undo, implement both functions. Custom Undo Commands -------------------- diff --git a/include/QtNodes/internal/AbstractGraphModel.hpp b/include/QtNodes/internal/AbstractGraphModel.hpp index cf8e2491..9901cd08 100644 --- a/include/QtNodes/internal/AbstractGraphModel.hpp +++ b/include/QtNodes/internal/AbstractGraphModel.hpp @@ -159,6 +159,14 @@ class NODE_EDITOR_PUBLIC AbstractGraphModel : public QObject /** * Reimplement the function if you want to store/restore the node's * inner state during undo/redo node deletion operations. + * + * The library writes the "id" key itself after this call. You do not + * need to save the node id for the undo commands. + * + * To restore a deleted node by undo, saveNode() and loadNode() must + * save and restore the node state. The default implementation returns + * an empty object. Then undo of a delete cannot restore the node. The + * node stays deleted. */ virtual QJsonObject saveNode(NodeId const) const { return {}; } @@ -183,6 +191,9 @@ class NODE_EDITOR_PUBLIC AbstractGraphModel : public QObject * * The function must do almost exacly the same thing as the normal addNode(). * The main difference is in a model-specific `inner-data` processing. + * + * The default implementation does nothing. Then undo of a delete cannot + * restore the node. The node stays deleted. */ virtual void loadNode(QJsonObject const &) {} diff --git a/include/QtNodes/internal/ConnectionIdUtils.hpp b/include/QtNodes/internal/ConnectionIdUtils.hpp index 3e29babf..39909e1b 100644 --- a/include/QtNodes/internal/ConnectionIdUtils.hpp +++ b/include/QtNodes/internal/ConnectionIdUtils.hpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace QtNodes { @@ -138,20 +139,46 @@ inline QJsonObject toJson(ConnectionId const &connId) return connJson; } +// The invalid value is the same for all ids. jsonValueToUInt32() returns it on failure. +static_assert(InvalidNodeId == InvalidGroupId && InvalidNodeId == InvalidPortIndex + && InvalidNodeId == std::numeric_limits::max(), + "InvalidNodeId, InvalidGroupId and InvalidPortIndex must be the same value"); + +/** + * Reads an unsigned 32-bit id from JSON. + * Returns the maximum value (InvalidNodeId, InvalidGroupId, InvalidPortIndex) when the value is + * not a number in the unsigned 32-bit range. + * QJsonValue::toInt() returns the default value for numbers above the int range. + * This function accepts the whole unsigned range. + */ +inline quint32 jsonValueToUInt32(QJsonValue const &value) +{ + constexpr quint32 invalid = std::numeric_limits::max(); + + if (!value.isDouble()) + return invalid; + + double const number = value.toDouble(); + if (number < 0.0 || number > static_cast(invalid)) + return invalid; + + return static_cast(number); +} + inline ConnectionId fromJson(QJsonObject const &connJson) { // Support both "inNodeId" (correct) and "intNodeId" (legacy typo) for backward compatibility NodeId inNodeId = InvalidNodeId; if (connJson.contains("inNodeId")) { - inNodeId = static_cast(connJson["inNodeId"].toInt(InvalidNodeId)); + inNodeId = jsonValueToUInt32(connJson["inNodeId"]); } else if (connJson.contains("intNodeId")) { - inNodeId = static_cast(connJson["intNodeId"].toInt(InvalidNodeId)); + inNodeId = jsonValueToUInt32(connJson["intNodeId"]); } - ConnectionId connId{static_cast(connJson["outNodeId"].toInt(InvalidNodeId)), - static_cast(connJson["outPortIndex"].toInt(InvalidPortIndex)), + ConnectionId connId{jsonValueToUInt32(connJson["outNodeId"]), + jsonValueToUInt32(connJson["outPortIndex"]), inNodeId, - static_cast(connJson["inPortIndex"].toInt(InvalidPortIndex))}; + jsonValueToUInt32(connJson["inPortIndex"])}; return connId; } diff --git a/src/UndoCommands.cpp b/src/UndoCommands.cpp index 00f52c6a..c655d19a 100644 --- a/src/UndoCommands.cpp +++ b/src/UndoCommands.cpp @@ -18,6 +18,14 @@ namespace QtNodes { +// The library writes the node id itself. A model may not store it in saveNode(). +static QJsonObject saveNodeWithId(AbstractGraphModel const &graphModel, NodeId const nodeId) +{ + QJsonObject json = graphModel.saveNode(nodeId); + json["id"] = static_cast(nodeId); + return json; +} + static QJsonObject serializeSelectedItems(BasicGraphicsScene *scene) { QJsonObject serializedScene; @@ -37,7 +45,7 @@ static QJsonObject serializeSelectedItems(BasicGraphicsScene *scene) auto const inserted = selectedNodes.insert(node->nodeId()); if (inserted.second) { - nodesJsonArray.append(graphModel.saveNode(node->nodeId())); + nodesJsonArray.append(saveNodeWithId(graphModel, node->nodeId())); } }; @@ -115,9 +123,12 @@ static void insertSerializedItems(QJsonObject const &json, BasicGraphicsScene *s graphModel.loadNode(obj); - auto id = obj["id"].toInt(); - scene->nodeGraphicsObject(id)->setZValue(1.0); - scene->nodeGraphicsObject(id)->setSelected(true); + // The model may not restore the node, for example with the default loadNode(). + NodeId const id = jsonValueToUInt32(obj["id"]); + if (auto *ngo = scene->nodeGraphicsObject(id)) { + ngo->setZValue(1.0); + ngo->setSelected(true); + } } QJsonArray const &connJsonArray = json["connections"].toArray(); @@ -130,7 +141,8 @@ static void insertSerializedItems(QJsonObject const &json, BasicGraphicsScene *s // Restore the connection graphModel.addConnection(connId); - scene->connectionGraphicsObject(connId)->setSelected(true); + if (auto *cgo = scene->connectionGraphicsObject(connId)) + cgo->setSelected(true); } if (json.contains("groups")) { @@ -145,7 +157,7 @@ static void insertSerializedItems(QJsonObject const &json, BasicGraphicsScene *s std::vector groupNodes; for (const QJsonValue &idVal : nodeIdsJson) { - NodeId nodeId = static_cast(idVal.toInt()); + NodeId nodeId = jsonValueToUInt32(idVal); if (auto *ngo = scene->nodeGraphicsObject(nodeId)) { groupNodes.push_back(ngo); } @@ -172,7 +184,7 @@ static void deleteSerializedItems(QJsonObject &sceneJson, AbstractGraphModel &gr for (QJsonValueRef node : nodesJsonArray) { QJsonObject nodeJson = node.toObject(); - graphModel.deleteNode(nodeJson["id"].toInt()); + graphModel.deleteNode(jsonValueToUInt32(nodeJson["id"])); } } @@ -213,7 +225,7 @@ CreateCommand::CreateCommand(BasicGraphicsScene *scene, void CreateCommand::undo() { QJsonArray nodesJsonArray; - nodesJsonArray.append(_scene->graphModel().saveNode(_nodeId)); + nodesJsonArray.append(saveNodeWithId(_scene->graphModel(), _nodeId)); _sceneJson["nodes"] = nodesJsonArray; _scene->graphModel().deleteNode(_nodeId); @@ -264,7 +276,7 @@ DeleteCommand::DeleteCommand(BasicGraphicsScene *scene) connJsonArray.append(toJson(cid)); } - nodesJsonArray.append(graphModel.saveNode(node->nodeId())); + nodesJsonArray.append(saveNodeWithId(graphModel, node->nodeId())); }; QJsonArray groupsJsonArray; @@ -448,7 +460,7 @@ QJsonObject PasteCommand::makeNewNodeIdsInScene(QJsonObject const &sceneJson) for (QJsonValueRef node : nodesJsonArray) { QJsonObject nodeJson = node.toObject(); - NodeId oldNodeId = nodeJson["id"].toInt(); + NodeId oldNodeId = jsonValueToUInt32(nodeJson["id"]); NodeId newNodeId = graphModel.newNodeId(); @@ -491,7 +503,7 @@ QJsonObject PasteCommand::makeNewNodeIdsInScene(QJsonObject const &sceneJson) QJsonArray newNodeIdsJson; for (const QJsonValue &idVal : nodeIdsJson) { - NodeId oldId = static_cast(idVal.toInt()); + NodeId oldId = jsonValueToUInt32(idVal); NodeId newId = mapNodeIds[oldId]; newNodeIdsJson.append(static_cast(newId)); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a233155e..53ba18ea 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ add_executable(test_nodes src/TestPaintedNodeShadows.cpp src/TestNodeStyleRole.cpp src/TestCopyPaste.cpp + src/TestSerializedItemCommands.cpp src/TestZoomFeatures.cpp src/TestLoopDetection.cpp include/ApplicationSetup.hpp diff --git a/test/include/TestGraphModel.hpp b/test/include/TestGraphModel.hpp index ee8918aa..016b8764 100644 --- a/test/include/TestGraphModel.hpp +++ b/test/include/TestGraphModel.hpp @@ -229,7 +229,7 @@ class TestGraphModel : public AbstractGraphModel QJsonObject saveNode(NodeId const nodeId) const override { QJsonObject result; - result["id"] = static_cast(nodeId); + result["id"] = static_cast(nodeId); auto nodeIt = _nodeData.find(nodeId); if (nodeIt != _nodeData.end()) { const auto &data = nodeIt->second; @@ -251,7 +251,7 @@ class TestGraphModel : public AbstractGraphModel void loadNode(QJsonObject const &nodeJson) override { - NodeId id = static_cast(nodeJson["id"].toInt()); + NodeId id = static_cast(nodeJson["id"].toDouble()); _nodeIds.insert(id); diff --git a/test/src/TestSerializedItemCommands.cpp b/test/src/TestSerializedItemCommands.cpp new file mode 100644 index 00000000..95b6af3c --- /dev/null +++ b/test/src/TestSerializedItemCommands.cpp @@ -0,0 +1,517 @@ +#include "ApplicationSetup.hpp" +#include "TestGraphModel.hpp" + +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include + +using QtNodes::BasicGraphicsScene; +using QtNodes::ConnectionId; +using QtNodes::CopyCommand; +using QtNodes::CreateCommand; +using QtNodes::DeleteCommand; +using QtNodes::NodeId; +using QtNodes::NodeRole; +using QtNodes::PasteCommand; + +namespace { + +/// Records the ids that reach deleteNode(). The first real node has the id 5. +class RecordingGraphModel : public TestGraphModel +{ +public: + RecordingGraphModel() + { + // Burn the ids 1 to 4. + for (int i = 0; i < 4; ++i) { + NodeId const id = addNode("Unused"); + TestGraphModel::deleteNode(id); + } + } + + bool deleteNode(NodeId const nodeId) override + { + deletedIds.push_back(nodeId); + return TestGraphModel::deleteNode(nodeId); + } + + std::vector deletedIds; +}; + +NodeId addNodeAt(RecordingGraphModel &model, QPointF const &pos) +{ + NodeId const id = model.addNode("TestNode"); + model.setNodeData(id, NodeRole::Position, pos); + return id; +} + +void selectOnly(BasicGraphicsScene &scene, std::vector const &ids) +{ + scene.clearSelection(); + for (NodeId id : ids) { + auto *ngo = scene.nodeGraphicsObject(id); + REQUIRE(ngo != nullptr); + ngo->setSelected(true); + } +} + +} // namespace + +TEST_CASE("Test nodes get ids that do not start at zero", "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + + CHECK(a == 5); + CHECK(b == 6); +} + +TEST_CASE("DeleteCommand deletes the selected node and not another one", "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + NodeId const c = addNodeAt(model, QPointF(200, 0)); + model.deletedIds.clear(); + + // Select the middle node. + selectOnly(scene, {b}); + scene.undoStack().push(new DeleteCommand(&scene)); + + CHECK(model.deletedIds == std::vector{b}); + CHECK(model.nodeExists(a)); + CHECK_FALSE(model.nodeExists(b)); + CHECK(model.nodeExists(c)); +} + +TEST_CASE("DeleteCommand passes the id of each selected node to deleteNode", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + NodeId const c = addNodeAt(model, QPointF(200, 0)); + NodeId const d = addNodeAt(model, QPointF(300, 0)); + model.deletedIds.clear(); + + selectOnly(scene, {a, c}); + scene.undoStack().push(new DeleteCommand(&scene)); + + std::sort(model.deletedIds.begin(), model.deletedIds.end()); + CHECK(model.deletedIds == std::vector{a, c}); + CHECK_FALSE(model.nodeExists(a)); + CHECK(model.nodeExists(b)); + CHECK_FALSE(model.nodeExists(c)); + CHECK(model.nodeExists(d)); +} + +TEST_CASE("Undo of a delete restores the node and its position, redo deletes it again", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(120, 80)); + NodeId const c = addNodeAt(model, QPointF(200, 0)); + + selectOnly(scene, {b}); + scene.undoStack().push(new DeleteCommand(&scene)); + REQUIRE_FALSE(model.nodeExists(b)); + + scene.undoStack().undo(); + + CHECK(model.nodeExists(a)); + CHECK(model.nodeExists(b)); + CHECK(model.nodeExists(c)); + CHECK(model.nodeData(b, NodeRole::Position).toPointF() == QPointF(120, 80)); + CHECK(scene.nodeGraphicsObject(b) != nullptr); + + model.deletedIds.clear(); + scene.undoStack().redo(); + + CHECK(model.deletedIds == std::vector{b}); + CHECK_FALSE(model.nodeExists(b)); + CHECK(model.nodeExists(a)); + CHECK(model.nodeExists(c)); + CHECK(scene.nodeGraphicsObject(b) == nullptr); +} + +TEST_CASE("Undo of a delete restores several nodes", "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(10, 20)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + NodeId const c = addNodeAt(model, QPointF(30, 40)); + + selectOnly(scene, {a, c}); + scene.undoStack().push(new DeleteCommand(&scene)); + REQUIRE(model.allNodeIds().size() == 1); + + scene.undoStack().undo(); + + CHECK(model.allNodeIds().size() == 3); + CHECK(model.nodeData(a, NodeRole::Position).toPointF() == QPointF(10, 20)); + CHECK(model.nodeData(b, NodeRole::Position).toPointF() == QPointF(100, 0)); + CHECK(model.nodeData(c, NodeRole::Position).toPointF() == QPointF(30, 40)); + CHECK(scene.nodeGraphicsObject(a) != nullptr); + CHECK(scene.nodeGraphicsObject(c) != nullptr); +} + +TEST_CASE("Deleting a node removes its connections and undo restores them", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + NodeId const c = addNodeAt(model, QPointF(200, 0)); + + ConnectionId const ab{a, 0, b, 0}; + ConnectionId const bc{b, 0, c, 0}; + ConnectionId const ac{a, 0, c, 0}; + model.addConnection(ab); + model.addConnection(bc); + model.addConnection(ac); + + selectOnly(scene, {b}); + scene.undoStack().push(new DeleteCommand(&scene)); + + CHECK_FALSE(model.connectionExists(ab)); + CHECK_FALSE(model.connectionExists(bc)); + CHECK(model.connectionExists(ac)); + CHECK(scene.connectionGraphicsObject(ac) != nullptr); + + scene.undoStack().undo(); + + CHECK(model.connectionExists(ab)); + CHECK(model.connectionExists(bc)); + CHECK(model.connectionExists(ac)); + CHECK(scene.connectionGraphicsObject(ab) != nullptr); + CHECK(scene.connectionGraphicsObject(bc) != nullptr); + + scene.undoStack().redo(); + + CHECK_FALSE(model.connectionExists(ab)); + CHECK_FALSE(model.connectionExists(bc)); + CHECK(model.connectionExists(ac)); +} + +TEST_CASE("CreateCommand adds a node, undo removes it and redo restores it", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const other = addNodeAt(model, QPointF(0, 0)); + model.deletedIds.clear(); + + scene.undoStack().push(new CreateCommand(&scene, "TestNode", QPointF(50, 60))); + + REQUIRE(model.allNodeIds().size() == 2); + NodeId created = other; + for (NodeId id : model.allNodeIds()) { + if (id != other) + created = id; + } + REQUIRE(created != other); + CHECK(model.nodeData(created, NodeRole::Position).toPointF() == QPointF(50, 60)); + + scene.undoStack().undo(); + + CHECK(model.deletedIds == std::vector{created}); + CHECK_FALSE(model.nodeExists(created)); + CHECK(model.nodeExists(other)); + + scene.undoStack().redo(); + + CHECK(model.nodeExists(created)); + CHECK(model.nodeData(created, NodeRole::Position).toPointF() == QPointF(50, 60)); + CHECK(scene.nodeGraphicsObject(created) != nullptr); +} + +TEST_CASE("Paste creates new node ids and keeps the connections between pasted nodes", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + NodeId const c = addNodeAt(model, QPointF(200, 0)); + model.addConnection(ConnectionId{a, 0, b, 0}); + model.addConnection(ConnectionId{b, 0, c, 0}); + + // Copy only a and b. The connection b -> c leaves the selection. + selectOnly(scene, {a, b}); + scene.undoStack().push(new CopyCommand(&scene)); + scene.undoStack().push(new PasteCommand(&scene, QPointF(500, 500))); + + REQUIRE(model.allNodeIds().size() == 5); + + std::vector added; + for (NodeId id : model.allNodeIds()) { + if (id != a && id != b && id != c) + added.push_back(id); + } + std::sort(added.begin(), added.end()); + REQUIRE(added.size() == 2); + CHECK(added[0] > c); + CHECK(added[1] > c); + + // The pasted nodes are connected like the originals. + // The order of the new ids follows the selection order, so accept both directions. + CHECK((model.connectionExists(ConnectionId{added[0], 0, added[1], 0}) + || model.connectionExists(ConnectionId{added[1], 0, added[0], 0}))); + CHECK(model.allConnectionIds(added[0]).size() == 1); + CHECK(model.allConnectionIds(added[1]).size() == 1); + + // The original nodes and connections are untouched. + CHECK(model.connectionExists(ConnectionId{a, 0, b, 0})); + CHECK(model.connectionExists(ConnectionId{b, 0, c, 0})); + + // Pasted nodes are centered on the paste position. + QPointF const p0 = model.nodeData(added[0], NodeRole::Position).toPointF(); + QPointF const p1 = model.nodeData(added[1], NodeRole::Position).toPointF(); + CHECK((p0 + p1) / 2.0 == QPointF(500, 500)); +} + +TEST_CASE("Undo of a paste deletes the pasted nodes by their new ids", "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + model.addConnection(ConnectionId{a, 0, b, 0}); + + selectOnly(scene, {a, b}); + scene.undoStack().push(new CopyCommand(&scene)); + scene.undoStack().push(new PasteCommand(&scene, QPointF(300, 300))); + REQUIRE(model.allNodeIds().size() == 4); + + model.deletedIds.clear(); + scene.undoStack().undo(); + + CHECK(model.allNodeIds().size() == 2); + CHECK(model.nodeExists(a)); + CHECK(model.nodeExists(b)); + CHECK(model.connectionExists(ConnectionId{a, 0, b, 0})); + REQUIRE(model.deletedIds.size() == 2); + for (NodeId id : model.deletedIds) { + CHECK(id != a); + CHECK(id != b); + } + + scene.undoStack().redo(); + CHECK(model.allNodeIds().size() == 4); +} + +namespace { + +/// Uses the default saveNode() and loadNode() of AbstractGraphModel. +/// The default saveNode() returns an empty object. The default loadNode() does nothing. +class DefaultSaveGraphModel : public RecordingGraphModel +{ +public: + QJsonObject saveNode(NodeId const nodeId) const override + { + return AbstractGraphModel::saveNode(nodeId); + } + + void loadNode(QJsonObject const &nodeJson) override { AbstractGraphModel::loadNode(nodeJson); } +}; + +} // namespace + +TEST_CASE("DeleteCommand deletes the selected node when the model keeps the default saveNode", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + DefaultSaveGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + model.deletedIds.clear(); + + selectOnly(scene, {b}); + scene.undoStack().push(new DeleteCommand(&scene)); + + CHECK(model.deletedIds == std::vector{b}); + CHECK(model.nodeExists(a)); + CHECK_FALSE(model.nodeExists(b)); +} + +TEST_CASE("Undo and redo of a delete do not crash when the model keeps the default saveNode", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + DefaultSaveGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + NodeId const c = addNodeAt(model, QPointF(200, 0)); + model.addConnection(ConnectionId{a, 0, b, 0}); + + selectOnly(scene, {b}); + scene.undoStack().push(new DeleteCommand(&scene)); + REQUIRE_FALSE(model.nodeExists(b)); + + // The default loadNode() cannot restore the node. + scene.undoStack().undo(); + CHECK_FALSE(model.nodeExists(b)); + CHECK(scene.nodeGraphicsObject(b) == nullptr); + CHECK(model.nodeExists(a)); + CHECK(model.nodeExists(c)); + + // Redo has nothing left to delete. The other nodes stay. + scene.undoStack().redo(); + CHECK_FALSE(model.nodeExists(b)); + CHECK(model.nodeExists(a)); + CHECK(model.nodeExists(c)); +} + +TEST_CASE("Paste does not crash when the model keeps the default saveNode", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + DefaultSaveGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + model.addConnection(ConnectionId{a, 0, b, 0}); + + selectOnly(scene, {a, b}); + scene.undoStack().push(new CopyCommand(&scene)); + scene.undoStack().push(new PasteCommand(&scene, QPointF(300, 300))); + + // The default loadNode() creates no nodes. The original nodes stay. + CHECK(model.allNodeIds().size() == 2); + CHECK(model.nodeExists(a)); + CHECK(model.nodeExists(b)); + CHECK(model.connectionExists(ConnectionId{a, 0, b, 0})); +} + +namespace { + +/// Node ids start above the range of a signed 32-bit integer. +class BigIdGraphModel : public RecordingGraphModel +{ +public: + NodeId newNodeId() override { return _nextBigId++; } + + static constexpr NodeId firstId = 3000000000u; + +private: + NodeId _nextBigId = firstId; +}; + +} // namespace + +TEST_CASE("Delete and undo work for node ids above the signed 32-bit range", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + BigIdGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 50)); + REQUIRE(a == BigIdGraphModel::firstId); + REQUIRE(b == BigIdGraphModel::firstId + 1); + + ConnectionId const ab{a, 0, b, 0}; + model.addConnection(ab); + model.deletedIds.clear(); + + selectOnly(scene, {b}); + scene.undoStack().push(new DeleteCommand(&scene)); + + CHECK(model.deletedIds == std::vector{b}); + CHECK(model.nodeExists(a)); + CHECK_FALSE(model.nodeExists(b)); + + scene.undoStack().undo(); + + CHECK(model.nodeExists(b)); + CHECK(model.nodeData(b, NodeRole::Position).toPointF() == QPointF(100, 50)); + CHECK(model.connectionExists(ab)); + CHECK(scene.nodeGraphicsObject(b) != nullptr); + + model.deletedIds.clear(); + scene.undoStack().redo(); + + CHECK(model.deletedIds == std::vector{b}); + CHECK_FALSE(model.nodeExists(b)); +} + +TEST_CASE("Paste keeps connections for node ids above the signed 32-bit range", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + BigIdGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + model.addConnection(ConnectionId{a, 0, b, 0}); + + selectOnly(scene, {a, b}); + scene.undoStack().push(new CopyCommand(&scene)); + scene.undoStack().push(new PasteCommand(&scene, QPointF(300, 300))); + + REQUIRE(model.allNodeIds().size() == 4); + for (NodeId id : model.allNodeIds()) + CHECK(id >= BigIdGraphModel::firstId); + + model.deletedIds.clear(); + scene.undoStack().undo(); + + CHECK(model.allNodeIds().size() == 2); + REQUIRE(model.deletedIds.size() == 2); + for (NodeId id : model.deletedIds) + CHECK(id >= BigIdGraphModel::firstId); +}