Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

92 строки
3.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. obj["registerId2"] = item.registerId2;
  16. obj["compare"] = item.compare;
  17. obj["imagePath"] = item.imagePath;
  18. obj["onImagePath"] = item.onImagePath;
  19. obj["offImagePath"] = item.offImagePath;
  20. obj["width"] = item.size.width();
  21. obj["height"] = item.size.height();
  22. itemsArray.append(obj);
  23. }
  24. root["items"] = itemsArray;
  25. QJsonArray connArray;
  26. for (const ConnectionData& c : connections_) {
  27. QJsonObject obj;
  28. obj["from"] = c.from;
  29. obj["to"] = c.to;
  30. obj["fromType"] = c.fromType;
  31. obj["toType"] = c.toType;
  32. connArray.append(obj);
  33. }
  34. root["connections"] = connArray;
  35. QFile file(filePath);
  36. if (!file.open(QIODevice::WriteOnly))
  37. return false;
  38. file.write(QJsonDocument(root).toJson());
  39. file.close();
  40. return true;
  41. }
  42. bool Project::loadFromFile(const QString& filePath) {
  43. clear();
  44. QFile file(filePath);
  45. if (!file.open(QIODevice::ReadOnly))
  46. return false;
  47. QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
  48. file.close();
  49. if (doc.isNull()) return false;
  50. QJsonObject root = doc.object();
  51. QJsonArray itemsArray = root["items"].toArray();
  52. for (const QJsonValue& v : itemsArray) {
  53. QJsonObject obj = v.toObject();
  54. ItemData d;
  55. d.type = obj["type"].toString();
  56. d.x = obj["x"].toDouble();
  57. d.y = obj["y"].toDouble();
  58. d.registerId = obj.contains("registerId") ? obj["registerId"].toString() : "";
  59. d.registerId2 = obj.contains("registerId2") ? obj["registerId2"].toString() : "";
  60. d.compare = obj.contains("compare") ? obj["compare"].toString() : "CP";
  61. d.imagePath = obj.contains("imagePath") ? obj["imagePath"].toString() : "";
  62. d.onImagePath = obj.contains("onImagePath") ? obj["onImagePath"].toString() : "";
  63. d.offImagePath = obj.contains("offImagePath") ? obj["offImagePath"].toString() : "";
  64. // 加载尺寸信息
  65. double width = obj.contains("width") ? obj["width"].toDouble() : 50.0;
  66. double height = obj.contains("height") ? obj["height"].toDouble() : 50.0;
  67. d.size = QSizeF(width, height);
  68. items_.append(d);
  69. }
  70. QJsonArray connArray = root["connections"].toArray();
  71. for (const QJsonValue& v : connArray) {
  72. QJsonObject obj = v.toObject();
  73. ConnectionData d;
  74. d.from = obj["from"].toInt();
  75. d.to = obj["to"].toInt();
  76. d.fromType = obj["fromType"].toInt();
  77. d.toType = obj["toType"].toInt();
  78. connections_.append(d);
  79. }
  80. return true;
  81. }