File indexing completed on 2025-01-05 03:58:06
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2010-06-29 0007 * Description : Pressable Button class using QGraphicsItem 0008 * based on Frederico Duarte implementation. 0009 * 0010 * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0011 * 0012 * SPDX-License-Identifier: GPL-2.0-or-later 0013 * 0014 * ============================================================ */ 0015 0016 #include "demobutton.h" 0017 0018 // Qt includes 0019 0020 #include <QGraphicsScene> 0021 0022 namespace FaceEngineDemo 0023 { 0024 0025 class Q_DECL_HIDDEN Button::Private 0026 { 0027 public: 0028 0029 explicit Private() 0030 : isPressed(false) 0031 { 0032 } 0033 0034 bool isPressed; 0035 QPixmap normal; 0036 QPixmap pressed; 0037 }; 0038 0039 Button::Button(QGraphicsItem* const parent) 0040 : QGraphicsItem(parent), 0041 d(new Private) 0042 { 0043 } 0044 0045 Button::Button(const QString& normal, const QString& pressed, QGraphicsItem* const parent) 0046 : QGraphicsItem(parent), 0047 d(new Private) 0048 { 0049 setPixmap(normal, pressed); 0050 } 0051 0052 Button::Button(const QPixmap& normal, const QPixmap& pressed, QGraphicsItem* const parent) 0053 : QGraphicsItem(parent), 0054 d(new Private) 0055 { 0056 setPixmap(normal, pressed); 0057 } 0058 0059 Button::~Button() 0060 { 0061 delete d; 0062 } 0063 0064 QRectF Button::boundingRect() const 0065 { 0066 return d->normal.rect(); 0067 } 0068 0069 void Button::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 0070 { 0071 Q_UNUSED(option); 0072 Q_UNUSED(widget); 0073 0074 if (d->isPressed) 0075 { 0076 painter->drawPixmap(0, 0, d->pressed); 0077 } 0078 else 0079 { 0080 painter->drawPixmap(0, 0, d->normal); 0081 } 0082 } 0083 0084 void Button::setPixmap(const QString& normal, const QString& pressed) 0085 { 0086 if (! d->normal.isNull()) 0087 { 0088 d->normal.detach(); 0089 d->pressed.detach(); 0090 } 0091 0092 d->normal.load(normal); 0093 d->pressed.load(pressed); 0094 } 0095 0096 void Button::setPixmap(const QPixmap& normal, const QPixmap& pressed) 0097 { 0098 d->normal = normal; 0099 d->pressed = pressed; 0100 } 0101 0102 void Button::mousePressEvent(QGraphicsSceneMouseEvent* event) 0103 { 0104 if (event->button() == Qt::LeftButton) 0105 { 0106 if (contains(event->pos())) 0107 { 0108 d->isPressed = true; 0109 } 0110 0111 update(); 0112 } 0113 } 0114 0115 void Button::mouseMoveEvent(QGraphicsSceneMouseEvent* event) 0116 { 0117 if (event->buttons() & Qt::LeftButton) 0118 { 0119 if (contains(event->pos())) 0120 { 0121 d->isPressed = true; 0122 } 0123 else 0124 { 0125 d->isPressed = false; 0126 } 0127 0128 update(); 0129 } 0130 } 0131 0132 void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) 0133 { 0134 if (event->button() == Qt::LeftButton) 0135 { 0136 d->isPressed = false; 0137 0138 update(); 0139 0140 if (contains(event->pos())) 0141 { 0142 Q_EMIT clicked(); 0143 } 0144 } 0145 } 0146 0147 } // namespace FaceEngineDemo 0148 0149 #include "moc_demobutton.cpp"