From 4b14352bcb60ff906f617c6cd94f3ad29a948a67 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Thu, 27 May 2021 15:14:03 +1000 Subject: [PATCH] save new node contexts to project file --- app/node/graph.h | 5 ++ app/node/project/project.cpp | 91 ++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/app/node/graph.h b/app/node/graph.h index 5ebe8d5fa..198fac466 100644 --- a/app/node/graph.h +++ b/app/node/graph.h @@ -98,6 +98,11 @@ public: return position_map_[context]; } + const QMap &GetPositionMap() const + { + return position_map_; + } + signals: /** * @brief Signal emitted when a Node is added to the graph diff --git a/app/node/project/project.cpp b/app/node/project/project.cpp index a3f4aca9e..5cc50d274 100644 --- a/app/node/project/project.cpp +++ b/app/node/project/project.cpp @@ -136,6 +136,71 @@ void Project::Load(QXmlStreamReader *reader, MainWindowLayoutInfo* layout, uint } } + } else if (reader->name() == QStringLiteral("positions")) { + + while (XMLReadNextStartElement(reader)) { + + if (reader->name() == QStringLiteral("context")) { + + quintptr context_ptr = 0; + XMLAttributeLoop(reader, attr) { + if (attr.name() == QStringLiteral("ptr")) { + context_ptr = attr.value().toULongLong(); + break; + } + } + + Node *context = xml_node_data.node_ptrs.value(context_ptr); + + if (!context) { + qWarning() << "Failed to find pointer for context"; + reader->skipCurrentElement(); + } else { + while (XMLReadNextStartElement(reader)) { + if (reader->name() == QStringLiteral("node")) { + quintptr node_ptr = 0; + XMLAttributeLoop(reader, attr) { + if (attr.name() == QStringLiteral("ptr")) { + node_ptr = attr.value().toULongLong(); + break; + } + } + + Node *node = xml_node_data.node_ptrs.value(node_ptr); + + if (!node) { + qWarning() << "Failed to find pointer for node position"; + reader->skipCurrentElement(); + } else { + QPointF pos; + + while (XMLReadNextStartElement(reader)) { + if (reader->name() == QStringLiteral("x")) { + pos.setX(reader->readElementText().toDouble()); + } else if (reader->name() == QStringLiteral("y")) { + pos.setY(reader->readElementText().toDouble()); + } else { + reader->skipCurrentElement(); + } + } + + SetNodePosition(node, context, pos); + } + + } else { + reader->skipCurrentElement(); + } + } + } + + } else { + + reader->skipCurrentElement(); + + } + + } + } else { // Skip this @@ -177,6 +242,32 @@ void Project::Save(QXmlStreamWriter *writer) const writer->writeEndElement(); // nodes + writer->writeStartElement(QStringLiteral("positions")); + + for (auto it=GetPositionMap().cbegin(); it!=GetPositionMap().cend(); it++) { + writer->writeStartElement(QStringLiteral("context")); + + writer->writeAttribute(QStringLiteral("ptr"), QString::number(reinterpret_cast(it.key()))); + + const PositionMap &map = it.value(); + + for (auto jt=map.cbegin(); jt!=map.cend(); jt++) { + writer->writeStartElement(QStringLiteral("node")); + + writer->writeAttribute(QStringLiteral("ptr"), QString::number(reinterpret_cast(jt.key()))); + + const QPointF &pos = jt.value(); + writer->writeTextElement(QStringLiteral("x"), QString::number(pos.x())); + writer->writeTextElement(QStringLiteral("y"), QString::number(pos.y())); + + writer->writeEndElement(); // node + } + + writer->writeEndElement(); // context + } + + writer->writeEndElement(); // positions + // Save main window project layout MainWindowLayoutInfo main_window_info = Core::instance()->main_window()->SaveLayout(); main_window_info.toXml(writer);