综合平台编辑器
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.

93 lines
2.4 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. enum class ComponentType {
  10. Button,
  11. Indicator
  12. };
  13. // HMI组件基类
  14. class HmiComponent : public QGraphicsObject
  15. {
  16. Q_OBJECT
  17. Q_PROPERTY(QColor color READ color WRITE setColor) // 当前颜色(默认使用 OFF 状态)
  18. Q_PROPERTY(QString componentName READ componentName WRITE setComponentName)
  19. // 新增address属性
  20. Q_PROPERTY(int address READ address WRITE setAddress)
  21. signals:
  22. void selected(HmiComponent* item);
  23. void copyRequested(HmiComponent* item);
  24. void deleteRequested(HmiComponent* item);
  25. // 添加信号用于改变状态颜色
  26. void changeOnColorRequested(HmiComponent* item);
  27. void changeOffColorRequested(HmiComponent* item);
  28. public:
  29. explicit HmiComponent(QGraphicsItem *parent = nullptr);
  30. void setColor(const QColor& color); // 当前显示颜色
  31. QColor color() const;
  32. void setComponentName(const QString& name);
  33. QString componentName() const;
  34. // 设置 ON / OFF 状态颜色
  35. void setOnColor(const QColor& color);
  36. void setOffColor(const QColor& color);
  37. QColor onColor() const;
  38. QColor offColor() const;
  39. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
  40. // 地址相关
  41. void setAddress(int address);
  42. int address() const;
  43. protected:
  44. void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
  45. void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
  46. virtual void paintShape(QPainter* painter) = 0;
  47. void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
  48. protected:
  49. QColor m_color; // 当前显示颜色(编辑器中为 OFF)
  50. QColor m_onColor = Qt::green;
  51. QColor m_offColor;
  52. QString m_componentName;
  53. int m_address = 0;
  54. };
  55. // 按钮类
  56. class HmiButton : public HmiComponent
  57. {
  58. public:
  59. HmiButton(QGraphicsItem *parent = nullptr);
  60. QRectF boundingRect() const override;
  61. protected:
  62. void paintShape(QPainter *painter) override;
  63. };
  64. // 指示灯类
  65. class HmiIndicator : public HmiComponent
  66. {
  67. public:
  68. HmiIndicator(QGraphicsItem *parent = nullptr);
  69. QRectF boundingRect() const override;
  70. protected:
  71. void paintShape(QPainter *painter) override;
  72. };
  73. #endif // CUSTOMGRAPHICS_H