综合平台编程器项目的远程存储
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

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