Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

95 righe
2.5 KiB

  1. #include "hmiwidget.h"
  2. #include <QDebug>
  3. #include<QMenu>
  4. #include<QGraphicsScene>
  5. // ResizableShape 模板类的实现
  6. template <typename BaseShape>
  7. ResizableShape<BaseShape>::ResizableShape(qreal x, qreal y, qreal w, qreal h)
  8. : BaseShape(x, y, w, h), resizing(false)
  9. {
  10. this->setFlag(QGraphicsItem::ItemIsMovable, true);
  11. this->setFlag(QGraphicsItem::ItemIsSelectable, true);
  12. this->setAcceptHoverEvents(true);
  13. }
  14. template <typename BaseShape>
  15. void ResizableShape<BaseShape>::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
  16. {
  17. if (isInResizeArea(event->pos()))
  18. {
  19. this->setCursor(Qt::SizeFDiagCursor);
  20. }
  21. else
  22. {
  23. this->setCursor(Qt::SizeAllCursor);
  24. }
  25. BaseShape::hoverMoveEvent(event);
  26. }
  27. template <typename BaseShape>
  28. bool ResizableShape<BaseShape>::isInResizeArea(const QPointF &pos) const
  29. {
  30. QRectF r = this->rect();
  31. QRectF resizeArea(r.bottomRight() - QPointF(20, 20), r.bottomRight());
  32. return resizeArea.contains(pos);
  33. }
  34. template <typename BaseShape>
  35. void ResizableShape<BaseShape>::mousePressEvent(QGraphicsSceneMouseEvent *event)
  36. {
  37. if (isInResizeArea(event->pos()))
  38. {
  39. resizing = true;
  40. this->setCursor(Qt::SizeFDiagCursor);
  41. }
  42. else
  43. {
  44. resizing = false;
  45. this->setCursor(Qt::SizeAllCursor);
  46. BaseShape::mousePressEvent(event);
  47. }
  48. }
  49. template <typename BaseShape>
  50. void ResizableShape<BaseShape>::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  51. {
  52. if (resizing)
  53. {
  54. QRectF newRect = this->rect();
  55. newRect.setBottomRight(event->pos());
  56. this->setRect(newRect);
  57. }
  58. else
  59. {
  60. BaseShape::mouseMoveEvent(event);
  61. }
  62. }
  63. template <typename BaseShape>
  64. void ResizableShape<BaseShape>::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  65. {
  66. resizing = false;
  67. this->setCursor(Qt::SizeAllCursor);
  68. BaseShape::mouseReleaseEvent(event);
  69. }
  70. template <typename BaseShape>//虚函数
  71. void ResizableShape<BaseShape>::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  72. {
  73. BaseShape::paint(painter, option, widget);
  74. }
  75. // ResizableRectangle 实现
  76. ResizableRectangle::ResizableRectangle(qreal x, qreal y, qreal w, qreal h)
  77. : ResizableShape<QGraphicsRectItem>(x, y, w, h)
  78. {
  79. }
  80. // ResizableEllipse 实现
  81. ResizableEllipse::ResizableEllipse(qreal x, qreal y, qreal w, qreal h)
  82. : ResizableShape<QGraphicsEllipseItem>(x, y, w, h)
  83. {
  84. }
  85. // 显式实例化模板类
  86. template class ResizableShape<QGraphicsRectItem>;
  87. template class ResizableShape<QGraphicsEllipseItem>;