QGIS API Documentation  2.14.0-Essen
qgsvectorcolorrampv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorcolorrampv2.h
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk 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 
16 #ifndef QGSVECTORCOLORRAMPV2_H
17 #define QGSVECTORCOLORRAMPV2_H
18 
19 #include <QColor>
20 #include <QGradient>
21 
22 #include "qgssymbollayerv2.h" // for QgsStringMap
23 #include "qgslogger.h"
24 
25 class CORE_EXPORT QgsVectorColorRampV2
26 {
27  public:
28 
29  virtual ~QgsVectorColorRampV2() {}
30 
33  virtual int count() const = 0;
34 
37  virtual double value( int index ) const = 0;
38 
39  virtual QColor color( double value ) const = 0;
40 
41  virtual QString type() const = 0;
42 
43  virtual QgsVectorColorRampV2* clone() const = 0;
44 
45  virtual QgsStringMap properties() const = 0;
46 
47 };
48 
50 {
51  double offset; // relative (0,1)
53  QgsGradientStop( double o, const QColor& c ) : offset( o ), color( c ) { }
54 };
55 
57 
58 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
59 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
60 
62 {
63  public:
65  const QColor& color2 = DEFAULT_GRADIENT_COLOR2,
66  bool discrete = false,
67  const QgsGradientStopsList& stops = QgsGradientStopsList() );
68 
69  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
70 
71  virtual int count() const override { return mStops.count() + 2; }
72 
73  virtual double value( int index ) const override;
74 
75  virtual QColor color( double value ) const override;
76 
77  virtual QString type() const override { return "gradient"; }
78 
79  virtual QgsVectorGradientColorRampV2* clone() const override;
80 
81  virtual QgsStringMap properties() const override;
82 
83  QColor color1() const { return mColor1; }
84  QColor color2() const { return mColor2; }
85  void setColor1( const QColor& color ) { mColor1 = color; }
86  void setColor2( const QColor& color ) { mColor2 = color; }
87 
88  bool isDiscrete() const { return mDiscrete; }
89  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
90  void convertToDiscrete( bool discrete );
91 
92  void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
93  const QgsGradientStopsList& stops() const { return mStops; }
94 
95  QgsStringMap info() const { return mInfo; }
96  void setInfo( const QgsStringMap& info ) { mInfo = info; }
97 
101  void addStopsToGradient( QGradient* gradient, double alpha = 1 );
102 
103  protected:
104  QColor mColor1, mColor2;
105  bool mDiscrete;
108 };
109 
110 #define DEFAULT_RANDOM_COUNT 10
111 #define DEFAULT_RANDOM_HUE_MIN 0
112 #define DEFAULT_RANDOM_HUE_MAX 359
113 #define DEFAULT_RANDOM_VAL_MIN 200
114 #define DEFAULT_RANDOM_VAL_MAX 240
115 #define DEFAULT_RANDOM_SAT_MIN 100
116 #define DEFAULT_RANDOM_SAT_MAX 240
117 
119 {
120  public:
122  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
123  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
124  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
125 
126  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
127 
128  virtual double value( int index ) const override;
129 
130  virtual QColor color( double value ) const override;
131 
132  virtual QString type() const override { return "random"; }
133 
134  virtual QgsVectorRandomColorRampV2* clone() const override;
135 
136  virtual QgsStringMap properties() const override;
137 
141  static QList<QColor> randomColors( int count,
142  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
143  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
144  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
145 
146  void updateColors();
147 
148  int count() const override { return mCount; }
149  int hueMin() const { return mHueMin; }
150  int hueMax() const { return mHueMax; }
151  int satMin() const { return mSatMin; }
152  int satMax() const { return mSatMax; }
153  int valMin() const { return mValMin; }
154  int valMax() const { return mValMax; }
155 
156  void setCount( int val ) { mCount = val; }
157  void setHueMin( int val ) { mHueMin = val; }
158  void setHueMax( int val ) { mHueMax = val; }
159  void setSatMin( int val ) { mSatMin = val; }
160  void setSatMax( int val ) { mSatMax = val; }
161  void setValMin( int val ) { mValMin = val; }
162  void setValMax( int val ) { mValMax = val; }
163 
164  protected:
165  int mCount;
166  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
168 };
169 
170 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
171 {
172  public:
175 
176  int count() const override;
177 
178  double value( int index ) const override;
179 
180  QColor color( double value ) const override;
181 
188  virtual void setTotalColorCount( const int colorCount );
189 
190  QString type() const override;
191 
192  QgsRandomColorsV2* clone() const override;
193 
194  QgsStringMap properties() const override;
195 
196  protected:
197 
200 
201 };
202 
203 
204 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
205 #define DEFAULT_COLORBREWER_COLORS 5
206 
208 {
209  public:
211  int colors = DEFAULT_COLORBREWER_COLORS );
212 
213  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
214 
215  virtual double value( int index ) const override;
216 
217  virtual QColor color( double value ) const override;
218 
219  virtual QString type() const override { return "colorbrewer"; }
220 
221  virtual QgsVectorColorBrewerColorRampV2* clone() const override;
222 
223  virtual QgsStringMap properties() const override;
224 
225  QString schemeName() const { return mSchemeName; }
226  virtual int count() const override { return mColors; }
227  int colors() const { return mColors; }
228 
229  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; loadPalette(); }
230  void setColors( int colors ) { mColors = colors; loadPalette(); }
231 
232  static QStringList listSchemeNames();
233  static QList<int> listSchemeVariants( const QString& schemeName );
234 
235  protected:
236 
237  void loadPalette();
238 
240  int mColors;
242 };
243 
244 
245 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
246 #define DEFAULT_CPTCITY_VARIANTNAME "05"
247 
249 {
250  public:
252  const QString& variantName = DEFAULT_CPTCITY_VARIANTNAME,
253  bool doLoadFile = true );
254  QgsCptCityColorRampV2( const QString& schemeName, const QStringList& variantList,
255  const QString& variantName = QString(), bool doLoadFile = true );
256 
257  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
258 
259  virtual QString type() const override { return "cpt-city"; }
260 
261  virtual QgsCptCityColorRampV2* clone() const override;
262  void copy( const QgsCptCityColorRampV2* other );
263  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
264 
265  virtual QgsStringMap properties() const override;
266 
267  QString schemeName() const { return mSchemeName; }
268  QString variantName() const { return mVariantName; }
269  QStringList variantList() const { return mVariantList; }
270 
271  /* lazy loading - have to call loadPalette() explicitly */
272  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
273  void setVariantName( const QString& variantName ) { mVariantName = variantName; mFileLoaded = false; }
274  void setVariantList( const QStringList& variantList ) { mVariantList = variantList; }
275  void setName( const QString& schemeName, const QString& variantName = "", const QStringList& variantList = QStringList() )
276  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
277 
278  void loadPalette() { loadFile(); }
279  bool hasMultiStops() const { return mMultiStops; }
280 
281  QString fileName() const;
282  bool loadFile();
283  bool fileLoaded() const { return mFileLoaded; }
284 
285  QString copyingFileName() const;
286  QString descFileName() const;
287  QgsStringMap copyingInfo() const;
288 
289  protected:
290 
296 };
297 
298 
299 #endif
static unsigned index
int count() const override
Returns number of defined colors, or -1 if undefined.
void setName(const QString &schemeName, const QString &variantName="", const QStringList &variantList=QStringList())
#define DEFAULT_CPTCITY_VARIANTNAME
#define DEFAULT_COLORBREWER_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MIN
void setInfo(const QgsStringMap &info)
#define DEFAULT_CPTCITY_SCHEMENAME
void setColor2(const QColor &color)
virtual QString type() const override
QMap< QString, QString > QgsStringMap
Definition: qgis.h:384
#define DEFAULT_RANDOM_SAT_MAX
void setColor1(const QColor &color)
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
QString variantName() const
void setVariantName(const QString &variantName)
QStringList variantList() const
#define DEFAULT_GRADIENT_COLOR2
void setSchemeName(const QString &schemeName)
virtual QString type() const override
#define DEFAULT_GRADIENT_COLOR1
void setStops(const QgsGradientStopsList &stops)
#define DEFAULT_RANDOM_VAL_MAX
virtual QString type() const override
#define DEFAULT_RANDOM_SAT_MIN
QList< QgsGradientStop > QgsGradientStopsList
QgsGradientStop(double o, const QColor &c)
void setSchemeName(const QString &schemeName)
virtual QString type() const override
#define DEFAULT_COLORBREWER_COLORS
const QgsGradientStopsList & stops() const
QList< QColor > mPrecalculatedColors
#define DEFAULT_RANDOM_HUE_MAX
#define DEFAULT_RANDOM_VAL_MIN
void setVariantList(const QStringList &variantList)
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
#define DEFAULT_RANDOM_COUNT