QGIS API Documentation  2.99.0-Master (6cb9fa8)
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"
24 #include <QRegExp>
25 
27 
31 class CORE_EXPORT QgsRendererRange
32 {
33  public:
35  QgsRendererRange( double lowerValue, double upperValue, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true );
36  QgsRendererRange( const QgsRendererRange &range );
37 
38  // default dtor is OK
39  QgsRendererRange &operator=( QgsRendererRange range );
40 
41  bool operator<( const QgsRendererRange &other ) const;
42 
43  double lowerValue() const;
44  double upperValue() const;
45 
46  QgsSymbol *symbol() const;
47  QString label() const;
48 
49  void setSymbol( QgsSymbol *s SIP_TRANSFER );
50  void setLabel( const QString &label );
51  void setLowerValue( double lowerValue );
52  void setUpperValue( double upperValue );
53 
54  // \since QGIS 2.5
55  bool renderState() const;
56  void setRenderState( bool render );
57 
58  // debugging
59  QString dump() const;
60 
68  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props, bool firstRange = false ) const;
69 
70  protected:
71  double mLowerValue, mUpperValue;
72  std::unique_ptr<QgsSymbol> mSymbol;
73  QString mLabel;
74  bool mRender;
75 
76  // for cpy+swap idiom
77  void swap( QgsRendererRange &other );
78 };
79 
80 typedef QList<QgsRendererRange> QgsRangeList;
81 
82 
87 class CORE_EXPORT QgsRendererRangeLabelFormat
88 {
89  public:
91  QgsRendererRangeLabelFormat( const QString &format, int precision = 4, bool trimTrailingZeroes = false );
92 
93  bool operator==( const QgsRendererRangeLabelFormat &other ) const;
94  bool operator!=( const QgsRendererRangeLabelFormat &other ) const;
95 
96  QString format() const { return mFormat; }
97  void setFormat( const QString &format ) { mFormat = format; }
98 
99  int precision() const { return mPrecision; }
100  void setPrecision( int precision );
101 
102  bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
103  void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
104 
106  QString labelForRange( double lower, double upper ) const SIP_PYNAME( labelForLowerUpper );
107  QString labelForRange( const QgsRendererRange &range ) const;
108  QString formatNumber( double value ) const;
109 
110  void setFromDomElement( QDomElement &element );
111  void saveToDomElement( QDomElement &element );
112 
113  static const int MAX_PRECISION;
114  static const int MIN_PRECISION;
115 
116  protected:
117  QString mFormat;
120  // values used to manage number formatting - precision and trailing zeroes
121  double mNumberScale;
122  QString mNumberSuffix;
125 };
126 
127 class QgsVectorLayer;
128 class QgsColorRamp;
129 
134 {
135  public:
136 
137  QgsGraduatedSymbolRenderer( const QString &attrName = QString(), const QgsRangeList &ranges = QgsRangeList() );
138 
139  virtual ~QgsGraduatedSymbolRenderer();
140 
141  virtual QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
142  virtual QgsSymbol *originalSymbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
143  virtual void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
144  virtual void stopRender( QgsRenderContext &context ) override;
145  virtual QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
146  virtual QString dump() const override;
147  virtual QgsGraduatedSymbolRenderer *clone() const override SIP_FACTORY;
148  virtual void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
149  virtual QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
150  virtual QgsSymbolList symbols( QgsRenderContext &context ) override;
151 
152  QString classAttribute() const { return mAttrName; }
153  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
154 
155  const QgsRangeList &ranges() const { return mRanges; }
156 
157  bool updateRangeSymbol( int rangeIndex, QgsSymbol *symbol SIP_TRANSFER );
158  bool updateRangeLabel( int rangeIndex, const QString &label );
159  bool updateRangeUpperValue( int rangeIndex, double value );
160  bool updateRangeLowerValue( int rangeIndex, double value );
162  bool updateRangeRenderState( int rangeIndex, bool render );
163 
164  void addClass( QgsSymbol *symbol );
166  void addClass( const QgsRendererRange &range ) SIP_PYNAME( addClassRange );
168  void addClass( double lower, double upper ) SIP_PYNAME( addClassLowerUpper );
169 
177  void addBreak( double breakValue, bool updateSymbols = true );
178 
179  void deleteClass( int idx );
180  void deleteAllClasses();
181 
183  void moveClass( int from, int to );
184 
189  bool rangesOverlap() const;
190 
195  bool rangesHaveGaps() const;
196 
197  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
198  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
199 
200  enum Mode
201  {
207  Custom
208  };
209 
210  Mode mode() const { return mMode; }
211  void setMode( Mode mode ) { mMode = mode; }
217  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
218 
221  const QgsRendererRangeLabelFormat &labelFormat() const { return mLabelFormat; }
226  void setLabelFormat( const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges = false );
227 
231  void calculateLabelPrecision( bool updateRanges = true );
232 
243  static QgsGraduatedSymbolRenderer *createRenderer( QgsVectorLayer *vlayer,
244  const QString &attrName,
245  int classes,
246  Mode mode,
247  QgsSymbol *symbol SIP_TRANSFER,
248  QgsColorRamp *ramp SIP_TRANSFER,
250 
252  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
253 
254  virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
255  QgsLegendSymbolList legendSymbolItems() const override;
256  virtual QSet< QString > legendKeysForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
257 
263  QgsSymbol *sourceSymbol();
264 
271  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
272 
277  QgsColorRamp *sourceColorRamp();
278 
282  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
283 
288  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER = 0 );
289 
295  void updateSymbols( QgsSymbol *sym );
296 
300  void setSymbolSizes( double minSize, double maxSize );
301 
304  double minSymbolSize() const;
305 
308  double maxSymbolSize() const;
309 
311  {
312  GraduatedColor = 0,
313  GraduatedSize = 1
314  };
315 
318  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
319 
322  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
323 
324  virtual bool legendSymbolItemsCheckable() const override;
325  virtual bool legendSymbolItemChecked( const QString &key ) override;
326  virtual void checkLegendSymbolItem( const QString &key, bool state = true ) override;
327  virtual void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
328  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
329 
333  static QgsGraduatedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
334 
345  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
346 
352  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
353 
354  protected:
355  QString mAttrName;
358  std::unique_ptr<QgsSymbol> mSourceSymbol;
359  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
361 
362  std::unique_ptr<QgsExpression> mExpression;
365  int mAttrNum;
366  bool mCounting;
367 
368  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
369 
370  QgsSymbol *symbolForValue( double value );
371 
374  QString legendKeyForValue( double value ) const;
375 
377  static const char *graduatedMethodStr( GraduatedMethod method ) SIP_SKIP;
378 
379  private:
380 
383  QVariant valueForFeature( QgsFeature &feature, QgsRenderContext &context ) const;
384 
386  QgsLegendSymbolList baseLegendSymbolItems() const;
387 
388 #ifdef SIP_RUN
391 #endif
392 
393 };
394 
395 #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:81
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:347
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:86
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.cc:425
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.