QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  protected:
138  QVariant mValue;
139  std::unique_ptr<QgsSymbol> mSymbol;
140  QString mLabel;
141  bool mRender = true;
142 
143  void swap( QgsRendererCategory &other );
144 };
145 
146 typedef QList<QgsRendererCategory> QgsCategoryList;
147 
153 {
154  public:
155 
164  QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
166 
167  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
168  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
169  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
170  void stopRender( QgsRenderContext &context ) override;
171  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
172  bool filterNeedsGeometry() const override;
173  QString dump() const override;
174  QgsCategorizedSymbolRenderer *clone() const override SIP_FACTORY;
175  void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
176  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
177  QString filter( const QgsFields &fields = QgsFields() ) override;
178  QgsSymbolList symbols( QgsRenderContext &context ) const override;
179  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
180 
187  void updateSymbols( QgsSymbol *sym );
188 
192  const QgsCategoryList &categories() const { return mCategories; }
193 
197  int categoryIndexForValue( const QVariant &val );
198 
203  int categoryIndexForLabel( const QString &val );
204 
214  bool updateCategoryValue( int catIndex, const QVariant &value );
215 
225  bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
226 
237  bool updateCategoryLabel( int catIndex, const QString &label );
238 
251  bool updateCategoryRenderState( int catIndex, bool render );
252 
258  void addCategory( const QgsRendererCategory &category );
259 
265  bool deleteCategory( int catIndex );
266 
272  void deleteAllCategories();
273 
277  void moveCategory( int from, int to );
278 
284  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
285 
291  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
292 
300  QString classAttribute() const { return mAttrName; }
301 
309  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
310 
314  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
315 
316  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
317  QgsLegendSymbolList legendSymbolItems() const override;
318  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
319 
326  QgsSymbol *sourceSymbol();
327 
335  const QgsSymbol *sourceSymbol() const SIP_SKIP;
336 
344  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
345 
351  QgsColorRamp *sourceColorRamp();
352 
359  const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
360 
367  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
368 
374  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
375 
376  bool legendSymbolItemsCheckable() const override;
377  bool legendSymbolItemChecked( const QString &key ) override;
378  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
379  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
380  QString legendClassificationAttribute() const override { return classAttribute(); }
381 
390  static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
391 
402  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
403 
409  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
410 
428  int matchToSymbols( QgsStyle *style, Qgis::SymbolType type,
429  QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
430 
431 
442  static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
443 
444  protected:
445  QString mAttrName;
446  QgsCategoryList mCategories;
447  std::unique_ptr<QgsSymbol> mSourceSymbol;
448  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
449  std::unique_ptr<QgsExpression> mExpression;
450 
451  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
452 
454  int mAttrNum = -1;
455 
457  QHash<QString, QgsSymbol *> mSymbolHash;
458  bool mCounting = false;
459 
460  void rebuildHash();
461 
465  Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
466 
471  Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
472 
473  // TODO QGIS 4.0 - rename Python method to symbolForValue
474 
488  QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
489 
490  private:
491 #ifdef SIP_RUN
494 #endif
495 
497  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
498 
500  QgsLegendSymbolList baseLegendSymbolItems() const;
501 };
502 
503 #endif // QGSCATEGORIZEDSYMBOLRENDERER_H
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:62
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
QString classAttribute() const
Returns the class attribute for the renderer, which is the field name or expression string from the l...
void setClassAttribute(const QString &attr)
Sets the class attribute for the renderer, which is the field name or expression string from the laye...
~QgsCategorizedSymbolRenderer() override
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Class for parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
Represents an individual category (class) from a QgsCategorizedSymbolRenderer.
std::unique_ptr< QgsSymbol > mSymbol
QgsRendererCategory()=default
Constructor for QgsRendererCategory.
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
Represents a vector layer which manages a vector based data sets.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
QList< QgsRendererCategory > QgsCategoryList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43