QGIS API Documentation  2.17.0-Master (3a3b9ab7)
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 
27 class CORE_EXPORT QgsRendererRangeV2
28 {
29  public:
31  QgsRendererRangeV2( double lowerValue, double upperValue, QgsSymbolV2* symbol, const QString& label, bool render = true );
32  QgsRendererRangeV2( const QgsRendererRangeV2& range );
33 
35 
36  // default dtor is ok
37  QgsRendererRangeV2& operator=( QgsRendererRangeV2 range );
38 
39  bool operator<( const QgsRendererRangeV2 &other ) const;
40 
41  double lowerValue() const;
42  double upperValue() const;
43 
44  QgsSymbolV2* symbol() const;
45  QString label() const;
46 
47  void setSymbol( QgsSymbolV2* s );
48  void setLabel( const QString& label );
49  void setLowerValue( double lowerValue );
50  void setUpperValue( double upperValue );
51 
52  // @note added in 2.5
53  bool renderState() const;
54  void setRenderState( bool render );
55 
56  // debugging
57  QString dump() const;
58 
66  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props, bool firstRange = false ) const;
67 
68  protected:
69  double mLowerValue, mUpperValue;
72  bool mRender;
73 
74  // for cpy+swap idiom
75  void swap( QgsRendererRangeV2 & other );
76 };
77 
79 
80 
86 {
87  public:
89  QgsRendererRangeV2LabelFormat( const QString& format, int precision = 4, bool trimTrailingZeroes = false );
90 
91  bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
92  bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;
93 
94  QString format() const { return mFormat; }
95  void setFormat( const QString& format ) { mFormat = format; }
96 
97  int precision() const { return mPrecision; }
98  void setPrecision( int precision );
99 
100  bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
101  void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
102 
104  QString labelForRange( double lower, double upper ) const;
105  QString labelForRange( const QgsRendererRangeV2 &range ) const;
106  QString formatNumber( double value ) const;
107 
108  void setFromDomElement( QDomElement &element );
109  void saveToDomElement( QDomElement &element );
110 
111  static int MaxPrecision;
112  static int MinPrecision;
113 
114  protected:
118  // values used to manage number formatting - precision and trailing zeroes
119  double mNumberScale;
123 };
124 
125 class QgsVectorLayer;
127 
133 {
134  public:
135 
136  QgsGraduatedSymbolRendererV2( const QString& attrName = QString(), const QgsRangeList& ranges = QgsRangeList() );
137 
138  virtual ~QgsGraduatedSymbolRendererV2();
139 
141  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext &context ) override;
143  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext &context ) override;
144 
145  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
146 
147  virtual void stopRender( QgsRenderContext& context ) override;
148 
149  virtual QList<QString> usedAttributes() override;
150 
151  virtual QString dump() const override;
152 
153  virtual QgsGraduatedSymbolRendererV2* clone() const override;
154 
155  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
156 
158  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
159 
161  virtual QgsSymbolV2List symbols( QgsRenderContext &context ) override;
162 
163  QString classAttribute() const { return mAttrName; }
164  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
165 
166  const QgsRangeList& ranges() const { return mRanges; }
167 
168  bool updateRangeSymbol( int rangeIndex, QgsSymbolV2* symbol );
169  bool updateRangeLabel( int rangeIndex, const QString& label );
170  bool updateRangeUpperValue( int rangeIndex, double value );
171  bool updateRangeLowerValue( int rangeIndex, double value );
173  bool updateRangeRenderState( int rangeIndex, bool render );
174 
175  void addClass( QgsSymbolV2* symbol );
177  void addClass( const QgsRendererRangeV2& range );
179  void addClass( double lower, double upper );
180 
188  void addBreak( double breakValue, bool updateSymbols = true );
189 
190  void deleteClass( int idx );
191  void deleteAllClasses();
192 
194  void moveClass( int from, int to );
195 
200  bool rangesOverlap() const;
201 
206  bool rangesHaveGaps() const;
207 
208  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
209  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
210 
211  enum Mode
212  {
218  Custom
219  };
220 
221  Mode mode() const { return mMode; }
222  void setMode( Mode mode ) { mMode = mode; }
228  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
229 
234  Q_DECL_DEPRECATED QList<double> getDataValues( QgsVectorLayer *vlayer );
235 
238  const QgsRendererRangeV2LabelFormat &labelFormat() const { return mLabelFormat; }
243  void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = false );
244 
248  void calculateLabelPrecision( bool updateRanges = true );
249 
250  static QgsGraduatedSymbolRendererV2* createRenderer(
251  QgsVectorLayer* vlayer,
252  const QString& attrName,
253  int classes,
254  Mode mode,
255  QgsSymbolV2* symbol,
256  QgsVectorColorRampV2* ramp,
257  bool inverted = false,
259  );
260 
262  static QgsFeatureRendererV2* create( QDomElement& element );
263 
265  virtual QDomElement save( QDomDocument& doc ) override;
266 
268  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
269 
272  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
273 
276 
277  virtual QSet< QString > legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
278 
284  QgsSymbolV2* sourceSymbol();
285 
292  void setSourceSymbol( QgsSymbolV2* sym );
293 
298  QgsVectorColorRampV2* sourceColorRamp();
299 
303  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
304 
306  bool invertedColorRamp() { return mInvertedColorRamp; }
307  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
308 
314  void updateColorRamp( QgsVectorColorRampV2* ramp = nullptr, bool inverted = false );
315 
321  void updateSymbols( QgsSymbolV2* sym );
322 
326  void setSymbolSizes( double minSize, double maxSize );
327 
330  double minSymbolSize() const;
331 
334  double maxSymbolSize() const;
335 
336  enum GraduatedMethod {GraduatedColor = 0, GraduatedSize = 1 };
337 
340  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
341 
344  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
345 
346  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
347  Q_DECL_DEPRECATED QString rotationField() const override;
348 
349  void setSizeScaleField( const QString& fieldOrExpression );
350  QString sizeScaleField() const;
351 
352  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
353  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
354 
357  virtual bool legendSymbolItemsCheckable() const override;
358 
361  virtual bool legendSymbolItemChecked( const QString& key ) override;
362 
365  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
366 
367  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
368 
371  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
372 
376  static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
377 
378  protected:
386 
393  int mAttrNum;
394  bool mCounting;
395 
398 
399  QgsSymbolV2* symbolForValue( double value );
400 
403  QString legendKeyForValue( double value ) const;
404 
406  static const char * graduatedMethodStr( GraduatedMethod method );
407 
408  private:
409 
412  QVariant valueForFeature( QgsFeature& feature, QgsRenderContext &context ) const;
413 
414 };
416 
417 #endif // QGSGRADUATEDSYMBOLRENDERERV2_H
QList< QgsRendererRangeV2 > QgsRangeList
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context)
Return legend keys matching a specified feature.
void setFormat(const QString &format)
QScopedPointer< QgsVectorColorRampV2 > mSourceColorRamp
virtual QString dump() const
for debugging
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual Q_DECL_DEPRECATED QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature)
Return symbol for feature.
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual QDomElement save(QDomDocument &doc)
store renderer info to XML element
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:515
Container of fields for a vector layer.
Definition: qgsfield.h:209
QHash< QgsSymbolV2 *, QgsSymbolV2 * > mTempSymbols
temporary symbols, used for data-defined rotation and scaling
virtual QList< QString > usedAttributes()=0
Returns a set of attributes required for this renderer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize)
return a list of symbology items for the legend
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
Needs to be called when a new render cycle is started.
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
QScopedPointer< QgsSymbolV2 > mSymbol
QScopedPointer< QgsExpression > mExpression
virtual int capabilities() override
returns bitwise OR-ed capabilities of the renderer
virtual QgsFeatureRendererV2 * clone() const =0
virtual Q_DECL_DEPRECATED QgsSymbolV2 * symbolForFeature(QgsFeature &feature)
To be overridden.
void setGraduatedMethod(GraduatedMethod method)
set the method used for graduation (either size or color)
QgsSymbolV2::ScaleMethod scaleMethod() const
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="")
return a list of item text / symbol
virtual Q_DECL_DEPRECATED QString rotationField() const
return rotation field name (or empty string if not set or not supported by renderer) ...
GraduatedMethod graduatedMethod() const
return the method used for graduation (either size or color)
QgsRendererRangeV2LabelFormat mLabelFormat
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
For symbol levels.
const QgsRendererRangeV2LabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:516
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
Contains information about the context of a rendering operation.
int mAttrNum
attribute index (derived from attribute name in startRender)
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:90
const QgsRangeList & ranges() const
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const
Return a list of symbology items for the legend.
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
void setClassAttribute(const QString &attr)
QScopedPointer< QgsSymbolV2 > mSourceSymbol
virtual QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.
void setTrimTrailingZeroes(bool trimTrailingZeroes)
QScopedPointer< QgsExpression > mRotation
QScopedPointer< QgsExpression > mSizeScale
features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
virtual Q_DECL_DEPRECATED void setRotationField(const QString &fieldName)
sets rotation field of renderer (if supported by the renderer)
Represents a vector layer which manages a vector based data sets.
Abstract base class for color ramps.
virtual void setLegendSymbolItem(const QString &key, QgsSymbolV2 *symbol)
Sets the symbol to be used for a legend symbol item.