QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgscategorizedsymbolrendererv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscategorizedsymbolrendererv2.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 "qgssymbolv2.h"
19 #include "qgsrendererv2.h"
20 #include "qgsexpression.h"
21 
22 #include <QHash>
23 #include <QScopedPointer>
24 
26 class QgsVectorLayer;
27 
31 class CORE_EXPORT QgsRendererCategoryV2
32 {
33  public:
35 
37  QgsRendererCategoryV2( const QVariant& value, QgsSymbolV2* symbol, const QString& label, bool render = true );
38 
41 
43 
45 
46  QVariant value() const;
47  QgsSymbolV2* symbol() const;
48  QString label() const;
49 
50  void setValue( const QVariant &value );
51  void setSymbol( QgsSymbolV2* s );
52  void setLabel( const QString &label );
53 
54  // @note added in 2.5
55  bool renderState() const;
56  void setRenderState( bool render );
57 
58  // debugging
59  QString dump() const;
60 
66  void toSld( QDomDocument& doc, QDomElement &element, const QgsStringMap& props ) const;
67 
68  protected:
72  bool mRender;
73 
74  void swap( QgsRendererCategoryV2 & other );
75 };
76 
78 
84 {
85  public:
86 
87  QgsCategorizedSymbolRendererV2( const QString& attrName = QString(), const QgsCategoryList& categories = QgsCategoryList() );
88 
90 
92  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
93 
95  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
96 
97  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
98 
99  virtual void stopRender( QgsRenderContext& context ) override;
100 
101  virtual QList<QString> usedAttributes() override;
102 
103  virtual QString dump() const override;
104 
105  virtual QgsCategorizedSymbolRendererV2* clone() const override;
106 
108  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
110  virtual void toSld( QDomDocument& doc, QDomElement &element, const QgsStringMap& props ) const override;
111 
113  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
114 
115  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
116 
118  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
119 
125  void updateSymbols( QgsSymbolV2 * sym );
126 
127  const QgsCategoryList& categories() const { return mCategories; }
128 
130  int categoryIndexForValue( const QVariant& val );
131 
134  int categoryIndexForLabel( const QString& val );
135 
136  bool updateCategoryValue( int catIndex, const QVariant &value );
137  bool updateCategorySymbol( int catIndex, QgsSymbolV2* symbol );
138  bool updateCategoryLabel( int catIndex, const QString& label );
139 
141  bool updateCategoryRenderState( int catIndex, bool render );
142 
143  void addCategory( const QgsRendererCategoryV2 &category );
144  bool deleteCategory( int catIndex );
145  void deleteAllCategories();
146 
148  void moveCategory( int from, int to );
149 
150  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
151  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
152 
153  QString classAttribute() const { return mAttrName; }
154  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
155 
157  static QgsFeatureRendererV2* create( QDomElement& element );
158 
160  virtual QDomElement save( QDomDocument& doc ) override;
161 
163  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
164 
167  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
168 
171 
172  virtual QSet< QString > legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
173 
179  QgsSymbolV2* sourceSymbol();
180 
187  void setSourceSymbol( QgsSymbolV2* sym );
188 
193  QgsVectorColorRampV2* sourceColorRamp();
194 
200  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
201 
203  bool invertedColorRamp() { return mInvertedColorRamp; }
204  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
205 
211  void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );
212 
213  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
214  Q_DECL_DEPRECATED QString rotationField() const override;
215 
216  void setSizeScaleField( const QString& fieldOrExpression );
217  QString sizeScaleField() const;
218 
219  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
220  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
221 
224  virtual bool legendSymbolItemsCheckable() const override;
225 
227  // @note added in 2.5
228  virtual bool legendSymbolItemChecked( const QString& key ) override;
229 
230  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
231 
233  // @note added in 2.5
234  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
235 
238  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
239 
243  static QgsCategorizedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
244 
245  protected:
255 
257  int mAttrNum;
258 
261  bool mCounting;
262 
265 
266  void rebuildHash();
267 
268  QgsSymbolV2* skipRender();
269 
270  QgsSymbolV2* symbolForValue( const QVariant& value );
271 
272  private:
273 
275  QVariant valueForFeature( QgsFeature& feature, QgsRenderContext &context ) const;
276 
277 };
279 
280 
281 #endif // QGSCATEGORIZEDSYMBOLRENDERERV2_H
const QgsCategoryList & categories() const
virtual Q_DECL_DEPRECATED QString rotationField() const
return rotation field name (or empty string if not set or not supported by renderer) ...
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context)
Return legend keys matching a specified feature.
virtual Q_DECL_DEPRECATED QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature)
Return symbol for feature.
virtual QDomElement save(QDomDocument &doc)
store renderer info to XML element
QScopedPointer< QgsSymbolV2 > mSourceSymbol
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:515
QScopedPointer< QgsExpression > mRotation
Container of fields for a vector layer.
Definition: qgsfield.h:252
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const
Return a list of symbology items for the legend.
virtual int capabilities() override
returns bitwise OR-ed capabilities of the renderer
virtual QList< QString > usedAttributes()=0
Returns a set of attributes required for this renderer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
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...
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
QScopedPointer< QgsSymbolV2 > mSymbol
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize)
return a list of symbology items for the legend
QHash< QString, QgsSymbolV2 * > mSymbolHash
hashtable for faster access to symbols
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
Needs to be called when a new render cycle is started.
QScopedPointer< QgsExpression > mSizeScale
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
QgsSymbolV2::ScaleMethod scaleMethod() const
virtual QgsFeatureRendererV2 * clone() const =0
QList< QgsRendererCategoryV2 > QgsCategoryList
virtual Q_DECL_DEPRECATED QgsSymbolV2 * symbolForFeature(QgsFeature &feature)
To be overridden.
virtual QString dump() const
for debugging
QScopedPointer< QgsVectorColorRampV2 > mSourceColorRamp
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="")
return a list of item text / symbol
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
For symbol levels.
QHash< QgsSymbolV2 *, QgsSymbolV2 * > mTempSymbols
temporary symbols, used for data-defined rotation and scaling
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:516
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:90
int mAttrNum
attribute index (derived from attribute name in startRender)
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 ...
virtual Q_DECL_DEPRECATED void setRotationField(const QString &fieldName)
sets rotation field of renderer (if supported by the renderer)
Represents a vector layer which manages a vector based data sets.
Abstract base class for color ramps.
virtual void setLegendSymbolItem(const QString &key, QgsSymbolV2 *symbol)
Sets the symbol to be used for a legend symbol item.
QScopedPointer< QgsExpression > mExpression
virtual QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.