File indexing completed on 2024-05-05 05:28:58

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003  * SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #pragma once
0009 
0010 #include "breezedecoration.h"
0011 #include <KDecoration2/DecorationButton>
0012 
0013 #include <QHash>
0014 #include <QImage>
0015 
0016 class QVariantAnimation;
0017 
0018 namespace Breeze
0019 {
0020 class Button : public KDecoration2::DecorationButton
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     //* constructor
0026     explicit Button(QObject *parent, const QVariantList &args);
0027 
0028     //* destructor
0029     virtual ~Button() = default;
0030 
0031     //* button creation
0032     static Button *create(KDecoration2::DecorationButtonType type, KDecoration2::Decoration *decoration, QObject *parent);
0033 
0034     //* render
0035     virtual void paint(QPainter *painter, const QRect &repaintRegion) override;
0036 
0037     //* padding
0038     void setPadding(const QMargins &value)
0039     {
0040         m_padding = value;
0041     }
0042 
0043     //* left padding, for rendering
0044     void setLeftPadding(qreal value)
0045     {
0046         m_padding.setLeft(value);
0047     }
0048 
0049     //* right padding, for rendering
0050     void setRightPadding(qreal value)
0051     {
0052         m_padding.setRight(value);
0053     }
0054 
0055     //*@name active state change animation
0056     //@{
0057     void setOpacity(qreal value)
0058     {
0059         if (m_opacity == value) {
0060             return;
0061         }
0062         m_opacity = value;
0063         update();
0064     }
0065 
0066     qreal opacity() const
0067     {
0068         return m_opacity;
0069     }
0070 
0071     //@}
0072 
0073     void setPreferredSize(const QSizeF &size)
0074     {
0075         m_preferredSize = size;
0076     }
0077 
0078     QSizeF preferredSize() const
0079     {
0080         return m_preferredSize;
0081     }
0082 
0083 private Q_SLOTS:
0084 
0085     //* apply configuration changes
0086     void reconfigure();
0087 
0088     //* animation state
0089     void updateAnimationState(bool);
0090 
0091 private:
0092     //* private constructor
0093     explicit Button(KDecoration2::DecorationButtonType type, Decoration *decoration, QObject *parent = nullptr);
0094 
0095     //* draw button icon
0096     void drawIcon(QPainter *) const;
0097 
0098     //*@name colors
0099     //@{
0100     QColor foregroundColor() const;
0101     QColor backgroundColor() const;
0102     //@}
0103 
0104     //* active state change animation
0105     QVariantAnimation *m_animation;
0106 
0107     //* padding (for rendering)
0108     QMargins m_padding;
0109 
0110     //* implicit size
0111     QSizeF m_preferredSize;
0112 
0113     //* active state change opacity
0114     qreal m_opacity = 0;
0115 };
0116 
0117 } // namespace