GraphicsItem.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "GraphicsItem.h"
  2. GraphicsItem::GraphicsItem(QGraphicsItem *parent)
  3. :QAbstractGraphicsShapeItem(parent)
  4. {
  5. // QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
  6. // effect->setBlurRadius(8);
  7. // setGraphicsEffect(effect);
  8. }
  9. void GraphicsItem::updateGeometry()
  10. {
  11. const QRectF &geom = this->boundingRect();
  12. const int w = SELECTION_HANDLE_SIZE;
  13. const int h = SELECTION_HANDLE_SIZE;
  14. const Handles::iterator hend = m_handles.end();
  15. for (Handles::iterator it = m_handles.begin(); it != hend; ++it) {
  16. SizeHandleRect *hndl = *it;;
  17. switch (hndl->dir()) {
  18. case SizeHandleRect::LeftTop:
  19. hndl->move(geom.x() - w / 2, geom.y() - h / 2);
  20. break;
  21. case SizeHandleRect::Top:
  22. hndl->move(geom.x() + geom.width() / 2 - w / 2, geom.y() - h / 2);
  23. break;
  24. case SizeHandleRect::RightTop:
  25. hndl->move(geom.x() + geom.width() - w / 2, geom.y() - h / 2);
  26. break;
  27. case SizeHandleRect::Right:
  28. hndl->move(geom.x() + geom.width() - w / 2, geom.y() + geom.height() / 2 - h / 2);
  29. break;
  30. case SizeHandleRect::RightBottom:
  31. hndl->move(geom.x() + geom.width() - w / 2, geom.y() + geom.height() - h / 2);
  32. break;
  33. case SizeHandleRect::Bottom:
  34. hndl->move(geom.x() + geom.width() / 2 - w / 2, geom.y() + geom.height() - h / 2);
  35. break;
  36. case SizeHandleRect::LeftBottom:
  37. hndl->move(geom.x() - w / 2, geom.y() + geom.height() - h / 2);
  38. break;
  39. case SizeHandleRect::Left:
  40. hndl->move(geom.x() - w / 2, geom.y() + geom.height() / 2 - h / 2);
  41. break;
  42. case SizeHandleRect::Center:
  43. hndl->move(geom.center().x() - w / 2 , geom.center().y() - h / 2);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. }
  50. void GraphicsItem::setState(SelectionHandleState st)
  51. {
  52. const Handles::iterator hend = m_handles.end();
  53. for (Handles::iterator it = m_handles.begin(); it != hend; ++it)
  54. (*it)->setState(st);
  55. }
  56. SizeHandleRect::Direction GraphicsItem::hitTest(const QPointF &point) const
  57. {
  58. const Handles::const_iterator hend = m_handles.end();
  59. for (Handles::const_iterator it = m_handles.begin(); it != hend; ++it)
  60. {
  61. if ((*it)->hitTest(point) ){
  62. return (*it)->dir();
  63. }
  64. }
  65. return SizeHandleRect::None;
  66. }
  67. Qt::CursorShape GraphicsItem::getCursor(SizeHandleRect::Direction dir)
  68. {
  69. switch (dir) {
  70. case SizeHandleRect::Right:
  71. return Qt::SizeHorCursor;
  72. case SizeHandleRect::RightTop:
  73. return Qt::SizeBDiagCursor;
  74. case SizeHandleRect::RightBottom:
  75. return Qt::SizeFDiagCursor;
  76. case SizeHandleRect::LeftBottom:
  77. return Qt::SizeBDiagCursor;
  78. case SizeHandleRect::Bottom:
  79. return Qt::SizeVerCursor;
  80. case SizeHandleRect::LeftTop:
  81. return Qt::SizeFDiagCursor;
  82. case SizeHandleRect::Left:
  83. return Qt::SizeHorCursor;
  84. case SizeHandleRect::Top:
  85. return Qt::SizeVerCursor;
  86. default:
  87. break;
  88. }
  89. return Qt::ArrowCursor;
  90. }
  91. void GraphicsItem::resizeTo(SizeHandleRect::Direction dir, const QPointF &point)
  92. {
  93. }
  94. QVariant GraphicsItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
  95. {
  96. if ( change == QGraphicsItem::ItemSelectedHasChanged ) {
  97. qDebug()<<" Item Selected : " << value.toString();
  98. setState(value.toBool() ? SelectionHandleActive : SelectionHandleOff);
  99. }else if ( change == QGraphicsItem::ItemRotationHasChanged ){
  100. qDebug()<<"Item Rotation Changed:" << value.toString();
  101. }else if ( change == QGraphicsItem::ItemTransformOriginPointHasChanged ){
  102. qDebug()<<"ItemTransformOriginPointHasChanged:" << value.toPointF();
  103. }
  104. return value;
  105. }