QGIS API Documentation  2.15.0-Master (94d88e6)
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, const QString& label, bool render = true );
29  QgsRendererRangeV2( const QgsRendererRangeV2& range );
30 
32 
33  // default dtor is ok
34  QgsRendererRangeV2& operator=( QgsRendererRangeV2 range );
35 
36  bool operator<( const QgsRendererRangeV2 &other ) const;
37 
38  double lowerValue() const;
39  double upperValue() const;
40 
41  QgsSymbolV2* symbol() const;
42  QString label() const;
43 
44  void setSymbol( QgsSymbolV2* s );
45  void setLabel( const QString& label );
46  void setLowerValue( double lowerValue );
47  void setUpperValue( double upperValue );
48 
49  // @note added in 2.5
50  bool renderState() const;
51  void setRenderState( bool render );
52 
53  // debugging
54  QString dump() const;
55 
56  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
57 
58  protected:
59  double mLowerValue, mUpperValue;
62  bool mRender;
63 
64  // for cpy+swap idiom
65  void swap( QgsRendererRangeV2 & other );
66 };
67 
69 
70 
71 // @note added in 2.6
73 {
74  public:
76  QgsRendererRangeV2LabelFormat( const QString& format, int precision = 4, bool trimTrailingZeroes = false );
77 
78  bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
79  bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;
80 
81  QString format() const { return mFormat; }
82  void setFormat( const QString& format ) { mFormat = format; }
83 
84  int precision() const { return mPrecision; }
85  void setPrecision( int precision );
86 
87  bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
88  void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
89 
91  QString labelForRange( double lower, double upper ) const;
92  QString labelForRange( const QgsRendererRangeV2 &range ) const;
93  QString formatNumber( double value ) const;
94 
95  void setFromDomElement( QDomElement &element );
96  void saveToDomElement( QDomElement &element );
97 
98  static int MaxPrecision;
99  static int MinPrecision;
100 
101  protected:
105  // values used to manage number formatting - precision and trailing zeroes
106  double mNumberScale;
110 };
111 
112 class QgsVectorLayer;
114 
117 {
118  public:
119 
120  QgsGraduatedSymbolRendererV2( const QString& attrName = QString(), const QgsRangeList& ranges = QgsRangeList() );
121 
122  virtual ~QgsGraduatedSymbolRendererV2();
123 
125  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext &context ) override;
127  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext &context ) override;
128 
129  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
130 
131  virtual void stopRender( QgsRenderContext& context ) override;
132 
133  virtual QList<QString> usedAttributes() override;
134 
135  virtual QString dump() const override;
136 
137  virtual QgsGraduatedSymbolRendererV2* clone() const override;
138 
139  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
140 
142  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
143 
145  virtual QgsSymbolV2List symbols( QgsRenderContext &context ) override;
146 
147  QString classAttribute() const { return mAttrName; }
148  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
149 
150  const QgsRangeList& ranges() const { return mRanges; }
151 
152  bool updateRangeSymbol( int rangeIndex, QgsSymbolV2* symbol );
153  bool updateRangeLabel( int rangeIndex, const QString& label );
154  bool updateRangeUpperValue( int rangeIndex, double value );
155  bool updateRangeLowerValue( int rangeIndex, double value );
157  bool updateRangeRenderState( int rangeIndex, bool render );
158 
159  void addClass( QgsSymbolV2* symbol );
161  void addClass( const QgsRendererRangeV2& range );
163  void addClass( double lower, double upper );
164 
172  void addBreak( double breakValue, bool updateSymbols = true );
173 
174  void deleteClass( int idx );
175  void deleteAllClasses();
176 
178  void moveClass( int from, int to );
179 
184  bool rangesOverlap() const;
185 
190  bool rangesHaveGaps() const;
191 
192  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
193  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
194 
195  enum Mode
196  {
202  Custom
203  };
204 
205  Mode mode() const { return mMode; }
206  void setMode( Mode mode ) { mMode = mode; }
212  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
213 
218  Q_DECL_DEPRECATED QList<double> getDataValues( QgsVectorLayer *vlayer );
219 
222  const QgsRendererRangeV2LabelFormat &labelFormat() const { return mLabelFormat; }
227  void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = false );
228 
232  void calculateLabelPrecision( bool updateRanges = true );
233 
234  static QgsGraduatedSymbolRendererV2* createRenderer(
235  QgsVectorLayer* vlayer,
236  const QString& attrName,
237  int classes,
238  Mode mode,
239  QgsSymbolV2* symbol,
240  QgsVectorColorRampV2* ramp,
241  bool inverted = false,
243  );
244 
246  static QgsFeatureRendererV2* create( QDomElement& element );
247 
249  virtual QDomElement save( QDomDocument& doc ) override;
250 
252  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
253 
256  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
257 
260 
261  virtual QSet< QString > legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
262 
268  QgsSymbolV2* sourceSymbol();
269 
276  void setSourceSymbol( QgsSymbolV2* sym );
277 
282  QgsVectorColorRampV2* sourceColorRamp();
283 
287  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
288 
290  bool invertedColorRamp() { return mInvertedColorRamp; }
291  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
292 
298  void updateColorRamp( QgsVectorColorRampV2* ramp = nullptr, bool inverted = false );
299 
305  void updateSymbols( QgsSymbolV2* sym );
306 
310  void setSymbolSizes( double minSize, double maxSize );
311 
314  double minSymbolSize() const;
315 
318  double maxSymbolSize() const;
319 
320  enum GraduatedMethod {GraduatedColor = 0, GraduatedSize = 1 };
321 
324  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
325 
328  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
329 
330  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
331  Q_DECL_DEPRECATED QString rotationField() const override;
332 
333  void setSizeScaleField( const QString& fieldOrExpression );
334  QString sizeScaleField() const;
335 
336  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
337  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
338 
341  virtual bool legendSymbolItemsCheckable() const override;
342 
345  virtual bool legendSymbolItemChecked( const QString& key ) override;
346 
349  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
350 
351  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
352 
355  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
356 
360  static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
361 
362  protected:
370 
377  int mAttrNum;
378  bool mCounting;
379 
382 
383  QgsSymbolV2* symbolForValue( double value );
384 
387  QString legendKeyForValue( double value ) const;
388 
390  static const char * graduatedMethodStr( GraduatedMethod method );
391 
392  private:
393 
396  QVariant valueForFeature( QgsFeature& feature, QgsRenderContext &context ) const;
397 
398 };
400 
401 #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:504
Container of fields for a vector layer.
Definition: qgsfield.h:193
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:505
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:87
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
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.