QGIS API Documentation  3.17.0-Master (3b262f2a79)
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() );
164  ~QgsCategorizedSymbolRenderer() override;
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
Class for parsing and evaluation of expressions (formerly called "search strings").
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.
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Represents an individual category (class) from a QgsCategorizedSymbolRenderer.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
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:312
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
Definition: qgsrenderer.h:256
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
store renderer info to XML element
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
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
QString classAttribute() const
Returns the class attribute for the renderer, which is the field name or expression string from the l...
Container of fields for a vector layer.
Definition: qgsfields.h:44
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< QgsSymbol > mSourceSymbol
QMap< QString, QString > QgsStringMap
Definition: qgis.h:759
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
QList< QgsRendererCategory > QgsCategoryList
SymbolType
Type of the symbol.
Definition: qgssymbol.h:86
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:45
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
#define SIP_SKIP
Definition: qgis_sip.h:126
std::unique_ptr< QgsSymbol > mSymbol
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
void setClassAttribute(const QString &attr)
Sets the class attribute for the renderer, which is the field name or expression string from the laye...
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
#define SIP_FACTORY
Definition: qgis_sip.h:76
#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...
Contains information about the context of a rendering operation.
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:93
#define SIP_OUT
Definition: qgis_sip.h:58
std::unique_ptr< QgsColorRamp > mSourceColorRamp
QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.
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
QHash< QString, QgsSymbol * > mSymbolHash
hashtable for faster access to symbols
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
virtual bool filterNeedsGeometry() const
Returns true if this renderer requires the geometry to apply the filter.
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< QgsExpression > mExpression
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
virtual QString dump() const
Returns debug information about this renderer.