QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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_sip.h"
23
31class GUI_EXPORT QgsColorSwatchGrid : public QWidget
32{
33 Q_OBJECT
34
35 public:
36
43 QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
44
45 //Reimplemented to set fixed size on widget
46 QSize minimumSizeHint() const override;
47
48 //Reimplemented to set fixed size on widget
49 QSize sizeHint() const override;
50
56 QString context() const { return mContext; }
57
63 void setContext( const QString &context );
64
70 QColor baseColor() const { return mBaseColor; }
71
77 void setBaseColor( const QColor &baseColor );
78
83 QgsNamedColorList *colors() { return &mColors; }
84
85 public slots:
86
90 void refreshColors();
91
92 signals:
93
98 void colorChanged( const QColor &color );
99
103 void hovered();
104
105 protected:
106
107 //reimplemented QWidget events
108 void paintEvent( QPaintEvent *event ) override;
109 void mouseMoveEvent( QMouseEvent *event ) override;
110 void mousePressEvent( QMouseEvent *event ) override;
111 void mouseReleaseEvent( QMouseEvent *event ) override;
112 void keyPressEvent( QKeyEvent *event ) override;
113 void focusInEvent( QFocusEvent *event ) override;
114 void focusOutEvent( QFocusEvent *event ) override;
115
116 private:
117 QgsColorScheme *mScheme = nullptr;
118 QString mContext;
119 QgsNamedColorList mColors;
120 QColor mBaseColor;
121
122 bool mDrawBoxDepressed;
123 int mCurrentHoverBox;
124
125 bool mFocused;
126 int mCurrentFocusBox;
127
128 int mWidth;
130 int mLabelHeight = 0;
132 int mLabelMargin = 0;
133
135 int mSwatchSize = 0;
137 int mSwatchOutlineSize = 0;
138
140 int mSwatchMargin = 0;
141
143 int mSwatchSpacing = 0;
144
145 bool mPressedOnWidget;
146
151 int calculateHeight() const;
152
157 void draw( QPainter &painter );
158
164 int swatchForPosition( QPoint position ) const;
165
170 void updateTooltip( int colorIdx );
171
176 QPixmap transparentBackground();
177};
178
179
187class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction
188{
189 Q_OBJECT
190
191 public:
192
200 QgsColorSwatchGridAction( QgsColorScheme *scheme, QMenu *menu = nullptr, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
201
207 void setBaseColor( const QColor &baseColor );
208
214 QColor baseColor() const;
215
221 QString context() const;
222
228 void setContext( const QString &context );
229
238 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
239
245 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
246
247 public slots:
248
252 void refreshColors();
253
254 signals:
255
260 void colorChanged( const QColor &color );
261
262 private:
263 QMenu *mMenu = nullptr;
264 QgsColorSwatchGrid *mColorSwatchGrid = nullptr;
265
266 //used to suppress recursion with hover events
267 bool mSuppressRecurse;
268 bool mDismissOnColorSelection;
269
270 private slots:
271
275 void setColor( const QColor &color );
276
280 void onHover();
281};
282
283#endif
Abstract base class for color schemes.
A color swatch grid which can be embedded into a menu.
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action'...
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action's color w...
A grid of color swatches, which allows for user selection.
QColor baseColor() const
Gets the base color for the widget.
QgsNamedColorList * colors()
Gets the list of colors shown in the grid.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
void hovered()
Emitted when mouse hovers over widget.
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.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53