|
- #include "hmiwidget.h"
- #include <QDebug>
- #include<QMenu>
- #include<QGraphicsScene>
- // ResizableShape 模板类的实现
- template <typename BaseShape>
- ResizableShape<BaseShape>::ResizableShape(qreal x, qreal y, qreal w, qreal h)
- : BaseShape(x, y, w, h), resizing(false)
- {
- this->setFlag(QGraphicsItem::ItemIsMovable, true);
- this->setFlag(QGraphicsItem::ItemIsSelectable, true);
- this->setAcceptHoverEvents(true);
- }
- template <typename BaseShape>
- void ResizableShape<BaseShape>::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
- {
- if (isInResizeArea(event->pos()))
- {
- this->setCursor(Qt::SizeFDiagCursor);
- }
- else
- {
- this->setCursor(Qt::SizeAllCursor);
- }
- BaseShape::hoverMoveEvent(event);
- }
-
- template <typename BaseShape>
- bool ResizableShape<BaseShape>::isInResizeArea(const QPointF &pos) const
- {
- QRectF r = this->rect();
- QRectF resizeArea(r.bottomRight() - QPointF(20, 20), r.bottomRight());
- return resizeArea.contains(pos);
- }
-
- template <typename BaseShape>
- void ResizableShape<BaseShape>::mousePressEvent(QGraphicsSceneMouseEvent *event)
- {
- if (isInResizeArea(event->pos()))
- {
- resizing = true;
- this->setCursor(Qt::SizeFDiagCursor);
- }
- else
- {
- resizing = false;
- this->setCursor(Qt::SizeAllCursor);
- BaseShape::mousePressEvent(event);
- }
- }
-
- template <typename BaseShape>
- void ResizableShape<BaseShape>::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
- {
- if (resizing)
- {
- QRectF newRect = this->rect();
- newRect.setBottomRight(event->pos());
- this->setRect(newRect);
- }
- else
- {
- BaseShape::mouseMoveEvent(event);
- }
- }
-
- template <typename BaseShape>
- void ResizableShape<BaseShape>::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
- {
- resizing = false;
- this->setCursor(Qt::SizeAllCursor);
- BaseShape::mouseReleaseEvent(event);
- }
-
- template <typename BaseShape>//虚函数
- void ResizableShape<BaseShape>::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
- {
- BaseShape::paint(painter, option, widget);
- }
- // ResizableRectangle 实现
- ResizableRectangle::ResizableRectangle(qreal x, qreal y, qreal w, qreal h)
- : ResizableShape<QGraphicsRectItem>(x, y, w, h)
- {
- }
-
- // ResizableEllipse 实现
- ResizableEllipse::ResizableEllipse(qreal x, qreal y, qreal w, qreal h)
- : ResizableShape<QGraphicsEllipseItem>(x, y, w, h)
- {
- }
-
- // 显式实例化模板类
- template class ResizableShape<QGraphicsRectItem>;
- template class ResizableShape<QGraphicsEllipseItem>;
|