Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

128 Zeilen
3.4 KiB

  1. // plcdocument.h
  2. #ifndef PLCDOCUMENT_H
  3. #define PLCDOCUMENT_H
  4. #include "basedocument.h"
  5. #include "plcitems.h" // 确保包含PLCItem的定义
  6. #include <QGraphicsScene>
  7. #include <QGraphicsView>
  8. #include <QGraphicsLineItem>
  9. #include <QGraphicsRectItem>
  10. #include <QMap>
  11. #include <QSet>
  12. #include <QVector>
  13. #include <QDragEnterEvent>
  14. #include <QDragMoveEvent>
  15. #include <QDropEvent>
  16. #include <QMimeData>
  17. #include <QPair>
  18. class PLCItem;
  19. class ConnectionLine;
  20. class PLCDocument : public BaseDocument
  21. {
  22. Q_OBJECT
  23. public:
  24. explicit PLCDocument(QWidget *parent = nullptr);
  25. ~PLCDocument() override;
  26. QString title() const override;
  27. QGraphicsView *view() const { return m_view; }
  28. QGraphicsScene *scene() const { return m_scene; }
  29. bool saveToFile(const QString &filePath) override;
  30. bool loadFromFile(const QString &filePath) override;
  31. // 创建PLC元件
  32. void createPLCItem(const QString &type, const QPointF &pos);
  33. // 连接管理
  34. void startConnection(PLCItem *startItem);
  35. void endConnection(PLCItem *endItem);
  36. void clearCurrentConnection();
  37. // 运行状态控制
  38. void setSimulationRunning(bool running);
  39. void resetSimulation();
  40. void setCurrentTool(const QString &tool) { m_currentTool = tool; }
  41. // 获取表格参数
  42. int rowCount() const { return m_rows; }
  43. int columnCount() const { return m_columns; }
  44. int cellSize() const { return m_cellSize; }
  45. // 计算单元格中心位置
  46. QPointF snapToCellCenter(const QPointF &pos) const;
  47. // 检查并修正位置,确保在表格内
  48. QPointF constrainToTable(const QPointF &pos) const;
  49. void setTitle(const QString &title) { m_title = title; }
  50. protected:
  51. bool eventFilter(QObject *obj, QEvent *event) override;
  52. void resizeEvent(QResizeEvent *event) override;
  53. private slots:
  54. // 处理场景变化(用于更新连接线)
  55. void handleSceneChanged();
  56. private:
  57. // 表格管理
  58. void createRealTable();
  59. void clearTable();
  60. void adjustTableToFitView(); // 调整表格以适应视图大小
  61. // 项目位置改变处理
  62. void handleItemPositionChange(PLCItem *item);
  63. // 仿真与连接管理
  64. void updateConnections();
  65. void createContextMenu(QPoint globalPos);
  66. // 根据类型创建PLC项 - 新增的辅助函数
  67. PLCItem* createItemByType(PLCItem::ItemType type);
  68. QGraphicsScene *m_scene;
  69. QGraphicsView *m_view;
  70. QString m_currentTool; // 当前选择的工具
  71. // 连接管理
  72. PLCItem *m_connectionStart = nullptr;
  73. QGraphicsLineItem *m_tempLine = nullptr;
  74. QList<ConnectionLine*> m_connections;
  75. // 运行状态
  76. bool m_simulationRunning = false;
  77. QSet<PLCItem*> m_activeItems;
  78. // 表格参数 - 使用常量定义默认值
  79. int m_rows = 15; // 表格行数
  80. int m_columns = 20; // 表格列数
  81. int m_cellSize = 50; // 单元格大小(像素)
  82. // 表格图形项
  83. QGraphicsRectItem* m_tableFrame = nullptr;
  84. QVector<QGraphicsLineItem*> m_horizontalLines;
  85. QVector<QGraphicsLineItem*> m_verticalLines;
  86. QString m_title; // 添加标题成员变量
  87. };
  88. // 连接线类
  89. class ConnectionLine : public QGraphicsLineItem
  90. {
  91. public:
  92. ConnectionLine(PLCItem *start, PLCItem *end, QGraphicsItem *parent = nullptr);
  93. void updatePosition();
  94. PLCItem* startItem() const { return m_startItem; }
  95. PLCItem* endItem() const { return m_endItem; }
  96. private:
  97. PLCItem *m_startItem;
  98. PLCItem *m_endItem;
  99. };
  100. #endif // PLCDOCUMENT_H