QGIS API Documentation 3.43.0-Master (32433f7016e)
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
32class GUI_EXPORT QgsColorSwatchGrid : public QWidget
33{
34 Q_OBJECT
35
36 public:
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 //reimplemented QWidget events
107 void paintEvent( QPaintEvent *event ) override;
108 void mouseMoveEvent( QMouseEvent *event ) override;
109 void mousePressEvent( QMouseEvent *event ) override;
110 void mouseReleaseEvent( QMouseEvent *event ) override;
111 void keyPressEvent( QKeyEvent *event ) override;
112 void focusInEvent( QFocusEvent *event ) override;
113 void focusOutEvent( QFocusEvent *event ) override;
114
115 private:
116 QgsColorScheme *mScheme = nullptr;
117 QString mContext;
118 QgsNamedColorList mColors;
119 QColor mBaseColor;
120
121 bool mDrawBoxDepressed;
122 int mCurrentHoverBox;
123
124 bool mFocused;
125 int mCurrentFocusBox;
126
127 int mWidth;
129 int mLabelHeight = 0;
131 int mLabelMargin = 0;
132
134 int mSwatchSize = 0;
136 int mSwatchOutlineSize = 0;
137
139 int mSwatchMargin = 0;
140
142 int mSwatchSpacing = 0;
143
144 bool mPressedOnWidget;
145
150 int calculateHeight() const;
151
156 void draw( QPainter &painter );
157
163 int swatchForPosition( QPoint position ) const;
164
169 void updateTooltip( int colorIdx );
170
175 QPixmap transparentBackground();
176
177 friend class QgsColorTooltip;
178};
179
180
188class GUI_EXPORT QgsColorSwatchGridAction : public QWidgetAction
189{
190 Q_OBJECT
191
192 public:
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.
Helper class for generating color tooltips.
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