1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2010-06-29
7 * Description : Pressable Button class using QGraphicsItem
8 * based on Frederico Duarte implementation.
9 *
10 * Copyright (C) 2010-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software Foundation;
15 * either version 2, or (at your option)
16 * any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * ============================================================ */
24
25 #include "demobutton.h"
26
27 // Qt includes
28
29 #include <QGraphicsScene>
30
31 namespace FaceEngineDemo
32 {
33
34 class Q_DECL_HIDDEN Button::Private
35 {
36 public:
37
Private()38 explicit Private()
39 : isPressed(false)
40 {
41 }
42
43 bool isPressed;
44 QPixmap normal;
45 QPixmap pressed;
46 };
47
Button(QGraphicsItem * const parent)48 Button::Button(QGraphicsItem* const parent)
49 : QGraphicsItem(parent),
50 d(new Private)
51 {
52 }
53
Button(const QString & normal,const QString & pressed,QGraphicsItem * const parent)54 Button::Button(const QString& normal, const QString& pressed, QGraphicsItem* const parent)
55 : QGraphicsItem(parent),
56 d(new Private)
57 {
58 setPixmap(normal, pressed);
59 }
60
Button(const QPixmap & normal,const QPixmap & pressed,QGraphicsItem * const parent)61 Button::Button(const QPixmap& normal, const QPixmap& pressed, QGraphicsItem* const parent)
62 : QGraphicsItem(parent),
63 d(new Private)
64 {
65 setPixmap(normal, pressed);
66 }
67
~Button()68 Button::~Button()
69 {
70 delete d;
71 }
72
boundingRect() const73 QRectF Button::boundingRect() const
74 {
75 return d->normal.rect();
76 }
77
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)78 void Button::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
79 {
80 Q_UNUSED(option);
81 Q_UNUSED(widget);
82
83 if (d->isPressed)
84 {
85 painter->drawPixmap(0, 0, d->pressed);
86 }
87 else
88 {
89 painter->drawPixmap(0, 0, d->normal);
90 }
91 }
92
setPixmap(const QString & normal,const QString & pressed)93 void Button::setPixmap(const QString& normal, const QString& pressed)
94 {
95 if (! d->normal.isNull())
96 {
97 d->normal.detach();
98 d->pressed.detach();
99 }
100
101 d->normal.load(normal);
102 d->pressed.load(pressed);
103 }
104
setPixmap(const QPixmap & normal,const QPixmap & pressed)105 void Button::setPixmap(const QPixmap& normal, const QPixmap& pressed)
106 {
107 d->normal = normal;
108 d->pressed = pressed;
109 }
110
mousePressEvent(QGraphicsSceneMouseEvent * event)111 void Button::mousePressEvent(QGraphicsSceneMouseEvent* event)
112 {
113 if (event->button() == Qt::LeftButton)
114 {
115 if (contains(event->pos()))
116 {
117 d->isPressed = true;
118 }
119
120 update();
121 }
122 }
123
mouseMoveEvent(QGraphicsSceneMouseEvent * event)124 void Button::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
125 {
126 if (event->buttons() & Qt::LeftButton)
127 {
128 if (contains(event->pos()))
129 {
130 d->isPressed = true;
131 }
132 else
133 {
134 d->isPressed = false;
135 }
136
137 update();
138 }
139 }
140
mouseReleaseEvent(QGraphicsSceneMouseEvent * event)141 void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
142 {
143 if (event->button() == Qt::LeftButton)
144 {
145 d->isPressed = false;
146
147 update();
148
149 if (contains(event->pos()))
150 {
151 emit clicked();
152 }
153 }
154 }
155
156 } // namespace FaceEngineDemo
157