综合平台编辑器
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

119 linhas
3.2 KiB

  1. #ifndef CUSTOMGRAPHICS_H
  2. #define CUSTOMGRAPHICS_H
  3. #include <QGraphicsObject>
  4. #include <QColor>
  5. // 只需要前向声明,因为头文件中只用到了指针
  6. class QPainter;
  7. class QGraphicsSceneMouseEvent;
  8. class QGraphicsSceneContextMenuEvent;
  9. class QGraphicsSceneHoverEvent;
  10. enum class ComponentType
  11. {
  12. Button,
  13. Indicator
  14. };
  15. // HMI组件基类
  16. class HmiComponent : public QGraphicsObject
  17. {
  18. Q_OBJECT
  19. Q_PROPERTY(QColor color READ color WRITE setColor) // 当前颜色(默认使用 OFF 状态)
  20. Q_PROPERTY(QString componentName READ componentName WRITE setComponentName)
  21. // 新增address属性
  22. Q_PROPERTY(int address READ address WRITE setAddress)
  23. signals:
  24. void selected(HmiComponent* item);
  25. void copyRequested(HmiComponent* item);
  26. void deleteRequested(HmiComponent* item);
  27. // 添加信号用于改变状态颜色
  28. void changeOnColorRequested(HmiComponent* item);
  29. void changeOffColorRequested(HmiComponent* item);
  30. public:
  31. explicit HmiComponent(QGraphicsItem *parent = nullptr);
  32. void setColor(const QColor& color); // 当前显示颜色
  33. QColor color() const;
  34. void setComponentName(const QString& name);
  35. QString componentName() const;
  36. // 设置 ON / OFF 状态颜色
  37. void setOnColor(const QColor& color);
  38. void setOffColor(const QColor& color);
  39. QColor onColor() const;
  40. QColor offColor() const;
  41. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
  42. // 地址相关
  43. void setAddress(int address);
  44. int address() const;
  45. void setSize(double width, double height);
  46. QSizeF size() const { return QSizeF(m_width, m_height); }
  47. protected:
  48. void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
  49. void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
  50. virtual void paintShape(QPainter* painter) = 0;
  51. void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
  52. void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
  53. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
  54. protected:
  55. QColor m_color; // 当前显示颜色(编辑器中为 OFF)
  56. QColor m_onColor = Qt::green;
  57. QColor m_offColor;
  58. QString m_componentName;
  59. int m_address = 0;
  60. // 新增尺寸成员
  61. double m_width = 65.0;
  62. double m_height = 30.0;
  63. enum ResizeHandle { None, BottomRight }; // 缩放控制点位置
  64. ResizeHandle m_resizeHandle = None;
  65. bool m_resizing = false;
  66. QPointF m_resizeStartPos;
  67. QSizeF m_resizeStartSize;
  68. };
  69. // 按钮类
  70. class HmiButton : public HmiComponent
  71. {
  72. public:
  73. HmiButton(QGraphicsItem *parent = nullptr);
  74. QRectF boundingRect() const override;
  75. void setSize(double width, double height);
  76. // 新增状态标志,true = ON,false = OFF
  77. bool isOn() const;
  78. void setOn(bool on);
  79. protected:
  80. void paintShape(QPainter *painter) override;
  81. private:
  82. bool m_isOn = false; // 默认OFF
  83. };
  84. // 指示灯类
  85. class HmiIndicator : public HmiComponent
  86. {
  87. public:
  88. HmiIndicator(QGraphicsItem *parent = nullptr);
  89. QRectF boundingRect() const override;
  90. void setSize(double width, double height);
  91. protected:
  92. void paintShape(QPainter *painter) override;
  93. };
  94. #endif // CUSTOMGRAPHICS_H