File indexing completed on 2024-12-22 04:13:17
0001 /* This file is part of the KDE libraries 0002 0003 SPDX-FileCopyrightText: 1999 Daniel M. Duley <mosfet@kde.org> 0004 SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-only 0007 */ 0008 0009 #ifndef KODUALCOLORBUTTON_H 0010 #define KODUALCOLORBUTTON_H 0011 0012 #include <kritaui_export.h> 0013 #include <KoColorDisplayRendererInterface.h> 0014 #include <QWidget> 0015 0016 class KoColor; 0017 class KoColorSpace; 0018 /** 0019 * @short A widget for selecting two related colors. 0020 * 0021 * KoDualColorButton allows the user to select two cascaded colors (usually a 0022 * foreground and background color). Other features include drag and drop 0023 * from other KDE color widgets, a reset to black and white control, and a 0024 * swap colors control. 0025 * 0026 * When the user clicks on the foreground or background rectangle the 0027 * rectangle is first sunken and the selectionChanged() signal is emitted. 0028 * Further clicks will present a color dialog and emit either the foregroundColorChanged() 0029 * or backgroundColorChanged() if a new color is selected. 0030 * 0031 * Note: With drag and drop when dropping a color the current selected color 0032 * will be set, while when dragging a color it will use whatever color 0033 * rectangle the mouse was pressed inside. 0034 * 0035 * @author Daniel M. Duley <mosfet@kde.org> 0036 */ 0037 class KRITAUI_EXPORT KoDualColorButton : public QWidget 0038 { 0039 Q_OBJECT 0040 Q_ENUMS( Selection ) 0041 Q_PROPERTY( KoColor foregroundColor READ foregroundColor WRITE setForegroundColor ) 0042 Q_PROPERTY( KoColor backgroundColor READ backgroundColor WRITE setBackgroundColor ) 0043 Q_PROPERTY( bool popDialog READ popDialog WRITE setPopDialog ) 0044 0045 0046 public: 0047 enum Selection { 0048 Foreground, 0049 Background 0050 }; 0051 0052 /** 0053 * Constructs a new KoDualColorButton with the supplied foreground and 0054 * background colors. 0055 * 0056 * @param parent The parent widget of the KoDualColorButton. 0057 * @param dialogParent The parent widget of the color selection dialog. 0058 */ 0059 KoDualColorButton(const KoColor &foregroundColor, const KoColor &backgroundColor, 0060 QWidget *parent = 0, QWidget* dialogParent = 0 ); 0061 0062 KoDualColorButton(const KoColor &foregroundColor, const KoColor &backgroundColor, 0063 const KoColorDisplayRendererInterface *displayRenderer, 0064 QWidget *parent = 0, QWidget* dialogParent = 0 ); 0065 0066 /** 0067 * Destroys the KoDualColorButton. 0068 */ 0069 ~KoDualColorButton() override; 0070 0071 /** 0072 * Returns the current foreground color. 0073 */ 0074 KoColor foregroundColor() const; 0075 0076 /** 0077 * Returns the current background color. 0078 */ 0079 KoColor backgroundColor() const; 0080 0081 /** 0082 * Returns if a dialog with a color chooser will be popped up when clicking 0083 * If false then you could/should connect to the pleasePopDialog signal 0084 * and pop your own dialog. Just set the current color afterwards. 0085 */ 0086 bool popDialog() const; 0087 0088 /** 0089 * Returns the minimum size needed to display the widget and all its 0090 * controls. 0091 */ 0092 QSize sizeHint() const override; 0093 0094 public Q_SLOTS: 0095 /** 0096 * Sets the foreground color. 0097 */ 0098 void setForegroundColor( const KoColor &color ); 0099 0100 /** 0101 * Sets the background color. 0102 */ 0103 void setBackgroundColor( const KoColor &color ); 0104 0105 void slotSetForeGroundColorFromDialog (const KoColor color); 0106 0107 void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer = KoDumbColorDisplayRenderer::instance()); 0108 0109 /** 0110 * @brief setColorSpace 0111 * set ColorSpace so we can lock the selector. Right now this'll be changed per view-change. 0112 * @param cs 0113 */ 0114 void setColorSpace(const KoColorSpace *cs); 0115 0116 /** 0117 * @brief getColorFromDisplayRenderer 0118 * convenience function to get the right qcolor from the display renderer, including checking 0119 * whether the display renderer actually exists. 0120 * @param c the kocolor to convert. 0121 * @return the qcolor to use for display. 0122 */ 0123 QColor getColorFromDisplayRenderer(KoColor c); 0124 0125 /** 0126 * Sets if a dialog with a color chooser should be popped up when clicking 0127 * If you set this to false then you could connect to the pleasePopDialog signal 0128 * and pop your own dialog. Just set the current color afterwards. 0129 */ 0130 void setPopDialog( bool popDialog ); 0131 0132 void openForegroundDialog(); 0133 0134 void openBackgroundDialog(); 0135 Q_SIGNALS: 0136 /** 0137 * Emitted when the foreground color is changed. 0138 */ 0139 void foregroundColorChanged( const KoColor &color ); 0140 0141 /** 0142 * Emitted when the background color is changed. 0143 */ 0144 void backgroundColorChanged( const KoColor &color ); 0145 0146 protected: 0147 /** 0148 * Sets the supplied rectangles to the proper size and position for the 0149 * current widget size. You can reimplement this to change the layout 0150 * of the widget. Restrictions are that the swap control will always 0151 * be at the top right, the reset control will always be at the bottom 0152 * left, and you must leave at least a 14x14 space in those corners. 0153 */ 0154 virtual void metrics( QRect &foregroundRect, QRect &backgroundRect ); 0155 0156 void paintEvent( QPaintEvent *event ) override; 0157 void mousePressEvent( QMouseEvent *event ) override; 0158 void mouseMoveEvent( QMouseEvent *event ) override; 0159 void mouseReleaseEvent( QMouseEvent *event ) override; 0160 void dragEnterEvent( QDragEnterEvent *event ) override; 0161 void dropEvent( QDropEvent *event ) override; 0162 void changeEvent(QEvent *event) override; 0163 bool event(QEvent *event) override; 0164 0165 private: 0166 class Private; 0167 Private *const d; 0168 }; 0169 0170 #endif 0171