QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 "qgssymbol.h"
21 #include "qgsrenderer.h"
22 #include "qgsexpression.h"
23 #include "qgscolorramp.h"
25 
26 #include <QHash>
27 
28 class QgsVectorLayer;
29 class QgsStyle;
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 
165  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
166  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
167  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
168  void stopRender( QgsRenderContext &context ) override;
169  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
170  bool filterNeedsGeometry() const override;
171  QString dump() const override;
173  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
174  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
175  QString filter( const QgsFields &fields = QgsFields() ) override;
176  QgsSymbolList symbols( QgsRenderContext &context ) const override;
177  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
178 
185  void updateSymbols( QgsSymbol *sym );
186 
190  const QgsCategoryList &categories() const { return mCategories; }
191 
195  int categoryIndexForValue( const QVariant &val );
196 
201  int categoryIndexForLabel( const QString &val );
202 
212  bool updateCategoryValue( int catIndex, const QVariant &value );
213 
223  bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
224 
235  bool updateCategoryLabel( int catIndex, const QString &label );
236 
249  bool updateCategoryRenderState( int catIndex, bool render );
250 
256  void addCategory( const QgsRendererCategory &category );
257 
263  bool deleteCategory( int catIndex );
264 
270  void deleteAllCategories();
271 
275  void moveCategory( int from, int to );
276 
282  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
283 
289  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
290 
298  QString classAttribute() const { return mAttrName; }
299 
307  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
308 
312  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
313 
314  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
315  QgsLegendSymbolList legendSymbolItems() const override;
316  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
317 
324  QgsSymbol *sourceSymbol();
325 
333  const QgsSymbol *sourceSymbol() const SIP_SKIP;
334 
342  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
343 
349  QgsColorRamp *sourceColorRamp();
350 
357  const QgsColorRamp *sourceColorRamp() const SIP_SKIP;
358 
365  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
366 
372  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
373 
374  bool legendSymbolItemsCheckable() const override;
375  bool legendSymbolItemChecked( const QString &key ) override;
376  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
377  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
378  QString legendClassificationAttribute() const override { return classAttribute(); }
379 
385  static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
386 
397  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
398 
404  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
405 
423  int matchToSymbols( QgsStyle *style, QgsSymbol::SymbolType type,
424  QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
425 
426 
437  static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
438 
439  protected:
440  QString mAttrName;
442  std::unique_ptr<QgsSymbol> mSourceSymbol;
443  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
444  std::unique_ptr<QgsExpression> mExpression;
445 
446  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
447 
449  int mAttrNum = -1;
450 
452  QHash<QString, QgsSymbol *> mSymbolHash;
453  bool mCounting = false;
454 
455  void rebuildHash();
456 
460  Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
461 
466  Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
467 
468  // TODO QGIS 4.0 - rename Python method to symbolForValue
469 
483  QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
484 
485  private:
486 #ifdef SIP_RUN
489 #endif
490 
492  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
493 
495  QgsLegendSymbolList baseLegendSymbolItems() const;
496 };
497 
498 #endif // QGSCATEGORIZEDSYMBOLRENDERER_H
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:61
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:301
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
Definition: qgsrenderer.h:245
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:194
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:42
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:612
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
QList< QgsRendererCategory > QgsCategoryList
SymbolType
Type of the symbol.
Definition: qgssymbol.h:83
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
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.