PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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