#include "project.h" #include #include #include #include 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; 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(); 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; }