QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 QVariantMap &props = QVariantMap() ) 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 
61  QString classAttribute() const { return mAttrName; }
62 
68  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
69 
73  const QgsRangeList &ranges() const { return mRanges; }
74 
75  bool updateRangeSymbol( int rangeIndex, QgsSymbol *symbol SIP_TRANSFER );
76  bool updateRangeLabel( int rangeIndex, const QString &label );
77  bool updateRangeUpperValue( int rangeIndex, double value );
78  bool updateRangeLowerValue( int rangeIndex, double value );
80  bool updateRangeRenderState( int rangeIndex, bool render );
81 
82  void addClass( QgsSymbol *symbol );
84  void addClass( const QgsRendererRange &range ) SIP_PYNAME( addClassRange );
86  void addClass( double lower, double upper ) SIP_PYNAME( addClassLowerUpper );
87 
96  void addBreak( double breakValue, bool updateSymbols = true );
97 
98  void deleteClass( int idx );
99  void deleteAllClasses();
100 
102  void moveClass( int from, int to );
103 
109  bool rangesOverlap() const;
110 
116  bool rangesHaveGaps() const;
117 
118  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
119  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
120 
125  QgsClassificationMethod *classificationMethod() const;
126 
132  void setClassificationMethod( QgsClassificationMethod *method SIP_TRANSFER );
133 
138  enum Mode
139  {
145  Custom
146  };
147  // TODO QGIS 4: remove
148  // this could not be tagged with Q_DECL_DEPRECATED due to Doxygen warning
149  // might be fixed in newer Doxygen (does not on 1.8.13, might be ok on 1.8.16)
150 
151 
153  Q_DECL_DEPRECATED Mode mode() const SIP_DEPRECATED { return modeFromMethodId( mClassificationMethod->id() ); }
155  Q_DECL_DEPRECATED void setMode( Mode mode ) SIP_DEPRECATED;
156 
162  Q_DECL_DEPRECATED bool useSymmetricMode() const SIP_DEPRECATED { return mClassificationMethod->symmetricModeEnabled(); }
163 
169  Q_DECL_DEPRECATED void setUseSymmetricMode( bool useSymmetricMode ) SIP_DEPRECATED;
170 
176  Q_DECL_DEPRECATED double symmetryPoint() const SIP_DEPRECATED { return mClassificationMethod->symmetryPoint(); }
177 
183  Q_DECL_DEPRECATED void setSymmetryPoint( double symmetryPoint ) SIP_DEPRECATED;
184 
185 
191  Q_DECL_DEPRECATED bool astride() const SIP_DEPRECATED { return mClassificationMethod->symmetryAstride(); }
192 
198  Q_DECL_DEPRECATED void setAstride( bool astride ) SIP_DEPRECATED;
199 
209  Q_DECL_DEPRECATED static void makeBreaksSymmetric( QList<double> &breaks SIP_INOUT, double symmetryPoint, bool astride ) SIP_DEPRECATED;
210 
221  Q_DECL_DEPRECATED static QList<double> calcEqualIntervalBreaks( double minimum, double maximum, int classes, bool useSymmetricMode, double symmetryPoint, bool astride ) SIP_DEPRECATED;
222 
234  Q_DECL_DEPRECATED void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses, bool useSymmetricMode = false, double symmetryPoint = 0.0, bool astride = false ) SIP_DEPRECATED;
235 
241  void updateClasses( const QgsVectorLayer *vl, int nclasses );
242 
244 
250  Q_DECL_DEPRECATED QgsRendererRangeLabelFormat labelFormat() const SIP_DEPRECATED;
251 
259  Q_DECL_DEPRECATED void setLabelFormat( const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges = false ) SIP_DEPRECATED;
260 
262 
268  void calculateLabelPrecision( bool updateRanges = true );
269 
271 
288  Q_DECL_DEPRECATED static QgsGraduatedSymbolRenderer *createRenderer( QgsVectorLayer *vlayer,
289  const QString &attrName,
290  int classes,
291  Mode mode,
292  QgsSymbol *symbol SIP_TRANSFER,
295  bool useSymmetricMode = false,
296  double symmetryPoint = 0.0,
297  const QStringList &listForCboPrettyBreaks = QStringList(),
298  bool astride = false ) SIP_DEPRECATED;
300 
302  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
303 
304  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
305  QgsLegendSymbolList legendSymbolItems() const override;
306  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
307  QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
308 
315  QgsSymbol *sourceSymbol();
316 
324  const QgsSymbol *sourceSymbol() const SIP_SKIP;
325 
333  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
334 
340  QgsColorRamp *sourceColorRamp();
341 
348  const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
349 
354  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
355 
361  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER = nullptr );
362 
369  void updateSymbols( QgsSymbol *sym );
370 
376  void setSymbolSizes( double minSize, double maxSize );
377 
382  double minSymbolSize() const;
383 
388  double maxSymbolSize() const;
389 
396  Qgis::GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
397 
404  void setGraduatedMethod( Qgis::GraduatedMethod method ) { mGraduatedMethod = method; }
405 
406  bool legendSymbolItemsCheckable() const override;
407  bool legendSymbolItemChecked( const QString &key ) override;
408  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
409  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
410  QString legendClassificationAttribute() const override { return classAttribute(); }
411 
417  static QgsGraduatedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
418 
429  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
430 
436  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
437 
442  void updateRangeLabels();
443 
450  const QgsRendererRange *rangeForValue( double value ) const;
451 
452  protected:
453  QString mAttrName;
455  std::unique_ptr<QgsSymbol> mSourceSymbol;
456  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
457 
458  std::unique_ptr<QgsExpression> mExpression;
459  Qgis::GraduatedMethod mGraduatedMethod = Qgis::GraduatedMethod::Color;
461 
462  int mAttrNum = -1;
463  bool mCounting = false;
464 
465  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
466 
470  QgsSymbol *symbolForValue( double value ) const;
471 
475  QString legendKeyForValue( double value ) const;
476 
478  static QString graduatedMethodStr( Qgis::GraduatedMethod method ) SIP_SKIP;
479 
480  std::shared_ptr<QgsClassificationMethod> mClassificationMethod;
481 
482  private:
483 
487  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
488 
490  QgsLegendSymbolList baseLegendSymbolItems() const;
491 
492  // TODO QGIS 4: remove
494  static QString methodIdFromMode( QgsGraduatedSymbolRenderer::Mode mode );
495  static QgsGraduatedSymbolRenderer::Mode modeFromMethodId( const QString &methodId );
497 
498 #ifdef SIP_RUN
501 #endif
502 
503  friend class TestQgsGraduatedSymbolRenderer;
504 
505 };
506 
507 #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:365
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
QgsFeatureRenderer::Filter
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
Definition: qgsrenderer.h:265
QgsGraduatedSymbolRenderer::mDataDefinedSizeLegend
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
Definition: qgsgraduatedsymbolrenderer.h:465
QgsFeatureRenderer::originalSymbolForFeature
virtual QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns symbol for feature.
Definition: qgsrenderer.cpp:84
QgsRendererRangeLabelFormat
Definition: qgsrendererrange.h:200
QgsClassificationMethod
QgsClassificationMethod is an abstract class for implementations of classification methods.
Definition: qgsclassificationmethod.h:88
QgsGraduatedSymbolRenderer::Mode
Mode
Classification mode.
Definition: qgsgraduatedsymbolrenderer.h:138
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsGraduatedSymbolRenderer::classAttribute
QString classAttribute() const
Returns the attribute name (or expression) used for the classification.
Definition: qgsgraduatedsymbolrenderer.h:61
QgsGraduatedSymbolRenderer::mSourceSymbol
std::unique_ptr< QgsSymbol > mSourceSymbol
Definition: qgsgraduatedsymbolrenderer.h:455
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:489
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsRendererRange
Definition: qgsrendererrange.h:36
QgsDataDefinedSizeLegend
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Definition: qgsdatadefinedsizelegend.h:42
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsFeatureRenderer::usedAttributes
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
Definition: qgsstyleentityvisitor.h:33
QgsGraduatedSymbolRenderer::setGraduatedMethod
void setGraduatedMethod(Qgis::GraduatedMethod method)
Set the method used for graduation (either size or color).
Definition: qgsgraduatedsymbolrenderer.h:404
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
MathUtils::lower
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
Definition: MathUtils.cpp:407
QgsGraduatedSymbolRenderer::EqualInterval
@ EqualInterval
Definition: qgsgraduatedsymbolrenderer.h:140
QgsGraduatedSymbolRenderer::Quantile
@ Quantile
Definition: qgsgraduatedsymbolrenderer.h:141
QgsGraduatedSymbolRenderer::symmetryPoint
Q_DECL_DEPRECATED double symmetryPoint() const
Returns the pivot value for symmetric classification.
Definition: qgsgraduatedsymbolrenderer.h:176
QgsGraduatedSymbolRenderer::mAttrName
QString mAttrName
Definition: qgsgraduatedsymbolrenderer.h:453
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:110
QgsGraduatedSymbolRenderer::Pretty
@ Pretty
Definition: qgsgraduatedsymbolrenderer.h:144
QgsFeatureRenderer::legendSymbolItemChecked
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
Definition: qgsrenderer.cpp:353
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsGraduatedSymbolRenderer::mExpression
std::unique_ptr< QgsExpression > mExpression
Definition: qgsgraduatedsymbolrenderer.h:458
QgsFeatureRenderer::legendSymbolItemsCheckable
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
Definition: qgsrenderer.cpp:348
QgsGraduatedSymbolRenderer::mClassificationMethod
std::shared_ptr< QgsClassificationMethod > mClassificationMethod
Definition: qgsgraduatedsymbolrenderer.h:480
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:2820
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:122
QgsRangeList
QList< QgsRendererRange > QgsRangeList
Definition: qgsrendererrange.h:191
QgsFeatureRenderer::checkLegendSymbolItem
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
Definition: qgsrenderer.cpp:359
QgsGraduatedSymbolRenderer::mode
Q_DECL_DEPRECATED Mode mode() const
Definition: qgsgraduatedsymbolrenderer.h:153
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
QgsGraduatedSymbolRenderer::StdDev
@ StdDev
Definition: qgsgraduatedsymbolrenderer.h:143
QgsGraduatedSymbolRenderer
Definition: qgsgraduatedsymbolrenderer.h:35
QgsFeatureRenderer::toSld
virtual void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
Definition: qgsrenderer.h:326
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:151
QgsGraduatedSymbolRenderer::mSourceColorRamp
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Definition: qgsgraduatedsymbolrenderer.h:456
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:191
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
qgsclassificationmethod.h
QgsGraduatedSymbolRenderer::ranges
const QgsRangeList & ranges() const
Returns a list of all ranges used in the classification.
Definition: qgsgraduatedsymbolrenderer.h:73
QgsGraduatedSymbolRenderer::setClassAttribute
void setClassAttribute(const QString &attr)
Sets the attribute name (or expression) used for the classification.
Definition: qgsgraduatedsymbolrenderer.h:68
Qgis::GraduatedMethod
GraduatedMethod
Methods for modifying symbols by range in a graduated symbol renderer.
Definition: qgis.h:1561
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsGraduatedSymbolRenderer::Jenks
@ Jenks
Definition: qgsgraduatedsymbolrenderer.h:142
qgsrendererrange.h
QgsGraduatedSymbolRenderer::useSymmetricMode
Q_DECL_DEPRECATED bool useSymmetricMode() const
Returns if we want to classify symmetric around a given value.
Definition: qgsgraduatedsymbolrenderer.h:162
QgsGraduatedSymbolRenderer::mRanges
QgsRangeList mRanges
Definition: qgsgraduatedsymbolrenderer.h:454
Qgis
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:71
QgsFeatureRenderer::dump
virtual QString dump() const
Returns debug information about this renderer.
Definition: qgsrenderer.cpp:146
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
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:96
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2819
QgsGraduatedSymbolRenderer::legendClassificationAttribute
QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.
Definition: qgsgraduatedsymbolrenderer.h:410