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
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/BasicGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ using QtNodes::NodeId;
NodeId jsonValueToNodeId(QJsonValue const &value)
{
if (value.isDouble()) {
return static_cast<NodeId>(value.toInt());
return QtNodes::jsonValueToUInt32(value);
}

if (value.isString()) {
Expand Down Expand Up @@ -614,7 +614,7 @@ std::unordered_map<QUuid, QUuid> BasicGraphicsScene::loadItems(const QByteArray
nodeById.reserve(nodesJsonArray.size());
for (const QJsonValue &v : nodesJsonArray) {
QJsonObject o = v.toObject();
NodeId id = static_cast<NodeId>(o.value("id").toInt(InvalidNodeId));
NodeId id = QtNodes::jsonValueToUInt32(o.value("id"));
if (id != InvalidNodeId)
nodeById.insert(id, o);
}
Expand Down Expand Up @@ -653,7 +653,7 @@ std::unordered_map<QUuid, QUuid> BasicGraphicsScene::loadItems(const QByteArray

for (QJsonValueRef node : nodesJsonArray) {
QJsonObject nodeObj = node.toObject();
NodeId oldNodeId = static_cast<NodeId>(nodeObj.value("id").toInt(InvalidNodeId));
NodeId oldNodeId = QtNodes::jsonValueToUInt32(nodeObj.value("id"));

if (createdOldNodeIds.contains(oldNodeId)) {
continue;
Expand Down Expand Up @@ -811,7 +811,7 @@ BasicGraphicsScene::restoreGroup(QJsonObject const &groupJson,
QJsonObject nodeObject;

if (nodeVal.isDouble()) {
NodeId const oldNodeId = static_cast<NodeId>(nodeVal.toInt(InvalidNodeId));
NodeId const oldNodeId = QtNodes::jsonValueToUInt32(nodeVal);
if (oldNodeId == InvalidNodeId) {
qWarning() << "restoreGroup(): invalid node id in group:" << nodeVal;
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/DataFlowGraphModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/DataFlowGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ using QtNodes::InvalidGroupId;
GroupId jsonValueToGroupId(QJsonValue const &value)
{
if (value.isDouble()) {
return static_cast<GroupId>(value.toInt());
return QtNodes::jsonValueToUInt32(value);
}

if (value.isString()) {
Expand Down Expand Up @@ -269,7 +269,7 @@ bool DataFlowGraphicsScene::load()
groupNodes.reserve(nodeIdsJson.size());

for (QJsonValue idValue : nodeIdsJson) {
NodeId const nodeId = static_cast<NodeId>(idValue.toInt());
NodeId const nodeId = jsonValueToUInt32(idValue);
if (auto *nodeObject = nodeGraphicsObject(nodeId)) {
groupNodes.push_back(nodeObject);
}
Expand Down
36 changes: 36 additions & 0 deletions test/src/TestSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QtNodes/NodeDelegateModel>
#include <QtNodes/NodeDelegateModelRegistry>
#include <QtNodes/Definitions>
#include <QtNodes/internal/ConnectionIdUtils.hpp>

#include <catch2/catch.hpp>

Expand Down Expand Up @@ -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<NodeDelegateModelRegistry>();
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<qint64>(node["id"].toInt() == static_cast<int>(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}));
}
46 changes: 46 additions & 0 deletions test/src/TestSerializedItemCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
#include <QtNodes/BasicGraphicsScene>
#include <QtNodes/Definitions>
#include <QtNodes/internal/NodeGraphicsObject.hpp>
#include <QtNodes/internal/GroupGraphicsObject.hpp>
#include <QtNodes/internal/NodeGroup.hpp>
#include <QtNodes/internal/UndoCommands.hpp>

#include <catch2/catch.hpp>

#include <QApplication>
#include <QClipboard>
#include <QCoreApplication>
#include <QMimeData>
#include <QUndoStack>

#include <algorithm>
Expand Down Expand Up @@ -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);
}
Loading