QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
184  void updateSymbols( QgsSymbol *sym );
185 
189  const QgsCategoryList &categories() const { return mCategories; }
190 
194  int categoryIndexForValue( const QVariant &val );
195 
200  int categoryIndexForLabel( const QString &val );
201 
211  bool updateCategoryValue( int catIndex, const QVariant &value );
212 
222  bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
223 
234  bool updateCategoryLabel( int catIndex, const QString &label );
235 
248  bool updateCategoryRenderState( int catIndex, bool render );
249 
255  void addCategory( const QgsRendererCategory &category );
256 
262  bool deleteCategory( int catIndex );
263 
269  void deleteAllCategories();
270 
274  void moveCategory( int from, int to );
275 
281  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
282 
288  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
289 
297  QString classAttribute() const { return mAttrName; }
298 
306  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
307 
311  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
312 
313  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
314  QgsLegendSymbolList legendSymbolItems() const override;
315  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
316 
323  QgsSymbol *sourceSymbol();
324 
332  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
333 
339  QgsColorRamp *sourceColorRamp();
340 
347  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
348 
354  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
355 
356  bool legendSymbolItemsCheckable() const override;
357  bool legendSymbolItemChecked( const QString &key ) override;
358  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
359  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
360  QString legendClassificationAttribute() const override { return classAttribute(); }
361 
367  static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
368 
379  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
380 
386  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
387 
405  int matchToSymbols( QgsStyle *style, QgsSymbol::SymbolType type,
406  QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
407 
408 
419  static QgsCategoryList createCategories( const QVariantList &values, const QgsSymbol *symbol, QgsVectorLayer *layer = nullptr, const QString &fieldName = QString() );
420 
421  protected:
422  QString mAttrName;
424  std::unique_ptr<QgsSymbol> mSourceSymbol;
425  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
426  std::unique_ptr<QgsExpression> mExpression;
427 
428  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
429 
431  int mAttrNum = -1;
432 
434  QHash<QString, QgsSymbol *> mSymbolHash;
435  bool mCounting = false;
436 
437  void rebuildHash();
438 
442  Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
443 
448  Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
449 
450  // TODO QGIS 4.0 - rename Python method to symbolForValue
451 
465  QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
466 
467  private:
468 #ifdef SIP_RUN
471 #endif
472 
474  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
475 
477  QgsLegendSymbolList baseLegendSymbolItems() const;
478 };
479 
480 #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:300
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
Definition: qgsrenderer.h:244
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:193
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
std::unique_ptr< QgsSymbol > mSourceSymbol
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
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:43
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
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:69
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
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.
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:51
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:74
virtual QString dump() const
Returns debug information about this renderer.