summaryrefslogtreecommitdiffstats
path: root/shared/button.cpp
blob: bf70098e199a79e59d8b66c5243ef48bf9ea0ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: qt-info@nokia.com
**
** This software, including documentation, is protected by copyright
** controlled by Nokia Corporation.  You may use this software in
** accordance with the terms and conditions contained in the Qt Phone
** Demo License Agreement.
**
****************************************************************************/

#include <QPixmap>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>

#include "button.h"


class ButtonPrivate
{
public:
    ButtonPrivate(Button *qptr);

    void init();

    Button *q;
    QString text;
    bool isPressed;
    QPixmap normalPixmap;
    QPixmap pressedPixmap;
    QPixmap disabledPixmap;
};

ButtonPrivate::ButtonPrivate(Button *qptr)
    : q(qptr), isPressed(false)
{

}

void ButtonPrivate::init()
{
    q->setMinimumSize(normalPixmap.size());
    q->setMaximumSize(normalPixmap.size());
}


Button::Button(const QPixmap &normal, QGraphicsItem *parent)
    : QGraphicsWidget(parent),
      d(new ButtonPrivate(this))
{
    d->normalPixmap = normal;
    d->init();
}

Button::Button(const QPixmap &normal, const QPixmap &pressed,
               QGraphicsItem *parent)
    : QGraphicsWidget(parent),
      d(new ButtonPrivate(this))
{
    d->normalPixmap = normal;
    d->pressedPixmap = pressed;
    d->init();
}

Button::Button(const QPixmap &normal, const QPixmap &pressed,
               const QPixmap &disabled, QGraphicsItem *parent)
    : QGraphicsWidget(parent),
      d(new ButtonPrivate(this))
{
    d->normalPixmap = normal;
    d->pressedPixmap = pressed;
    d->disabledPixmap = disabled;
    d->init();
}

Button::~Button()
{
    delete d;
}

void Button::setPixmap(State state, const QPixmap &pixmap)
{
    bool dirty = false;

    if (state == NormalState) {
        d->normalPixmap = pixmap;
        dirty = (!d->isPressed && isEnabled());
        setMinimumSize(d->normalPixmap.size());
        setMaximumSize(d->normalPixmap.size());
    } else if (state == PressedState) {
        dirty = d->isPressed;
        d->pressedPixmap = pixmap;
    } else if (state == DisabledState) {
        dirty = !isEnabled();
        d->disabledPixmap = pixmap;
    }

    if (dirty)
        update();
}

QString Button::text() const
{
    return d->text;
}

void Button::setText(const QString &value)
{
    if (d->text != value) {
        d->text = value;
        update();
    }
}

void Button::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        d->isPressed = true;
        update();
        emit pressed();
    }
}

void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        const bool isClick = d->isPressed;

        d->isPressed = false;
        update();
        emit released();

        if (isClick && contains(e->pos()))
            emit clicked();
    }
}

void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                   QWidget *widget)
{
    Q_UNUSED(widget);

    if (!(option->state & QStyle::State_Enabled)) {
        const bool support = !d->disabledPixmap.isNull();
        painter->drawPixmap(0, 0, support ? d->disabledPixmap : d->normalPixmap);
    } else if (d->isPressed && !d->pressedPixmap.isNull())
        painter->drawPixmap(0, 0, d->pressedPixmap);
    else if (!d->normalPixmap.isNull())
        painter->drawPixmap(0, 0, d->normalPixmap);


    if (!d->text.isEmpty()) {
        const QRect &textRect = boundingRect().toRect();

        QFontMetrics metrics(font());
        const QString &elidedText = metrics.elidedText(d->text, Qt::ElideRight,
                                                       textRect.width());

        painter->setFont(font());
        painter->setPen(Qt::white);
        painter->drawText(textRect, Qt::TextSingleLine | Qt::AlignCenter, elidedText);
    }
}