QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 );
63 
71  QVariant value() const;
72 
77  QgsSymbol *symbol() const;
78 
84  QString label() const;
85 
93  void setValue( const QVariant &value );
94 
102  void setSymbol( QgsSymbol *s SIP_TRANSFER );
103 
109  void setLabel( const QString &label );
110 
116  bool renderState() const;
117 
123  void setRenderState( bool render );
124 
125  // debugging
126 
130  QString dump() const;
131 
135  void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
136 
137 #ifdef SIP_RUN
138  SIP_PYOBJECT __repr__();
139  % MethodCode
140  const QString str = !sipCpp->value().isValid()
141  ? QStringLiteral( "<QgsRendererCategory>" )
142  : sipCpp->label().isEmpty()
143  ? QStringLiteral( "<QgsRendererCategory: %1>" ).arg( sipCpp->value().toString() )
144  : QStringLiteral( "<QgsRendererCategory: %1 (%2)>" ).arg( sipCpp->value().toString(), sipCpp->label() );
145  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
146  % End
147 #endif
148 
149  protected:
150  QVariant mValue;
151  std::unique_ptr<QgsSymbol> mSymbol;
152  QString mLabel;
153  bool mRender = true;
154 
155  void swap( QgsRendererCategory &other );
156 };
157 
158 typedef QList<QgsRendererCategory> QgsCategoryList;
159 
165 {
166  public:
167 
176  QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
177  ~QgsCategorizedSymbolRenderer() override;
178 
179  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
180  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
181  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
182  void stopRender( QgsRenderContext &context ) override;
183  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
184  bool filterNeedsGeometry() const override;
185  QString dump() const override;
187  void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
188  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
189  QString filter( const QgsFields &fields = QgsFields() ) override;
190  QgsSymbolList symbols( QgsRenderContext &context ) const override;
191  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
192 
199  void updateSymbols( QgsSymbol *sym );
200 
204  const QgsCategoryList &categories() const { return mCategories; }
205 
209  int categoryIndexForValue( const QVariant &val );
210 
215  int categoryIndexForLabel( const QString &val );
216 
226  bool updateCategoryValue( int catIndex, const QVariant &value );
227 
237  bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
238 
249  bool updateCategoryLabel( int catIndex, const QString &label );
250 
263  bool updateCategoryRenderState( int catIndex, bool render );
264 
270  void addCategory( const QgsRendererCategory &category );
271 
277  bool deleteCategory( int catIndex );
278 
284  void deleteAllCategories();
285 
289  void moveCategory( int from, int to );
290 
296  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
297 
303  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
304 
312  QString classAttribute() const { return mAttrName; }
313 
321  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
322 
326  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
327 
328  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
329  QgsLegendSymbolList legendSymbolItems() const override;
330  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
331  QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
332 
339  QgsSymbol *sourceSymbol();
340 
348  const QgsSymbol *sourceSymbol() const SIP_SKIP;
349 
357  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
358 
364  QgsColorRamp *sourceColorRamp();
365 
372  const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
373 
380  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
381 
387  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
388 
389  bool legendSymbolItemsCheckable() const override;
390  bool legendSymbolItemChecked( const QString &key ) override;
391  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
392  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
393  QString legendClassificationAttribute() const override { return classAttribute(); }
394 
403  static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
404 
415  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
416 
422  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
423 
441  int matchToSymbols( QgsStyle *style, Qgis::SymbolType type,
442  QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
443 
444 
455  static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
456 
466  static QString displayString( const QVariant &value, int precision = -1 );
467 
468 
469  protected:
470  QString mAttrName;
471  QgsCategoryList mCategories;
472  std::unique_ptr<QgsSymbol> mSourceSymbol;
473  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
474  std::unique_ptr<QgsExpression> mExpression;
475 
476  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
477 
479  int mAttrNum = -1;
480 
482  QHash<QString, QgsSymbol *> mSymbolHash;
483  bool mCounting = false;
484 
485  void rebuildHash();
486 
490  Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
491 
496  Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
497 
498  // TODO QGIS 4.0 - rename Python method to symbolForValue
499 
513  QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
514 
515  private:
516 #ifdef SIP_RUN
519 #endif
520 
522  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
523 
525  QgsLegendSymbolList baseLegendSymbolItems() const;
526 
527 };
528 
529 #endif // QGSCATEGORIZEDSYMBOLRENDERER_H
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
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
QgsFeatureRenderer::originalSymbolForFeature
virtual QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns symbol for feature.
Definition: qgsrenderer.cpp:84
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
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
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:204
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsRendererCategory::mLabel
QString mLabel
Definition: qgscategorizedsymbolrenderer.h:152
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:35
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
QgsRendererCategory::mValue
QVariant mValue
Definition: qgscategorizedsymbolrenderer.h:150
QgsFeatureRenderer::legendSymbolItems
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
Definition: qgsrenderer.cpp:377
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
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:321
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
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:206
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsFeatureRenderer::save
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
Stores renderer properties to an XML element.
Definition: qgsrenderer.cpp:193
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
QgsCategoryList
QList< QgsRendererCategory > QgsCategoryList
Definition: qgscategorizedsymbolrenderer.h:158
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
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
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
QgsFeatureRenderer::symbolForFeature
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
qgsrenderer.h
QgsFeatureRenderer::legendKeyToExpression
virtual QString legendKeyToExpression(const QString &key, QgsVectorLayer *layer, bool &ok) const
Attempts to convert the specified legend rule key to a QGIS expression matching the features displaye...
Definition: qgsrenderer.cpp:371
QgsCategorizedSymbolRenderer::capabilities
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
Definition: qgscategorizedsymbolrenderer.h:188
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsStyle
Definition: qgsstyle.h:159
str
#define str(x)
Definition: qgis.cpp:37
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
Qgis
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:71
QgsCategorizedSymbolRenderer
Definition: qgscategorizedsymbolrenderer.h:164
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
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:312
QgsFeatureRenderer::legendKeysForFeature
virtual QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns legend keys matching a specified feature.
Definition: qgsrenderer.cpp:89
QgsRendererCategory::mSymbol
std::unique_ptr< QgsSymbol > mSymbol
Definition: qgscategorizedsymbolrenderer.h:151