GraphicsPixmapItem.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "GraphicsPixmapItem.h"
  2. GraphicsPixmapItem::GraphicsPixmapItem(const QPixmap &pixmap ,GraphicsItem *parent)
  3. :GraphicsItem(parent)
  4. ,m_pixmap(pixmap)
  5. ,m_width(pixmap.width())
  6. ,m_height(pixmap.height())
  7. {
  8. m_rect =pixmap.rect();
  9. m_handles.reserve(SizeHandleRect::None);
  10. for (int i = SizeHandleRect::LeftTop; i <= SizeHandleRect::Left; ++i) {
  11. SizeHandleRect *shr = new SizeHandleRect(this, static_cast<SizeHandleRect::Direction>(i), this);
  12. m_handles.push_back(shr);
  13. }
  14. updateGeometry();
  15. setFlag(QGraphicsItem::ItemIsMovable, true);
  16. setFlag(QGraphicsItem::ItemIsSelectable, true);
  17. setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
  18. this->setAcceptHoverEvents(true);
  19. }
  20. QRectF GraphicsPixmapItem::boundingRect() const
  21. {
  22. return rect();
  23. }
  24. QPainterPath GraphicsPixmapItem::shape() const
  25. {
  26. QPainterPath path;
  27. path.addRect(boundingRect());
  28. return qt_graphicsItem_shapeFromPath(path,_pen);
  29. }
  30. void GraphicsPixmapItem::resizeTo(SizeHandleRect::Direction dir, const QPointF &point)
  31. {
  32. QPointF local = mapFromScene(point);
  33. QString dirName;
  34. QRect delta =m_rect.toRect();
  35. switch (dir) {
  36. case SizeHandleRect::LeftTop:
  37. delta.setTopLeft(local.toPoint());
  38. break;
  39. case SizeHandleRect::Top:
  40. dirName = "Top";
  41. delta.setTop(local.y());
  42. break;
  43. case SizeHandleRect::RightTop:
  44. dirName = "RightTop";
  45. delta.setTopRight(local.toPoint());
  46. break;
  47. case SizeHandleRect::Left:
  48. dirName = "Left";
  49. delta.setLeft(local.x());
  50. break;
  51. case SizeHandleRect::Right:
  52. dirName = "Rigth";
  53. delta.setRight(local.x());
  54. break;
  55. case SizeHandleRect::LeftBottom:
  56. dirName = "LeftBottom";
  57. delta.setBottomLeft(local.toPoint());
  58. break;
  59. case SizeHandleRect::Bottom:
  60. dirName = "Bottom";
  61. delta.setBottom(local.y());
  62. break;
  63. case SizeHandleRect::RightBottom:
  64. dirName = "RightBottom";
  65. delta.setBottomRight(local.toPoint());
  66. break;
  67. default:
  68. break;
  69. }
  70. prepareGeometryChange();
  71. m_width = delta.width();
  72. m_height = delta.height();
  73. m_rect =delta;
  74. updateGeometry();
  75. }
  76. void GraphicsPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  77. {
  78. qreal x =rect().left();
  79. if(rect().left() >rect().right())
  80. x =rect().right();
  81. qreal y =rect().top();
  82. if(rect().top() >rect().bottom())
  83. y =rect().bottom();
  84. painter->drawPixmap(x,y,m_pixmap.scaled(abs(rect().width()),abs(rect().height()),
  85. Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
  86. }