| 1 |
/******************************************************************************** |
| 2 |
** |
| 3 |
** GNU General Public License Usage |
| 4 |
** |
| 5 |
** This program is free software: you can redistribute it and/or modify |
| 6 |
** it under the terms of the GNU General Public License as published by |
| 7 |
** the Free Software Foundation, either version 3 of the License, or |
| 8 |
** (at your option) any later version. |
| 9 |
** |
| 10 |
** This program is distributed in the hope that it will be useful, |
| 11 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
** GNU General Public License for more details. |
| 14 |
** |
| 15 |
** You should have received a copy of the GNU General Public License |
| 16 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
** |
| 18 |
********************************************************************************/ |
| 19 |
|
| 20 |
#include "button.h" |
| 21 |
|
| 22 |
Button::Button(QGraphicsItem *parent) |
| 23 |
: QGraphicsItem(parent), m_isPressed(false) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
Button::Button(const QString normal, const QString pressed, |
| 28 |
QGraphicsItem *parent) |
| 29 |
: QGraphicsItem(parent), m_isPressed(false) |
| 30 |
{ |
| 31 |
setPixmap(normal, pressed); |
| 32 |
} |
| 33 |
|
| 34 |
QRectF Button::boundingRect() const |
| 35 |
{ |
| 36 |
return m_normal.rect(); |
| 37 |
} |
| 38 |
|
| 39 |
void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, |
| 40 |
QWidget *widget) |
| 41 |
{ |
| 42 |
Q_UNUSED(option); |
| 43 |
Q_UNUSED(widget); |
| 44 |
|
| 45 |
if (m_isPressed) |
| 46 |
painter->drawPixmap(0, 0, m_pressed); |
| 47 |
else |
| 48 |
painter->drawPixmap(0, 0, m_normal); |
| 49 |
} |
| 50 |
|
| 51 |
void Button::setPixmap(QString normal, QString pressed) |
| 52 |
{ |
| 53 |
if (! m_normal.isNull()) { |
| 54 |
m_normal.detach(); |
| 55 |
m_pressed.detach(); |
| 56 |
} |
| 57 |
|
| 58 |
m_normal.load(normal); |
| 59 |
m_pressed.load(pressed); |
| 60 |
} |
| 61 |
|
| 62 |
void Button::mousePressEvent(QGraphicsSceneMouseEvent *event) |
| 63 |
{ |
| 64 |
if (event->button() == Qt::LeftButton) { |
| 65 |
if (contains(event->pos())) |
| 66 |
m_isPressed = true; |
| 67 |
|
| 68 |
update(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void Button::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
| 73 |
{ |
| 74 |
if (event->buttons() & Qt::LeftButton) { |
| 75 |
if (contains(event->pos())) { |
| 76 |
m_isPressed = true; |
| 77 |
} else { |
| 78 |
m_isPressed = false; |
| 79 |
} |
| 80 |
|
| 81 |
update(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
| 86 |
{ |
| 87 |
if (event->button() == Qt::LeftButton) { |
| 88 |
m_isPressed = false; |
| 89 |
|
| 90 |
update(); |
| 91 |
|
| 92 |
if (contains(event->pos())) { |
| 93 |
emit clicked(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |