/experiments/graphicstherapist/basegraphicsobject.cpp

https://code.google.com/p/dwarftherapist/ · C++ · 97 lines · 76 code · 12 blank · 9 comment · 6 complexity · e42f957a3771873d716d6e2772651198 MD5 · raw file

  1. #include "basegraphicsobject.h"
  2. BaseGraphicsObject::BaseGraphicsObject(QGraphicsItem *parent)
  3. : QGraphicsObject(parent)
  4. , m_is_click(false)
  5. , m_is_hovering(false)
  6. , m_double_clicked(false)
  7. {
  8. setAcceptHoverEvents(true);
  9. this->setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton | Qt::MidButton);
  10. setFlag(QGraphicsItem::ItemIsSelectable, true);
  11. }
  12. QRectF BaseGraphicsObject::boundingRect() const {
  13. return QRectF(0, 0, 100, 20);
  14. }
  15. void BaseGraphicsObject::paint(QPainter *p, const QStyleOptionGraphicsItem *opt,
  16. QWidget *widget) {
  17. p->drawText(0, 0, "NOT IMPLEMENTED");
  18. }
  19. QVariant BaseGraphicsObject::itemChange(GraphicsItemChange change,
  20. const QVariant &value) {
  21. QVariant ret_val = QGraphicsItem::itemChange(change, value);
  22. if (change == ItemSceneChange) {
  23. QGraphicsScene *new_scene = value.value<QGraphicsScene*>();
  24. if (!scene() && new_scene) {
  25. on_added_to_scene(new_scene);
  26. }
  27. }
  28. return ret_val;
  29. }
  30. void BaseGraphicsObject::maybe_single_click() {
  31. if (!m_double_clicked) {
  32. //qDebug() << "single click for sure";
  33. emit single_click();
  34. emit single_click(m_last_click_position);
  35. }
  36. m_double_clicked = false;
  37. }
  38. void BaseGraphicsObject::maybe_update() {
  39. prepareGeometryChange();
  40. //QGraphicsObject::update();
  41. }
  42. void BaseGraphicsObject::on_added_to_scene(QGraphicsScene *scene) {
  43. Q_UNUSED(scene);
  44. // subclasses may want to do things once they are added to the scene
  45. }
  46. //! Mouse Handlers
  47. void BaseGraphicsObject::hoverEnterEvent(QGraphicsSceneHoverEvent *e) {
  48. //qDebug() << "mouse enter" << e->pos();
  49. m_is_hovering = true;
  50. m_is_click = false;
  51. emit hover_start();
  52. emit hover_start(e->pos());
  53. QGraphicsObject::hoverEnterEvent(e);
  54. }
  55. void BaseGraphicsObject::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) {
  56. //qDebug() << "mouse exit";
  57. m_is_hovering = false;
  58. m_is_click = false;
  59. emit hover_stop();
  60. emit hover_stop(e->pos());
  61. QGraphicsObject::hoverLeaveEvent(e);
  62. }
  63. void BaseGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *e) {
  64. //qDebug() << "PRESS";
  65. m_is_click = true;
  66. m_last_click_position = e->pos();
  67. QGraphicsObject::mousePressEvent(e);
  68. }
  69. void BaseGraphicsObject::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
  70. //qDebug() << "RELEASE";
  71. if (m_is_click) {
  72. m_is_click = false;
  73. m_last_click_position = e->pos();
  74. QTimer::singleShot(100, this, SLOT(maybe_single_click()));
  75. }
  76. QGraphicsObject::mouseReleaseEvent(e);
  77. }
  78. void BaseGraphicsObject::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e) {
  79. //qDebug() << "DOUBLE CLICK";
  80. m_is_click = false;
  81. m_double_clicked = true;
  82. emit double_click();
  83. emit double_click(e->pos());
  84. QGraphicsObject::mouseDoubleClickEvent(e);
  85. }