QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  virtual ~QgsVectorColorRampV2() {}
29 
30  // Number of defined colors
31  virtual int count() const = 0;
32 
33  // Relative value (0,1) of color at index
34  virtual double value( int index ) const = 0;
35 
36  virtual QColor color( double value ) const = 0;
37 
38  virtual QString type() const = 0;
39 
40  virtual QgsVectorColorRampV2* clone() const = 0;
41 
42  virtual QgsStringMap properties() const = 0;
43 
44 };
45 
47 {
48  double offset; // relative (0,1)
49  QColor color;
50  QgsGradientStop( double o, const QColor& c ) : offset( o ), color( c ) { }
51 };
52 
53 typedef QList<QgsGradientStop> QgsGradientStopsList;
54 
55 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
56 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
57 
59 {
60  public:
62  QColor color2 = DEFAULT_GRADIENT_COLOR2,
63  bool discrete = false,
65 
66  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
67 
68  virtual int count() const { return mStops.count() + 2; }
69 
70  virtual double value( int index ) const;
71 
72  virtual QColor color( double value ) const;
73 
74  virtual QString type() const { return "gradient"; }
75 
76  virtual QgsVectorColorRampV2* clone() const;
77 
78  virtual QgsStringMap properties() const;
79 
80  QColor color1() const { return mColor1; }
81  QColor color2() const { return mColor2; }
82  void setColor1( QColor color ) { mColor1 = color; }
83  void setColor2( QColor color ) { mColor2 = color; }
84 
85  bool isDiscrete() const { return mDiscrete; }
86  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
87  void convertToDiscrete( bool discrete );
88 
89  void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
90  const QgsGradientStopsList& stops() const { return mStops; }
91 
92  QgsStringMap info() const { return mInfo; }
93  void setInfo( const QgsStringMap& info ) { mInfo = info; }
94 
97  void addStopsToGradient( QGradient* gradient, double alpha = 1 );
98 
99  protected:
100  QColor mColor1, mColor2;
101  bool mDiscrete;
104 };
105 
106 #define DEFAULT_RANDOM_COUNT 10
107 #define DEFAULT_RANDOM_HUE_MIN 0
108 #define DEFAULT_RANDOM_HUE_MAX 359
109 #define DEFAULT_RANDOM_VAL_MIN 200
110 #define DEFAULT_RANDOM_VAL_MAX 240
111 #define DEFAULT_RANDOM_SAT_MIN 100
112 #define DEFAULT_RANDOM_SAT_MAX 240
113 
115 {
116  public:
118  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
119  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
120  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
121 
122  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
123 
124  virtual double value( int index ) const;
125 
126  virtual QColor color( double value ) const;
127 
128  virtual QString type() const { return "random"; }
129 
130  virtual QgsVectorColorRampV2* clone() const;
131 
132  virtual QgsStringMap properties() const;
133 
134  void updateColors();
135 
136  int count() const { return mCount; }
137  int hueMin() const { return mHueMin; }
138  int hueMax() const { return mHueMax; }
139  int satMin() const { return mSatMin; }
140  int satMax() const { return mSatMax; }
141  int valMin() const { return mValMin; }
142  int valMax() const { return mValMax; }
143 
144  void setCount( int val ) { mCount = val; }
145  void setHueMin( int val ) { mHueMin = val; }
146  void setHueMax( int val ) { mHueMax = val; }
147  void setSatMin( int val ) { mSatMin = val; }
148  void setSatMax( int val ) { mSatMax = val; }
149  void setValMin( int val ) { mValMin = val; }
150  void setValMax( int val ) { mValMax = val; }
151 
152  protected:
153  int mCount;
154  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
155  QList<QColor> mColors;
156 };
157 
158 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
159 {
160  public:
163 
164  int count() const;
165 
166  double value( int index ) const;
167 
168  QColor color( double value ) const;
169 
170  QString type() const;
171 
172  QgsVectorColorRampV2* clone() const;
173 
174  QgsStringMap properties() const;
175 };
176 
177 
178 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
179 #define DEFAULT_COLORBREWER_COLORS 5
180 
182 {
183  public:
185  int colors = DEFAULT_COLORBREWER_COLORS );
186 
187  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
188 
189  virtual double value( int index ) const;
190 
191  virtual QColor color( double value ) const;
192 
193  virtual QString type() const { return "colorbrewer"; }
194 
195  virtual QgsVectorColorRampV2* clone() const;
196 
197  virtual QgsStringMap properties() const;
198 
199  QString schemeName() const { return mSchemeName; }
200  virtual int count() const { return mColors; }
201  int colors() const { return mColors; }
202 
203  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; loadPalette(); }
204  void setColors( int colors ) { mColors = colors; loadPalette(); }
205 
206  static QStringList listSchemeNames();
207  static QList<int> listSchemeVariants( QString schemeName );
208 
209  protected:
210 
211  void loadPalette();
212 
213  QString mSchemeName;
214  int mColors;
215  QList<QColor> mPalette;
216 };
217 
218 
219 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
220 #define DEFAULT_CPTCITY_VARIANTNAME "05"
221 
223 {
224  public:
226  QString variantName = DEFAULT_CPTCITY_VARIANTNAME,
227  bool doLoadFile = true );
228  QgsCptCityColorRampV2( QString schemeName, QStringList variantList,
229  QString variantName = QString(), bool doLoadFile = true );
230 
231  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
232 
233  virtual QString type() const { return "cpt-city"; }
234 
235  virtual QgsVectorColorRampV2* clone() const;
236  void copy( const QgsCptCityColorRampV2* other );
237  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
238 
239  virtual QgsStringMap properties() const;
240 
241  QString schemeName() const { return mSchemeName; }
242  QString variantName() const { return mVariantName; }
243  QStringList variantList() const { return mVariantList; }
244 
245  /* lazy loading - have to call loadPalette() explicitly */
246  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
247  void setVariantName( QString variantName ) { mVariantName = variantName; mFileLoaded = false; }
248  void setVariantList( QStringList variantList ) { mVariantList = variantList; }
249  void setName( QString schemeName, QString variantName = "", QStringList variantList = QStringList() )
250  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
251 
252  void loadPalette() { loadFile(); }
253  bool hasMultiStops() const { return mMultiStops; }
254 
255  QString fileName() const;
256  bool loadFile();
257  bool fileLoaded() const { return mFileLoaded; }
258 
259  QString copyingFileName() const;
260  QString descFileName() const;
261  QgsStringMap copyingInfo() const;
262 
263  protected:
264 
265  QString mSchemeName;
266  QString mVariantName;
267  QStringList mVariantList;
270 };
271 
272 
273 #endif
static unsigned index
void setName(QString schemeName, QString variantName="", QStringList variantList=QStringList())
virtual QString type() const =0
#define DEFAULT_CPTCITY_VARIANTNAME
#define DEFAULT_COLORBREWER_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MIN
void setInfo(const QgsStringMap &info)
virtual QgsStringMap properties() const =0
#define DEFAULT_CPTCITY_SCHEMENAME
QMap< QString, QString > QgsStringMap
Definition: qgis.h:416
#define DEFAULT_RANDOM_SAT_MAX
QString variantName() const
void setSchemeName(QString schemeName)
QStringList variantList() const
#define DEFAULT_GRADIENT_COLOR2
virtual double value(int index) const =0
virtual QgsVectorColorRampV2 * clone() const =0
void setVariantName(QString variantName)
#define DEFAULT_GRADIENT_COLOR1
virtual QString type() const
void setStops(const QgsGradientStopsList &stops)
#define DEFAULT_RANDOM_VAL_MAX
virtual QgsStringMap properties() const
virtual QColor color(double value) const =0
#define DEFAULT_RANDOM_SAT_MIN
static QgsVectorColorRampV2 * create(const QgsStringMap &properties=QgsStringMap())
virtual int count() const =0
QList< QgsGradientStop > QgsGradientStopsList
QgsGradientStop(double o, const QColor &c)
#define DEFAULT_COLORBREWER_COLORS
const QgsGradientStopsList & stops() const
void setVariantList(QStringList variantList)
#define DEFAULT_RANDOM_HUE_MAX
#define DEFAULT_RANDOM_VAL_MIN
virtual QString type() const
virtual QString type() const
void setSchemeName(QString schemeName)
#define DEFAULT_RANDOM_COUNT
virtual QgsVectorColorRampV2 * clone() const