You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
- #include "project.h"
- #include <QJsonDocument>
- #include <QJsonObject>
- #include <QJsonArray>
- #include <QFile>
-
- bool Project::saveToFile(const QString& filePath) const {
- QJsonObject root;
- QJsonArray itemsArray;
- for (const ItemData& item : items_) {
- QJsonObject obj;
- obj["type"] = item.type;
- obj["x"] = item.x;
- obj["y"] = item.y;
- obj["registerId"] = item.registerId;
- itemsArray.append(obj);
- }
- root["items"] = itemsArray;
-
- QJsonArray connArray;
- for (const ConnectionData& c : connections_) {
- QJsonObject obj;
- obj["from"] = c.from;
- obj["to"] = c.to;
- obj["fromType"] = c.fromType;
- obj["toType"] = c.toType;
- connArray.append(obj);
- }
- root["connections"] = connArray;
-
- QFile file(filePath);
- if (!file.open(QIODevice::WriteOnly))
- return false;
- file.write(QJsonDocument(root).toJson());
- file.close();
- return true;
- }
-
- bool Project::loadFromFile(const QString& filePath) {
- clear();
- QFile file(filePath);
- if (!file.open(QIODevice::ReadOnly))
- return false;
- QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
- file.close();
- if (doc.isNull()) return false;
-
- QJsonObject root = doc.object();
- QJsonArray itemsArray = root["items"].toArray();
- for (const QJsonValue& v : itemsArray) {
- QJsonObject obj = v.toObject();
- ItemData d;
- d.type = obj["type"].toString();
- d.x = obj["x"].toDouble();
- d.y = obj["y"].toDouble();
- d.registerId = obj.contains("registerId") ? obj["registerId"].toString() : "";
- items_.append(d);
- }
- QJsonArray connArray = root["connections"].toArray();
- for (const QJsonValue& v : connArray) {
- QJsonObject obj = v.toObject();
- ConnectionData d;
- d.from = obj["from"].toInt();
- d.to = obj["to"].toInt();
- d.fromType = obj["fromType"].toInt();
- d.toType = obj["toType"].toInt();
- connections_.append(d);
- }
- return true;
- }
|