QGIS API Documentation  2.99.0-Master (6c64c5a)
qgscolorscheme.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorscheme.h
3  -------------------
4  begin : July 2014
5  copyright : (C) 2014 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSCOLORSCHEME_H
19 #define QGSCOLORSCHEME_H
20 
21 #include <QString>
22 #include <QColor>
23 #include <QPair>
24 #include <QObject>
25 
26 #include "qgis_core.h"
27 #include "qgis.h"
28 
34 typedef QList< QPair< QColor, QString > > QgsNamedColorList;
35 
46 class CORE_EXPORT QgsColorScheme
47 {
48 
49 #ifdef SIP_RUN
51  if ( dynamic_cast<QgsUserColorScheme *>( sipCpp ) != NULL )
52  sipType = sipType_QgsUserColorScheme;
53  else if ( dynamic_cast<QgsRecentColorScheme *>( sipCpp ) != NULL )
54  sipType = sipType_QgsRecentColorScheme;
55  else if ( dynamic_cast<QgsCustomColorScheme *>( sipCpp ) != NULL )
56  sipType = sipType_QgsCustomColorScheme;
57  else if ( dynamic_cast<QgsProjectColorScheme *>( sipCpp ) != NULL )
58  sipType = sipType_QgsProjectColorScheme;
59  else if ( dynamic_cast<QgsGplColorScheme *>( sipCpp ) != NULL )
60  sipType = sipType_QgsGplColorScheme;
61  else
62  sipType = sipType_QgsColorScheme;
63  SIP_END
64 #endif
65 
66  public:
67 
72  {
73  ShowInColorDialog = 0x01,
74  ShowInColorButtonMenu = 0x02,
75  ShowInAllContexts = ShowInColorDialog | ShowInColorButtonMenu
76  };
77  Q_DECLARE_FLAGS( SchemeFlags, SchemeFlag )
78 
79 
82  QgsColorScheme() = default;
83 
84  virtual ~QgsColorScheme() = default;
85 
90  virtual QString schemeName() const = 0;
91 
96  virtual SchemeFlags flags() const { return ShowInColorDialog; }
97 
109  virtual QgsNamedColorList fetchColors( const QString &context = QString(),
110  const QColor &baseColor = QColor() ) = 0;
111 
117  virtual bool isEditable() const { return false; }
118 
127  virtual bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() );
128 
133  virtual QgsColorScheme *clone() const = 0 SIP_FACTORY;
134 };
135 
136 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsColorScheme::SchemeFlags )
137 
138 
144 class CORE_EXPORT QgsGplColorScheme : public QgsColorScheme
145 {
146  public:
147 
151  QgsGplColorScheme() = default;
152 
153  QgsNamedColorList fetchColors( const QString &context = QString(),
154  const QColor &baseColor = QColor() ) override;
155 
156  bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
157 
158  protected:
159 
164  virtual QString gplFilePath() = 0;
165 
166 };
167 
175 class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
176 {
177  public:
178 
183  QgsUserColorScheme( const QString &filename );
184 
185  QString schemeName() const override;
186 
187  QgsUserColorScheme *clone() const override SIP_FACTORY;
188 
189  bool isEditable() const override { return true; }
190 
191  QgsColorScheme::SchemeFlags flags() const override;
192 
197  void setName( const QString &name ) { mName = name; }
198 
203  bool erase();
204 
210  void setShowSchemeInMenu( bool show );
211 
212  protected:
213 
214  QString mName;
215 
216  QString mFilename;
217 
218  QString gplFilePath() override;
219 
220 };
221 
228 class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
229 {
230  public:
231 
235  QgsRecentColorScheme() = default;
236 
237  QString schemeName() const override { return QObject::tr( "Recent colors" ); }
238 
239  SchemeFlags flags() const override { return ShowInAllContexts; }
240 
241  QgsNamedColorList fetchColors( const QString &context = QString(),
242  const QColor &baseColor = QColor() ) override;
243 
244  QgsRecentColorScheme *clone() const override SIP_FACTORY;
245 
252  static void addRecentColor( const QColor &color );
253 
259  static QColor lastUsedColor();
260 };
261 
268 class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme
269 {
270  public:
271 
275  QgsCustomColorScheme() = default;
276 
277  QString schemeName() const override { return QObject::tr( "Standard colors" ); }
278 
279  SchemeFlags flags() const override { return ShowInAllContexts; }
280 
281  QgsNamedColorList fetchColors( const QString &context = QString(),
282  const QColor &baseColor = QColor() ) override;
283 
284  bool isEditable() const override { return true; }
285 
286  bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
287 
288  QgsCustomColorScheme *clone() const override SIP_FACTORY;
289 };
290 
297 class CORE_EXPORT QgsProjectColorScheme : public QgsColorScheme
298 {
299  public:
300 
304  QgsProjectColorScheme() = default;
305 
306  QString schemeName() const override { return QObject::tr( "Project colors" ); }
307 
308  SchemeFlags flags() const override { return ShowInAllContexts; }
309 
310  QgsNamedColorList fetchColors( const QString &context = QString(),
311  const QColor &baseColor = QColor() ) override;
312 
313  bool isEditable() const override { return true; }
314 
315  bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
316 
317  QgsProjectColorScheme *clone() const override SIP_FACTORY;
318 };
319 
320 #endif
A color scheme which contains custom colors set through QGIS app options dialog.
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...
void setName(const QString &name)
Sets the name for the scheme.
bool isEditable() const override
Returns whether the color scheme is editable.
A color scheme which contains project specific colors set through project properties dialog...
QString schemeName() const override
Gets the name for the color scheme.
bool isEditable() const override
Returns whether the color scheme is editable.
virtual QgsColorScheme * clone() const =0
Clones a color scheme.
virtual SchemeFlags flags() const
Returns the current flags for the color scheme.
Abstract base class for color schemes.
SchemeFlags flags() const override
Returns the current flags for the color scheme.
Show scheme in all contexts.
QString schemeName() const override
Gets the name for the color scheme.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
#define SIP_END
Definition: qgis_sip.h:175
SchemeFlags flags() const override
Returns the current flags for the color scheme.
#define SIP_FACTORY
Definition: qgis_sip.h:69
virtual QString gplFilePath()=0
Returns the file path for the associated gpl palette file.
bool isEditable() const override
Returns whether the color scheme is editable.
A color scheme which contains the most recently used colors.
virtual bool isEditable() const
Returns whether the color scheme is editable.
virtual QString schemeName() const =0
Gets the name for the color scheme.
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
SchemeFlags flags() const override
Returns the current flags for the color scheme.
QString schemeName() const override
Gets the name for the color scheme.
SchemeFlag
Flags for controlling behavior of color scheme.
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the colors for the scheme.
A color scheme which stores its colors in a gpl palette file.