GraphicsItem.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include "preheader.h"
  3. #include "sizehandle.h"
  4. #define M_PI 3.14159265358979323846
  5. static QPainterPath qt_graphicsItem_shapeFromPath(const QPainterPath &path, const QPen &pen)
  6. {
  7. // We unfortunately need this hack as QPainterPathStroker will set a width of 1.0
  8. // if we pass a value of 0.0 to QPainterPathStroker::setWidth()
  9. const qreal penWidthZero = qreal(0.00000001);
  10. if (path == QPainterPath() || pen == Qt::NoPen)
  11. return path;
  12. QPainterPathStroker ps;
  13. ps.setCapStyle(pen.capStyle());
  14. ps.setWidth(pen.widthF());
  15. ps.setJoinStyle(pen.joinStyle());
  16. ps.setMiterLimit(pen.miterLimit());
  17. QPainterPath p = ps.createStroke(path);
  18. p.addPath(path);
  19. return p;
  20. }
  21. class GraphicsItem : public QAbstractGraphicsShapeItem
  22. {
  23. public:
  24. GraphicsItem(QGraphicsItem * parent );
  25. enum {Type = UserType+1};
  26. int type() const { return Type; }
  27. SizeHandleRect::Direction hitTest( const QPointF & point ) const;
  28. virtual void resizeTo(SizeHandleRect::Direction dir, const QPointF & point );
  29. virtual QPointF origin () const { return QPointF(0,0); }
  30. virtual Qt::CursorShape getCursor(SizeHandleRect::Direction dir );
  31. virtual QRectF rect() const { return QRectF(0,0,0,0);}
  32. virtual void resetShape(){}
  33. void resetArrow()
  34. {
  35. if(m_rect.top()>m_rect.bottom())
  36. {
  37. qreal tmp =m_rect.bottom();
  38. m_rect.setBottom(m_rect.top());
  39. m_rect.setTop(tmp);
  40. }
  41. if(m_rect.left()>m_rect.right())
  42. {
  43. qreal tmp =m_rect.right();
  44. m_rect.setRight(m_rect.left());
  45. m_rect.setLeft(tmp);
  46. }
  47. updateGeometry();
  48. }
  49. protected:
  50. virtual void updateGeometry();
  51. void setState(SelectionHandleState st);
  52. virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event){}
  53. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
  54. typedef QVector<SizeHandleRect*> Handles;
  55. Handles m_handles;
  56. QRectF m_rect;
  57. QPen _pen;
  58. QColor _color =Qt::red;
  59. };