#include "hmiwidget.h" #include #include #include // ResizableShape 模板类的实现 template ResizableShape::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 void ResizableShape::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { if (isInResizeArea(event->pos())) { this->setCursor(Qt::SizeFDiagCursor); } else { this->setCursor(Qt::SizeAllCursor); } BaseShape::hoverMoveEvent(event); } template bool ResizableShape::isInResizeArea(const QPointF &pos) const { QRectF r = this->rect(); QRectF resizeArea(r.bottomRight() - QPointF(20, 20), r.bottomRight()); return resizeArea.contains(pos); } template void ResizableShape::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (isInResizeArea(event->pos())) { resizing = true; this->setCursor(Qt::SizeFDiagCursor); } else { resizing = false; this->setCursor(Qt::SizeAllCursor); BaseShape::mousePressEvent(event); } } template void ResizableShape::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (resizing) { QRectF newRect = this->rect(); newRect.setBottomRight(event->pos()); this->setRect(newRect); } else { BaseShape::mouseMoveEvent(event); } } template void ResizableShape::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { resizing = false; this->setCursor(Qt::SizeAllCursor); BaseShape::mouseReleaseEvent(event); } template //虚函数 void ResizableShape::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(x, y, w, h) { } // ResizableEllipse 实现 ResizableEllipse::ResizableEllipse(qreal x, qreal y, qreal w, qreal h) : ResizableShape(x, y, w, h) { } // 显式实例化模板类 template class ResizableShape; template class ResizableShape;