1 /***************************************************************************
2     qgscompoundcolorwidget.h
3     ------------------------
4     begin                : April 2016
5     copyright            : (C) 2016 by Nyall Dawson
6     email                : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 
16 #ifndef QGSCOMPOUNDCOLORWIDGET_H
17 #define QGSCOMPOUNDCOLORWIDGET_H
18 
19 #include "qgsguiutils.h"
20 #include "qgis_sip.h"
21 #include "qgspanelwidget.h"
22 #include "ui_qgscompoundcolorwidget.h"
23 #include "qgis_gui.h"
24 
25 /**
26  * \ingroup gui
27  * \class QgsCompoundColorWidget
28  * \brief A custom QGIS widget for selecting a color, including options for selecting colors via
29  * hue wheel, color swatches, and a color sampler.
30  * \since QGIS 2.16
31  */
32 
33 class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::QgsCompoundColorWidgetBase
34 {
35 
36     Q_OBJECT
37 
38   public:
39 
40     //! Widget layout
41     enum Layout
42     {
43       LayoutDefault = 0, //!< Use the default (rectangular) layout
44       LayoutVertical, //!< Use a narrower, vertically stacked layout
45     };
46 
47     /**
48      * Constructor for QgsCompoundColorWidget
49      * \param parent parent widget
50      * \param color initial color for dialog
51      * \param layout widget layout to use
52      */
53     QgsCompoundColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QColor &color = QColor(), Layout layout = LayoutDefault );
54 
55     ~QgsCompoundColorWidget() override;
56 
57     /**
58      * Returns the current color for the dialog
59      * \returns dialog color
60      */
61     QColor color() const;
62 
63     /**
64      * Sets whether opacity modification (transparency) is permitted
65      * for the color dialog. Defaults to TRUE.
66      * \param allowOpacity set to FALSE to disable opacity modification
67      * \since QGIS 3.0
68      */
69     void setAllowOpacity( bool allowOpacity );
70 
71     /**
72      * Sets whether the widget's color has been "discarded" and the selected color should not
73      * be stored in the recent color list.
74      * \param discarded set to TRUE to avoid adding color to recent color list on widget destruction.
75      * \since QGIS 3.0
76      */
setDiscarded(bool discarded)77     void setDiscarded( bool discarded ) { mDiscarded = discarded; }
78 
79     /**
80      * Triggers a user prompt for importing a new color scheme from an existing GPL file.
81      *
82      * The \a parent argument must be set to a valid parent widget for the dialog prompts.
83      *
84      *
85      * \see createNewUserPalette()
86      * \see removeUserPalette()
87      * \since QGIS 3.2
88      */
89     static QgsUserColorScheme *importUserPaletteFromFile( QWidget *parent );
90 
91     /**
92      * Triggers a user prompt for creating a new user color scheme.
93      *
94      * The \a parent argument must be set to a valid parent widget for the dialog prompts.
95      *
96      *
97      * \see importUserPaletteFromFile()
98      * \see removeUserPalette()
99      * \since QGIS 3.2
100      */
101     static QgsUserColorScheme *createNewUserPalette( QWidget *parent );
102 
103     /**
104      * Triggers a user prompt for removing an existing user color \a scheme.
105      *
106      * The \a parent argument must be set to a valid parent widget for the dialog prompts.
107      *
108      *
109      * \see importUserPaletteFromFile()
110      * \see createNewUserPalette()
111      * \since QGIS 3.2
112      */
113     static bool removeUserPalette( QgsUserColorScheme *scheme, QWidget *parent );
114 
115   signals:
116 
117     /**
118      * Emitted when the dialog's color changes
119      * \param color current color
120      */
121     void currentColorChanged( const QColor &color );
122 
123   public slots:
124 
125     /**
126      * Sets the current color for the dialog
127      * \param color desired color
128      */
129     void setColor( const QColor &color );
130 
131     /**
132      * Sets the color to show in an optional "previous color" section
133      * \param color previous color
134      */
135     void setPreviousColor( const QColor &color );
136 
137   protected:
138 
139     void hideEvent( QHideEvent *e ) override;
140 
141     void mousePressEvent( QMouseEvent *e ) override;
142 
143     void mouseMoveEvent( QMouseEvent *e ) override;
144 
145     void mouseReleaseEvent( QMouseEvent *e ) override;
146 
147     void keyPressEvent( QKeyEvent *e ) override;
148 
149   private slots:
150 
151     void mHueRadio_toggled( bool checked );
152     void mSaturationRadio_toggled( bool checked );
153     void mValueRadio_toggled( bool checked );
154     void mRedRadio_toggled( bool checked );
155     void mGreenRadio_toggled( bool checked );
156     void mBlueRadio_toggled( bool checked );
157 
158     void mAddColorToSchemeButton_clicked();
159 
160     void importPalette();
161     void removePalette();
162     void newPalette();
163 
164     void schemeIndexChanged( int index );
165     void listSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected );
166 
167     void mAddCustomColorButton_clicked();
168 
169     void mSampleButton_clicked();
170     void mTabWidget_currentChanged( int index );
171 
172   private slots:
173 
174     void mActionShowInButtons_toggled( bool state );
175 
176   private:
177 
178     static QScreen *findScreenAt( QPoint pos );
179 
180     bool mAllowAlpha = true;
181 
182     int mLastCustomColorIndex = 0;
183 
184     bool mPickingColor = false;
185 
186     bool mDiscarded = false;
187 
188     /**
189      * Saves all widget settings
190      */
191     void saveSettings();
192 
193     /**
194      * Ends a color picking operation
195      * \param eventPos global position of pixel to sample color from
196      * \param takeSample set to TRUE to actually sample the color, FALSE to just cancel
197      * the color picking operation
198      */
199     void stopPicking( QPoint eventPos, bool takeSample = true );
200 
201     /**
202      * Returns the average color from the pixels in an image
203      * \param image image to sample
204      * \returns average color from image
205      */
206     QColor averageColor( const QImage &image ) const;
207 
208     /**
209      * Samples a color from the desktop
210      * \param point position of color to sample
211      * \returns average color from sampled position
212      */
213     QColor sampleColor( QPoint point ) const;
214 
215     /**
216      * Repopulates the scheme combo box with current color schemes
217      */
218     void refreshSchemeComboBox();
219 
220     /**
221      * Returns the path to the user's palette folder
222      */
223     static QString gplFilePath();
224 
225     //! Updates the state of actions for the current selected scheme
226     void updateActionsForCurrentScheme();
227 };
228 
229 #endif // QGSCOMPOUNDCOLORWIDGET_H
230