File indexing completed on 2024-04-28 15:31:59

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1997 Martin Jones <mjones@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KCOLORBUTTON_H
0009 #define KCOLORBUTTON_H
0010 
0011 #include <kwidgetsaddons_export.h>
0012 
0013 #include <QPushButton>
0014 #include <memory>
0015 
0016 class KColorButtonPrivate;
0017 /**
0018  * @class KColorButton kcolorbutton.h KColorButton
0019  *
0020  * @short A pushbutton to display or allow user selection of a color.
0021  *
0022  * This widget can be used to display or allow user selection of a color.
0023  *
0024  * \image html kcolorbutton.png "KColorButton Widget"
0025  *
0026  * @see QColorDialog
0027  */
0028 class KWIDGETSADDONS_EXPORT KColorButton : public QPushButton
0029 {
0030     Q_OBJECT
0031     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed USER true)
0032     Q_PROPERTY(QColor defaultColor READ defaultColor WRITE setDefaultColor)
0033     Q_PROPERTY(bool alphaChannelEnabled READ isAlphaChannelEnabled WRITE setAlphaChannelEnabled)
0034 
0035 public:
0036     /**
0037      * Creates a color button.
0038      */
0039     explicit KColorButton(QWidget *parent = nullptr);
0040 
0041     /**
0042      * Creates a color button with an initial color @p c.
0043      */
0044     explicit KColorButton(const QColor &c, QWidget *parent = nullptr);
0045 
0046     /**
0047      * Creates a color button with an initial color @p c and default color @p defaultColor.
0048      */
0049     KColorButton(const QColor &c, const QColor &defaultColor, QWidget *parent = nullptr);
0050 
0051     ~KColorButton() override;
0052 
0053     /**
0054      * Returns the currently chosen color.
0055      */
0056     QColor color() const;
0057 
0058     /**
0059      * Sets the current color to @p c.
0060      */
0061     void setColor(const QColor &c);
0062 
0063     /**
0064      * When set to true, allow the user to change the alpha component
0065      * of the color. The default value is false.
0066      * @since 4.5
0067      */
0068     void setAlphaChannelEnabled(bool alpha);
0069 
0070     /**
0071      * Returns true if the user is allowed to change the alpha component.
0072      * @since 4.5
0073      */
0074     bool isAlphaChannelEnabled() const;
0075 
0076     /**
0077      * Returns the default color or an invalid color
0078      * if no default color is set.
0079      */
0080     QColor defaultColor() const;
0081 
0082     /**
0083      * Sets the default color to @p c.
0084      */
0085     void setDefaultColor(const QColor &c);
0086 
0087     QSize sizeHint() const override;
0088     QSize minimumSizeHint() const override;
0089 
0090 Q_SIGNALS:
0091     /**
0092      * Emitted when the color of the widget
0093      * is changed, either with setColor() or via user selection.
0094      */
0095     void changed(const QColor &newColor);
0096 
0097 protected:
0098     void paintEvent(QPaintEvent *pe) override;
0099     void dragEnterEvent(QDragEnterEvent *) override;
0100     void dropEvent(QDropEvent *) override;
0101     void mousePressEvent(QMouseEvent *e) override;
0102     void mouseMoveEvent(QMouseEvent *e) override;
0103     void keyPressEvent(QKeyEvent *e) override;
0104 
0105 private:
0106     std::unique_ptr<class KColorButtonPrivate> const d;
0107 };
0108 
0109 #endif