diff --git a/AGENTS.md b/AGENTS.md index 8099efce..87d467a0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -54,6 +54,8 @@ signals. - Styling: JSON styles via `NodeStyle`, `ConnectionStyle`, `GraphicsViewStyle`, managed by the `StyleCollection` singleton. - Serialization: the `Serializable` interface, `save()`/`load()` with `QJsonObject`. + Read ids from JSON with `jsonValueToUInt32()`, never `toInt()`. `toInt()` returns 0 for + ids of 2^31 or more. Core types: `NodeId` and `PortIndex` are `unsigned int` (`InvalidNodeId` is the maximum value). `PortType` is `In`, `Out` or `None`. `ConnectionId` is diff --git a/src/BasicGraphicsScene.cpp b/src/BasicGraphicsScene.cpp index e5460016..8f7e1929 100644 --- a/src/BasicGraphicsScene.cpp +++ b/src/BasicGraphicsScene.cpp @@ -49,7 +49,7 @@ using QtNodes::NodeId; NodeId jsonValueToNodeId(QJsonValue const &value) { if (value.isDouble()) { - return static_cast(value.toInt()); + return QtNodes::jsonValueToUInt32(value); } if (value.isString()) { @@ -614,7 +614,7 @@ std::unordered_map BasicGraphicsScene::loadItems(const QByteArray nodeById.reserve(nodesJsonArray.size()); for (const QJsonValue &v : nodesJsonArray) { QJsonObject o = v.toObject(); - NodeId id = static_cast(o.value("id").toInt(InvalidNodeId)); + NodeId id = QtNodes::jsonValueToUInt32(o.value("id")); if (id != InvalidNodeId) nodeById.insert(id, o); } @@ -653,7 +653,7 @@ std::unordered_map BasicGraphicsScene::loadItems(const QByteArray for (QJsonValueRef node : nodesJsonArray) { QJsonObject nodeObj = node.toObject(); - NodeId oldNodeId = static_cast(nodeObj.value("id").toInt(InvalidNodeId)); + NodeId oldNodeId = QtNodes::jsonValueToUInt32(nodeObj.value("id")); if (createdOldNodeIds.contains(oldNodeId)) { continue; @@ -811,7 +811,7 @@ BasicGraphicsScene::restoreGroup(QJsonObject const &groupJson, QJsonObject nodeObject; if (nodeVal.isDouble()) { - NodeId const oldNodeId = static_cast(nodeVal.toInt(InvalidNodeId)); + NodeId const oldNodeId = QtNodes::jsonValueToUInt32(nodeVal); if (oldNodeId == InvalidNodeId) { qWarning() << "restoreGroup(): invalid node id in group:" << nodeVal; continue; diff --git a/src/DataFlowGraphModel.cpp b/src/DataFlowGraphModel.cpp index 95d02887..2a812830 100644 --- a/src/DataFlowGraphModel.cpp +++ b/src/DataFlowGraphModel.cpp @@ -602,7 +602,7 @@ void DataFlowGraphModel::loadNode(QJsonObject const &nodeJson) // loading. // 2. When undoing the deletion command. Conflict is not possible // because all the new ids were created past the removed nodes. - NodeId restoredNodeId = nodeJson["id"].toInt(); + NodeId restoredNodeId = jsonValueToUInt32(nodeJson["id"]); _nextNodeId = std::max(_nextNodeId, restoredNodeId + 1); diff --git a/src/DataFlowGraphicsScene.cpp b/src/DataFlowGraphicsScene.cpp index 5a0e390c..4edfd8f4 100644 --- a/src/DataFlowGraphicsScene.cpp +++ b/src/DataFlowGraphicsScene.cpp @@ -38,7 +38,7 @@ using QtNodes::InvalidGroupId; GroupId jsonValueToGroupId(QJsonValue const &value) { if (value.isDouble()) { - return static_cast(value.toInt()); + return QtNodes::jsonValueToUInt32(value); } if (value.isString()) { @@ -269,7 +269,7 @@ bool DataFlowGraphicsScene::load() groupNodes.reserve(nodeIdsJson.size()); for (QJsonValue idValue : nodeIdsJson) { - NodeId const nodeId = static_cast(idValue.toInt()); + NodeId const nodeId = jsonValueToUInt32(idValue); if (auto *nodeObject = nodeGraphicsObject(nodeId)) { groupNodes.push_back(nodeObject); } diff --git a/test/src/TestSerialization.cpp b/test/src/TestSerialization.cpp index 854076f4..9e4b1a69 100644 --- a/test/src/TestSerialization.cpp +++ b/test/src/TestSerialization.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -153,3 +154,38 @@ TEST_CASE("Individual node serialization", "[serialization]") CHECK(nodeJson.contains("position")); } } + +TEST_CASE("DataFlowGraphModel loads node ids above the signed 32-bit range", "[serialization]") +{ + auto app = applicationSetup(); + auto registry = std::make_shared(); + registry->registerModel(createSerializableModel, "SerializableTestModel"); + + DataFlowGraphModel model(registry); + NodeId const a = model.addNode("SerializableTestModel"); + NodeId const b = model.addNode("SerializableTestModel"); + model.addConnection(ConnectionId{a, 0, b, 0}); + + quint32 const bigA = 3000000000u; + quint32 const bigB = 3000000001u; + + QJsonObject json = model.save(); + + QJsonArray nodes = json["nodes"].toArray(); + for (int i = 0; i < nodes.size(); ++i) { + QJsonObject node = nodes[i].toObject(); + node["id"] = static_cast(node["id"].toInt() == static_cast(a) ? bigA : bigB); + nodes[i] = node; + } + json["nodes"] = nodes; + + QJsonObject conn = QtNodes::toJson(ConnectionId{bigA, 0, bigB, 0}); + json["connections"] = QJsonArray{conn}; + + DataFlowGraphModel loaded(registry); + loaded.load(json); + + CHECK(loaded.nodeExists(bigA)); + CHECK(loaded.nodeExists(bigB)); + CHECK(loaded.connectionExists(ConnectionId{bigA, 0, bigB, 0})); +} diff --git a/test/src/TestSerializedItemCommands.cpp b/test/src/TestSerializedItemCommands.cpp index 95b6af3c..25f0f8c1 100644 --- a/test/src/TestSerializedItemCommands.cpp +++ b/test/src/TestSerializedItemCommands.cpp @@ -4,11 +4,16 @@ #include #include #include +#include +#include #include #include +#include +#include #include +#include #include #include @@ -515,3 +520,44 @@ TEST_CASE("Paste keeps connections for node ids above the signed 32-bit range", for (NodeId id : model.deletedIds) CHECK(id >= BigIdGraphModel::firstId); } + +TEST_CASE("Loading serialized items restores a group and a connection for ids above the signed " + "32-bit range", + "[undo][serialized][group]") +{ + auto app = applicationSetup(); + + BigIdGraphModel model; + BasicGraphicsScene scene(model); + scene.setGroupingEnabled(true); + + 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}); + auto const group = scene.createGroupFromSelection("Big").lock(); + REQUIRE(group != nullptr); + + // Nodes in a group are locked. Select the group to copy it with its nodes. + scene.clearSelection(); + group->groupGraphicsObject().setSelected(true); + scene.undoStack().push(new CopyCommand(&scene)); + + QMimeData const *mimeData = QApplication::clipboard()->mimeData(); + REQUIRE(mimeData != nullptr); + QByteArray const data = mimeData->data("application/qt-nodes-graph"); + REQUIRE_FALSE(data.isEmpty()); + + BigIdGraphModel otherModel; + BasicGraphicsScene otherScene(otherModel); + otherScene.setGroupingEnabled(true); + otherScene.loadFromMemory(data); + + CHECK(otherModel.allNodeIds().size() == 2); + REQUIRE(otherScene.groups().size() == 1); + CHECK(otherScene.groups().begin()->second->childNodes().size() == 2); + + NodeId const first = *otherModel.allNodeIds().begin(); + CHECK(otherModel.allConnectionIds(first).size() == 1); +}