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.

71 lines
2.0 KiB

  1. #include "project.h"
  2. #include <QJsonDocument>
  3. #include <QJsonObject>
  4. #include <QJsonArray>
  5. #include <QFile>
  6. bool Project::saveToFile(const QString& filePath) const {
  7. QJsonObject root;
  8. QJsonArray itemsArray;
  9. for (const ItemData& item : items_) {
  10. QJsonObject obj;
  11. obj["type"] = item.type;
  12. obj["x"] = item.x;
  13. obj["y"] = item.y;
  14. obj["registerId"] = item.registerId;
  15. itemsArray.append(obj);
  16. }
  17. root["items"] = itemsArray;
  18. QJsonArray connArray;
  19. for (const ConnectionData& c : connections_) {
  20. QJsonObject obj;
  21. obj["from"] = c.from;
  22. obj["to"] = c.to;
  23. obj["fromType"] = c.fromType;
  24. obj["toType"] = c.toType;
  25. connArray.append(obj);
  26. }
  27. root["connections"] = connArray;
  28. QFile file(filePath);
  29. if (!file.open(QIODevice::WriteOnly))
  30. return false;
  31. file.write(QJsonDocument(root).toJson());
  32. file.close();
  33. return true;
  34. }
  35. bool Project::loadFromFile(const QString& filePath) {
  36. clear();
  37. QFile file(filePath);
  38. if (!file.open(QIODevice::ReadOnly))
  39. return false;
  40. QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
  41. file.close();
  42. if (doc.isNull()) return false;
  43. QJsonObject root = doc.object();
  44. QJsonArray itemsArray = root["items"].toArray();
  45. for (const QJsonValue& v : itemsArray) {
  46. QJsonObject obj = v.toObject();
  47. ItemData d;
  48. d.type = obj["type"].toString();
  49. d.x = obj["x"].toDouble();
  50. d.y = obj["y"].toDouble();
  51. d.registerId = obj.contains("registerId") ? obj["registerId"].toString() : "";
  52. items_.append(d);
  53. }
  54. QJsonArray connArray = root["connections"].toArray();
  55. for (const QJsonValue& v : connArray) {
  56. QJsonObject obj = v.toObject();
  57. ConnectionData d;
  58. d.from = obj["from"].toInt();
  59. d.to = obj["to"].toInt();
  60. d.fromType = obj["fromType"].toInt();
  61. d.toType = obj["toType"].toInt();
  62. connections_.append(d);
  63. }
  64. return true;
  65. }