QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsgraduatedsymbolrendererv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgraduatedsymbolrendererv2.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 #ifndef QGSGRADUATEDSYMBOLRENDERERV2_H
16 #define QGSGRADUATEDSYMBOLRENDERERV2_H
17 
18 #include "qgssymbolv2.h"
19 #include "qgsrendererv2.h"
20 #include "qgsexpression.h"
21 #include <QScopedPointer>
22 #include <QRegExp>
23 
24 class CORE_EXPORT QgsRendererRangeV2
25 {
26  public:
28  QgsRendererRangeV2( double lowerValue, double upperValue, QgsSymbolV2* symbol, QString label, bool render = true );
29  QgsRendererRangeV2( const QgsRendererRangeV2& range );
30 
31  // default dtor is ok
32  QgsRendererRangeV2& operator=( QgsRendererRangeV2 range );
33 
34  bool operator<( const QgsRendererRangeV2 &other ) const;
35 
36  double lowerValue() const;
37  double upperValue() const;
38 
39  QgsSymbolV2* symbol() const;
40  QString label() const;
41 
42  void setSymbol( QgsSymbolV2* s );
43  void setLabel( QString label );
44  void setLowerValue( double lowerValue );
45  void setUpperValue( double upperValue );
46 
47  // @note added in 2.5
48  bool renderState() const;
49  void setRenderState( bool render );
50 
51  // debugging
52  QString dump() const;
53 
54  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
55 
56  protected:
57  double mLowerValue, mUpperValue;
58  QScopedPointer<QgsSymbolV2> mSymbol;
59  QString mLabel;
60  bool mRender;
61 
62  // for cpy+swap idiom
63  void swap( QgsRendererRangeV2 & other );
64 };
65 
66 typedef QList<QgsRendererRangeV2> QgsRangeList;
67 
68 
69 // @note added in 2.6
71 {
72  public:
74  QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );
75 
76  bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
77  bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;
78 
79  QString format() const { return mFormat; }
80  void setFormat( QString format ) { mFormat = format; }
81 
82  int precision() const { return mPrecision; }
83  void setPrecision( int precision );
84 
85  bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
86  void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
87 
89  QString labelForRange( double lower, double upper ) const;
90  QString labelForRange( const QgsRendererRangeV2 &range ) const;
91  QString formatNumber( double value ) const;
92 
93  void setFromDomElement( QDomElement &element );
94  void saveToDomElement( QDomElement &element );
95 
96  static int MaxPrecision;
97  static int MinPrecision;
98 
99  protected:
100  QString mFormat;
103  // values used to manage number formatting - precision and trailing zeroes
104  double mNumberScale;
105  QString mNumberSuffix;
108 };
109 
110 class QgsVectorLayer;
112 
114 {
115  public:
116  QgsGraduatedSymbolRendererV2( QString attrName = QString(), QgsRangeList ranges = QgsRangeList() );
118 
119  virtual ~QgsGraduatedSymbolRendererV2();
120 
121  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
122 
123  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature );
124 
125  virtual void startRender( QgsRenderContext& context, const QgsFields& fields );
126 
127  virtual void stopRender( QgsRenderContext& context );
128 
129  virtual QList<QString> usedAttributes();
130 
131  virtual QString dump() const;
132 
133  virtual QgsFeatureRendererV2* clone() const;
134 
135  virtual void toSld( QDomDocument& doc, QDomElement &element ) const;
136 
139  virtual int capabilities() { return SymbolLevels | RotationField | Filter; }
140 
141  virtual QgsSymbolV2List symbols();
142 
143  QString classAttribute() const { return mAttrName; }
144  void setClassAttribute( QString attr ) { mAttrName = attr; }
145 
146  const QgsRangeList& ranges() const { return mRanges; }
147 
148  bool updateRangeSymbol( int rangeIndex, QgsSymbolV2* symbol );
149  bool updateRangeLabel( int rangeIndex, QString label );
150  bool updateRangeUpperValue( int rangeIndex, double value );
151  bool updateRangeLowerValue( int rangeIndex, double value );
153  bool updateRangeRenderState( int rangeIndex, bool render );
154 
155  void addClass( QgsSymbolV2* symbol );
157  void addClass( QgsRendererRangeV2 range );
159  void addClass( double lower, double upper );
160  void deleteClass( int idx );
161  void deleteAllClasses();
162 
164  void moveClass( int from, int to );
165 
166  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
167  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
168 
169  enum Mode
170  {
176  Custom
177  };
178 
179  Mode mode() const { return mMode; }
180  void setMode( Mode mode ) { mMode = mode; }
186  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
190  QList<double> getDataValues( QgsVectorLayer *vlayer );
191 
194  const QgsRendererRangeV2LabelFormat &labelFormat() const { return mLabelFormat; }
199  void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = false );
200 
204  void calculateLabelPrecision( bool updateRanges = true );
205 
206  static QgsGraduatedSymbolRendererV2* createRenderer(
207  QgsVectorLayer* vlayer,
208  QString attrName,
209  int classes,
210  Mode mode,
211  QgsSymbolV2* symbol,
212  QgsVectorColorRampV2* ramp,
213  bool inverted = false,
215  );
216 
218  static QgsFeatureRendererV2* create( QDomElement& element );
219 
221  virtual QDomElement save( QDomDocument& doc );
222 
224  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );
225 
229  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = QString() );
230 
231  QgsSymbolV2* sourceSymbol();
232  void setSourceSymbol( QgsSymbolV2* sym );
233 
234  QgsVectorColorRampV2* sourceColorRamp();
235  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
237  bool invertedColorRamp() { return mInvertedColorRamp; }
238  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
239 
243  void updateColorRamp( QgsVectorColorRampV2* ramp = 0, bool inverted = false );
244 
246  void updateSymbols( QgsSymbolV2* sym );
247 
249  void setRotationField( QString fieldOrExpression );
251  QString rotationField() const;
252 
254  void setSizeScaleField( QString fieldOrExpression );
256  QString sizeScaleField() const;
257 
259  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
261  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
262 
265  virtual bool legendSymbolItemsCheckable() const;
266 
269  virtual bool legendSymbolItemChecked( QString key );
270 
273  virtual void checkLegendSymbolItem( QString key, bool state = true );
274 
277  virtual QString legendClassificationAttribute() const { return classAttribute(); }
278 
282  static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
283 
284  protected:
285  QString mAttrName;
288  QScopedPointer<QgsSymbolV2> mSourceSymbol;
289  QScopedPointer<QgsVectorColorRampV2> mSourceColorRamp;
292  QScopedPointer<QgsExpression> mRotation;
293  QScopedPointer<QgsExpression> mSizeScale;
295  QScopedPointer<QgsExpression> mExpression;
297  int mAttrNum;
298  bool mCounting;
299 
301  QHash<QgsSymbolV2*, QgsSymbolV2*> mTempSymbols;
302 
303  QgsSymbolV2* symbolForValue( double value );
304 
305 };
306 
307 #endif // QGSGRADUATEDSYMBOLRENDERERV2_H
QList< QgsRendererRangeV2 > QgsRangeList
Definition: qgsgraduatedsymbolrendererv2.h:66
Mode mode() const
Definition: qgsgraduatedsymbolrendererv2.h:179
virtual QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature)
Return symbol for feature.
Definition: qgsrendererv2.h:94
virtual void setRotationField(QString fieldName)
sets rotation field of renderer (if supported by the renderer)
Definition: qgsrendererv2.h:198
virtual void checkLegendSymbolItem(QString key, bool state=true)
item in symbology was checked
Definition: qgsrendererv2.cpp:523
void setInvertedColorRamp(bool inverted)
Definition: qgsgraduatedsymbolrendererv2.h:238
QList< QgsSymbolV2 * > QgsSymbolV2List
Definition: qgsrendererv2.h:37
QScopedPointer< QgsVectorColorRampV2 > mSourceColorRamp
Definition: qgsgraduatedsymbolrendererv2.h:289
virtual QString dump() const
for debugging
Definition: qgsrendererv2.cpp:367
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:214
void setFormat(QString format)
Definition: qgsgraduatedsymbolrendererv2.h:80
Definition: qgsgraduatedsymbolrendererv2.h:171
QRegExp mReNegativeZero
Definition: qgsgraduatedsymbolrendererv2.h:107
QString classAttribute() const
Definition: qgsgraduatedsymbolrendererv2.h:143
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:219
virtual QDomElement save(QDomDocument &doc)
store renderer info to XML element
Definition: qgsrendererv2.cpp:395
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, QString rule="")
return a list of item text / symbol
Definition: qgsrendererv2.cpp:529
Container of fields for a vector layer.
Definition: qgsfield.h:172
int precision() const
Definition: qgsgraduatedsymbolrendererv2.h:82
QHash< QgsSymbolV2 *, QgsSymbolV2 * > mTempSymbols
temporary symbols, used for data-defined rotation and scaling
Definition: qgsgraduatedsymbolrendererv2.h:301
Definition: qgsgraduatedsymbolrendererv2.h:174
bool trimTrailingZeroes() const
Definition: qgsgraduatedsymbolrendererv2.h:85
virtual QList< QString > usedAttributes()=0
bool mInvertedColorRamp
Definition: qgsgraduatedsymbolrendererv2.h:290
Mode
Definition: qgsgraduatedsymbolrendererv2.h:169
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
QString mFormat
Definition: qgsgraduatedsymbolrendererv2.h:100
Mode mMode
Definition: qgsgraduatedsymbolrendererv2.h:287
QMap< QString, QString > QgsStringMap
Definition: qgis.h:416
QString format() const
Definition: qgsgraduatedsymbolrendererv2.h:79
Definition: qgsgraduatedsymbolrendererv2.h:113
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize)
return a list of symbology items for the legend
Definition: qgsrendererv2.cpp:505
bool mTrimTrailingZeroes
Definition: qgsgraduatedsymbolrendererv2.h:102
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
virtual void stopRender(QgsRenderContext &context)=0
virtual QString rotationField() const
return rotation field name (or empty string if not set or not supported by renderer) ...
Definition: qgsrendererv2.h:195
QScopedPointer< QgsSymbolV2 > mSymbol
Definition: qgsgraduatedsymbolrendererv2.h:58
virtual QgsSymbolV2List symbols()=0
for symbol levels
bool mCounting
Definition: qgsgraduatedsymbolrendererv2.h:298
QgsRangeList mRanges
Definition: qgsgraduatedsymbolrendererv2.h:286
QScopedPointer< QgsExpression > mExpression
Definition: qgsgraduatedsymbolrendererv2.h:295
virtual QgsFeatureRendererV2 * clone() const =0
QgsSymbolV2::ScaleMethod scaleMethod() const
Definition: qgsgraduatedsymbolrendererv2.h:261
virtual int capabilities()
returns bitwise OR-ed capabilities of the renderer
Definition: qgsgraduatedsymbolrendererv2.h:139
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
Definition: qgsgraduatedsymbolrendererv2.h:277
virtual bool legendSymbolItemChecked(QString key)
items of symbology items in legend is checked
Definition: qgsrendererv2.cpp:517
static int MinPrecision
Definition: qgsgraduatedsymbolrendererv2.h:97
bool invertedColorRamp()
Definition: qgsgraduatedsymbolrendererv2.h:237
Definition: qgsgraduatedsymbolrendererv2.h:24
Definition: qgsgraduatedsymbolrendererv2.h:172
void setClassAttribute(QString attr)
Definition: qgsgraduatedsymbolrendererv2.h:144
QgsRendererRangeV2LabelFormat mLabelFormat
Definition: qgsgraduatedsymbolrendererv2.h:291
QString mAttrName
Definition: qgsgraduatedsymbolrendererv2.h:285
Definition: qgssymbolv2.h:43
QList< QPair< QString, QPixmap > > QgsLegendSymbologyList
Definition: qgspluginlayer.h:20
double mNumberScale
Definition: qgsgraduatedsymbolrendererv2.h:104
Definition: qgsgraduatedsymbolrendererv2.h:175
const QgsRendererRangeV2LabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
Definition: qgsgraduatedsymbolrendererv2.h:194
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
Definition: qgsrendererv2.h:158
QRegExp mReTrailingZeroes
Definition: qgsgraduatedsymbolrendererv2.h:106
int ANALYSIS_EXPORT lower(int n, int i)
lower function
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:39
static int MaxPrecision
Definition: qgsgraduatedsymbolrendererv2.h:96
int mAttrNum
attribute index (derived from attribute name in startRender)
Definition: qgsgraduatedsymbolrendererv2.h:297
ScaleMethod
Definition: qgssymbolv2.h:61
QString mNumberSuffix
Definition: qgsgraduatedsymbolrendererv2.h:105
Definition: qgsgraduatedsymbolrendererv2.h:173
const QgsRangeList & ranges() const
Definition: qgsgraduatedsymbolrendererv2.h:146
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
Definition: qgsrendererv2.cpp:512
double mUpperValue
Definition: qgsgraduatedsymbolrendererv2.h:57
Definition: qgsgraduatedsymbolrendererv2.h:70
QScopedPointer< QgsSymbolV2 > mSourceSymbol
Definition: qgsgraduatedsymbolrendererv2.h:288
void setTrimTrailingZeroes(bool trimTrailingZeroes)
Definition: qgsgraduatedsymbolrendererv2.h:86
void setMode(Mode mode)
Definition: qgsgraduatedsymbolrendererv2.h:180
QScopedPointer< QgsExpression > mRotation
Definition: qgsgraduatedsymbolrendererv2.h:292
Definition: qgsrendererv2.h:72
QgsSymbolV2::ScaleMethod mScaleMethod
Definition: qgsgraduatedsymbolrendererv2.h:294
QScopedPointer< QgsExpression > mSizeScale
Definition: qgsgraduatedsymbolrendererv2.h:293
Definition: qgsrendererv2.h:119
int mPrecision
Definition: qgsgraduatedsymbolrendererv2.h:101
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:470
bool mRender
Definition: qgsgraduatedsymbolrendererv2.h:60
Definition: qgsvectorcolorrampv2.h:25
QList< QPair< QString, QgsSymbolV2 * > > QgsLegendSymbolList
Definition: qgsrendererv2.h:41
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature)=0
to be overridden
QString mLabel
Definition: qgsgraduatedsymbolrendererv2.h:59