QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
32 class GUI_EXPORT QgsColorSwatchGrid : public QWidget
33 {
34  Q_OBJECT
35 
36  public:
37 
44  QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
45 
46  //Reimplemented to set fixed size on widget
47  QSize minimumSizeHint() const override;
48 
49  //Reimplemented to set fixed size on widget
50  QSize sizeHint() const override;
51 
57  QString context() const { return mContext; }
58 
64  void setContext( const QString &context );
65 
71  QColor baseColor() const { return mBaseColor; }
72 
78  void setBaseColor( const QColor &baseColor );
79 
84  QgsNamedColorList *colors() { return &mColors; }
85 
86  public slots:
87 
91  void refreshColors();
92 
93  signals:
94 
99  void colorChanged( const QColor &color );
100 
104  void hovered();
105 
106  protected:
107 
108  //reimplemented QWidget events
109  void paintEvent( QPaintEvent *event ) override;
110  void mouseMoveEvent( QMouseEvent *event ) override;
111  void mousePressEvent( QMouseEvent *event ) override;
112  void mouseReleaseEvent( QMouseEvent *event ) override;
113  void keyPressEvent( QKeyEvent *event ) override;
114  void focusInEvent( QFocusEvent *event ) override;
115  void focusOutEvent( QFocusEvent *event ) override;
116 
117  private:
118  QgsColorScheme *mScheme = nullptr;
119  QString mContext;
120  QgsNamedColorList mColors;
121  QColor mBaseColor;
122 
123  bool mDrawBoxDepressed;
124  int mCurrentHoverBox;
125 
126  bool mFocused;
127  int mCurrentFocusBox;
128 
129  int mWidth;
131  int mLabelHeight = 0;
133  int mLabelMargin = 0;
134 
136  int mSwatchSize = 0;
138  int mSwatchOutlineSize = 0;
139 
141  int mSwatchMargin = 0;
142 
144  int mSwatchSpacing = 0;
145 
146  bool mPressedOnWidget;
147 
152  int calculateHeight() const;
153 
158  void draw( QPainter &painter );
159 
165  int swatchForPosition( QPoint position ) const;
166 
171  void updateTooltip( int colorIdx );
172 
177  QPixmap transparentBackground();
178 };
179 
180 
189 class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction
190 {
191  Q_OBJECT
192 
193  public:
194 
202  QgsColorSwatchGridAction( QgsColorScheme *scheme, QMenu *menu = nullptr, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
203 
209  void setBaseColor( const QColor &baseColor );
210 
216  QColor baseColor() const;
217 
223  QString context() const;
224 
230  void setContext( const QString &context );
231 
241  void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
242 
249  bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
250 
251  public slots:
252 
256  void refreshColors();
257 
258  signals:
259 
264  void colorChanged( const QColor &color );
265 
266  private:
267  QMenu *mMenu = nullptr;
268  QgsColorSwatchGrid *mColorSwatchGrid = nullptr;
269 
270  //used to suppress recursion with hover events
271  bool mSuppressRecurse;
272  bool mDismissOnColorSelection;
273 
274  private slots:
275 
279  void setColor( const QColor &color );
280 
284  void onHover();
285 };
286 
287 #endif
A color swatch grid which can be embedded into a menu.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Abstract base class for color schemes.
A grid of color swatches, which allows for user selection.
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action&#39;s color w...
QgsNamedColorList * colors()
Gets the list of colors shown in the grid.
QString context() const
Gets the current context for the grid.
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;...
QColor baseColor() const
Gets the base color for the widget.