QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 "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( const QgsFeature &feature, QgsRenderContext &context ) const override;
151  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const 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  bool filterNeedsGeometry() const override;
156  QString dump() const override;
157  QgsGraduatedSymbolRenderer *clone() const override SIP_FACTORY;
158  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
159  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
160  QgsSymbolList symbols( QgsRenderContext &context ) const override;
161 
162  QString classAttribute() const { return mAttrName; }
163  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
164 
165  const QgsRangeList &ranges() const { return mRanges; }
166 
167  bool updateRangeSymbol( int rangeIndex, QgsSymbol *symbol SIP_TRANSFER );
168  bool updateRangeLabel( int rangeIndex, const QString &label );
169  bool updateRangeUpperValue( int rangeIndex, double value );
170  bool updateRangeLowerValue( int rangeIndex, double value );
172  bool updateRangeRenderState( int rangeIndex, bool render );
173 
174  void addClass( QgsSymbol *symbol );
176  void addClass( const QgsRendererRange &range ) SIP_PYNAME( addClassRange );
178  void addClass( double lower, double upper ) SIP_PYNAME( addClassLowerUpper );
179 
188  void addBreak( double breakValue, bool updateSymbols = true );
189 
190  void deleteClass( int idx );
191  void deleteAllClasses();
192 
194  void moveClass( int from, int to );
195 
201  bool rangesOverlap() const;
202 
208  bool rangesHaveGaps() const;
209 
210  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
211  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
212 
213  enum Mode
214  {
220  Custom
221  };
222 
223  Mode mode() const { return mMode; }
224  void setMode( Mode mode ) { mMode = mode; }
225 
230  bool useSymmetricMode() const { return mUseSymmetricMode; }
231 
236  void setUseSymmetricMode( bool useSymmetricMode ) { mUseSymmetricMode = useSymmetricMode; }
237 
242  double symmetryPoint() const { return mSymmetryPoint; }
243 
248  void setSymmetryPoint( double symmetryPoint ) { mSymmetryPoint = symmetryPoint; }
249 
255  QStringList listForCboPrettyBreaks() const { return mListForCboPrettyBreaks; } SIP_SKIP
256 
262  void setListForCboPrettyBreaks( const QStringList &listForCboPrettyBreaks ) { mListForCboPrettyBreaks = listForCboPrettyBreaks; } SIP_SKIP
263 
268  bool astride() const { return mAstride; }
269 
274  void setAstride( bool astride ) { mAstride = astride; }
275 
284  static void makeBreaksSymmetric( QList<double> &breaks, double symmetryPoint, bool astride );
285 
295  static QList<double> calcEqualIntervalBreaks( double minimum, double maximum, int classes, bool useSymmetricMode, double symmetryPoint, bool astride );
296 
307  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses, bool useSymmetricMode = false, double symmetryPoint = 0.0, bool astride = false );
308 
313  const QgsRendererRangeLabelFormat &labelFormat() const { return mLabelFormat; }
314 
321  void setLabelFormat( const QgsRendererRangeLabelFormat &labelFormat, bool updateRanges = false );
322 
328  void calculateLabelPrecision( bool updateRanges = true );
329 
345  static QgsGraduatedSymbolRenderer *createRenderer( QgsVectorLayer *vlayer,
346  const QString &attrName,
347  int classes,
348  Mode mode,
349  QgsSymbol *symbol SIP_TRANSFER,
350  QgsColorRamp *ramp SIP_TRANSFER,
352  bool useSymmetricMode = false,
353  double symmetryPoint = 0.0,
354  QStringList listForCboPrettyBreaks = QStringList(),
355  bool astride = false );
356 
358  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
359 
360  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
361  QgsLegendSymbolList legendSymbolItems() const override;
362  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
363 
370  QgsSymbol *sourceSymbol();
371 
379  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
380 
386  QgsColorRamp *sourceColorRamp();
387 
392  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
393 
399  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER = nullptr );
400 
407  void updateSymbols( QgsSymbol *sym );
408 
414  void setSymbolSizes( double minSize, double maxSize );
415 
420  double minSymbolSize() const;
421 
426  double maxSymbolSize() const;
427 
429  {
430  GraduatedColor = 0,
431  GraduatedSize = 1
432  };
433 
438  GraduatedMethod graduatedMethod() const { return mGraduatedMethod; }
439 
444  void setGraduatedMethod( GraduatedMethod method ) { mGraduatedMethod = method; }
445 
446  bool legendSymbolItemsCheckable() const override;
447  bool legendSymbolItemChecked( const QString &key ) override;
448  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
449  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
450  QString legendClassificationAttribute() const override { return classAttribute(); }
451 
457  static QgsGraduatedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
458 
469  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
470 
476  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
477 
478  protected:
479  QString mAttrName;
481  Mode mMode = Custom;
482  std::unique_ptr<QgsSymbol> mSourceSymbol;
483  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
485 
486  std::unique_ptr<QgsExpression> mExpression;
487  GraduatedMethod mGraduatedMethod = GraduatedColor;
489 
490  int mAttrNum = -1;
491  bool mCounting = false;
492 
493  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
494 
498  QgsSymbol *symbolForValue( double value ) const;
499 
503  QString legendKeyForValue( double value ) const;
504 
506  static const char *graduatedMethodStr( GraduatedMethod method ) SIP_SKIP;
507 
508  bool mUseSymmetricMode = false;
509  double mSymmetryPoint = 0.0;
510  QStringList mListForCboPrettyBreaks = QStringList();
511  bool mAstride = false;
512 
513  private:
514 
518  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
519 
521  QgsLegendSymbolList baseLegendSymbolItems() const;
522 
523 #ifdef SIP_RUN
526 #endif
527 
528 };
529 
530 #endif // QGSGRADUATEDSYMBOLRENDERER_H
const QgsRendererRangeLabelFormat & labelFormat() const
Returns 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.
int precision
std::unique_ptr< QgsSymbol > mSourceSymbol
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
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:300
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:244
void setSymmetryPoint(double symmetryPoint)
Set the pivot point.
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
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
void setAstride(bool astride)
Set if we want a central class astride the pivot value.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
std::unique_ptr< QgsExpression > mExpression
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
void setTrimTrailingZeroes(bool trimTrailingZeroes)
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
void setUseSymmetricMode(bool useSymmetricMode)
Set if we want to classify symmetric around a given value.
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
void setListForCboPrettyBreaks(const QStringList &listForCboPrettyBreaks)
Set the list of breaks used in the prettybreaks mode, which is needed to recover this list in saved c...
#define SIP_SKIP
Definition: qgis_sip.h:119
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
bool astride() const
Returns if we want to have a central class astride the pivot value.
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
#define SIP_FACTORY
Definition: qgis_sip.h:69
double symmetryPoint() const
Returns the pivot value for symmetric classification.
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)
Contains information about the context of a rendering operation.
QgsRendererRangeLabelFormat mLabelFormat
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)
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:93
bool useSymmetricMode() const
Returns if we want to classify symmetric around a given value.
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
virtual QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns legend keys matching a specified feature.
Definition: qgsrenderer.cpp:86
std::unique_ptr< QgsSymbol > mSymbol
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
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.
QStringList listForCboPrettyBreaks() const
Returns the list of breaks used in the prettybreaks mode.