QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 QGSCATEGORIZEDSYMBOLRENDERERV2_H
16 #define QGSCATEGORIZEDSYMBOLRENDERERV2_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 
53  QgsRendererCategory( const QVariant &value, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true );
54 
59  QgsRendererCategory &operator=( QgsRendererCategory cat );
60 
65  QVariant value() const;
66 
71  QgsSymbol *symbol() const;
72 
78  QString label() const;
79 
84  void setValue( const QVariant &value );
85 
93  void setSymbol( QgsSymbol *s SIP_TRANSFER );
94 
100  void setLabel( const QString &label );
101 
107  bool renderState() const;
108 
114  void setRenderState( bool render );
115 
116  // debugging
117 
121  QString dump() const;
122 
126  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
127 
128  protected:
129  QVariant mValue;
130  std::unique_ptr<QgsSymbol> mSymbol;
131  QString mLabel;
132  bool mRender = true;
133 
134  void swap( QgsRendererCategory &other );
135 };
136 
137 typedef QList<QgsRendererCategory> QgsCategoryList;
138 
144 {
145  public:
146 
155  QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
156 
157  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
158  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
159  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
160  void stopRender( QgsRenderContext &context ) override;
161  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
162  bool filterNeedsGeometry() const override;
163  QString dump() const override;
165  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
166  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
167  QString filter( const QgsFields &fields = QgsFields() ) override;
168  QgsSymbolList symbols( QgsRenderContext &context ) const override;
169 
176  void updateSymbols( QgsSymbol *sym );
177 
181  const QgsCategoryList &categories() const { return mCategories; }
182 
186  int categoryIndexForValue( const QVariant &val );
187 
192  int categoryIndexForLabel( const QString &val );
193 
201  bool updateCategoryValue( int catIndex, const QVariant &value );
202 
212  bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
213 
224  bool updateCategoryLabel( int catIndex, const QString &label );
225 
238  bool updateCategoryRenderState( int catIndex, bool render );
239 
245  void addCategory( const QgsRendererCategory &category );
246 
252  bool deleteCategory( int catIndex );
253 
259  void deleteAllCategories();
260 
264  void moveCategory( int from, int to );
265 
271  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
272 
278  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
279 
287  QString classAttribute() const { return mAttrName; }
288 
296  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
297 
301  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
302 
303  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
304  QgsLegendSymbolList legendSymbolItems() const override;
305  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
306 
313  QgsSymbol *sourceSymbol();
314 
322  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
323 
329  QgsColorRamp *sourceColorRamp();
330 
337  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
338 
344  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
345 
346  bool legendSymbolItemsCheckable() const override;
347  bool legendSymbolItemChecked( const QString &key ) override;
348  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
349  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
350  QString legendClassificationAttribute() const override { return classAttribute(); }
351 
357  static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
358 
369  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
370 
376  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
377 
395  int matchToSymbols( QgsStyle *style, QgsSymbol::SymbolType type,
396  QVariantList &unmatchedCategories SIP_OUT, QStringList &unmatchedSymbols SIP_OUT, bool caseSensitive = true, bool useTolerantMatch = false );
397 
398  protected:
399  QString mAttrName;
401  std::unique_ptr<QgsSymbol> mSourceSymbol;
402  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
403  std::unique_ptr<QgsExpression> mExpression;
404 
405  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
406 
408  int mAttrNum = -1;
409 
411  QHash<QString, QgsSymbol *> mSymbolHash;
412  bool mCounting = false;
413 
414  void rebuildHash();
415 
419  Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
420 
425  Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) const SIP_DEPRECATED;
426 
427  // TODO QGIS 4.0 - rename Python method to symbolForValue
428 
442  QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) const SIP_PYNAME( symbolForValue2 );
443 
444  private:
445 #ifdef SIP_RUN
448 #endif
449 
451  QVariant valueForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
452 
454  QgsLegendSymbolList baseLegendSymbolItems() const;
455 };
456 
457 #endif // QGSCATEGORIZEDSYMBOLRENDERERV2_H
The class is used as a container of context for various read/write operations on other objects...
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Represents an individual category (class) from a QgsCategorizedSymbolRenderer.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
Definition: qgsrenderer.h:244
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
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
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
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
virtual QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns symbol for feature.
Definition: qgsrenderer.cpp:80
QMap< QString, QString > QgsStringMap
Definition: qgis.h:577
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
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
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
std::unique_ptr< QgsSymbol > mSymbol
#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
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
#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.
QString classAttribute() const
Returns the class attribute for the renderer, which is the field name or expression string from the l...
Contains information about the context of a rendering operation.
virtual QString dump() const
Returns debug information about this renderer.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:92
#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.
QHash< QString, QgsSymbol * > mSymbolHash
hashtable for faster access to symbols
virtual QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns legend keys matching a specified feature.
Definition: qgsrenderer.cpp:85
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