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.

87 lines
2.2 KiB

  1. #ifndef HMIDOCUMENT_H
  2. #define HMIDOCUMENT_H
  3. #include "basedocument.h"
  4. #include <QGraphicsScene>
  5. #include <QGraphicsView>
  6. #include <QJsonObject>
  7. #include"modbussimulator.h"
  8. // 前向声明
  9. class ResizableRectangle;
  10. class ResizableEllipse;
  11. class NamedItem;
  12. class HMIDocument : public BaseDocument
  13. {
  14. Q_OBJECT
  15. public:
  16. explicit HMIDocument(QWidget *parent = nullptr);
  17. ~HMIDocument() override;
  18. QString title() const override;
  19. void setTitle(const QString &title);
  20. QGraphicsView *view() const;
  21. QGraphicsScene *scene() const;
  22. // 绘图控制
  23. void setDrawEllipse(bool enable);
  24. void setDrawRectangle(bool enable);
  25. // 编辑功能
  26. void copySelectedItems();
  27. void pasteItems();
  28. void deleteSelectedItems();
  29. void showItemProperties();
  30. void startDrawingEllipse();
  31. void startDrawingRectangle();
  32. // 保存/加载
  33. bool saveToFile(const QString &filePath) override;
  34. bool loadFromFile(const QString &filePath) override;
  35. void markModified();
  36. // 仿真控制
  37. void startSimulation();
  38. void stopSimulation();
  39. bool isSimulationRunning() const;
  40. // 写线圈方法
  41. void writeCoil(int address, bool state);
  42. signals:
  43. void titleChanged(const QString &title);
  44. protected:
  45. bool eventFilter(QObject *obj, QEvent *event) override;
  46. private slots:
  47. void onCoilStatusRead(int address, bool state);
  48. void onSimulationError(const QString &message);
  49. private:
  50. ModbusSimulator *m_modbusSimulator;
  51. // 辅助方法
  52. void createShape(const QString& type, const QPointF &pos);
  53. void resetDrawFlags();
  54. void showContextMenu(QPoint globalPos);
  55. // 序列化相关
  56. QByteArray serializeItem(QGraphicsItem *item);
  57. QGraphicsItem *deserializeItem(const QByteArray &data);
  58. QJsonObject itemToJson(QGraphicsItem *item);
  59. QGraphicsItem *jsonToItem(const QJsonObject &json);
  60. // 成员变量
  61. QString m_title;
  62. QGraphicsScene *m_scene;
  63. QGraphicsView *m_view;
  64. QScrollArea *m_scrollArea;
  65. bool m_canDrawEllipse;
  66. bool m_canDrawRectangle;
  67. // 复制粘贴缓冲区
  68. QList<QByteArray> m_copiedItemsData;
  69. QPointF m_lastPastePos;
  70. };
  71. #endif // HMIDOCUMENT_H