1 /*
2     This file is part of the KDE libraries
3     SPDX-FileCopyrightText: 1997 Martin Jones <mjones@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef KCOLORBUTTON_H
9 #define KCOLORBUTTON_H
10 
11 #include <kwidgetsaddons_export.h>
12 
13 #include <QPushButton>
14 #include <memory>
15 
16 class KColorButtonPrivate;
17 /**
18  * @class KColorButton kcolorbutton.h KColorButton
19  *
20  * @short A pushbutton to display or allow user selection of a color.
21  *
22  * This widget can be used to display or allow user selection of a color.
23  *
24  * \image html kcolorbutton.png "KColorButton Widget"
25  *
26  * @see QColorDialog
27  */
28 class KWIDGETSADDONS_EXPORT KColorButton : public QPushButton
29 {
30     Q_OBJECT
31     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed USER true)
32     Q_PROPERTY(QColor defaultColor READ defaultColor WRITE setDefaultColor)
33     Q_PROPERTY(bool alphaChannelEnabled READ isAlphaChannelEnabled WRITE setAlphaChannelEnabled)
34 
35 public:
36     /**
37      * Creates a color button.
38      */
39     explicit KColorButton(QWidget *parent = nullptr);
40 
41     /**
42      * Creates a color button with an initial color @p c.
43      */
44     explicit KColorButton(const QColor &c, QWidget *parent = nullptr);
45 
46     /**
47      * Creates a color button with an initial color @p c and default color @p defaultColor.
48      */
49     KColorButton(const QColor &c, const QColor &defaultColor, QWidget *parent = nullptr);
50 
51     ~KColorButton() override;
52 
53     /**
54      * Returns the currently chosen color.
55      */
56     QColor color() const;
57 
58     /**
59      * Sets the current color to @p c.
60      */
61     void setColor(const QColor &c);
62 
63     /**
64      * When set to true, allow the user to change the alpha component
65      * of the color. The default value is false.
66      * @since 4.5
67      */
68     void setAlphaChannelEnabled(bool alpha);
69 
70     /**
71      * Returns true if the user is allowed to change the alpha component.
72      * @since 4.5
73      */
74     bool isAlphaChannelEnabled() const;
75 
76     /**
77      * Returns the default color or an invalid color
78      * if no default color is set.
79      */
80     QColor defaultColor() const;
81 
82     /**
83      * Sets the default color to @p c.
84      */
85     void setDefaultColor(const QColor &c);
86 
87     QSize sizeHint() const override;
88     QSize minimumSizeHint() const override;
89 
90 Q_SIGNALS:
91     /**
92      * Emitted when the color of the widget
93      * is changed, either with setColor() or via user selection.
94      */
95     void changed(const QColor &newColor);
96 
97 protected:
98     void paintEvent(QPaintEvent *pe) override;
99     void dragEnterEvent(QDragEnterEvent *) override;
100     void dropEvent(QDropEvent *) override;
101     void mousePressEvent(QMouseEvent *e) override;
102     void mouseMoveEvent(QMouseEvent *e) override;
103     void keyPressEvent(QKeyEvent *e) override;
104 
105 private:
106     std::unique_ptr<class KColorButtonPrivate> const d;
107 };
108 
109 #endif
110