综合平台编程器项目的远程存储
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

139 rindas
5.1 KiB

  1. /**
  2. * @file logic_editor_widget.h
  3. * @brief 定义结构化梯形图编辑和运行轨迹画布
  4. * @version 1.0.0
  5. * @author suyu
  6. * @date 2026-08-22
  7. */
  8. #pragma once
  9. #include "domain/control_logic_model.h"
  10. #include "services/logic_editor_service.h"
  11. #include "services/software_logic_executor.h"
  12. #include <QGraphicsView>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. class QGraphicsScene;
  17. class QResizeEvent;
  18. /** 将结构化梯形图表达式投影成网格图元;不保存自由线段,编辑通过服务提交 */
  19. class LogicEditorWidget final : public QGraphicsView
  20. {
  21. Q_OBJECT
  22. public:
  23. /** 梯形图中的条件、输出、横线和辅助选择图元类型 */
  24. class NodeItem;
  25. class WireItem;
  26. class WireCellItem;
  27. class VerticalConnectorItem;
  28. class RungItem;
  29. class EmptySlotItem;
  30. /** 创建梯形图画布并绑定编辑服务 */
  31. explicit LogicEditorWidget(
  32. LogicEditorService &editor_service,
  33. QWidget *parent = nullptr);
  34. /** 切换当前显示的控制逻辑;不存在时显示空场景 */
  35. void setLogicId(const std::string &logic_id);
  36. /** 设置是否允许编辑梯形图 */
  37. void setEditingEnabled(bool enabled);
  38. /** 设置离线运行轨迹;真机模式应传入空轨迹,避免显示本地伪轨迹 */
  39. void setRuntimeTrace(
  40. const LogicTraceSnapshot &trace,
  41. const std::string &fault_node_id = {});
  42. /** 清除当前运行轨迹和故障节点标记 */
  43. void clearRuntimeTrace();
  44. /** 根据当前逻辑模型重建梯形图场景 */
  45. void reloadLogic();
  46. /** 选中指定节点并滚动到可见区域 */
  47. void selectNode(const std::string &node_id);
  48. /** 返回当前选中的单个节点标识 */
  49. std::string selectedNodeId() const;
  50. /** 返回当前选中的多个节点标识 */
  51. std::vector<std::string> selectedNodeIds() const;
  52. /** 返回当前选中的网络标识 */
  53. std::string selectedRungId() const;
  54. /** 新增一个空的梯形图网络 */
  55. LogicEditorResult addRung();
  56. /** 将当前选择转换为服务调用并新增串联条件 */
  57. LogicEditorResult addCondition(const LogicNodeConfig &config);
  58. /** 将当前选择转换为服务调用并新增并联支路 */
  59. LogicEditorResult addParallelBranch(const LogicNodeConfig &config);
  60. /** 在当前选择位置新增横线 */
  61. LogicEditorResult addHorizontalWire();
  62. /** 在当前选择位置新增竖线 */
  63. LogicEditorResult addVerticalWire();
  64. /** 删除当前选择位置的横线 */
  65. LogicEditorResult deleteHorizontalWire();
  66. /** 删除当前选择位置的竖线 */
  67. LogicEditorResult deleteVerticalWire();
  68. /** 设置当前网络的输出线圈 */
  69. LogicEditorResult setOutput(
  70. const LogicNodeConfig &config, bool configured = false);
  71. /** 按当前画布选择位置粘贴一组连续条件 */
  72. LogicEditorResult pasteConditionNodes(const std::vector<LogicNode> &nodes);
  73. /** 删除当前选中的节点、线段或网络 */
  74. LogicEditorResult deleteSelected();
  75. signals:
  76. /** 当前选中的节点发生变化时发出 */
  77. void nodeSelected(const QString &node_id);
  78. /** 梯形图模型成功发生变化时发出 */
  79. void graphChanged();
  80. /** 编辑服务操作失败时发出可显示的错误信息 */
  81. void editorError(const QString &message);
  82. protected:
  83. /** 在窗口大小变化后重新布局梯形图场景 */
  84. void resizeEvent(QResizeEvent *event) override;
  85. private:
  86. /** 处理图形场景选择变化 */
  87. void handleSelectionChanged();
  88. /** 把编辑服务结果转换为错误信号 */
  89. void reportFailure(const LogicEditorResult &result);
  90. /** 返回当前选中的网络标识 */
  91. std::string currentRungId() const;
  92. /** 选中指定表达式图元 */
  93. void selectExpression(const std::string &expression_id);
  94. /** 选中指定横线图元 */
  95. void selectWire(const std::string &wire_id);
  96. /** 收集当前选中的表达式标识 */
  97. std::vector<std::string> selectedExpressionIds() const;
  98. /** 收集当前选中的横线标识 */
  99. std::vector<std::string> selectedWireIds() const;
  100. /** 收集当前选中的并联支路标识 */
  101. std::vector<std::string> selectedBranchIds() const;
  102. /** 收集当前选中的空网格位置 */
  103. std::vector<std::pair<std::string, int>> selectedEmptySlots() const;
  104. /** 收集当前选中的横线网格位置 */
  105. std::vector<std::pair<std::string, int>> selectedWireCells() const;
  106. /** 判断当前是否选中了网络内的图元 */
  107. bool hasSelectedRungItem() const;
  108. /** 梯形图编辑服务,不由控件拥有 */
  109. LogicEditorService &editor_service_;
  110. /** 承载梯形图图元的场景 */
  111. QGraphicsScene *scene_ = nullptr;
  112. /** 当前显示的控制逻辑标识 */
  113. std::string logic_id_;
  114. /** 当前选中的网络标识 */
  115. std::string current_rung_id_;
  116. /** 最近一次收到的运行轨迹 */
  117. LogicTraceSnapshot trace_;
  118. /** 最近一次运行故障节点标识 */
  119. std::string fault_node_id_;
  120. /** 是否显示运行轨迹 */
  121. bool runtime_trace_enabled_ = false;
  122. /** 是否允许编辑梯形图 */
  123. bool editing_enabled_ = true;
  124. };