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.

77 lines
2.3 KiB

  1. #pragma once
  2. #include "editorwidget.h"
  3. #include <QLabel>
  4. #include <QPushButton>
  5. #include <QListWidgetItem>
  6. #include <QKeyEvent>
  7. #include <QEvent>
  8. #include <QGraphicsScene>
  9. class HmiControlItem;
  10. /**
  11. * @brief HMI (Human Machine Interface) 图形编辑器控件
  12. *
  13. * 继承自 EditorWidget,扩展了多页面管理、点击放置与“旁边粘贴”
  14. */
  15. class HmiEditorWidget : public EditorWidget {
  16. Q_OBJECT
  17. public:
  18. explicit HmiEditorWidget(QWidget* parent = nullptr);
  19. ~HmiEditorWidget();
  20. // 页面管理接口
  21. QGraphicsScene* getCurrentScene() const override;
  22. QList<QGraphicsScene*> getPages() const override { return m_pages; }
  23. int getCurrentPageIndex() const override { return m_currentPageIndex; }
  24. void clearPages() override;
  25. void createNewPage();
  26. void switchToPage(int index);
  27. void refreshView();
  28. protected:
  29. // 覆盖 EditorWidget 接口
  30. void initToolbar() override;
  31. void onToolbarItemClicked(QListWidgetItem* item) override;
  32. void keyPressEvent(QKeyEvent* event) override;
  33. bool eventFilter(QObject* obj, QEvent* event) override;
  34. void resizeEvent(QResizeEvent* event) override;
  35. void showEvent(QShowEvent* event) override;
  36. private slots:
  37. void onNewPageClicked();
  38. void onDeletePageClicked();
  39. void onPreviousPageClicked();
  40. void onNextPageClicked();
  41. private:
  42. // —— 辅助 —— //
  43. void initViewScale();
  44. void updatePageLabel();
  45. // 根据类型名创建控件并放到 scenePos 上;成功返回新控件指针
  46. HmiControlItem* createItemByType(const QString& type, const QPointF& scenePos);
  47. // 设置/清除“放置模式”的鼠标提示
  48. void setPlacingCursor(bool on);
  49. // 计算“在原控件旁边”的粘贴位置(第 n 次粘贴做轻微错行)
  50. QPointF calcPasteBesidePos(HmiControlItem* src, int pasteIndex = 0) const;
  51. private:
  52. // 页面
  53. QList<QGraphicsScene*> m_pages;
  54. int m_currentPageIndex = -1;
  55. // UI
  56. QLabel* m_pageLabel = nullptr;
  57. QPushButton* m_newPageButton = nullptr;
  58. QPushButton* m_deletePageButton = nullptr;
  59. QPushButton* m_prevPageButton = nullptr;
  60. QPushButton* m_nextPageButton = nullptr;
  61. // 交互状态:待放置的控件类型(与工具栏 item 的 UserRole 文本一致:如 “按钮”“指示灯”)
  62. QString m_pendingType;
  63. };