QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 QGSGRADUATEDSYMBOLRENDERER_H
16 #define QGSGRADUATEDSYMBOLRENDERER_H
17 
18 #include "qgis_core.h"
19 #include "qgis_sip.h"
20 #include "qgis.h"
21 #include "qgsrenderer.h"
22 #include "qgsrendererrange.h"
24 
25 class QgsVectorLayer;
26 class QgsColorRamp;
28 class QgsSymbol;
29 class QgsExpression;
30 
36 {
37  public:
38 
39  QgsGraduatedSymbolRenderer( const QString &attrName = QString(), const QgsRangeList &ranges = QgsRangeList() );
40 
41  ~QgsGraduatedSymbolRenderer() override;
42 
43  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
44  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
45  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
46  void stopRender( QgsRenderContext &context ) override;
47  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
48  bool filterNeedsGeometry() const override;
49  QString dump() const override;
51  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
52  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
53  QgsSymbolList symbols( QgsRenderContext &context ) const override;
54  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
55 
56  QString classAttribute() const { return mAttrName; }
57  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
58 
59  const QgsRangeList &ranges() const { return mRanges; }
60 
61  bool updateRangeSymbol( int rangeIndex, QgsSymbol *symbol SIP_TRANSFER );
62  bool updateRangeLabel( int rangeIndex, const QString &label );
63  bool updateRangeUpperValue( int rangeIndex, double value );
64  bool updateRangeLowerValue( int rangeIndex, double value );
66  bool updateRangeRenderState( int rangeIndex, bool render );
67 
68  void addClass( QgsSymbol *symbol );
70  void addClass( const QgsRendererRange &range ) SIP_PYNAME( addClassRange );
72  void addClass( double lower, double upper ) SIP_PYNAME( addClassLowerUpper );
73 
82  void addBreak( double breakValue, bool updateSymbols = true );
83 
84  void deleteClass( int idx );
85  void deleteAllClasses();
86 
88  void moveClass( int from, int to );
89 
95  bool rangesOverlap() const;
96 
102  bool rangesHaveGaps() const;
103 
104  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
105  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
106 
111  QgsClassificationMethod *classificationMethod() const;
112 
118  void setClassificationMethod( QgsClassificationMethod *method SIP_TRANSFER );
119 
124  enum Mode
125  {
131  Custom
132  };
133  // TODO QGIS 4: remove
134  // this could not be tagged with Q_DECL_DEPRECATED due to Doxygen warning
135  // might be fixed in newer Doxygen (does not on 1.8.13, might be ok on 1.8.16)
136 
137 
139  Q_DECL_DEPRECATED Mode mode() const SIP_DEPRECATED { return modeFromMethodId( mClassificationMethod->id() ); }
141  Q_DECL_DEPRECATED void setMode( Mode mode ) SIP_DEPRECATED;
142 
148  Q_DECL_DEPRECATED bool useSymmetricMode() const SIP_DEPRECATED { return mClassificationMethod->symmetricModeEnabled(); }
149 
155  Q_DECL_DEPRECATED void setUseSymmetricMode( bool useSymmetricMode ) SIP_DEPRECATED;
156 
162  Q_DECL_DEPRECATED double symmetryPoint() const SIP_DEPRECATED { return mClassificationMethod->symmetryPoint(); }
163 
169  Q_DECL_DEPRECATED void setSymmetryPoint( double symmetryPoint ) SIP_DEPRECATED;
170 
171 
177  Q_DECL_DEPRECATED bool astride() const SIP_DEPRECATED { return mClassificationMethod->symmetryAstride(); }
178 
184  Q_DECL_DEPRECATED void setAstride( bool astride ) SIP_DEPRECATED;
185 
195  Q_DECL_DEPRECATED static void makeBreaksSymmetric( QList<double> &breaks SIP_INOUT, double symmetryPoint, bool astride ) SIP_DEPRECATED;
196 
207  Q_DECL_DEPRECATED static QList<double> calcEqualIntervalBreaks( double minimum, double maximum, int classes, bool useSymmetricMode, double symmetryPoint, bool astride ) SIP_DEPRECATED;
208 
220  Q_DECL_DEPRECATED void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses, bool useSymmetricMode = false, double symmetryPoint = 0.0, bool astride = false ) SIP_DEPRECATED;
221 
227  void updateClasses( const QgsVectorLayer *vl, int nclasses );
228 
230 
236  Q_DECL_DEPRECATED QgsRendererRangeLabelFormat labelFormat() const SIP_DEPRECATED;
237 
245  Q_DECL_DEPRECATED void setLabelFormat( const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges = false ) SIP_DEPRECATED;
246 
248 
254  void calculateLabelPrecision( bool updateRanges = true );
255 
257 
274  static Q_DECL_DEPRECATED QgsGraduatedSymbolRenderer *createRenderer( QgsVectorLayer *vlayer,
275  const QString &attrName,
276  int classes,
277  Mode mode,
278  QgsSymbol *symbol SIP_TRANSFER,
281  bool useSymmetricMode = false,
282  double symmetryPoint = 0.0,
283  const QStringList &listForCboPrettyBreaks = QStringList(),
284  bool astride = false ) SIP_DEPRECATED;
286 
288  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
289 
290  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
291  QgsLegendSymbolList legendSymbolItems() const override;
292  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
293 
300  QgsSymbol *sourceSymbol();
301 
309  const QgsSymbol *sourceSymbol() const SIP_SKIP;
310 
318  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
319 
325  QgsColorRamp *sourceColorRamp();
326 
333  const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
334 
339  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
340 
346  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER = nullptr );
347 
354  void updateSymbols( QgsSymbol *sym );
355 
361  void setSymbolSizes( double minSize, double maxSize );
362 
367  double minSymbolSize() const;
368 
373  double maxSymbolSize() const;
374 
376  {
377  GraduatedColor = 0,
378  GraduatedSize = 1
379  };
380 
385  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
386 
391  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
392 
393  bool legendSymbolItemsCheckable() const override;
394  bool legendSymbolItemChecked( const QString &key ) override;
395  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
396  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
397  QString legendClassificationAttribute() const override { return classAttribute(); }
398 
404  static QgsGraduatedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
405 
416  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
417 
423  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
424 
429  void updateRangeLabels();
430 
437  const QgsRendererRange *rangeForValue( double value ) const;
438 
439  protected:
440  QString mAttrName;
442  std::unique_ptr<QgsSymbol> mSourceSymbol;
443  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
444 
445  std::unique_ptr<QgsExpression> mExpression;
446  GraduatedMethod mGraduatedMethod = GraduatedColor;
448 
449  int mAttrNum = -1;
450  bool mCounting = false;
451 
452  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
453 
457  QgsSymbol *symbolForValue( double value ) const;
458 
462  QString legendKeyForValue( double value ) const;
463 
465  static QString graduatedMethodStr( GraduatedMethod method ) SIP_SKIP;
466 
467  std::shared_ptr<QgsClassificationMethod> mClassificationMethod;
468 
469  private:
470 
474  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
475 
477  QgsLegendSymbolList baseLegendSymbolItems() const;
478 
479  // TODO QGIS 4: remove
481  static QString methodIdFromMode( QgsGraduatedSymbolRenderer::Mode mode );
482  static QgsGraduatedSymbolRenderer::Mode modeFromMethodId( const QString &methodId );
484 
485 #ifdef SIP_RUN
488 #endif
489 
490  friend class TestQgsGraduatedSymbolRenderer;
491 
492 };
493 
494 #endif // QGSGRADUATEDSYMBOLRENDERER_H
QgsGraduatedSymbolRenderer::capabilities
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
Definition: qgsgraduatedsymbolrenderer.h:52
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsFeatureRenderer::setLegendSymbolItem
virtual void setLegendSymbolItem(const QString &key, QgsSymbol *symbol)
Sets the symbol to be used for a legend symbol item.
Definition: qgsrenderer.cpp:347
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QgsFeatureRenderer::Filter
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
Definition: qgsrenderer.h:256
QgsGraduatedSymbolRenderer::mDataDefinedSizeLegend
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
Definition: qgsgraduatedsymbolrenderer.h:452
QgsFeatureRenderer::originalSymbolForFeature
virtual QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns symbol for feature.
Definition: qgsrenderer.cpp:81
QgsRendererRangeLabelFormat
Definition: qgsrendererrange.h:107
QgsClassificationMethod
Definition: qgsclassificationmethod.h:87
QgsGraduatedSymbolRenderer::Mode
Mode
Classification mode.
Definition: qgsgraduatedsymbolrenderer.h:124
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsGraduatedSymbolRenderer::classAttribute
QString classAttribute() const
Definition: qgsgraduatedsymbolrenderer.h:56
QgsGraduatedSymbolRenderer::setGraduatedMethod
void setGraduatedMethod(GraduatedMethod method)
set the method used for graduation (either size or color)
Definition: qgsgraduatedsymbolrenderer.h:391
QgsGraduatedSymbolRenderer::mSourceSymbol
std::unique_ptr< QgsSymbol > mSourceSymbol
Definition: qgsgraduatedsymbolrenderer.h:442
QgsFeatureRenderer::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
Definition: qgsrenderer.cpp:465
QgsFields
Definition: qgsfields.h:44
QgsRendererRange
Definition: qgsrendererrange.h:36
QgsDataDefinedSizeLegend
Definition: qgsdatadefinedsizelegend.h:41
qgis.h
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsFeatureRenderer::usedAttributes
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
QgsStyleEntityVisitorInterface
Definition: qgsstyleentityvisitor.h:33
QgsSymbol
Definition: qgssymbol.h:63
MathUtils::lower
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
Definition: MathUtils.cpp:407
QgsGraduatedSymbolRenderer::EqualInterval
@ EqualInterval
Definition: qgsgraduatedsymbolrenderer.h:126
QgsFeatureRenderer::toSld
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:312
QgsGraduatedSymbolRenderer::Quantile
@ Quantile
Definition: qgsgraduatedsymbolrenderer.h:127
QgsGraduatedSymbolRenderer::symmetryPoint
Q_DECL_DEPRECATED double symmetryPoint() const
Returns the pivot value for symmetric classification.
Definition: qgsgraduatedsymbolrenderer.h:162
QgsGraduatedSymbolRenderer::mAttrName
QString mAttrName
Definition: qgsgraduatedsymbolrenderer.h:440
QgsFeatureRenderer::stopRender
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
Definition: qgsrenderer.cpp:107
QgsGraduatedSymbolRenderer::Pretty
@ Pretty
Definition: qgsgraduatedsymbolrenderer.h:130
QgsFeatureRenderer::legendSymbolItemChecked
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
Definition: qgsrenderer.cpp:335
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsGraduatedSymbolRenderer::mExpression
std::unique_ptr< QgsExpression > mExpression
Definition: qgsgraduatedsymbolrenderer.h:445
QgsFeatureRenderer::legendSymbolItemsCheckable
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
Definition: qgsrenderer.cpp:330
QgsGraduatedSymbolRenderer::mClassificationMethod
std::shared_ptr< QgsClassificationMethod > mClassificationMethod
Definition: qgsgraduatedsymbolrenderer.h:467
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:752
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsFeatureRenderer::filterNeedsGeometry
virtual bool filterNeedsGeometry() const
Returns true if this renderer requires the geometry to apply the filter.
Definition: qgsrenderer.cpp:114
QgsRangeList
QList< QgsRendererRange > QgsRangeList
Definition: qgsrendererrange.h:98
QgsFeatureRenderer::checkLegendSymbolItem
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
Definition: qgsrenderer.cpp:341
QgsGraduatedSymbolRenderer::mode
Q_DECL_DEPRECATED Mode mode() const
Definition: qgsgraduatedsymbolrenderer.h:139
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:45
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
QgsGraduatedSymbolRenderer::StdDev
@ StdDev
Definition: qgsgraduatedsymbolrenderer.h:129
QgsGraduatedSymbolRenderer
Definition: qgsgraduatedsymbolrenderer.h:35
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsFeatureRenderer::symbols
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
Definition: qgsrenderer.cpp:143
QgsGraduatedSymbolRenderer::mSourceColorRamp
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Definition: qgsgraduatedsymbolrenderer.h:443
QgsFeatureRenderer::symbolForFeature
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
qgsrenderer.h
QgsGraduatedSymbolRenderer::astride
Q_DECL_DEPRECATED bool astride() const
Returns if we want to have a central class astride the pivot value.
Definition: qgsgraduatedsymbolrenderer.h:177
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
qgsclassificationmethod.h
QgsGraduatedSymbolRenderer::ranges
const QgsRangeList & ranges() const
Definition: qgsgraduatedsymbolrenderer.h:59
QgsGraduatedSymbolRenderer::setClassAttribute
void setClassAttribute(const QString &attr)
Definition: qgsgraduatedsymbolrenderer.h:57
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsFeatureRenderer
Definition: qgsrenderer.h:102
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsGraduatedSymbolRenderer::Jenks
@ Jenks
Definition: qgsgraduatedsymbolrenderer.h:128
qgsrendererrange.h
QgsGraduatedSymbolRenderer::useSymmetricMode
Q_DECL_DEPRECATED bool useSymmetricMode() const
Returns if we want to classify symmetric around a given value.
Definition: qgsgraduatedsymbolrenderer.h:148
QgsGraduatedSymbolRenderer::mRanges
QgsRangeList mRanges
Definition: qgsgraduatedsymbolrenderer.h:441
QgsGraduatedSymbolRenderer::graduatedMethod
GraduatedMethod graduatedMethod() const
Returns the method used for graduation (either size or color)
Definition: qgsgraduatedsymbolrenderer.h:385
QgsFeatureRenderer::dump
virtual QString dump() const
Returns debug information about this renderer.
Definition: qgsrenderer.cpp:138
QgsFeature
Definition: qgsfeature.h:55
QgsExpression
Definition: qgsexpression.h:113
QgsLegendSymbolList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition: qgslegendsymbolitem.h:144
QgsFeatureRenderer::startRender
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:93
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:751
QgsGraduatedSymbolRenderer::legendClassificationAttribute
QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.
Definition: qgsgraduatedsymbolrenderer.h:397
QgsGraduatedSymbolRenderer::GraduatedMethod
GraduatedMethod
Definition: qgsgraduatedsymbolrenderer.h:375