QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgscategorizedsymbolrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscategorizedsymbolrenderer.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 QGSCATEGORIZEDSYMBOLRENDERER_H
16 #define QGSCATEGORIZEDSYMBOLRENDERER_H
17 
18 #include "qgis_core.h"
19 #include "qgis.h"
20 #include "qgsrenderer.h"
21 
22 #include <QHash>
23 
24 class QgsVectorLayer;
25 class QgsStyle;
27 class QgsSymbol;
28 class QgsExpression;
29 class QgsColorRamp;
30 
35 class CORE_EXPORT QgsRendererCategory
36 {
37  public:
38 
42  QgsRendererCategory() = default;
43 
55  QgsRendererCategory( const QVariant &value, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true );
56 
61  QgsRendererCategory &operator=( QgsRendererCategory cat );
62 
70  QVariant value() const;
71 
76  QgsSymbol *symbol() const;
77 
83  QString label() const;
84 
92  void setValue( const QVariant &value );
93 
101  void setSymbol( QgsSymbol *s SIP_TRANSFER );
102 
108  void setLabel( const QString &label );
109 
115  bool renderState() const;
116 
122  void setRenderState( bool render );
123 
124  // debugging
125 
129  QString dump() const;
130 
134  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
135 
136  protected:
137  QVariant mValue;
138  std::unique_ptr<QgsSymbol> mSymbol;
139  QString mLabel;
140  bool mRender = true;
141 
142  void swap( QgsRendererCategory &other );
143 };
144 
145 typedef QList<QgsRendererCategory> QgsCategoryList;
146 
152 {
153  public:
154 
163  QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
165 
166  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
167  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
168  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
169  void stopRender( QgsRenderContext &context ) override;
170  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
171  bool filterNeedsGeometry() const override;
172  QString dump() const override;
174  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
175  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
176  QString filter( const QgsFields &fields = QgsFields() ) override;
177  QgsSymbolList symbols( QgsRenderContext &context ) const override;
178  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
179 
186  void updateSymbols( QgsSymbol *sym );
187 
191  const QgsCategoryList &categories() const { return mCategories; }
192 
196  int categoryIndexForValue( const QVariant &val );
197 
202  int categoryIndexForLabel( const QString &val );
203 
213  bool updateCategoryValue( int catIndex, const QVariant &value );
214 
224  bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
225 
236  bool updateCategoryLabel( int catIndex, const QString &label );
237 
250  bool updateCategoryRenderState( int catIndex, bool render );
251 
257  void addCategory( const QgsRendererCategory &category );
258 
264  bool deleteCategory( int catIndex );
265 
271  void deleteAllCategories();
272 
276  void moveCategory( int from, int to );
277 
283  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
284 
290  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
291 
299  QString classAttribute() const { return mAttrName; }
300 
308  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
309 
313  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
314 
315  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
316  QgsLegendSymbolList legendSymbolItems() const override;
317  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
318 
325  QgsSymbol *sourceSymbol();
326 
334  const QgsSymbol *sourceSymbol() const SIP_SKIP;
335 
343  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
344 
350  QgsColorRamp *sourceColorRamp();
351 
358  const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
359 
366  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
367 
373  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
374 
375  bool legendSymbolItemsCheckable() const override;
376  bool legendSymbolItemChecked( const QString &key ) override;
377  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
378  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
379  QString legendClassificationAttribute() const override { return classAttribute(); }
380 
386  static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
387 
398  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
399 
405  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
406 
424  int matchToSymbols( QgsStyle *style, QgsSymbol::SymbolType type,
425  QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
426 
427 
438  static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
439 
440  protected:
441  QString mAttrName;
443  std::unique_ptr<QgsSymbol> mSourceSymbol;
444  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
445  std::unique_ptr<QgsExpression> mExpression;
446 
447  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
448 
450  int mAttrNum = -1;
451 
453  QHash<QString, QgsSymbol *> mSymbolHash;
454  bool mCounting = false;
455 
456  void rebuildHash();
457 
461  Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
462 
467  Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
468 
469  // TODO QGIS 4.0 - rename Python method to symbolForValue
470 
484  QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
485 
486  private:
487 #ifdef SIP_RUN
490 #endif
491 
493  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
494 
496  QgsLegendSymbolList baseLegendSymbolItems() const;
497 };
498 
499 #endif // QGSCATEGORIZEDSYMBOLRENDERER_H
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
QgsFeatureRenderer::Filter
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
Definition: qgsrenderer.h:256
QgsFeatureRenderer::originalSymbolForFeature
virtual QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns symbol for feature.
Definition: qgsrenderer.cpp:81
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
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
QgsCategorizedSymbolRenderer::mSourceColorRamp
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Definition: qgscategorizedsymbolrenderer.h:444
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsCategorizedSymbolRenderer::categories
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
Definition: qgscategorizedsymbolrenderer.h:191
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsRendererCategory::mLabel
QString mLabel
Definition: qgscategorizedsymbolrenderer.h:139
QgsDataDefinedSizeLegend
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Definition: qgsdatadefinedsizelegend.h:42
QgsRendererCategory
Represents an individual category (class) from a QgsCategorizedSymbolRenderer.
Definition: qgscategorizedsymbolrenderer.h:36
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
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.
Definition: qgsstyleentityvisitor.h:34
QgsRendererCategory::mValue
QVariant mValue
Definition: qgscategorizedsymbolrenderer.h:137
QgsFeatureRenderer::legendSymbolItems
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
Definition: qgsrenderer.cpp:353
QgsCategorizedSymbolRenderer::mDataDefinedSizeLegend
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
Definition: qgscategorizedsymbolrenderer.h:447
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsCategorizedSymbolRenderer::setClassAttribute
void setClassAttribute(const QString &attr)
Sets the class attribute for the renderer, which is the field name or expression string from the laye...
Definition: qgscategorizedsymbolrenderer.h:308
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
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsFeatureRenderer::filter
virtual QString filter(const QgsFields &fields=QgsFields())
If a renderer does not require all the features this method may be overridden and return an expressio...
Definition: qgsrenderer.h:205
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsFeatureRenderer::save
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
store renderer info to XML element
Definition: qgsrenderer.cpp:184
QgsCategorizedSymbolRenderer::~QgsCategorizedSymbolRenderer
~QgsCategorizedSymbolRenderer() override
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
QgsCategoryList
QList< QgsRendererCategory > QgsCategoryList
Definition: qgscategorizedsymbolrenderer.h:145
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:45
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
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
QgsCategorizedSymbolRenderer::mSourceSymbol
std::unique_ptr< QgsSymbol > mSourceSymbol
Definition: qgscategorizedsymbolrenderer.h:443
QgsFeatureRenderer::symbolForFeature
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
QgsCategorizedSymbolRenderer::mExpression
std::unique_ptr< QgsExpression > mExpression
Definition: qgscategorizedsymbolrenderer.h:445
QgsCategorizedSymbolRenderer::mSymbolHash
QHash< QString, QgsSymbol * > mSymbolHash
hashtable for faster access to symbols
Definition: qgscategorizedsymbolrenderer.h:453
QgsRendererCategory::QgsRendererCategory
QgsRendererCategory()=default
Constructor for QgsRendererCategory.
qgsrenderer.h
QgsCategorizedSymbolRenderer::mAttrName
QString mAttrName
Definition: qgscategorizedsymbolrenderer.h:441
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:758
QgsFeatureRenderer
Definition: qgsrenderer.h:103
QgsStyle
Definition: qgsstyle.h:160
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsCategorizedSymbolRenderer::mCategories
QgsCategoryList mCategories
Definition: qgscategorizedsymbolrenderer.h:442
QgsCategorizedSymbolRenderer
Definition: qgscategorizedsymbolrenderer.h:152
QgsFeatureRenderer::dump
virtual QString dump() const
Returns debug information about this renderer.
Definition: qgsrenderer.cpp:138
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsSymbol::SymbolType
SymbolType
Type of the symbol.
Definition: qgssymbol.h:86
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
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
QgsCategorizedSymbolRenderer::classAttribute
QString classAttribute() const
Returns the class attribute for the renderer, which is the field name or expression string from the l...
Definition: qgscategorizedsymbolrenderer.h:299
QgsFeatureRenderer::legendKeysForFeature
virtual QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns legend keys matching a specified feature.
Definition: qgsrenderer.cpp:86
QgsRendererCategory::mSymbol
std::unique_ptr< QgsSymbol > mSymbol
Definition: qgscategorizedsymbolrenderer.h:138