Skip to content

Commit a23cd5e

Browse files
committed
add docstring to methods and modularize
1 parent 6f9897e commit a23cd5e

2 files changed

Lines changed: 70 additions & 38 deletions

File tree

include/QtNodes/internal/BasicGraphicsScene.hpp

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -182,50 +182,45 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
182182
*/
183183
void removeNodeFromGroup(NodeId nodeId);
184184

185+
/**
186+
* @brief Loads serialized item (nodes, connections and groups) into the scene.
187+
* @param data Serialized scene payload.
188+
* @param pastePos Reference position used when pasting content.
189+
* @param usePastePos When true, places the loaded content relative to pastePos position.
190+
* @return Mapping between original node UUIDs and newly created node UUIDs.
191+
*/
185192
std::unordered_map<QUuid, QUuid> loadItems(const QByteArray &data,
186193
QPointF pastePos,
187194
bool usePastePos = true);
188195

196+
/**
197+
* @brief Loads scene data from memory.
198+
* @param data Serialized scene payload.
199+
* @return Mapping between original node UUIDs and newly created node UUIDs.
200+
*/
189201
std::unordered_map<QUuid, QUuid> loadFromMemory(const QByteArray &data);
190202

191-
QUuid encodeNodeId(NodeId nodeId)
192-
{
193-
QByteArray bytes(16, 0);
194-
QDataStream stream(&bytes, QIODevice::WriteOnly);
195-
stream << static_cast<quint32>(nodeId);
196-
return QUuid::fromRfc4122(bytes);
197-
}
198-
199-
NodeId decodeNodeUuid(QUuid const &uuid)
200-
{
201-
auto bytes = uuid.toRfc4122();
202-
if (bytes.size() < static_cast<int>(sizeof(quint32)))
203-
return QtNodes::InvalidNodeId;
204-
205-
QDataStream stream(bytes);
206-
quint32 value = 0;
207-
stream >> value;
208-
return static_cast<NodeId>(value);
209-
}
210-
211-
std::unordered_map<NodeId, NodeId> convertMap(std::unordered_map<QUuid, QUuid> const &uuidMap)
212-
{
213-
std::unordered_map<NodeId, NodeId> idMap;
214-
215-
// Iterando pelo mapa de QUuids e convertendo para NodeIds
216-
for (const auto &pair : uuidMap) {
217-
// Decodificando os QUuids para NodeIds
218-
NodeId keyNodeId = decodeNodeUuid(pair.first);
219-
NodeId valueNodeId = decodeNodeUuid(pair.second);
220-
221-
// Inserindo no novo mapa
222-
if (keyNodeId != QtNodes::InvalidNodeId && valueNodeId != QtNodes::InvalidNodeId) {
223-
idMap[keyNodeId] = valueNodeId;
224-
}
225-
}
226-
227-
return idMap;
228-
}
203+
/**
204+
* @brief Encodes NodeId into a QUuid representation.
205+
* @param nodeId Node identifier.
206+
* @return QUuid carrying the binary value of nodeId.
207+
*/
208+
QUuid encodeNodeId(NodeId nodeId);
209+
210+
/**
211+
* @brief Decodes a node QUuid into a NodeId.
212+
* @param uuid QUuid containing an encoded node id.
213+
* @return Decoded NodeId.
214+
*/
215+
NodeId decodeNodeUuid(QUuid const &uuid);
216+
217+
/**
218+
* @brief Converts a QUuid-to-QUuid map into a NodeId-to-NodeId map.
219+
* @param uuidMap Map containing encoded old and new node QUuid pairs.
220+
* @return Map with decoded NodeId pairs, excluding invalid entries.
221+
*/
222+
223+
std::unordered_map<NodeId, NodeId> convertMap(std::unordered_map<QUuid, QUuid> const &uuidMap);
229224

230225
public:
231226
/**

src/BasicGraphicsScene.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,43 @@ std::unordered_map<QUuid, QUuid> BasicGraphicsScene::loadFromMemory(const QByteA
681681
return map;
682682
}
683683

684+
QUuid BasicGraphicsScene::encodeNodeId(NodeId nodeId)
685+
{
686+
QByteArray bytes(16, 0);
687+
QDataStream stream(&bytes, QIODevice::WriteOnly);
688+
stream << static_cast<quint32>(nodeId);
689+
return QUuid::fromRfc4122(bytes);
690+
}
691+
692+
NodeId BasicGraphicsScene::decodeNodeUuid(QUuid const &uuid)
693+
{
694+
auto bytes = uuid.toRfc4122();
695+
if (bytes.size() < static_cast<int>(sizeof(quint32)))
696+
return QtNodes::InvalidNodeId;
697+
698+
QDataStream stream(bytes);
699+
quint32 value = 0;
700+
stream >> value;
701+
return static_cast<NodeId>(value);
702+
}
703+
704+
std::unordered_map<NodeId, NodeId> BasicGraphicsScene::convertMap(
705+
std::unordered_map<QUuid, QUuid> const &uuidMap)
706+
{
707+
std::unordered_map<NodeId, NodeId> idMap;
708+
709+
for (const auto &pair : uuidMap) {
710+
NodeId keyNodeId = decodeNodeUuid(pair.first);
711+
NodeId valueNodeId = decodeNodeUuid(pair.second);
712+
713+
if (keyNodeId != QtNodes::InvalidNodeId && valueNodeId != QtNodes::InvalidNodeId) {
714+
idMap[keyNodeId] = valueNodeId;
715+
}
716+
}
717+
718+
return idMap;
719+
}
720+
684721
std::weak_ptr<QtNodes::NodeGroup> BasicGraphicsScene::createGroupFromSelection(QString groupName)
685722
{
686723
if (!_groupingEnabled)

0 commit comments

Comments
 (0)