QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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_sip.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 mEditable; }
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  bool mEditable = false;
219 
220  QString gplFilePath() override;
221 
222 };
223 
230 class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
231 {
232  public:
233 
237  QgsRecentColorScheme() = default;
238 
239  QString schemeName() const override { return QObject::tr( "Recent colors" ); }
240 
241  SchemeFlags flags() const override { return ShowInAllContexts; }
242 
243  QgsNamedColorList fetchColors( const QString &context = QString(),
244  const QColor &baseColor = QColor() ) override;
245 
246  QgsRecentColorScheme *clone() const override SIP_FACTORY;
247 
254  static void addRecentColor( const QColor &color );
255 
261  static QColor lastUsedColor();
262 };
263 
270 class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme
271 {
272  public:
273 
277  QgsCustomColorScheme() = default;
278 
279  QString schemeName() const override { return QObject::tr( "Standard colors" ); }
280 
281  SchemeFlags flags() const override { return ShowInAllContexts; }
282 
283  QgsNamedColorList fetchColors( const QString &context = QString(),
284  const QColor &baseColor = QColor() ) override;
285 
286  bool isEditable() const override { return true; }
287 
288  bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
289 
290  QgsCustomColorScheme *clone() const override SIP_FACTORY;
291 };
292 
299 class CORE_EXPORT QgsProjectColorScheme : public QgsColorScheme
300 {
301  public:
302 
306  QgsProjectColorScheme() = default;
307 
308  QString schemeName() const override { return QObject::tr( "Project colors" ); }
309 
310  SchemeFlags flags() const override { return ShowInAllContexts; }
311 
312  QgsNamedColorList fetchColors( const QString &context = QString(),
313  const QColor &baseColor = QColor() ) override;
314 
315  bool isEditable() const override { return true; }
316 
317  bool setColors( const QgsNamedColorList &colors, const QString &context = QString(), const QColor &baseColor = QColor() ) override;
318 
319  QgsProjectColorScheme *clone() const override SIP_FACTORY;
320 };
321 
322 #endif
QgsColorScheme::flags
virtual SchemeFlags flags() const
Returns the current flags for the color scheme.
Definition: qgscolorscheme.h:96
QgsColorScheme::clone
virtual QgsColorScheme * clone() const =0
Clones a color scheme.
QgsUserColorScheme::isEditable
bool isEditable() const override
Returns whether the color scheme is editable.
Definition: qgscolorscheme.h:189
QgsUserColorScheme
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...
Definition: qgscolorscheme.h:175
QgsNamedColorList
QList< QPair< QColor, QString > > QgsNamedColorList
Definition: qgscolorscheme.h:34
QgsUserColorScheme::mName
QString mName
Definition: qgscolorscheme.h:214
QgsProjectColorScheme
A color scheme which contains project specific colors set through project properties dialog.
Definition: qgscolorscheme.h:299
QgsCustomColorScheme::schemeName
QString schemeName() const override
Gets the name for the color scheme.
Definition: qgscolorscheme.h:279
QgsColorScheme::ShowInAllContexts
@ ShowInAllContexts
Show scheme in all contexts.
Definition: qgscolorscheme.h:75
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsUserColorScheme::mFilename
QString mFilename
Definition: qgscolorscheme.h:216
QgsProjectColorScheme::isEditable
bool isEditable() const override
Returns whether the color scheme is editable.
Definition: qgscolorscheme.h:315
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsRecentColorScheme::flags
SchemeFlags flags() const override
Returns the current flags for the color scheme.
Definition: qgscolorscheme.h:241
QgsGplColorScheme::gplFilePath
virtual QString gplFilePath()=0
Returns the file path for the associated gpl palette file.
QgsUserColorScheme::setName
void setName(const QString &name)
Sets the name for the scheme.
Definition: qgscolorscheme.h:197
qgis_sip.h
QgsProjectColorScheme::schemeName
QString schemeName() const override
Gets the name for the color scheme.
Definition: qgscolorscheme.h:308
QgsCustomColorScheme::flags
SchemeFlags flags() const override
Returns the current flags for the color scheme.
Definition: qgscolorscheme.h:281
QgsCustomColorScheme::isEditable
bool isEditable() const override
Returns whether the color scheme is editable.
Definition: qgscolorscheme.h:286
QgsRecentColorScheme::schemeName
QString schemeName() const override
Gets the name for the color scheme.
Definition: qgscolorscheme.h:239
QgsGplColorScheme
A color scheme which stores its colors in a gpl palette file.
Definition: qgscolorscheme.h:144
QgsColorScheme::schemeName
virtual QString schemeName() const =0
Gets the name for the color scheme.
QgsColorScheme::fetchColors
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
QgsColorScheme
Abstract base class for color schemes.
Definition: qgscolorscheme.h:46
QgsCustomColorScheme
A color scheme which contains custom colors set through QGIS app options dialog.
Definition: qgscolorscheme.h:270
QgsColorScheme::SchemeFlag
SchemeFlag
Flags for controlling behavior of color scheme.
Definition: qgscolorscheme.h:71
QgsRecentColorScheme
A color scheme which contains the most recently used colors.
Definition: qgscolorscheme.h:230
QgsProjectColorScheme::flags
SchemeFlags flags() const override
Returns the current flags for the color scheme.
Definition: qgscolorscheme.h:310
QgsColorScheme::isEditable
virtual bool isEditable() const
Returns whether the color scheme is editable.
Definition: qgscolorscheme.h:117
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
QgsColorScheme::setColors
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the colors for the scheme.
Definition: qgscolorscheme.cpp:28