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.

125 line
3.3 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. protected:
  50. bool eventFilter(QObject *obj, QEvent *event) override;
  51. void resizeEvent(QResizeEvent *event) override;
  52. private slots:
  53. // 处理场景变化(用于更新连接线)
  54. void handleSceneChanged();
  55. private:
  56. // 表格管理
  57. void createRealTable();
  58. void clearTable();
  59. void adjustTableToFitView(); // 调整表格以适应视图大小
  60. // 项目位置改变处理
  61. void handleItemPositionChange(PLCItem *item);
  62. // 仿真与连接管理
  63. void updateConnections();
  64. void createContextMenu(QPoint globalPos);
  65. // 根据类型创建PLC项 - 新增的辅助函数
  66. PLCItem* createItemByType(PLCItem::ItemType type);
  67. QGraphicsScene *m_scene;
  68. QGraphicsView *m_view;
  69. QString m_currentTool; // 当前选择的工具
  70. // 连接管理
  71. PLCItem *m_connectionStart = nullptr;
  72. QGraphicsLineItem *m_tempLine = nullptr;
  73. QList<ConnectionLine*> m_connections;
  74. // 运行状态
  75. bool m_simulationRunning = false;
  76. QSet<PLCItem*> m_activeItems;
  77. // 表格参数 - 使用常量定义默认值
  78. int m_rows = 15; // 表格行数
  79. int m_columns = 20; // 表格列数
  80. int m_cellSize = 50; // 单元格大小(像素)
  81. // 表格图形项
  82. QGraphicsRectItem* m_tableFrame = nullptr;
  83. QVector<QGraphicsLineItem*> m_horizontalLines;
  84. QVector<QGraphicsLineItem*> m_verticalLines;
  85. };
  86. // 连接线类
  87. class ConnectionLine : public QGraphicsLineItem
  88. {
  89. public:
  90. ConnectionLine(PLCItem *start, PLCItem *end, QGraphicsItem *parent = nullptr);
  91. void updatePosition();
  92. PLCItem* startItem() const { return m_startItem; }
  93. PLCItem* endItem() const { return m_endItem; }
  94. private:
  95. PLCItem *m_startItem;
  96. PLCItem *m_endItem;
  97. };
  98. #endif // PLCDOCUMENT_H