File indexing completed on 2024-05-12 16:02:00

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Wolthera van Hovell tot Westerflier <griffinvalley@gmail.com>
0003  *  This file is forked from the KF5 KColorButton
0004     SPDX-FileCopyrightText: 1997 Martin Jones (mjones@kde.org)
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KisColorButton_H
0010 #define KisColorButton_H
0011 #include <kritawidgets_export.h>
0012 
0013 #include <KoColor.h>
0014 #include <QPushButton>
0015 
0016 class KisColorButtonPrivate;
0017 /**
0018 * @short A pushbutton to display or allow user selection of a color.
0019 *
0020 * This widget can be used to display or allow user selection of a color.
0021 *
0022 * @see QColorDialog
0023 *
0024 * \image html KisColorButton.png "KDE Color Button"
0025 */
0026 class KRITAWIDGETS_EXPORT KisColorButton : public QPushButton
0027 {
0028     Q_OBJECT
0029 
0030     /**
0031      * QtCreator treats KoColor as a QColor in incorrect way, so just disable using them in QtCreator
0032      * https://bugs.kde.org/show_bug.cgi?id=368483
0033      */
0034     Q_PROPERTY(KoColor color READ color WRITE setColor NOTIFY changed USER true DESIGNABLE false)
0035     Q_PROPERTY(KoColor defaultColor READ defaultColor WRITE setDefaultColor DESIGNABLE false)
0036     Q_PROPERTY(bool alphaChannelEnabled READ isAlphaChannelEnabled WRITE setAlphaChannelEnabled)
0037 
0038 public:
0039     /**
0040      * Creates a color button.
0041      */
0042     explicit KisColorButton(QWidget *parent = 0);
0043 
0044     /**
0045      * Creates a color button with an initial color @p c.
0046      */
0047     explicit KisColorButton(const KoColor &c, QWidget *parent = 0);
0048 
0049     /**
0050      * Creates a color button with an initial color @p c and default color @p defaultColor.
0051      */
0052     KisColorButton(const KoColor &c, const KoColor &defaultColor, QWidget *parent = 0);
0053 
0054     ~KisColorButton() override;
0055 
0056     /**
0057      * Returns the currently chosen color.
0058      */
0059     KoColor color() const;
0060 
0061     /**
0062      * Sets the current color to @p c.
0063      */
0064     void setColor(const KoColor &c);
0065 
0066     /**
0067      * When set to true, allow the user to change the alpha component
0068      * of the color. The default value is false.
0069      */
0070     void setAlphaChannelEnabled(bool alpha);
0071 
0072     /**
0073      * Returns true if the user is allowed to change the alpha component.
0074      */
0075     bool isAlphaChannelEnabled() const;
0076 
0077     /**
0078      * Allow having a palette.
0079      */
0080     void setPaletteViewEnabled( bool enable);
0081 
0082     /**
0083      * @brief paletteViewEnabled
0084      * @return whether the palette is enabled.
0085      */
0086     bool paletteViewEnabled() const;
0087     /**
0088      * Returns the default color or an invalid color
0089      * if no default color is set.
0090      */
0091     KoColor defaultColor() const;
0092 
0093     /**
0094      * Sets the default color to @p c.
0095      */
0096     void setDefaultColor(const KoColor &c);
0097 
0098     QSize sizeHint() const override;
0099     QSize minimumSizeHint() const override;
0100 
0101 Q_SIGNALS:
0102     /**
0103      * Emitted when the color of the widget
0104      * is changed, either with setColor() or via user selection.
0105      */
0106     void changed(const KoColor &newColor);
0107 
0108 protected:
0109     void paintEvent(QPaintEvent *pe) override;
0110     void dragEnterEvent(QDragEnterEvent *) override;
0111     void dropEvent(QDropEvent *) override;
0112     void mousePressEvent(QMouseEvent *e) override;
0113     void mouseMoveEvent(QMouseEvent *e) override;
0114     void keyPressEvent(QKeyEvent *e) override;
0115 
0116 private:
0117     class KisColorButtonPrivate;
0118     KisColorButtonPrivate *const d;
0119 
0120     Q_PRIVATE_SLOT(d, void _k_chooseColor())
0121 };
0122 
0123 #endif