QGIS API Documentation  2.5.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 
136  static QList<QColor> randomColors( int count,
137  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
138  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
139  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
140  void updateColors();
141 
142  int count() const { return mCount; }
143  int hueMin() const { return mHueMin; }
144  int hueMax() const { return mHueMax; }
145  int satMin() const { return mSatMin; }
146  int satMax() const { return mSatMax; }
147  int valMin() const { return mValMin; }
148  int valMax() const { return mValMax; }
149 
150  void setCount( int val ) { mCount = val; }
151  void setHueMin( int val ) { mHueMin = val; }
152  void setHueMax( int val ) { mHueMax = val; }
153  void setSatMin( int val ) { mSatMin = val; }
154  void setSatMax( int val ) { mSatMax = val; }
155  void setValMin( int val ) { mValMin = val; }
156  void setValMax( int val ) { mValMax = val; }
157 
158  protected:
159  int mCount;
160  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
161  QList<QColor> mColors;
162 };
163 
164 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
165 {
166  public:
169 
170  int count() const;
171 
172  double value( int index ) const;
173 
174  QColor color( double value ) const;
175 
176  QString type() const;
177 
178  QgsVectorColorRampV2* clone() const;
179 
180  QgsStringMap properties() const;
181 };
182 
183 
184 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
185 #define DEFAULT_COLORBREWER_COLORS 5
186 
188 {
189  public:
191  int colors = DEFAULT_COLORBREWER_COLORS );
192 
193  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
194 
195  virtual double value( int index ) const;
196 
197  virtual QColor color( double value ) const;
198 
199  virtual QString type() const { return "colorbrewer"; }
200 
201  virtual QgsVectorColorRampV2* clone() const;
202 
203  virtual QgsStringMap properties() const;
204 
205  QString schemeName() const { return mSchemeName; }
206  virtual int count() const { return mColors; }
207  int colors() const { return mColors; }
208 
209  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; loadPalette(); }
210  void setColors( int colors ) { mColors = colors; loadPalette(); }
211 
212  static QStringList listSchemeNames();
213  static QList<int> listSchemeVariants( QString schemeName );
214 
215  protected:
216 
217  void loadPalette();
218 
219  QString mSchemeName;
220  int mColors;
221  QList<QColor> mPalette;
222 };
223 
224 
225 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
226 #define DEFAULT_CPTCITY_VARIANTNAME "05"
227 
229 {
230  public:
232  QString variantName = DEFAULT_CPTCITY_VARIANTNAME,
233  bool doLoadFile = true );
234  QgsCptCityColorRampV2( QString schemeName, QStringList variantList,
235  QString variantName = QString(), bool doLoadFile = true );
236 
237  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
238 
239  virtual QString type() const { return "cpt-city"; }
240 
241  virtual QgsVectorColorRampV2* clone() const;
242  void copy( const QgsCptCityColorRampV2* other );
243  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
244 
245  virtual QgsStringMap properties() const;
246 
247  QString schemeName() const { return mSchemeName; }
248  QString variantName() const { return mVariantName; }
249  QStringList variantList() const { return mVariantList; }
250 
251  /* lazy loading - have to call loadPalette() explicitly */
252  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
253  void setVariantName( QString variantName ) { mVariantName = variantName; mFileLoaded = false; }
254  void setVariantList( QStringList variantList ) { mVariantList = variantList; }
255  void setName( QString schemeName, QString variantName = "", QStringList variantList = QStringList() )
256  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
257 
258  void loadPalette() { loadFile(); }
259  bool hasMultiStops() const { return mMultiStops; }
260 
261  QString fileName() const;
262  bool loadFile();
263  bool fileLoaded() const { return mFileLoaded; }
264 
265  QString copyingFileName() const;
266  QString descFileName() const;
267  QgsStringMap copyingInfo() const;
268 
269  protected:
270 
271  QString mSchemeName;
272  QString mVariantName;
273  QStringList mVariantList;
276 };
277 
278 
279 #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