综合平台编程器项目的远程存储
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.
 
 
 
 

165 lines
5.3 KiB

  1. /**
  2. * @file hmi_editor_service.h
  3. * @brief 定义 HMI 页面和控件的编辑用例服务
  4. * @author suyu
  5. * @date 2026-08-10
  6. */
  7. #pragma once
  8. #include "domain/hmi_model.h"
  9. #include <string>
  10. class ProjectService;
  11. /**
  12. * @brief HMI 编辑操作的失败分类
  13. */
  14. enum class HmiEditorError
  15. {
  16. None,
  17. PageNotFound,
  18. ControlNotFound,
  19. DuplicateId,
  20. InvalidPage,
  21. InvalidControl
  22. };
  23. /**
  24. * @brief HMI 编辑操作的统一结果
  25. *
  26. * 成功时 `id` 保存新建或更新后的控件或页面标识
  27. */
  28. struct HmiEditorResult
  29. {
  30. bool succeeded = false;
  31. HmiEditorError error = HmiEditorError::None;
  32. std::string message;
  33. std::string id;
  34. };
  35. /**
  36. * @brief 编排 HMI 页面和控件的编辑操作,不依赖 Qt 视图
  37. *
  38. * 服务通过 ProjectService 修改工程并负责标记工程为已修改
  39. */
  40. class HmiEditorService
  41. {
  42. public:
  43. explicit HmiEditorService(ProjectService &project_service);
  44. /**
  45. * @brief 按页面标识查找只读页面
  46. * @param page_id 页面唯一标识
  47. * @return 找到时返回页面指针,未找到时返回空指针
  48. */
  49. const HmiPage *findPage(const std::string &page_id) const;
  50. /**
  51. * @brief 按页面和控件标识查找只读控件
  52. * @param page_id 所属页面唯一标识
  53. * @param control_id 控件唯一标识
  54. * @return 找到时返回控件指针,任一标识不存在时返回空指针
  55. */
  56. const HmiControl *findControl(
  57. const std::string &page_id, const std::string &control_id) const;
  58. /**
  59. * @brief 返回工程中第一个 HMI 页面标识
  60. * @return 工程没有 HMI 页面时返回空字符串
  61. */
  62. std::string firstPageId() const;
  63. /**
  64. * @brief 在工程没有 HMI 页面时创建默认操作页
  65. * @return 已有或新建页面的成功结果及其页面标识
  66. */
  67. HmiEditorResult ensureDefaultPage();
  68. /**
  69. * @brief 向指定页面添加待配置的基础控件
  70. * @param page_id 目标页面唯一标识
  71. * @param type 新控件类型
  72. * @return 成功时返回新控件标识,页面不存在时返回 PageNotFound
  73. *
  74. * 新控件可暂时没有寄存器绑定,完整绑定校验在工程保存前执行
  75. */
  76. HmiEditorResult addControl(
  77. const std::string &page_id, HmiControlType type);
  78. /**
  79. * @brief 删除指定页面中的控件
  80. * @param page_id 所属页面唯一标识
  81. * @param control_id 待删除控件唯一标识
  82. * @return 页面或控件不存在时返回对应错误
  83. */
  84. HmiEditorResult removeControl(
  85. const std::string &page_id, const std::string &control_id);
  86. /**
  87. * @brief 更新控件位置和尺寸
  88. * @param page_id 所属页面唯一标识
  89. * @param control_id 待移动控件唯一标识
  90. * @param bounds 新矩形,必须完整位于页面范围内
  91. * @return 页面、控件或矩形无效时返回失败结果
  92. */
  93. HmiEditorResult moveControl(
  94. const std::string &page_id,
  95. const std::string &control_id,
  96. const HmiRect &bounds);
  97. /**
  98. * @brief 更新控件可编辑属性
  99. * @param page_id 所属页面唯一标识
  100. * @param control_id 原控件唯一标识
  101. * @param control 更新后的控件配置,不允许改变控件类型
  102. * @return 控件标识重复、绑定区域错误或矩形越界时返回失败结果
  103. *
  104. * 允许控件暂时没有必需绑定,完整工程校验仍在保存前执行
  105. */
  106. HmiEditorResult updateControl(
  107. const std::string &page_id,
  108. const std::string &control_id,
  109. const HmiControl &control);
  110. private:
  111. /**
  112. * @brief 校验控件编辑后仍满足页面内的基础约束
  113. * @param page 控件所属页面
  114. * @param control 待校验控件
  115. * @param error 校验失败时写入原因,可为 nullptr
  116. * @return 控件标识、尺寸、位置和寄存器绑定区域均有效时返回 true
  117. */
  118. static bool validateEditableControl(
  119. const HmiPage &page,
  120. const HmiControl &control,
  121. std::string *error);
  122. /**
  123. * @brief 检查候选控件标识是否与页面内其他控件重复
  124. * @param page 待检查页面
  125. * @param excluded_id 更新场景中需要排除的原控件标识
  126. * @param candidate_id 待使用的控件标识
  127. * @return 存在同标识的其他控件时返回 true
  128. */
  129. static bool hasDuplicateControlId(
  130. const HmiPage &page,
  131. const std::string &excluded_id,
  132. const std::string &candidate_id);
  133. /**
  134. * @brief 按控件类型创建带默认属性的新控件
  135. * @param page 新控件所属页面,用于确定初始位置和唯一标识
  136. * @param type 新控件类型
  137. * @return 未绑定寄存器的默认控件配置
  138. */
  139. static HmiControl makeControl(
  140. const HmiPage &page, HmiControlType type);
  141. /**
  142. * @brief 在页面内生成带指定前缀的唯一控件标识
  143. * @param page 待检查页面
  144. * @param prefix 控件类型对应的标识前缀
  145. * @return 从 1 开始递增且不与现有控件重复的标识
  146. */
  147. static std::string makeUniqueId(
  148. const HmiPage &page, const std::string &prefix);
  149. /**
  150. * @brief 非拥有的工程服务依赖,由应用入口保证生命周期
  151. */
  152. ProjectService &project_service_;
  153. };