QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
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 friend class QgsColorTooltip;
179};
180
181
189class 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
240 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
241
247 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
248
249 public slots:
250
254 void refreshColors();
255
256 signals:
257
262 void colorChanged( const QColor &color );
263
264 private:
265 QMenu *mMenu = nullptr;
266 QgsColorSwatchGrid *mColorSwatchGrid = nullptr;
267
268 //used to suppress recursion with hover events
269 bool mSuppressRecurse;
270 bool mDismissOnColorSelection;
271
272 private slots:
273
277 void setColor( const QColor &color );
278
282 void onHover();
283};
284
285#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 to generate color tooltip
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