QGIS API Documentation  2.99.0-Master (d55fa22)
qgscolorswatchgrid.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorswatchgrid.h
3  ------------------
4  Date : July 2014
5  Copyright : (C) 2014 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 #ifndef QGSCOLORSWATCHGRID_H
16 #define QGSCOLORSWATCHGRID_H
17 
18 #include "qgscolorscheme.h"
19 #include <QWidget>
20 #include <QWidgetAction>
21 #include "qgis_gui.h"
22 #include "qgis.h"
23 
31 class GUI_EXPORT QgsColorSwatchGrid : public QWidget
32 {
33  Q_OBJECT
34 
35  public:
36 
42  QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
43 
44  //Reimplemented to set fixed size on widget
45  virtual QSize minimumSizeHint() const override;
46 
47  //Reimplemented to set fixed size on widget
48  virtual QSize sizeHint() const override;
49 
54  QString context() const { return mContext; }
55 
60  void setContext( const QString &context );
61 
66  QColor baseColor() const { return mBaseColor; }
67 
72  void setBaseColor( const QColor &baseColor );
73 
77  QgsNamedColorList *colors() { return &mColors; }
78 
79  public slots:
80 
83  void refreshColors();
84 
85  signals:
86 
90  void colorChanged( const QColor &color );
91 
94  void hovered();
95 
96  protected:
97 
98  //reimplemented QWidget events
99  void paintEvent( QPaintEvent *event ) override;
100  void mouseMoveEvent( QMouseEvent *event ) override;
101  void mousePressEvent( QMouseEvent *event ) override;
102  void mouseReleaseEvent( QMouseEvent *event ) override;
103  void keyPressEvent( QKeyEvent *event ) override;
104  void focusInEvent( QFocusEvent *event ) override;
105  void focusOutEvent( QFocusEvent *event ) override;
106 
107  private:
108  QgsColorScheme *mScheme = nullptr;
109  QString mContext;
110  QgsNamedColorList mColors;
111  QColor mBaseColor;
112 
113  bool mDrawBoxDepressed;
114  int mCurrentHoverBox;
115 
116  bool mFocused;
117  int mCurrentFocusBox;
118 
119  int mWidth;
120 
121  bool mPressedOnWidget;
122 
126  int calculateHeight() const;
127 
131  void draw( QPainter &painter );
132 
137  int swatchForPosition( QPoint position ) const;
138 
142  void updateTooltip( const int colorIdx );
143 
147  QPixmap transparentBackground();
148 };
149 
150 
158 class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction
159 {
160  Q_OBJECT
161 
162  public:
163 
170  QgsColorSwatchGridAction( QgsColorScheme *scheme, QMenu *menu = nullptr, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
171 
176  void setBaseColor( const QColor &baseColor );
177 
182  QColor baseColor() const;
183 
188  QString context() const;
189 
194  void setContext( const QString &context );
195 
204  void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
205 
211  bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
212 
213  public slots:
214 
217  void refreshColors();
218 
219  signals:
220 
224  void colorChanged( const QColor &color );
225 
226  private:
227  QMenu *mMenu = nullptr;
228  QgsColorSwatchGrid *mColorSwatchGrid = nullptr;
229 
230  //used to suppress recursion with hover events
231  bool mSuppressRecurse;
232  bool mDismissOnColorSelection;
233 
234  private slots:
235 
238  void setColor( const QColor &color );
239 
242  void onHover();
243 };
244 
245 #endif
A color swatch grid which can be embedded into a menu.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:34
Abstract base class for color schemes.
QString context() const
Get the current context for the grid.
A grid of color swatches, which allows for user selection.
QColor baseColor() const
Get the base color for the widget.
QgsNamedColorList * colors()
Gets the list of colors shown in the grid.
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action&#39;s color w...
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action&#39;...