|
- #ifndef HMIWIDGET_H
- #define HMIWIDGET_H
-
- #include <QGraphicsRectItem>
- #include <QGraphicsEllipseItem>
- #include <QGraphicsSceneMouseEvent>
- #include <QCursor>
- #include <QPainter>
-
- // 可调整大小的形状模板基类
- template <typename BaseShape>
- class ResizableShape : public BaseShape {
- protected:
- bool resizing;
-
- public:
- ResizableShape(qreal x, qreal y, qreal w, qreal h);
-
- // 事件处理
- void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
- void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
- void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
- void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
- void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
- protected:
- bool isInResizeArea(const QPointF &pos) const;
- };
-
- // 可调整大小的矩形
- class ResizableRectangle : public ResizableShape<QGraphicsRectItem> {
- public:
- ResizableRectangle(qreal x, qreal y, qreal w, qreal h);
- QColor pressedColor() const { return m_pressedColor; }
- void setPressedColor(const QColor& color) { m_pressedColor = color; }
-
- QColor releasedColor() const { return m_releasedColor; }
- void setReleasedColor(const QColor& color) { m_releasedColor = color; }
-
- QString name() const { return m_name; }
- void setName(const QString& name) { m_name = name; }
-
- private:
- QColor m_pressedColor = Qt::green; // 按钮按下时的颜色
- QColor m_releasedColor = Qt::yellow; // 按钮松开时的颜色
- QString m_name = "按钮"; // 默认名称
- };
- class NamedItem {
- public:
- virtual QString name() const = 0;
- virtual void setName(const QString& name) = 0;
- virtual ~NamedItem() = default;
- };
- // 可调整大小的椭圆
- class ResizableEllipse : public ResizableShape<QGraphicsEllipseItem> {
- public:
- ResizableEllipse(qreal x, qreal y, qreal w, qreal h);
- QColor onColor() const { return m_onColor; }
- void setOnColor(const QColor& color) { m_onColor = color; }
-
- QColor offColor() const { return m_offColor; }
- void setOffColor(const QColor& color) { m_offColor = color; }
-
- QString name() const { return m_name; }
- void setName(const QString& name) { m_name = name; }
-
- private:
- QColor m_onColor = Qt::green; // 默认开状态颜色
- QColor m_offColor = Qt::red; // 默认关状态颜色
- QString m_name = "指示灯"; // 默认名称
- };
-
- #endif // HMIWIDGET_H
|