综合平台编辑器
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 rivejä
1.8 KiB

  1. #ifndef CUSTOMGRAPHICS_H
  2. #define CUSTOMGRAPHICS_H
  3. #include <QGraphicsObject>
  4. // 只需要前向声明,因为头文件中只用到了指针
  5. class QPainter;
  6. class QGraphicsSceneMouseEvent;
  7. class QGraphicsSceneContextMenuEvent;
  8. enum class ComponentType {
  9. Button,
  10. Indicator
  11. };
  12. // HMI组件基类
  13. class HmiComponent : public QGraphicsObject
  14. {
  15. Q_OBJECT
  16. Q_PROPERTY(QColor color READ color WRITE setColor)
  17. Q_PROPERTY(QString componentName READ componentName WRITE setComponentName)
  18. signals:
  19. void selected(HmiComponent* item);
  20. // 为复制和删除请求添加信号
  21. void copyRequested(HmiComponent* item);
  22. void deleteRequested(HmiComponent* item);
  23. void appearanceChangedRequested(HmiComponent* item);
  24. public:
  25. explicit HmiComponent(QGraphicsItem *parent = nullptr);
  26. void setColor(const QColor& color);
  27. QColor color() const;
  28. void setComponentName(const QString& name);
  29. QString componentName() const;
  30. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
  31. public slots:
  32. void changeAppearance();
  33. protected:
  34. void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
  35. // 重写上下文菜单事件
  36. void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
  37. virtual void paintShape(QPainter* painter) = 0;
  38. protected:
  39. QColor m_color;
  40. QString m_componentName;
  41. };
  42. // 按钮类
  43. class HmiButton : public HmiComponent
  44. {
  45. public:
  46. HmiButton(QGraphicsItem *parent = nullptr);
  47. QRectF boundingRect() const override;
  48. protected:
  49. void paintShape(QPainter *painter) override;
  50. };
  51. // 指示灯类
  52. class HmiIndicator : public HmiComponent
  53. {
  54. public:
  55. HmiIndicator(QGraphicsItem *parent = nullptr);
  56. QRectF boundingRect() const override;
  57. protected:
  58. void paintShape(QPainter *painter) override;
  59. };
  60. #endif // CUSTOMGRAPHICS_H