QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 "qgssymbol.h"
22 #include "qgsrenderer.h"
23 #include "qgsrendererrange.h"
24 #include "qgsexpression.h"
27 
28 
29 class QgsVectorLayer;
30 class QgsColorRamp;
31 
37 {
38  public:
39 
40  QgsGraduatedSymbolRenderer( const QString &attrName = QString(), const QgsRangeList &ranges = QgsRangeList() );
41 
42  ~QgsGraduatedSymbolRenderer() override;
43 
44  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
45  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
46  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
47  void stopRender( QgsRenderContext &context ) override;
48  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
49  bool filterNeedsGeometry() const override;
50  QString dump() const override;
52  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
53  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
54  QgsSymbolList symbols( QgsRenderContext &context ) const override;
55  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
56 
57  QString classAttribute() const { return mAttrName; }
58  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
59 
60  const QgsRangeList &ranges() const { return mRanges; }
61 
62  bool updateRangeSymbol( int rangeIndex, QgsSymbol *symbol SIP_TRANSFER );
63  bool updateRangeLabel( int rangeIndex, const QString &label );
64  bool updateRangeUpperValue( int rangeIndex, double value );
65  bool updateRangeLowerValue( int rangeIndex, double value );
67  bool updateRangeRenderState( int rangeIndex, bool render );
68 
69  void addClass( QgsSymbol *symbol );
71  void addClass( const QgsRendererRange &range ) SIP_PYNAME( addClassRange );
73  void addClass( double lower, double upper ) SIP_PYNAME( addClassLowerUpper );
74 
83  void addBreak( double breakValue, bool updateSymbols = true );
84 
85  void deleteClass( int idx );
86  void deleteAllClasses();
87 
89  void moveClass( int from, int to );
90 
96  bool rangesOverlap() const;
97 
103  bool rangesHaveGaps() const;
104 
105  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
106  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
107 
112  QgsClassificationMethod *classificationMethod() const;
113 
119  void setClassificationMethod( QgsClassificationMethod *method SIP_TRANSFER );
120 
125  enum Mode
126  {
132  Custom
133  };
134  // TODO QGIS 4: remove
135  // this could not be tagged with Q_DECL_DEPRECATED due to Doxygen warning
136  // might be fixed in newer Doxygen (does not on 1.8.13, might be ok on 1.8.16)
137 
138 
140  Q_DECL_DEPRECATED Mode mode() const SIP_DEPRECATED { return modeFromMethodId( mClassificationMethod->id() ); }
142  Q_DECL_DEPRECATED void setMode( Mode mode ) SIP_DEPRECATED;
143 
149  Q_DECL_DEPRECATED bool useSymmetricMode() const SIP_DEPRECATED { return mClassificationMethod->symmetricModeEnabled(); }
150 
156  Q_DECL_DEPRECATED void setUseSymmetricMode( bool useSymmetricMode ) SIP_DEPRECATED;
157 
163  Q_DECL_DEPRECATED double symmetryPoint() const SIP_DEPRECATED { return mClassificationMethod->symmetryPoint(); }
164 
170  Q_DECL_DEPRECATED void setSymmetryPoint( double symmetryPoint ) SIP_DEPRECATED;
171 
172 
178  Q_DECL_DEPRECATED bool astride() const SIP_DEPRECATED { return mClassificationMethod->symmetryAstride(); }
179 
185  Q_DECL_DEPRECATED void setAstride( bool astride ) SIP_DEPRECATED;
186 
196  Q_DECL_DEPRECATED static void makeBreaksSymmetric( QList<double> &breaks SIP_INOUT, double symmetryPoint, bool astride ) SIP_DEPRECATED;
197 
208  Q_DECL_DEPRECATED static QList<double> calcEqualIntervalBreaks( double minimum, double maximum, int classes, bool useSymmetricMode, double symmetryPoint, bool astride ) SIP_DEPRECATED;
209 
221  Q_DECL_DEPRECATED void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses, bool useSymmetricMode = false, double symmetryPoint = 0.0, bool astride = false ) SIP_DEPRECATED;
222 
228  void updateClasses( const QgsVectorLayer *vl, int nclasses );
229 
231 
237  Q_DECL_DEPRECATED QgsRendererRangeLabelFormat labelFormat() const SIP_DEPRECATED;
238 
246  Q_DECL_DEPRECATED void setLabelFormat( const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges = false ) SIP_DEPRECATED;
247 
249 
255  void calculateLabelPrecision( bool updateRanges = true );
256 
258 
275  static Q_DECL_DEPRECATED QgsGraduatedSymbolRenderer *createRenderer( QgsVectorLayer *vlayer,
276  const QString &attrName,
277  int classes,
278  Mode mode,
279  QgsSymbol *symbol SIP_TRANSFER,
280  QgsColorRamp *ramp SIP_TRANSFER,
282  bool useSymmetricMode = false,
283  double symmetryPoint = 0.0,
284  const QStringList &listForCboPrettyBreaks = QStringList(),
285  bool astride = false ) SIP_DEPRECATED;
287 
289  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
290 
291  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
292  QgsLegendSymbolList legendSymbolItems() const override;
293  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
294 
301  QgsSymbol *sourceSymbol();
302 
310  const QgsSymbol *sourceSymbol() const SIP_SKIP;
311 
319  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
320 
326  QgsColorRamp *sourceColorRamp();
327 
334  const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
335 
340  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
341 
347  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER = nullptr );
348 
355  void updateSymbols( QgsSymbol *sym );
356 
362  void setSymbolSizes( double minSize, double maxSize );
363 
368  double minSymbolSize() const;
369 
374  double maxSymbolSize() const;
375 
377  {
378  GraduatedColor = 0,
379  GraduatedSize = 1
380  };
381 
386  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
387 
392  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
393 
394  bool legendSymbolItemsCheckable() const override;
395  bool legendSymbolItemChecked( const QString &key ) override;
396  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
397  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
398  QString legendClassificationAttribute() const override { return classAttribute(); }
399 
405  static QgsGraduatedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
406 
417  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
418 
424  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
425 
430  void updateRangeLabels();
431 
432  protected:
433  QString mAttrName;
435  std::unique_ptr<QgsSymbol> mSourceSymbol;
436  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
437 
438  std::unique_ptr<QgsExpression> mExpression;
439  GraduatedMethod mGraduatedMethod = GraduatedColor;
441 
442  int mAttrNum = -1;
443  bool mCounting = false;
444 
445  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
446 
450  QgsSymbol *symbolForValue( double value ) const;
451 
455  QString legendKeyForValue( double value ) const;
456 
458  static QString graduatedMethodStr( GraduatedMethod method ) SIP_SKIP;
459 
460  std::shared_ptr<QgsClassificationMethod> mClassificationMethod;
461 
462  private:
463 
467  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
468 
470  QgsLegendSymbolList baseLegendSymbolItems() const;
471 
472  // TODO QGIS 4: remove
474  static QString methodIdFromMode( QgsGraduatedSymbolRenderer::Mode mode );
475  static QgsGraduatedSymbolRenderer::Mode modeFromMethodId( const QString &methodId );
477 
478 #ifdef SIP_RUN
481 #endif
482 };
483 
484 #endif // QGSGRADUATEDSYMBOLRENDERER_H
The class is used as a container of context for various read/write operations on other objects...
std::unique_ptr< QgsSymbol > mSourceSymbol
QList< QgsRendererRange > QgsRangeList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
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:301
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
Definition: qgsrenderer.h:245
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:649
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
#define SIP_INOUT
Definition: qgis_sip.h:71
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
An interface for classes which can visit style entity (e.g.
std::unique_ptr< QgsExpression > mExpression
QMap< QString, QString > QgsStringMap
Definition: qgis.h:612
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
Q_DECL_DEPRECATED double symmetryPoint() const
Returns the pivot value for symmetric classification.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
Q_DECL_DEPRECATED Mode mode() const
#define SIP_SKIP
Definition: qgis_sip.h:126
virtual QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns symbol for feature.
Definition: qgsrenderer.cpp:81
#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:76
std::shared_ptr< QgsClassificationMethod > mClassificationMethod
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
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.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:650
Contains information about the context of a rendering operation.
Q_DECL_DEPRECATED bool useSymmetricMode() const
Returns if we want to classify symmetric around a given value.
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
GraduatedMethod graduatedMethod() const
Returns the method used for graduation (either size or color)
Q_DECL_DEPRECATED bool astride() const
Returns if we want to have a central class astride the pivot value.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:93
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
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
virtual bool filterNeedsGeometry() const
Returns true if this renderer requires the geometry to apply the filter.
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
QgsClassificationMethod is an abstract class for implementations of classification methods...
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
virtual QString dump() const
Returns debug information about this renderer.