QGIS API Documentation  3.0.2-Girona (307d082)
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 
33 class CORE_EXPORT QgsRendererRange
34 {
35  public:
36 
40  QgsRendererRange() = default;
41  QgsRendererRange( double lowerValue, double upperValue, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true );
42  QgsRendererRange( const QgsRendererRange &range );
43 
44  // default dtor is OK
45  QgsRendererRange &operator=( QgsRendererRange range );
46 
47  bool operator<( const QgsRendererRange &other ) const;
48 
49  double lowerValue() const;
50  double upperValue() const;
51 
52  QgsSymbol *symbol() const;
53  QString label() const;
54 
55  void setSymbol( QgsSymbol *s SIP_TRANSFER );
56  void setLabel( const QString &label );
57  void setLowerValue( double lowerValue );
58  void setUpperValue( double upperValue );
59 
60  // \since QGIS 2.5
61  bool renderState() const;
62  void setRenderState( bool render );
63 
64  // debugging
65  QString dump() const;
66 
75  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props, bool firstRange = false ) const;
76 
77  protected:
78  double mLowerValue = 0, mUpperValue = 0;
79  std::unique_ptr<QgsSymbol> mSymbol;
80  QString mLabel;
81  bool mRender = true;
82 
83  // for cpy+swap idiom
84  void swap( QgsRendererRange &other );
85 };
86 
87 typedef QList<QgsRendererRange> QgsRangeList;
88 
89 
95 class CORE_EXPORT QgsRendererRangeLabelFormat
96 {
97  public:
99  QgsRendererRangeLabelFormat( const QString &format, int precision = 4, bool trimTrailingZeroes = false );
100 
101  bool operator==( const QgsRendererRangeLabelFormat &other ) const;
102  bool operator!=( const QgsRendererRangeLabelFormat &other ) const;
103 
104  QString format() const { return mFormat; }
105  void setFormat( const QString &format ) { mFormat = format; }
106 
107  int precision() const { return mPrecision; }
108  void setPrecision( int precision );
109 
110  bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
111  void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
112 
114  QString labelForRange( double lower, double upper ) const SIP_PYNAME( labelForLowerUpper );
115  QString labelForRange( const QgsRendererRange &range ) const;
116  QString formatNumber( double value ) const;
117 
118  void setFromDomElement( QDomElement &element );
119  void saveToDomElement( QDomElement &element );
120 
121  static const int MAX_PRECISION;
122  static const int MIN_PRECISION;
123 
124  protected:
125  QString mFormat;
126  int mPrecision = 4;
127  bool mTrimTrailingZeroes = false;
128  // values used to manage number formatting - precision and trailing zeroes
129  double mNumberScale = 1.0;
130  QString mNumberSuffix;
133 };
134 
135 class QgsVectorLayer;
136 class QgsColorRamp;
137 
143 {
144  public:
145 
146  QgsGraduatedSymbolRenderer( const QString &attrName = QString(), const QgsRangeList &ranges = QgsRangeList() );
147 
148  ~QgsGraduatedSymbolRenderer() override;
149 
150  QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
151  QgsSymbol *originalSymbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
152  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
153  void stopRender( QgsRenderContext &context ) override;
154  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
155  QString dump() const override;
156  QgsGraduatedSymbolRenderer *clone() const override SIP_FACTORY;
157  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
158  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
159  QgsSymbolList symbols( QgsRenderContext &context ) override;
160 
161  QString classAttribute() const { return mAttrName; }
162  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
163 
164  const QgsRangeList &ranges() const { return mRanges; }
165 
166  bool updateRangeSymbol( int rangeIndex, QgsSymbol *symbol SIP_TRANSFER );
167  bool updateRangeLabel( int rangeIndex, const QString &label );
168  bool updateRangeUpperValue( int rangeIndex, double value );
169  bool updateRangeLowerValue( int rangeIndex, double value );
171  bool updateRangeRenderState( int rangeIndex, bool render );
172 
173  void addClass( QgsSymbol *symbol );
175  void addClass( const QgsRendererRange &range ) SIP_PYNAME( addClassRange );
177  void addClass( double lower, double upper ) SIP_PYNAME( addClassLowerUpper );
178 
187  void addBreak( double breakValue, bool updateSymbols = true );
188 
189  void deleteClass( int idx );
190  void deleteAllClasses();
191 
193  void moveClass( int from, int to );
194 
200  bool rangesOverlap() const;
201 
207  bool rangesHaveGaps() const;
208 
209  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
210  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
211 
212  enum Mode
213  {
219  Custom
220  };
221 
222  Mode mode() const { return mMode; }
223  void setMode( Mode mode ) { mMode = mode; }
224 
232  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
233 
238  const QgsRendererRangeLabelFormat &labelFormat() const { return mLabelFormat; }
239 
246  void setLabelFormat( const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges = false );
247 
253  void calculateLabelPrecision( bool updateRanges = true );
254 
266  static QgsGraduatedSymbolRenderer *createRenderer( QgsVectorLayer *vlayer,
267  const QString &attrName,
268  int classes,
269  Mode mode,
270  QgsSymbol *symbol SIP_TRANSFER,
271  QgsColorRamp *ramp SIP_TRANSFER,
273 
275  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
276 
277  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
278  QgsLegendSymbolList legendSymbolItems() const override;
279  QSet< QString > legendKeysForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
280 
287  QgsSymbol *sourceSymbol();
288 
296  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
297 
303  QgsColorRamp *sourceColorRamp();
304 
309  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
310 
316  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER = nullptr );
317 
324  void updateSymbols( QgsSymbol *sym );
325 
331  void setSymbolSizes( double minSize, double maxSize );
332 
337  double minSymbolSize() const;
338 
343  double maxSymbolSize() const;
344 
346  {
347  GraduatedColor = 0,
348  GraduatedSize = 1
349  };
350 
355  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
356 
361  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
362 
363  bool legendSymbolItemsCheckable() const override;
364  bool legendSymbolItemChecked( const QString &key ) override;
365  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
366  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
367  QString legendClassificationAttribute() const override { return classAttribute(); }
368 
374  static QgsGraduatedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
375 
386  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
387 
393  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
394 
395  protected:
396  QString mAttrName;
398  Mode mMode = Custom;
399  std::unique_ptr<QgsSymbol> mSourceSymbol;
400  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
402 
403  std::unique_ptr<QgsExpression> mExpression;
404  GraduatedMethod mGraduatedMethod = GraduatedColor;
406  int mAttrNum = -1;
407  bool mCounting = false;
408 
409  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
410 
411  QgsSymbol *symbolForValue( double value );
412 
416  QString legendKeyForValue( double value ) const;
417 
419  static const char *graduatedMethodStr( GraduatedMethod method ) SIP_SKIP;
420 
421  private:
422 
426  QVariant valueForFeature( QgsFeature &feature, QgsRenderContext &context ) const;
427 
429  QgsLegendSymbolList baseLegendSymbolItems() const;
430 
431 #ifdef SIP_RUN
434 #endif
435 
436 };
437 
438 #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:297
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:241
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:31
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:42
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
std::unique_ptr< QgsExpression > mExpression
QMap< QString, QString > QgsStringMap
Definition: qgis.h:479
void setTrimTrailingZeroes(bool trimTrailingZeroes)
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:266
virtual QgsSymbol * symbolForFeature(QgsFeature &feature, QgsRenderContext &context)=0
To be overridden.
Contains information about the context of a rendering operation.
QgsRendererRangeLabelFormat mLabelFormat
GraduatedMethod graduatedMethod() const
return the method used for graduation (either size or color)
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:92
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
std::unique_ptr< QgsSymbol > mSymbol
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context)
Return legend keys matching a specified feature.
Definition: qgsrenderer.cpp:85
QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
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
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
const QgsRangeList & ranges() const
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.