QGIS API Documentation  2.99.0-Master (314842d)
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 
30 class GUI_EXPORT QgsColorSwatchGrid : public QWidget
31 {
32  Q_OBJECT
33 
34  public:
35 
41  QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &context = QString(), QWidget *parent = nullptr );
42 
43  //Reimplemented to set fixed size on widget
44  virtual QSize minimumSizeHint() const override;
45 
46  //Reimplemented to set fixed size on widget
47  virtual QSize sizeHint() const override;
48 
53  QString context() const { return mContext; }
54 
59  void setContext( const QString &context );
60 
65  QColor baseColor() const { return mBaseColor; }
66 
71  void setBaseColor( const QColor &baseColor );
72 
76  QgsNamedColorList *colors() { return &mColors; }
77 
78  public slots:
79 
82  void refreshColors();
83 
84  signals:
85 
89  void colorChanged( const QColor &color );
90 
93  void hovered();
94 
95  protected:
96 
97  //reimplemented QWidget events
98  void paintEvent( QPaintEvent *event ) override;
99  void mouseMoveEvent( QMouseEvent *event ) override;
100  void mousePressEvent( QMouseEvent *event ) override;
101  void mouseReleaseEvent( QMouseEvent *event ) override;
102  void keyPressEvent( QKeyEvent *event ) override;
103  void focusInEvent( QFocusEvent *event ) override;
104  void focusOutEvent( QFocusEvent *event ) override;
105 
106  private:
107  QgsColorScheme *mScheme = nullptr;
108  QString mContext;
109  QgsNamedColorList mColors;
110  QColor mBaseColor;
111 
112  bool mDrawBoxDepressed;
113  int mCurrentHoverBox;
114 
115  bool mFocused;
116  int mCurrentFocusBox;
117 
118  int mWidth;
119 
120  bool mPressedOnWidget;
121 
125  int calculateHeight() const;
126 
130  void draw( QPainter &painter );
131 
136  int swatchForPosition( QPoint position ) const;
137 
141  void updateTooltip( const int colorIdx );
142 
146  QPixmap transparentBackground();
147 };
148 
149 
157 class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction
158 {
159  Q_OBJECT
160 
161  public:
162 
169  QgsColorSwatchGridAction( QgsColorScheme *scheme, QMenu *menu = nullptr, const QString &context = QString(), QWidget *parent = nullptr );
170 
175  void setBaseColor( const QColor &baseColor );
176 
181  QColor baseColor() const;
182 
187  QString context() const;
188 
193  void setContext( const QString &context );
194 
203  void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
204 
210  bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
211 
212  public slots:
213 
216  void refreshColors();
217 
218  signals:
219 
223  void colorChanged( const QColor &color );
224 
225  private:
226  QMenu *mMenu = nullptr;
227  QgsColorSwatchGrid *mColorSwatchGrid = nullptr;
228 
229  //used to suppress recursion with hover events
230  bool mSuppressRecurse;
231  bool mDismissOnColorSelection;
232 
233  private slots:
234 
237  void setColor( const QColor &color );
238 
241  void onHover();
242 };
243 
244 #endif
A color swatch grid which can be embedded into a menu.
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;...