PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/experiments/graphicstherapist/displaycell.cpp

https://code.google.com/p/dwarftherapist/
C++ | 97 lines | 72 code | 13 blank | 12 comment | 8 complexity | 4274dd537497a4b66a2bbac2cef08009 MD5 | raw file
 1#include "displaycell.h"
 2#include <QtGui>
 3
 4DisplayCell::DisplayCell(QGraphicsItem *parent)
 5    : BaseGraphicsObject(parent)
 6    , m_tooltip(new ToolTip("My Tooltip"))
 7    , m_show_tooltip(false)
 8    , m_labor_on(false)
 9    , m_bg(QBrush(Qt::gray))
10    , m_bg_selected(QBrush(Qt::yellow))
11{
12    setAcceptHoverEvents(true);
13    m_tooltip->setZValue(1);
14    m_tooltip->hide();
15    this->setFlag(QGraphicsItem::ItemIsFocusable, true);
16
17    connect(this, SIGNAL(hover_start()), SLOT(on_hover_start()));
18    connect(this, SIGNAL(hover_stop()), SLOT(on_hover_stop()));
19    connect(this, SIGNAL(single_click()), SLOT(toggle()));
20}
21
22DisplayCell::~DisplayCell() {
23    if (m_tooltip)
24        m_tooltip->deleteLater();
25}
26
27QRectF DisplayCell::boundingRect() const {
28    return QRectF(-box_w / 2.0f, -box_h / 2.0f, box_w, box_h);
29}
30
31void DisplayCell::paint(QPainter *p, const QStyleOptionGraphicsItem *opt,
32                        QWidget *widget) {
33    p->save();
34    if (scene()->focusItem() == this) {
35        p->setPen(QPen(Qt::white, 0.5));
36    } else {
37        p->setPen(QPen(Qt::black, 0.5));
38    }
39    if (isEnabled() && m_labor_on) {
40        p->setBrush(m_bg_selected);
41    } else {
42        p->setBrush(m_bg);
43    }
44    p->drawRect(boundingRect().adjusted(0.5f, 0.5f, -0.5f, -0.5f));
45    p->restore();
46}
47
48void DisplayCell::on_hover_start() {
49    scene()->setFocusItem(this, Qt::MouseFocusReason);
50    //qDebug() << "Hover";
51    m_show_tooltip = true;
52    QTimer::singleShot(100, this, SLOT(maybe_show_tooltip()));
53}
54
55void DisplayCell::on_hover_stop() {
56    //qDebug() << "Leave";
57    m_show_tooltip = false;
58    m_tooltip->hide();
59}
60
61void DisplayCell::on_added_to_scene(QGraphicsScene *scene) {
62    scene->addItem(m_tooltip);
63}
64
65void DisplayCell::maybe_show_tooltip() {
66    if (m_show_tooltip) {
67        m_tooltip->setPos(mapToScene(boundingRect().bottomRight()) +
68                          QPoint(4, 4));
69        m_tooltip->show();
70
71        /*
72        QPropertyAnimation *ani = new QPropertyAnimation(m_tooltip, "scale");
73        connect(ani, SIGNAL(finished()), ani, SLOT(deleteLater()));
74        ani->setDuration(200);
75        ani->setStartValue(0.4f);
76        ani->setEndValue(1.0f);
77        ani->setEasingCurve(QEasingCurve::InExpo);
78        */
79
80        QPropertyAnimation *ani2 = new QPropertyAnimation(m_tooltip, "opacity");
81        connect(ani2, SIGNAL(finished()), ani2, SLOT(deleteLater()));
82        ani2->setDuration(500);
83        ani2->setStartValue(0);
84        ani2->setEndValue(1.0f);
85
86        //ani->start();
87        ani2->start();
88    }
89    m_show_tooltip = false;
90}
91
92void DisplayCell::toggle() {
93    qDebug() << this << "toggled!";
94    m_labor_on = !m_labor_on;
95    maybe_update();
96    //update();
97}