QGIS API Documentation  2.99.0-Master (9caa722)
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 
33 typedef QList< QPair< QColor, QString > > QgsNamedColorList;
34 
44 class CORE_EXPORT QgsColorScheme
45 {
46 
47 #ifdef SIP_RUN
49  if ( dynamic_cast<QgsUserColorScheme *>( sipCpp ) != NULL )
50  sipType = sipType_QgsUserColorScheme;
51  else if ( dynamic_cast<QgsRecentColorScheme *>( sipCpp ) != NULL )
52  sipType = sipType_QgsRecentColorScheme;
53  else if ( dynamic_cast<QgsCustomColorScheme *>( sipCpp ) != NULL )
54  sipType = sipType_QgsCustomColorScheme;
55  else if ( dynamic_cast<QgsProjectColorScheme *>( sipCpp ) != NULL )
56  sipType = sipType_QgsProjectColorScheme;
57  else if ( dynamic_cast<QgsGplColorScheme *>( sipCpp ) != NULL )
58  sipType = sipType_QgsGplColorScheme;
59  else
60  sipType = sipType_QgsColorScheme;
61  SIP_END
62 #endif
63 
64  public:
65 
69  {
70  ShowInColorDialog = 0x01,
71  ShowInColorButtonMenu = 0x02,
72  ShowInAllContexts = ShowInColorDialog | ShowInColorButtonMenu
73  };
74  Q_DECLARE_FLAGS( SchemeFlags, SchemeFlag )
75 
76 
79  QgsColorScheme() = default;
80 
81  virtual ~QgsColorScheme() = default;
82 
86  virtual QString schemeName() const = 0;
87 
91  virtual SchemeFlags flags() const { return ShowInColorDialog; }
92 
103  virtual QgsNamedColorList fetchColors( const QString &context = QString(),
104  const QColor &baseColor = QColor() ) = 0;
105 
110  virtual bool isEditable() const { return false; }
111 
119  virtual bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() );
120 
124  virtual QgsColorScheme *clone() const = 0 SIP_FACTORY;
125 };
126 
127 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsColorScheme::SchemeFlags )
128 
129 
134 class CORE_EXPORT QgsGplColorScheme : public QgsColorScheme
135 {
136  public:
137 
139 
140  virtual QgsNamedColorList fetchColors( const QString &context = QString(),
141  const QColor &baseColor = QColor() ) override;
142 
143  virtual bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
144 
145  protected:
146 
150  virtual QString gplFilePath() = 0;
151 
152 };
153 
160 class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
161 {
162  public:
163 
167  QgsUserColorScheme( const QString &filename );
168 
169  virtual QString schemeName() const override;
170 
171  virtual QgsUserColorScheme *clone() const override SIP_FACTORY;
172 
173  virtual bool isEditable() const override { return true; }
174 
175  virtual QgsColorScheme::SchemeFlags flags() const override;
176 
180  void setName( const QString &name ) { mName = name; }
181 
185  bool erase();
186 
191  void setShowSchemeInMenu( bool show );
192 
193  protected:
194 
195  QString mName;
196 
197  QString mFilename;
198 
199  virtual QString gplFilePath() override;
200 
201 };
202 
208 class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
209 {
210  public:
211 
213 
214  virtual QString schemeName() const override { return QObject::tr( "Recent colors" ); }
215 
216  virtual SchemeFlags flags() const override { return ShowInAllContexts; }
217 
218  virtual QgsNamedColorList fetchColors( const QString &context = QString(),
219  const QColor &baseColor = QColor() ) override;
220 
221  QgsRecentColorScheme *clone() const override SIP_FACTORY;
222 
228  static void addRecentColor( const QColor &color );
229 
234  static QColor lastUsedColor();
235 };
236 
242 class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme
243 {
244  public:
245 
247 
248  virtual QString schemeName() const override { return QObject::tr( "Standard colors" ); }
249 
250  virtual SchemeFlags flags() const override { return ShowInAllContexts; }
251 
252  virtual QgsNamedColorList fetchColors( const QString &context = QString(),
253  const QColor &baseColor = QColor() ) override;
254 
255  virtual bool isEditable() const override { return true; }
256 
257  virtual bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
258 
259  QgsCustomColorScheme *clone() const override SIP_FACTORY;
260 };
261 
267 class CORE_EXPORT QgsProjectColorScheme : public QgsColorScheme
268 {
269  public:
270 
274  QgsProjectColorScheme() = default;
275 
276  virtual QString schemeName() const override { return QObject::tr( "Project colors" ); }
277 
278  virtual SchemeFlags flags() const override { return ShowInAllContexts; }
279 
280  virtual QgsNamedColorList fetchColors( const QString &context = QString(),
281  const QColor &baseColor = QColor() ) override;
282 
283  virtual bool isEditable() const override { return true; }
284 
285  virtual bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
286 
287  QgsProjectColorScheme *clone() const override SIP_FACTORY;
288 };
289 
290 #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.
virtual SchemeFlags flags() const override
Returns the current flags for the color scheme.
A color scheme which contains project specific colors set through project properties dialog...
virtual QgsColorScheme * clone() const =0
Clones a color scheme.
virtual SchemeFlags flags() const override
Returns the current flags for the color scheme.
virtual SchemeFlags flags() const
Returns the current flags for the color scheme.
Abstract base class for color schemes.
virtual bool isEditable() const override
Returns whether the color scheme is editable.
Show scheme in all contexts.
virtual bool isEditable() const override
Returns whether the color scheme is editable.
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
#define SIP_FACTORY
Definition: qgis_sip.h:69
virtual QString gplFilePath()=0
Returns the file path for the associated gpl palette file.
virtual QString schemeName() const override
Gets the name for the color scheme.
A color scheme which contains the most recently used colors.
virtual SchemeFlags flags() const override
Returns the current flags for the color scheme.
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
SchemeFlag
Flags for controlling behavior of color scheme.
virtual QString schemeName() const override
Gets the name for the color scheme.
virtual QString schemeName() const override
Gets the name for the 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.
virtual bool isEditable() const override
Returns whether the color scheme is editable.