File indexing completed on 2024-05-19 04:07:47

0001 /*
0002     SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef PALAPELI_MOUSEINPUTBUTTON_P_H
0008 #define PALAPELI_MOUSEINPUTBUTTON_P_H
0009 
0010 #include <QLabel>
0011 #include <QMouseEvent>
0012 
0013 namespace Palapeli
0014 {
0015     class FlatButton : public QLabel
0016     {
0017         Q_OBJECT
0018         public:
0019             explicit FlatButton(const QIcon& icon, QWidget* parent = nullptr) : QLabel(parent), m_icon(icon)
0020             {
0021                 leaveEvent(nullptr); //apply icon
0022                 setMouseTracking(true);
0023             }
0024         Q_SIGNALS:
0025             void clicked();
0026         protected:
0027             void enterEvent(QEnterEvent*) override
0028             {
0029                 //TODO: respect global icon size configuration
0030                 setPixmap(m_icon.pixmap(16, QIcon::Active));
0031             }
0032             void leaveEvent(QEvent*) override
0033             {
0034                 setPixmap(m_icon.pixmap(16, QIcon::Normal));
0035             }
0036             void mousePressEvent(QMouseEvent* event) override
0037             {
0038                 if (event->button() == Qt::LeftButton)
0039                     event->accept();
0040                 else
0041                     QLabel::mousePressEvent(event);
0042             }
0043             void mouseReleaseEvent(QMouseEvent* event) override
0044             {
0045                 if (event->button() == Qt::LeftButton)
0046                 {
0047                     event->accept();
0048                     if (rect().contains(event->pos()))
0049                         Q_EMIT clicked();
0050                 }
0051                 else
0052                     QLabel::mouseReleaseEvent(event);
0053             }
0054         private:
0055             QIcon m_icon;
0056     };
0057 }
0058 
0059 #endif // PALAPELI_MOUSEINPUTBUTTON_P_H