#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; obj["registerId"] = item.registerId; obj["registerId2"] = item.registerId2; obj["compare"] = item.compare; obj["imagePath"] = item.imagePath; obj["onImagePath"] = item.onImagePath; obj["offImagePath"] = item.offImagePath; obj["width"] = item.size.width(); obj["height"] = item.size.height(); 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() : ""; d.registerId2 = obj.contains("registerId2") ? obj["registerId2"].toString() : ""; d.compare = obj.contains("compare") ? obj["compare"].toString() : "CP"; d.imagePath = obj.contains("imagePath") ? obj["imagePath"].toString() : ""; d.onImagePath = obj.contains("onImagePath") ? obj["onImagePath"].toString() : ""; d.offImagePath = obj.contains("offImagePath") ? obj["offImagePath"].toString() : ""; // 加载尺寸信息 double width = obj.contains("width") ? obj["width"].toDouble() : 50.0; double height = obj.contains("height") ? obj["height"].toDouble() : 50.0; d.size = QSizeF(width, height); 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; }