|
- // plcdocument.h
- #ifndef PLCDOCUMENT_H
- #define PLCDOCUMENT_H
-
- #include "basedocument.h"
- #include "plcitems.h" // 确保包含PLCItem的定义
- #include <QGraphicsScene>
- #include <QGraphicsView>
- #include <QGraphicsLineItem>
- #include <QGraphicsRectItem>
- #include <QMap>
- #include <QSet>
- #include <QVector>
- #include <QDragEnterEvent>
- #include <QDragMoveEvent>
- #include <QDropEvent>
- #include <QMimeData>
- #include <QPair>
-
- class PLCItem;
- class ConnectionLine;
-
- class PLCDocument : public BaseDocument
- {
- Q_OBJECT
- public:
- explicit PLCDocument(QWidget *parent = nullptr);
- ~PLCDocument() override;
-
- QString title() const override;
- QGraphicsView *view() const { return m_view; }
- QGraphicsScene *scene() const { return m_scene; }
-
- bool saveToFile(const QString &filePath) override;
- bool loadFromFile(const QString &filePath) override;
-
- // 创建PLC元件
- void createPLCItem(const QString &type, const QPointF &pos);
-
- // 连接管理
- void startConnection(PLCItem *startItem);
- void endConnection(PLCItem *endItem);
- void clearCurrentConnection();
-
- // 运行状态控制
- void setSimulationRunning(bool running);
- void resetSimulation();
- void setCurrentTool(const QString &tool) { m_currentTool = tool; }
-
- // 获取表格参数
- int rowCount() const { return m_rows; }
- int columnCount() const { return m_columns; }
- int cellSize() const { return m_cellSize; }
-
- // 计算单元格中心位置
- QPointF snapToCellCenter(const QPointF &pos) const;
-
- // 检查并修正位置,确保在表格内
- QPointF constrainToTable(const QPointF &pos) const;
-
- void setTitle(const QString &title) { m_title = title; }
-
- protected:
- bool eventFilter(QObject *obj, QEvent *event) override;
- void resizeEvent(QResizeEvent *event) override;
-
- private slots:
- // 处理场景变化(用于更新连接线)
- void handleSceneChanged();
-
- private:
- // 表格管理
- void createRealTable();
- void clearTable();
- void adjustTableToFitView(); // 调整表格以适应视图大小
-
- // 项目位置改变处理
- void handleItemPositionChange(PLCItem *item);
-
- // 仿真与连接管理
- void updateConnections();
- void createContextMenu(QPoint globalPos);
-
- // 根据类型创建PLC项 - 新增的辅助函数
- PLCItem* createItemByType(PLCItem::ItemType type);
-
- QGraphicsScene *m_scene;
- QGraphicsView *m_view;
- QString m_currentTool; // 当前选择的工具
-
- // 连接管理
- PLCItem *m_connectionStart = nullptr;
- QGraphicsLineItem *m_tempLine = nullptr;
- QList<ConnectionLine*> m_connections;
-
- // 运行状态
- bool m_simulationRunning = false;
- QSet<PLCItem*> m_activeItems;
-
- // 表格参数 - 使用常量定义默认值
- int m_rows = 15; // 表格行数
- int m_columns = 20; // 表格列数
- int m_cellSize = 50; // 单元格大小(像素)
-
- // 表格图形项
- QGraphicsRectItem* m_tableFrame = nullptr;
- QVector<QGraphicsLineItem*> m_horizontalLines;
- QVector<QGraphicsLineItem*> m_verticalLines;
- QString m_title; // 添加标题成员变量
- };
-
- // 连接线类
- class ConnectionLine : public QGraphicsLineItem
- {
- public:
- ConnectionLine(PLCItem *start, PLCItem *end, QGraphicsItem *parent = nullptr);
-
- void updatePosition();
- PLCItem* startItem() const { return m_startItem; }
- PLCItem* endItem() const { return m_endItem; }
-
- private:
- PLCItem *m_startItem;
- PLCItem *m_endItem;
- };
-
- #endif // PLCDOCUMENT_H
|