QGIS API Documentation 3.41.0-Master (af5edcb665c)
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:
42 QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
43
44 //Reimplemented to set fixed size on widget
45 QSize minimumSizeHint() const override;
46
47 //Reimplemented to set fixed size on widget
48 QSize sizeHint() const override;
49
55 QString context() const { return mContext; }
56
62 void setContext( const QString &context );
63
69 QColor baseColor() const { return mBaseColor; }
70
76 void setBaseColor( const QColor &baseColor );
77
82 QgsNamedColorList *colors() { return &mColors; }
83
84 public slots:
85
89 void refreshColors();
90
91 signals:
92
97 void colorChanged( const QColor &color );
98
102 void hovered();
103
104 protected:
105 //reimplemented QWidget events
106 void paintEvent( QPaintEvent *event ) override;
107 void mouseMoveEvent( QMouseEvent *event ) override;
108 void mousePressEvent( QMouseEvent *event ) override;
109 void mouseReleaseEvent( QMouseEvent *event ) override;
110 void keyPressEvent( QKeyEvent *event ) override;
111 void focusInEvent( QFocusEvent *event ) override;
112 void focusOutEvent( QFocusEvent *event ) override;
113
114 private:
115 QgsColorScheme *mScheme = nullptr;
116 QString mContext;
117 QgsNamedColorList mColors;
118 QColor mBaseColor;
119
120 bool mDrawBoxDepressed;
121 int mCurrentHoverBox;
122
123 bool mFocused;
124 int mCurrentFocusBox;
125
126 int mWidth;
128 int mLabelHeight = 0;
130 int mLabelMargin = 0;
131
133 int mSwatchSize = 0;
135 int mSwatchOutlineSize = 0;
136
138 int mSwatchMargin = 0;
139
141 int mSwatchSpacing = 0;
142
143 bool mPressedOnWidget;
144
149 int calculateHeight() const;
150
155 void draw( QPainter &painter );
156
162 int swatchForPosition( QPoint position ) const;
163
168 void updateTooltip( int colorIdx );
169
174 QPixmap transparentBackground();
175
176 friend class QgsColorTooltip;
177};
178
179
187class GUI_EXPORT QgsColorSwatchGridAction : public QWidgetAction
188{
189 Q_OBJECT
190
191 public:
199 QgsColorSwatchGridAction( QgsColorScheme *scheme, QMenu *menu = nullptr, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
200
206 void setBaseColor( const QColor &baseColor );
207
213 QColor baseColor() const;
214
220 QString context() const;
221
227 void setContext( const QString &context );
228
237 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
238
244 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
245
246 public slots:
247
251 void refreshColors();
252
253 signals:
254
259 void colorChanged( const QColor &color );
260
261 private:
262 QMenu *mMenu = nullptr;
263 QgsColorSwatchGrid *mColorSwatchGrid = nullptr;
264
265 //used to suppress recursion with hover events
266 bool mSuppressRecurse;
267 bool mDismissOnColorSelection;
268
269 private slots:
270
274 void setColor( const QColor &color );
275
279 void onHover();
280};
281
282#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