QGIS API Documentation  3.0.2-Girona (307d082)
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 
34 class CORE_EXPORT QgsRendererCategory
35 {
36  public:
37 
41  QgsRendererCategory() = default;
42 
44  QgsRendererCategory( const QVariant &value, QgsSymbol *symbol SIP_TRANSFER, const QString &label, bool render = true );
45 
48 
49  QgsRendererCategory &operator=( QgsRendererCategory cat );
50 
51  QVariant value() const;
52  QgsSymbol *symbol() const;
53  QString label() const;
54 
55  void setValue( const QVariant &value );
56  void setSymbol( QgsSymbol *s SIP_TRANSFER );
57  void setLabel( const QString &label );
58 
64  bool renderState() const;
65 
71  void setRenderState( bool render );
72 
73  // debugging
74  QString dump() const;
75 
76  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
77 
78  protected:
79  QVariant mValue;
80  std::unique_ptr<QgsSymbol> mSymbol;
81  QString mLabel;
82  bool mRender = true;
83 
84  void swap( QgsRendererCategory &other );
85 };
86 
87 typedef QList<QgsRendererCategory> QgsCategoryList;
88 
94 {
95  public:
96 
97  QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
98 
99  QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
100  QgsSymbol *originalSymbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
101  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
102  void stopRender( QgsRenderContext &context ) override;
103  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
104  QString dump() const override;
106  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
107  QgsFeatureRenderer::Capabilities capabilities() override { return SymbolLevels | Filter; }
108  QString filter( const QgsFields &fields = QgsFields() ) override;
109  QgsSymbolList symbols( QgsRenderContext &context ) override;
110 
117  void updateSymbols( QgsSymbol *sym );
118 
119  const QgsCategoryList &categories() const { return mCategories; }
120 
122  int categoryIndexForValue( const QVariant &val );
123 
128  int categoryIndexForLabel( const QString &val );
129 
130  bool updateCategoryValue( int catIndex, const QVariant &value );
131  bool updateCategorySymbol( int catIndex, QgsSymbol *symbol SIP_TRANSFER );
132  bool updateCategoryLabel( int catIndex, const QString &label );
133 
135  bool updateCategoryRenderState( int catIndex, bool render );
136 
137  void addCategory( const QgsRendererCategory &category );
138  bool deleteCategory( int catIndex );
139  void deleteAllCategories();
140 
142  void moveCategory( int from, int to );
143 
144  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
145  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
146 
147  QString classAttribute() const { return mAttrName; }
148  void setClassAttribute( const QString &attr ) { mAttrName = attr; }
149 
151  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
152 
153  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
154  QgsLegendSymbolList legendSymbolItems() const override;
155  QSet< QString > legendKeysForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
156 
163  QgsSymbol *sourceSymbol();
164 
172  void setSourceSymbol( QgsSymbol *sym SIP_TRANSFER );
173 
179  QgsColorRamp *sourceColorRamp();
180 
187  void setSourceColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
188 
194  void updateColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
195 
196  bool legendSymbolItemsCheckable() const override;
197  bool legendSymbolItemChecked( const QString &key ) override;
198  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
199  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
200  QString legendClassificationAttribute() const override { return classAttribute(); }
201 
207  static QgsCategorizedSymbolRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
208 
219  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
220 
226  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
227 
228  protected:
229  QString mAttrName;
231  std::unique_ptr<QgsSymbol> mSourceSymbol;
232  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
233  std::unique_ptr<QgsExpression> mExpression;
234 
235  std::unique_ptr<QgsDataDefinedSizeLegend> mDataDefinedSizeLegend;
236 
238  int mAttrNum = -1;
239 
241  QHash<QString, QgsSymbol *> mSymbolHash;
242  bool mCounting = false;
243 
244  void rebuildHash();
245 
249  Q_DECL_DEPRECATED QgsSymbol *skipRender() SIP_DEPRECATED;
250 
255  Q_DECL_DEPRECATED QgsSymbol *symbolForValue( const QVariant &value ) SIP_DEPRECATED;
256 
257  // TODO QGIS 4.0 - rename Python method to symbolForValue
258 
272  QgsSymbol *symbolForValue( const QVariant &value, bool &foundMatchingSymbol SIP_OUT ) SIP_PYNAME( symbolForValue2 );
273 
274  private:
275 #ifdef SIP_RUN
278 #endif
279 
281  QVariant valueForFeature( QgsFeature &feature, QgsRenderContext &context ) const;
282 
284  QgsLegendSymbolList baseLegendSymbolItems() const;
285 };
286 
287 #endif // QGSCATEGORIZEDSYMBOLRENDERERV2_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
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:297
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
Definition: qgsrenderer.h:241
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:190
virtual QgsSymbol * originalSymbolForFeature(QgsFeature &feature, QgsRenderContext &context)
Return symbol for feature.
Definition: qgsrenderer.cpp:80
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:62
std::unique_ptr< QgsSymbol > mSourceSymbol
QMap< QString, QString > QgsStringMap
Definition: qgis.h:479
QList< QgsRendererCategory > QgsCategoryList
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Return a list of attributes required by this renderer.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
const QgsCategoryList & categories() const
std::unique_ptr< QgsSymbol > mSymbol
#define SIP_TRANSFER
Definition: qgis_sip.h:36
void setClassAttribute(const QString &attr)
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.
virtual QgsSymbolList symbols(QgsRenderContext &context)
Returns list of symbols used by the renderer.
Definition: qgsrenderer.h:266
virtual QgsSymbol * symbolForFeature(QgsFeature &feature, QgsRenderContext &context)=0
To be overridden.
Contains information about the context of a rendering operation.
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.
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context)
Return legend keys matching a specified feature.
Definition: qgsrenderer.cpp:85
QHash< QString, QgsSymbol * > mSymbolHash
hashtable for faster access to symbols
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
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.