QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
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
Abstract base class for color schemes.
SchemeFlag
Flags for controlling behavior of color scheme.
virtual QgsColorScheme * clone() const =0
Clones a color scheme.
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
virtual bool isEditable() const
Returns whether the color scheme is editable.
A color scheme which contains custom colors set through QGIS app options dialog.
SchemeFlags flags() const override
Returns the current flags for the color scheme.
bool isEditable() const override
Returns whether the color scheme is editable.
QString schemeName() const override
Gets the name for the color scheme.
QgsCustomColorScheme()=default
Constructor for QgsCustomColorScheme.
A color scheme which stores its colors in a gpl palette file.
virtual QString gplFilePath()=0
Returns the file path for the associated gpl palette file.
QgsGplColorScheme()=default
Constructor for QgsGplColorScheme.
A color scheme which contains project specific colors set through project properties dialog.
SchemeFlags flags() const override
Returns the current flags for the color scheme.
QgsProjectColorScheme()=default
Constructor for QgsProjectColorScheme.
bool isEditable() const override
Returns whether the color scheme is editable.
QString schemeName() const override
Gets the name for the color scheme.
A color scheme which contains the most recently used colors.
QgsRecentColorScheme()=default
Constructor for QgsRecentColorScheme.
QString schemeName() const override
Gets the name for the color scheme.
SchemeFlags flags() const override
Returns the current flags for the color scheme.
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.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.