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
16class KColorButtonPrivate;
17/*!
18 * \class KColorButton
19 * \inmodule KWidgetsAddons
20 *
21 * \brief A pushbutton to display or allow user selection of a color.
22 *
23 * This widget can be used to display or allow user selection of a color.
24 *
25 * \image kcolorbutton.png "KColorButton Widget"
26 *
27 * \sa QColorDialog
28 */
29class KWIDGETSADDONS_EXPORT KColorButton : public QPushButton
30{
31 Q_OBJECT
32
33 /*!
34 * \property KColorButton::color
35 */
36 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed USER true)
37
38 /*!
39 * \property KColorButton::defaultColor
40 */
41 Q_PROPERTY(QColor defaultColor READ defaultColor WRITE setDefaultColor)
42
43 /*!
44 * \property KColorButton::alphaChannelEnabled
45 */
46 Q_PROPERTY(bool alphaChannelEnabled READ isAlphaChannelEnabled WRITE setAlphaChannelEnabled)
47
48public:
49 /*!
50 * Creates a color button.
51 */
52 explicit KColorButton(QWidget *parent = nullptr);
53
54 /*!
55 * Creates a color button with an initial color \a c.
56 */
57 explicit KColorButton(const QColor &c, QWidget *parent = nullptr);
58
59 /*!
60 * Creates a color button with an initial color \a c and default color \a defaultColor.
61 */
62 KColorButton(const QColor &c, const QColor &defaultColor, QWidget *parent = nullptr);
63
64 ~KColorButton() override;
65
66 /*!
67 * Returns the currently chosen color.
68 */
69 QColor color() const;
70
71 /*!
72 * Sets the current color to \a c.
73 */
74 void setColor(const QColor &c);
75
76 /*!
77 * When set to true, allow the user to change the alpha component
78 * of the color. The default value is false.
79 * \since 4.5
80 */
81 void setAlphaChannelEnabled(bool alpha);
82
83 /*!
84 * Returns true if the user is allowed to change the alpha component.
85 * \since 4.5
86 */
87 bool isAlphaChannelEnabled() const;
88
89 /*!
90 * Returns the default color or an invalid color
91 * if no default color is set.
92 */
93 QColor defaultColor() const;
94
95 /*!
96 * Sets the default color to \a c.
97 */
98 void setDefaultColor(const QColor &c);
99
100 QSize sizeHint() const override;
101 QSize minimumSizeHint() const override;
102
103Q_SIGNALS:
104 /*!
105 * Emitted when the color of the widget
106 * is changed, either with setColor() or via user selection.
107 */
108 void changed(const QColor &newColor);
109
110protected:
111 void paintEvent(QPaintEvent *pe) override;
112 void dragEnterEvent(QDragEnterEvent *) override;
113 void dropEvent(QDropEvent *) override;
114 void mousePressEvent(QMouseEvent *e) override;
115 void mouseMoveEvent(QMouseEvent *e) override;
116 void keyPressEvent(QKeyEvent *e) override;
117
118private:
119 std::unique_ptr<class KColorButtonPrivate> const d;
120};
121
122#endif
123

source code of kwidgetsaddons/src/kcolorbutton.h