|
- #ifndef GRAPHICSITEMS_H
- #define GRAPHICSITEMS_H
-
- #include <QGraphicsRectItem>
- #include <QGraphicsEllipseItem>
- #include <QGraphicsSceneHoverEvent>
- #include <QGraphicsSceneMouseEvent>
- #include <QPainter>
- #include <QString>
- #include <QColor>
- #include <QCursor>
- #include <QGraphicsScene>
-
- // 命名项接口(用于属性设置)
- class NamedItem
- {
- public:
- virtual QString name() const = 0;
- virtual void setName(const QString &name) = 0;
- virtual ~NamedItem() = default;
- };
-
- // 可调整大小的图形基类(模板)
- template <typename BaseShape>
- class ResizableShape : public BaseShape, public NamedItem
- {
- protected:
- bool m_resizing;
- QString m_name;
- Qt::CursorShape m_currentCursor;
-
- public:
- ResizableShape(qreal x, qreal y, qreal w, qreal h)
- : BaseShape(x, y, w, h), m_resizing(false), m_currentCursor(Qt::SizeAllCursor)
- {
- this->setFlag(QGraphicsItem::ItemIsMovable, true);
- this->setFlag(QGraphicsItem::ItemIsSelectable, true);
- this->setAcceptHoverEvents(true);
- }
-
- // 事件处理(调整大小逻辑)
- void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override
- {
- if (isInResizeArea(event->pos()))
- {
- m_currentCursor = Qt::SizeFDiagCursor;
- this->setCursor(Qt::SizeFDiagCursor);
- } else {
- m_currentCursor = Qt::SizeAllCursor;
- }
- BaseShape::hoverMoveEvent(event);
- }
-
- void mousePressEvent(QGraphicsSceneMouseEvent *event) override
- {
- if (isInResizeArea(event->pos())) {
- m_resizing = true;
- m_currentCursor = Qt::SizeFDiagCursor;
- this->setCursor(Qt::SizeFDiagCursor);
- } else {
- m_resizing = false;
- m_currentCursor = Qt::SizeAllCursor;
- BaseShape::mousePressEvent(event);
- }
- }
-
- void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
- {
- if (m_resizing) {
- QRectF newRect = this->rect();
- newRect.setBottomRight(event->pos());
- this->setRect(newRect);
- } else {
- BaseShape::mouseMoveEvent(event);
- }
- }
-
- void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
- {
- m_resizing = false;
- m_currentCursor = Qt::SizeAllCursor;
- this->setCursor(Qt::SizeFDiagCursor);
- BaseShape::mouseReleaseEvent(event);
- }
-
- void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
- {
- BaseShape::paint(painter, option, widget);
- }
-
- // 命名项接口实现
- QString name() const override { return m_name; }
- void setName(const QString &name) override { m_name = name; }
-
- // 光标获取方法
- Qt::CursorShape currentCursor() const { return m_currentCursor; }
-
- protected:
- bool isInResizeArea(const QPointF &pos) const
- {
- QRectF r = this->rect();
- QRectF resizeArea(r.bottomRight() - QPointF(50, 50), r.bottomRight());
- return resizeArea.contains(pos);
- }
- };
-
- // 可调整大小的矩形(按钮)
- 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;
- this->setBrush(m_releasedColor); // 更新显示
- }
-
- private:
- QColor m_pressedColor; // 按下颜色
- QColor m_releasedColor; // 释放颜色
- };
-
- // 可调整大小的椭圆(指示灯)
- 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;
- this->setBrush(m_onColor); // 更新显示
- }
-
- QColor offColor() const { return m_offColor; }
- void setOffColor(const QColor &color) { m_offColor = color; }
-
- private:
- QColor m_onColor; // 开状态颜色
- QColor m_offColor; // 关状态颜色
- };
-
- #endif // GRAPHICSITEMS_H
|