QGIS API Documentation  2.99.0-Master (9caa722)
qgsgraduatedsymbolrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgraduatedsymbolrenderer.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 "qgis_core.h"
19 #include "qgis_sip.h"
20 #include "qgis.h"
21 #include "qgssymbol.h"
22 #include "qgsrenderer.h"
23 #include "qgsexpression.h"
25 
26 #include <QRegExp>
27 
28 
32 class CORE_EXPORT QgsRendererRange
33 {
34  public:
36  QgsRendererRange( double lowerValue, double upperValue, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true );
37  QgsRendererRange( const QgsRendererRange &range );
38 
39  // default dtor is OK
40  QgsRendererRange &operator=( QgsRendererRange range );
41 
42  bool operator<( const QgsRendererRange &other ) const;
43 
44  double lowerValue() const;
45  double upperValue() const;
46 
47  QgsSymbol *symbol() const;
48  QString label() const;
49 
50  void setSymbol( QgsSymbol *s SIP_TRANSFER );
51  void setLabel( const QString &label );
52  void setLowerValue( double lowerValue );
53  void setUpperValue( double upperValue );
54 
55  // \since QGIS 2.5
56  bool renderState() const;
57  void setRenderState( bool render );
58 
59  // debugging
60  QString dump() const;
61 
69  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props, bool firstRange = false ) const;
70 
71  protected:
72  double mLowerValue, mUpperValue;
73  std::unique_ptr<QgsSymbol> mSymbol;
74  QString mLabel;
75  bool mRender;
76 
77  // for cpy+swap idiom
78  void swap( QgsRendererRange &other );
79 };
80 
81 typedef QList<QgsRendererRange> QgsRangeList;
82 
83 
88 class CORE_EXPORT QgsRendererRangeLabelFormat
89 {
90  public:
92  QgsRendererRangeLabelFormat( const QString &format, int precision = 4, bool trimTrailingZeroes = false );
93 
94  bool operator==( const QgsRendererRangeLabelFormat &other ) const;
95  bool operator!=( const QgsRendererRangeLabelFormat &other ) const;
96 
97  QString format() const { return mFormat; }
98  void setFormat( const QString &format ) { mFormat = format; }
99 
100  int precision() const { return mPrecision; }
101  void setPrecision( int precision );
102 
103  bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
104  void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
105 
107  QString labelForRange( double lower, double upper ) const SIP_PYNAME( labelForLowerUpper );
108  QString labelForRange( const QgsRendererRange &range ) const;
109  QString formatNumber( double value ) const;
110 
111  void setFromDomElement( QDomElement &element );
112  void saveToDomElement( QDomElement &element );
113 
114  static const int MAX_PRECISION;
115  static const int MIN_PRECISION;
116 
117  protected:
118  QString mFormat;
121  // values used to manage number formatting - precision and trailing zeroes
122  double mNumberScale;
123  QString mNumberSuffix;
126 };
127 
128 class QgsVectorLayer;
129 class QgsColorRamp;
130 
135 {
136  public:
137 
138  QgsGraduatedSymbolRenderer( const QString &attrName = QString(), const QgsRangeList &ranges = QgsRangeList() );
139 
140  virtual ~QgsGraduatedSymbolRenderer();
141 
142  virtual QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
143  virtual QgsSymbol *originalSymbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
144  virtual void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
145  virtual void stopRender( QgsRenderContext &context ) override;
146  virtual QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
147  virtual QString dump() const override;
148  virtual QgsGraduatedSymbolRenderer *clone() const override SIP_FACTORY;
149  virtual void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
150  virtual QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
151  virtual QgsSymbolList symbols( QgsRenderContext &context ) override;
152 
153  QString classAttribute() const { return mAttrName; }
154  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
155 
156  const QgsRangeList &ranges() const { return mRanges; }
157 
158  bool updateRangeSymbol( int rangeIndex, QgsSymbol *symbol SIP_TRANSFER );
159  bool updateRangeLabel( int rangeIndex, const QString &label );
160  bool updateRangeUpperValue( int rangeIndex, double value );
161  bool updateRangeLowerValue( int rangeIndex, double value );
163  bool updateRangeRenderState( int rangeIndex, bool render );
164 
165  void addClass( QgsSymbol *symbol );
167  void addClass( const QgsRendererRange &range ) SIP_PYNAME( addClassRange );
169  void addClass( double lower, double upper ) SIP_PYNAME( addClassLowerUpper );
170 
178  void addBreak( double breakValue, bool updateSymbols = true );
179 
180  void deleteClass( int idx );
181  void deleteAllClasses();
182 
184  void moveClass( int from, int to );
185 
190  bool rangesOverlap() const;
191 
196  bool rangesHaveGaps() const;
197 
198  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
199  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
200 
201  enum Mode
202  {
208  Custom
209  };
210 
211  Mode mode() const { return mMode; }
212  void setMode( Mode mode ) { mMode = mode; }
218  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
219 
222  const QgsRendererRangeLabelFormat &labelFormat() const { return mLabelFormat; }
227  void setLabelFormat( const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges = false );
228 
232  void calculateLabelPrecision( bool updateRanges = true );
233 
244  static QgsGraduatedSymbolRenderer *createRenderer( QgsVectorLayer *vlayer,
245  const QString &attrName,
246  int classes,
247  Mode mode,
248  QgsSymbol *symbol SIP_TRANSFER,
249  QgsColorRamp *ramp SIP_TRANSFER,
251 
253  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
254 
255  virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
256  QgsLegendSymbolList legendSymbolItems() const override;
257  virtual QSet< QString > legendKeysForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
258 
264  QgsSymbol *sourceSymbol();
265 
272  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
273 
278  QgsColorRamp *sourceColorRamp();
279 
283  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
284 
289  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER = 0 );
290 
296  void updateSymbols( QgsSymbol *sym );
297 
301  void setSymbolSizes( double minSize, double maxSize );
302 
305  double minSymbolSize() const;
306 
309  double maxSymbolSize() const;
310 
312  {
313  GraduatedColor = 0,
314  GraduatedSize = 1
315  };
316 
319  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
320 
323  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
324 
325  virtual bool legendSymbolItemsCheckable() const override;
326  virtual bool legendSymbolItemChecked( const QString &key ) override;
327  virtual void checkLegendSymbolItem( const QString &key, bool state = true ) override;
328  virtual void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
329  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
330 
334  static QgsGraduatedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
335 
346  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
347 
353  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
354 
355  protected:
356  QString mAttrName;
359  std::unique_ptr<QgsSymbol> mSourceSymbol;
360  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
362 
363  std::unique_ptr<QgsExpression> mExpression;
366  int mAttrNum;
367  bool mCounting;
368 
369  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
370 
371  QgsSymbol *symbolForValue( double value );
372 
375  QString legendKeyForValue( double value ) const;
376 
378  static const char *graduatedMethodStr( GraduatedMethod method ) SIP_SKIP;
379 
380  private:
381 
384  QVariant valueForFeature( QgsFeature &feature, QgsRenderContext &context ) const;
385 
387  QgsLegendSymbolList baseLegendSymbolItems() const;
388 
389 #ifdef SIP_RUN
392 #endif
393 
394 };
395 
396 #endif // QGSGRADUATEDSYMBOLRENDERERV2_H
const QgsRendererRangeLabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
The class is used as a container of context for various read/write operations on other objects...
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
std::unique_ptr< QgsSymbol > mSourceSymbol
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QList< QgsRendererRange > QgsRangeList
virtual void toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
Definition: qgsrenderer.h:275
virtual QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
Definition: qgsrenderer.h:223
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
store renderer info to XML element
Abstract base class for color ramps.
Definition: qgscolorramp.h:30
virtual QgsSymbol * originalSymbolForFeature(QgsFeature &feature, QgsRenderContext &context)
Return symbol for feature.
Definition: qgsrenderer.cpp:80
void setGraduatedMethod(GraduatedMethod method)
set the method used for graduation (either size or color)
Container of fields for a vector layer.
Definition: qgsfields.h:41
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
std::unique_ptr< QgsExpression > mExpression
QMap< QString, QString > QgsStringMap
Definition: qgis.h:366
void setTrimTrailingZeroes(bool trimTrailingZeroes)
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
Needs to be called when a new render cycle is started.
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Return a list of attributes required by this renderer.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_TRANSFER
Definition: qgis_sip.h:36
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
#define SIP_FACTORY
Definition: qgis_sip.h:69
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
virtual void setLegendSymbolItem(const QString &key, QgsSymbol *symbol)
Sets the symbol to be used for a legend symbol item.
void setFormat(const QString &format)
virtual QgsSymbolList symbols(QgsRenderContext &context)
Returns list of symbols used by the renderer.
Definition: qgsrenderer.h:247
virtual QgsSymbol * symbolForFeature(QgsFeature &feature, QgsRenderContext &context)=0
To be overridden.
Contains information about the context of a rendering operation.
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
QgsRendererRangeLabelFormat mLabelFormat
GraduatedMethod graduatedMethod() const
return the method used for graduation (either size or color)
std::unique_ptr< QgsSymbol > mSymbol
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context)
Return legend keys matching a specified feature.
Definition: qgsrenderer.cpp:85
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
int mAttrNum
attribute index (derived from attribute name in startRender)
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
Definition: MathUtils.cpp:407
std::unique_ptr< QgsColorRamp > mSourceColorRamp
void setClassAttribute(const QString &attr)
Represents a vector layer which manages a vector based data sets.
Object that keeps configuration of appearance of marker symbol&#39;s data-defined size in legend...
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
const QgsRangeList & ranges() const
virtual QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74
virtual QString dump() const
Returns debug information about this renderer.