QGIS API Documentation  2.17.0-Master (00653d2)
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 
156  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
158  virtual void toSld( QDomDocument& doc, QDomElement &element, const QgsStringMap& props ) const override;
159 
161  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
162 
164  virtual QgsSymbolV2List symbols( QgsRenderContext &context ) override;
165 
166  QString classAttribute() const { return mAttrName; }
167  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
168 
169  const QgsRangeList& ranges() const { return mRanges; }
170 
171  bool updateRangeSymbol( int rangeIndex, QgsSymbolV2* symbol );
172  bool updateRangeLabel( int rangeIndex, const QString& label );
173  bool updateRangeUpperValue( int rangeIndex, double value );
174  bool updateRangeLowerValue( int rangeIndex, double value );
176  bool updateRangeRenderState( int rangeIndex, bool render );
177 
178  void addClass( QgsSymbolV2* symbol );
180  void addClass( const QgsRendererRangeV2& range );
182  void addClass( double lower, double upper );
183 
191  void addBreak( double breakValue, bool updateSymbols = true );
192 
193  void deleteClass( int idx );
194  void deleteAllClasses();
195 
197  void moveClass( int from, int to );
198 
203  bool rangesOverlap() const;
204 
209  bool rangesHaveGaps() const;
210 
211  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
212  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
213 
214  enum Mode
215  {
221  Custom
222  };
223 
224  Mode mode() const { return mMode; }
225  void setMode( Mode mode ) { mMode = mode; }
231  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
232 
237  Q_DECL_DEPRECATED QList<double> getDataValues( QgsVectorLayer *vlayer );
238 
241  const QgsRendererRangeV2LabelFormat &labelFormat() const { return mLabelFormat; }
246  void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = false );
247 
251  void calculateLabelPrecision( bool updateRanges = true );
252 
253  static QgsGraduatedSymbolRendererV2* createRenderer(
254  QgsVectorLayer* vlayer,
255  const QString& attrName,
256  int classes,
257  Mode mode,
258  QgsSymbolV2* symbol,
259  QgsVectorColorRampV2* ramp,
260  bool inverted = false,
262  );
263 
265  static QgsFeatureRendererV2* create( QDomElement& element );
266 
268  virtual QDomElement save( QDomDocument& doc ) override;
269 
271  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
272 
275  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
276 
279 
280  virtual QSet< QString > legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
281 
287  QgsSymbolV2* sourceSymbol();
288 
295  void setSourceSymbol( QgsSymbolV2* sym );
296 
301  QgsVectorColorRampV2* sourceColorRamp();
302 
306  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
307 
309  bool invertedColorRamp() { return mInvertedColorRamp; }
310  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
311 
317  void updateColorRamp( QgsVectorColorRampV2* ramp = nullptr, bool inverted = false );
318 
324  void updateSymbols( QgsSymbolV2* sym );
325 
329  void setSymbolSizes( double minSize, double maxSize );
330 
333  double minSymbolSize() const;
334 
337  double maxSymbolSize() const;
338 
339  enum GraduatedMethod {GraduatedColor = 0, GraduatedSize = 1 };
340 
343  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
344 
347  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
348 
349  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
350  Q_DECL_DEPRECATED QString rotationField() const override;
351 
352  void setSizeScaleField( const QString& fieldOrExpression );
353  QString sizeScaleField() const;
354 
355  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
356  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
357 
360  virtual bool legendSymbolItemsCheckable() const override;
361 
364  virtual bool legendSymbolItemChecked( const QString& key ) override;
365 
368  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
369 
370  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
371 
374  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
375 
379  static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
380 
381  protected:
389 
396  int mAttrNum;
397  bool mCounting;
398 
401 
402  QgsSymbolV2* symbolForValue( double value );
403 
406  QString legendKeyForValue( double value ) const;
407 
409  static const char * graduatedMethodStr( GraduatedMethod method );
410 
411  private:
412 
415  QVariant valueForFeature( QgsFeature& feature, QgsRenderContext &context ) const;
416 
417 };
419 
420 #endif // QGSGRADUATEDSYMBOLRENDERERV2_H
QgsSymbolV2::ScaleMethod scaleMethod() const
QList< QgsRendererRangeV2 > QgsRangeList
virtual Q_DECL_DEPRECATED QString rotationField() const
return rotation field name (or empty string if not set or not supported by renderer) ...
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context)
Return legend keys matching a specified feature.
void setFormat(const QString &format)
QScopedPointer< QgsVectorColorRampV2 > mSourceColorRamp
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)
GraduatedMethod graduatedMethod() const
return the method used for graduation (either size or color)
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:252
QHash< QgsSymbolV2 *, QgsSymbolV2 * > mTempSymbols
temporary symbols, used for data-defined rotation and scaling
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const
Return a list of symbology items for the legend.
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 void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
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)
virtual QString dump() const
for debugging
const QgsRangeList & ranges() const
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="")
return a list of item text / symbol
QgsRendererRangeV2LabelFormat mLabelFormat
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
For symbol levels.
#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)
const QgsRendererRangeV2LabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:90
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
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
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.