QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
29 class GUI_EXPORT QgsColorSwatchGrid : public QWidget
30 {
31  Q_OBJECT
32 
33  public:
34 
40  QgsColorSwatchGrid( QgsColorScheme* scheme, const QString& context = QString(), QWidget *parent = nullptr );
41 
42  virtual ~QgsColorSwatchGrid();
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;
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  const 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 = nullptr );
171 
172  virtual ~QgsColorSwatchGridAction();
173 
178  void setBaseColor( const QColor &baseColor );
179 
184  QColor baseColor() const;
185 
190  QString context() const;
191 
196  void setContext( const QString &context );
197 
206  void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
207 
213  bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
214 
215  public slots:
216 
219  void refreshColors();
220 
221  signals:
222 
226  void colorChanged( const QColor &color );
227 
228  private:
229  QMenu* mMenu;
230  QgsColorSwatchGrid* mColorSwatchGrid;
231 
232  //used to suppress recursion with hover events
233  bool mSuppressRecurse;
234  bool mDismissOnColorSelection;
235 
236  private slots:
237 
240  void setColor( const QColor &color );
241 
244  void onHover();
245 };
246 
247 #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.
virtual QSize minimumSizeHint() const
A grid of color swatches, which allows for user selection.
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual void focusOutEvent(QFocusEvent *event)
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...
virtual void paintEvent(QPaintEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void focusInEvent(QFocusEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action&#39;...
virtual QSize sizeHint() const
virtual void keyPressEvent(QKeyEvent *event)