QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "qgis_gui.h"
19#include "qgis_sip.h"
20#include "qgscolorscheme.h"
21
22#include <QWidget>
23#include <QWidgetAction>
24
33class GUI_EXPORT QgsColorSwatchGrid : public QWidget
34{
35 Q_OBJECT
36
37 public:
44 QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
45
46 //Reimplemented to set fixed size on widget
47 QSize minimumSizeHint() const override;
48
49 //Reimplemented to set fixed size on widget
50 QSize sizeHint() const override;
51
57 QString context() const { return mContext; }
58
64 void setContext( const QString &context );
65
71 QColor baseColor() const { return mBaseColor; }
72
78 void setBaseColor( const QColor &baseColor );
79
84 QgsNamedColorList *colors() { return &mColors; }
85
86 public slots:
87
91 void refreshColors();
92
93 signals:
94
99 void colorChanged( const QColor &color );
100
104 void hovered();
105
106 protected:
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 = false;
123 int mCurrentHoverBox = -1;
124
125 bool mFocused = false;
126 int mCurrentFocusBox = 0;
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 = false;
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
188
189class GUI_EXPORT QgsColorSwatchGridAction : public QWidgetAction
190{
191 Q_OBJECT
192
193 public:
201 QgsColorSwatchGridAction( QgsColorScheme *scheme, QMenu *menu = nullptr, const QString &context = QString(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
202
208 void setBaseColor( const QColor &baseColor );
209
215 QColor baseColor() const;
216
222 QString context() const;
223
229 void setContext( const QString &context );
230
239 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
240
246 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
247
248 public slots:
249
253 void refreshColors();
254
255 signals:
256
261 void colorChanged( const QColor &color );
262
263 private:
264 QMenu *mMenu = nullptr;
265 QgsColorSwatchGrid *mColorSwatchGrid = nullptr;
266
267 //used to suppress recursion with hover events
268 bool mSuppressRecurse = false;
269 bool mDismissOnColorSelection = true;
270
271 private slots:
272
276 void setColor( const QColor &color );
277
281 void onHover();
282};
283
284#endif
Abstract base class for color schemes.
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action'...
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
QString context() const
Gets the current context for the color grid.
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...
QgsColorSwatchGridAction(QgsColorScheme *scheme, QMenu *menu=nullptr, const QString &context=QString(), QWidget *parent=nullptr)
Construct a new color swatch grid action.
QColor baseColor() const
Gets the base color for the color grid.
void setContext(const QString &context)
Sets the current context for the color grid.
A grid of color swatches, which allows for user selection.
void mouseMoveEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
QColor baseColor() const
Gets the base color for the widget.
QgsColorSwatchGrid(QgsColorScheme *scheme, const QString &context=QString(), QWidget *parent=nullptr)
Construct a new color swatch grid.
void paintEvent(QPaintEvent *event) override
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.
QSize minimumSizeHint() const override
void mouseReleaseEvent(QMouseEvent *event) override
QSize sizeHint() const override
void focusInEvent(QFocusEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void focusOutEvent(QFocusEvent *event) override
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