GraphicsPathItem.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "GraphicsPathItem.h"
  2. GraphicsPathItem::GraphicsPathItem(const QPainterPath &path ,GraphicsItem *parent) :
  3. GraphicsItem(parent)
  4. ,_path(path)
  5. ,m_width(_path.boundingRect().width())
  6. ,m_height(_path.boundingRect().height())
  7. {
  8. m_handles.reserve(SizeHandleRect::None);
  9. for (int i = SizeHandleRect::LeftTop; i <= SizeHandleRect::Left; ++i) {
  10. SizeHandleRect *shr = new SizeHandleRect(this, static_cast<SizeHandleRect::Direction>(i), this);
  11. m_handles.push_back(shr);
  12. }
  13. updateGeometry();
  14. _boundingRect =_path.controlPointRect();
  15. setFlag(QGraphicsItem::ItemIsMovable, true);
  16. setFlag(QGraphicsItem::ItemIsSelectable, true);
  17. setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
  18. this->setAcceptHoverEvents(true);
  19. }
  20. QRectF GraphicsPathItem::boundingRect() const
  21. {
  22. if (_boundingRect.isNull()) {
  23. qreal pw = _pen.style() == Qt::NoPen ? qreal(0) : _pen.widthF();
  24. if (pw == 0.0)
  25. _boundingRect = _path.controlPointRect();
  26. else {
  27. _boundingRect = _path.controlPointRect();
  28. }
  29. }
  30. return _boundingRect;
  31. }
  32. QPainterPath GraphicsPathItem::shape() const
  33. {
  34. QPainterPath path;
  35. path.addRect(_boundingRect);
  36. return qt_graphicsItem_shapeFromPath(path,_pen);
  37. }
  38. void GraphicsPathItem::resizeTo(SizeHandleRect::Direction dir, const QPointF &point)
  39. {
  40. QPointF local = mapFromScene(point);
  41. QString dirName;
  42. const QRectF &geom = this->boundingRect();
  43. QRect delta = this->rect().toRect();
  44. switch (dir) {
  45. case SizeHandleRect::Right:
  46. dirName = "Rigth";
  47. delta.setRight(local.x());
  48. break;
  49. case SizeHandleRect::RightTop:
  50. dirName = "RightTop";
  51. delta.setTopRight(local.toPoint());
  52. break;
  53. case SizeHandleRect::RightBottom:
  54. dirName = "RightBottom";
  55. delta.setBottomRight(local.toPoint());
  56. break;
  57. case SizeHandleRect::LeftBottom:
  58. dirName = "LeftBottom";
  59. delta.setBottomLeft(local.toPoint());
  60. break;
  61. case SizeHandleRect::Bottom:
  62. dirName = "Bottom";
  63. delta.setBottom(local.y());
  64. break;
  65. case SizeHandleRect::LeftTop:
  66. dirName = "LeftTop";
  67. delta.setTopLeft(local.toPoint());
  68. break;
  69. case SizeHandleRect::Left:
  70. dirName = "Left";
  71. delta.setLeft(local.x());
  72. break;
  73. case SizeHandleRect::Top:
  74. dirName = "Top";
  75. delta.setTop(local.y());
  76. break;
  77. default:
  78. break;
  79. }
  80. prepareGeometryChange();
  81. m_width = qAbs(delta.width()) ;
  82. m_height = qAbs(delta.height());
  83. updateGeometry();
  84. }
  85. void GraphicsPathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  86. {
  87. QColor c = QColor(Qt::red);
  88. c.setAlpha(160);
  89. QPen _pen =_pen;
  90. _pen.setWidth(4);
  91. _pen.setColor(c);
  92. painter->setPen(_pen);
  93. painter->drawPath(_path);
  94. }