QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
117  QgsGraduatedSymbolRendererV2( QString attrName = QString(), QgsRangeList ranges = QgsRangeList() );
119 
120  virtual ~QgsGraduatedSymbolRendererV2();
121 
122  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature ) override;
123 
124  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature ) override;
125 
126  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
127 
128  virtual void stopRender( QgsRenderContext& context ) override;
129 
130  virtual QList<QString> usedAttributes() override;
131 
132  virtual QString dump() const override;
133 
134  virtual QgsFeatureRendererV2* clone() const override;
135 
136  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
137 
139  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
140 
141  virtual QgsSymbolV2List symbols() override;
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 
168  void addBreak( double breakValue, bool updateSymbols = true );
169 
170  void deleteClass( int idx );
171  void deleteAllClasses();
172 
174  void moveClass( int from, int to );
175 
176  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
177  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
178 
179  enum Mode
180  {
186  Custom
187  };
188 
189  Mode mode() const { return mMode; }
190  void setMode( Mode mode ) { mMode = mode; }
196  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
197 
202  Q_DECL_DEPRECATED QList<double> getDataValues( QgsVectorLayer *vlayer );
203 
206  const QgsRendererRangeV2LabelFormat &labelFormat() const { return mLabelFormat; }
211  void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = false );
212 
216  void calculateLabelPrecision( bool updateRanges = true );
217 
218  static QgsGraduatedSymbolRendererV2* createRenderer(
219  QgsVectorLayer* vlayer,
220  QString attrName,
221  int classes,
222  Mode mode,
223  QgsSymbolV2* symbol,
224  QgsVectorColorRampV2* ramp,
225  bool inverted = false,
227  );
228 
230  static QgsFeatureRendererV2* create( QDomElement& element );
231 
233  virtual QDomElement save( QDomDocument& doc ) override;
234 
236  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
237 
240  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = QString() ) override;
241 
244 
245 
246  QgsSymbolV2* sourceSymbol();
247  void setSourceSymbol( QgsSymbolV2* sym );
248 
249  QgsVectorColorRampV2* sourceColorRamp();
250  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
252  bool invertedColorRamp() { return mInvertedColorRamp; }
253  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
254 
258  void updateColorRamp( QgsVectorColorRampV2* ramp = 0, bool inverted = false );
259 
261  void updateSymbols( QgsSymbolV2* sym );
262 
266  void setSymbolSizes( double minSize, double maxSize );
267 
270  double minSymbolSize() const;
271 
274  double maxSymbolSize() const;
275 
276  enum GraduatedMethod {GraduatedColor = 0, GraduatedSize = 1 };
277 
280  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
281 
284  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
285 
286  void setRotationField( QString fieldOrExpression ) override;
287  QString rotationField() const override;
288 
289  void setSizeScaleField( QString fieldOrExpression );
290  QString sizeScaleField() const;
291 
292  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
293  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
294 
297  virtual bool legendSymbolItemsCheckable() const override;
298 
301  virtual bool legendSymbolItemChecked( QString key ) override;
302 
305  virtual void checkLegendSymbolItem( QString key, bool state = true ) override;
306 
309  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
310 
314  static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
315 
316  protected:
317  QString mAttrName;
320  QScopedPointer<QgsSymbolV2> mSourceSymbol;
321  QScopedPointer<QgsVectorColorRampV2> mSourceColorRamp;
324 
325  QScopedPointer<QgsExpression> mRotation;
326  QScopedPointer<QgsExpression> mSizeScale;
328  QScopedPointer<QgsExpression> mExpression;
331  int mAttrNum;
332  bool mCounting;
333 
335  QHash<QgsSymbolV2*, QgsSymbolV2*> mTempSymbols;
336 
337  QgsSymbolV2* symbolForValue( double value );
338 
339  static const char * graduatedMethodStr( GraduatedMethod method );
340 
341 };
342 
343 #endif // QGSGRADUATEDSYMBOLRENDERERV2_H