QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
Definition: mersenne-twister.cpp:36
void setName(QString schemeName, QString variantName="", QStringList variantList=QStringList())
Definition: qgsvectorcolorrampv2.h:255
Definition: qgsvectorcolorrampv2.h:228
int valMax() const
Definition: qgsvectorcolorrampv2.h:148
virtual QString type() const =0
#define DEFAULT_CPTCITY_VARIANTNAME
Definition: qgsvectorcolorrampv2.h:226
void setHueMin(int val)
Definition: qgsvectorcolorrampv2.h:151
#define DEFAULT_COLORBREWER_SCHEMENAME
Definition: qgsvectorcolorrampv2.h:184
#define DEFAULT_RANDOM_HUE_MIN
Definition: qgsvectorcolorrampv2.h:107
void setInfo(const QgsStringMap &info)
Definition: qgsvectorcolorrampv2.h:93
virtual QgsStringMap properties() const =0
QString mSchemeName
Definition: qgsvectorcolorrampv2.h:219
#define DEFAULT_CPTCITY_SCHEMENAME
Definition: qgsvectorcolorrampv2.h:225
bool mFileLoaded
Definition: qgsvectorcolorrampv2.h:274
bool mMultiStops
Definition: qgsvectorcolorrampv2.h:275
int hueMin() const
Definition: qgsvectorcolorrampv2.h:143
QMap< QString, QString > QgsStringMap
Definition: qgis.h:416
#define DEFAULT_RANDOM_SAT_MAX
Definition: qgsvectorcolorrampv2.h:112
Definition: qgsvectorcolorrampv2.h:187
void setSatMin(int val)
Definition: qgsvectorcolorrampv2.h:153
virtual int count() const
Definition: qgsvectorcolorrampv2.h:206
QList< QColor > mPalette
Definition: qgsvectorcolorrampv2.h:221
QgsStringMap info() const
Definition: qgsvectorcolorrampv2.h:92
bool hasMultiStops() const
Definition: qgsvectorcolorrampv2.h:259
void setValMax(int val)
Definition: qgsvectorcolorrampv2.h:156
int colors() const
Definition: qgsvectorcolorrampv2.h:207
Definition: qgsvectorcolorrampv2.h:46
QString variantName() const
Definition: qgsvectorcolorrampv2.h:248
void setColors(int colors)
Definition: qgsvectorcolorrampv2.h:210
void setSchemeName(QString schemeName)
Definition: qgsvectorcolorrampv2.h:209
QStringList variantList() const
Definition: qgsvectorcolorrampv2.h:249
#define DEFAULT_GRADIENT_COLOR2
Definition: qgsvectorcolorrampv2.h:56
void setCount(int val)
Definition: qgsvectorcolorrampv2.h:150
virtual double value(int index) const =0
virtual QgsVectorColorRampV2 * clone() const =0
virtual int count() const
Definition: qgsvectorcolorrampv2.h:68
void setVariantName(QString variantName)
Definition: qgsvectorcolorrampv2.h:253
#define DEFAULT_GRADIENT_COLOR1
Definition: qgsvectorcolorrampv2.h:55
QList< QColor > mColors
Definition: qgsvectorcolorrampv2.h:161
virtual QString type() const
Definition: qgsvectorcolorrampv2.h:74
QColor mColor2
Definition: qgsvectorcolorrampv2.h:100
void setStops(const QgsGradientStopsList &stops)
Definition: qgsvectorcolorrampv2.h:89
#define DEFAULT_RANDOM_VAL_MAX
Definition: qgsvectorcolorrampv2.h:110
QString mVariantName
Definition: qgsvectorcolorrampv2.h:272
virtual QString type() const
Definition: qgsvectorcolorrampv2.h:199
void setColor1(QColor color)
Definition: qgsvectorcolorrampv2.h:82
int mValMin
Definition: qgsvectorcolorrampv2.h:160
virtual QgsStringMap properties() const
Definition: qgsvectorcolorrampv2.cpp:155
QString schemeName() const
Definition: qgsvectorcolorrampv2.h:205
virtual QColor color(double value) const =0
void setColor2(QColor color)
Definition: qgsvectorcolorrampv2.h:83
void setSatMax(int val)
Definition: qgsvectorcolorrampv2.h:154
#define DEFAULT_RANDOM_SAT_MIN
Definition: qgsvectorcolorrampv2.h:111
Definition: qgsvectorcolorrampv2.h:164
static QgsVectorColorRampV2 * create(const QgsStringMap &properties=QgsStringMap())
Definition: qgsvectorcolorrampv2.cpp:47
Definition: qgsvectorcolorrampv2.h:114
virtual int count() const =0
QList< QgsGradientStop > QgsGradientStopsList
Definition: qgsvectorcolorrampv2.h:53
QgsGradientStop(double o, const QColor &c)
Definition: qgsvectorcolorrampv2.h:50
double offset
Definition: qgsvectorcolorrampv2.h:48
QStringList mVariantList
Definition: qgsvectorcolorrampv2.h:273
int mColors
Definition: qgsvectorcolorrampv2.h:220
QgsStringMap mInfo
Definition: qgsvectorcolorrampv2.h:103
#define DEFAULT_COLORBREWER_COLORS
Definition: qgsvectorcolorrampv2.h:185
const QgsGradientStopsList & stops() const
Definition: qgsvectorcolorrampv2.h:90
QString mSchemeName
Definition: qgsvectorcolorrampv2.h:271
void setVariantList(QStringList variantList)
Definition: qgsvectorcolorrampv2.h:254
virtual ~QgsVectorColorRampV2()
Definition: qgsvectorcolorrampv2.h:28
bool isDiscrete() const
Definition: qgsvectorcolorrampv2.h:85
void loadPalette()
Definition: qgsvectorcolorrampv2.h:258
#define DEFAULT_RANDOM_HUE_MAX
Definition: qgsvectorcolorrampv2.h:108
int hueMax() const
Definition: qgsvectorcolorrampv2.h:144
bool mDiscrete
Definition: qgsvectorcolorrampv2.h:101
int satMin() const
Definition: qgsvectorcolorrampv2.h:145
void setHueMax(int val)
Definition: qgsvectorcolorrampv2.h:152
#define DEFAULT_RANDOM_VAL_MIN
Definition: qgsvectorcolorrampv2.h:109
virtual QString type() const
Definition: qgsvectorcolorrampv2.h:239
QString schemeName() const
Definition: qgsvectorcolorrampv2.h:247
Definition: qgsvectorcolorrampv2.h:25
virtual QString type() const
Definition: qgsvectorcolorrampv2.h:128
Definition: qgsvectorcolorrampv2.h:58
void setSchemeName(QString schemeName)
Definition: qgsvectorcolorrampv2.h:252
QColor color2() const
Definition: qgsvectorcolorrampv2.h:81
QgsGradientStopsList mStops
Definition: qgsvectorcolorrampv2.h:102
QColor color1() const
Definition: qgsvectorcolorrampv2.h:80
int count() const
Definition: qgsvectorcolorrampv2.h:142
void setDiscrete(bool discrete)
Definition: qgsvectorcolorrampv2.h:86
QColor color
Definition: qgsvectorcolorrampv2.h:49
int mCount
Definition: qgsvectorcolorrampv2.h:159
#define DEFAULT_RANDOM_COUNT
Definition: qgsvectorcolorrampv2.h:106
int valMin() const
Definition: qgsvectorcolorrampv2.h:147
bool fileLoaded() const
Definition: qgsvectorcolorrampv2.h:263
int satMax() const
Definition: qgsvectorcolorrampv2.h:146
virtual QgsVectorColorRampV2 * clone() const
Definition: qgsvectorcolorrampv2.cpp:147
void setValMin(int val)
Definition: qgsvectorcolorrampv2.h:155