QGIS API Documentation  2.17.0-Master (973e4b0)
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 
61  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
62 
63  protected:
67  bool mRender;
68 
69  void swap( QgsRendererCategoryV2 & other );
70 };
71 
73 
79 {
80  public:
81 
82  QgsCategorizedSymbolRendererV2( const QString& attrName = QString(), const QgsCategoryList& categories = QgsCategoryList() );
83 
85 
87  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
88 
90  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
91 
92  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
93 
94  virtual void stopRender( QgsRenderContext& context ) override;
95 
96  virtual QList<QString> usedAttributes() override;
97 
98  virtual QString dump() const override;
99 
100  virtual QgsCategorizedSymbolRendererV2* clone() const override;
101 
102  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
103 
105  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
106 
107  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
108 
110  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
111 
117  void updateSymbols( QgsSymbolV2 * sym );
118 
119  const QgsCategoryList& categories() const { return mCategories; }
120 
122  int categoryIndexForValue( const QVariant& val );
123 
126  int categoryIndexForLabel( const QString& val );
127 
128  bool updateCategoryValue( int catIndex, const QVariant &value );
129  bool updateCategorySymbol( int catIndex, QgsSymbolV2* symbol );
130  bool updateCategoryLabel( int catIndex, const QString& label );
131 
133  bool updateCategoryRenderState( int catIndex, bool render );
134 
135  void addCategory( const QgsRendererCategoryV2 &category );
136  bool deleteCategory( int catIndex );
137  void deleteAllCategories();
138 
140  void moveCategory( int from, int to );
141 
142  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
143  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
144 
145  QString classAttribute() const { return mAttrName; }
146  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
147 
149  static QgsFeatureRendererV2* create( QDomElement& element );
150 
152  virtual QDomElement save( QDomDocument& doc ) override;
153 
155  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
156 
159  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
160 
163 
164  virtual QSet< QString > legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
165 
171  QgsSymbolV2* sourceSymbol();
172 
179  void setSourceSymbol( QgsSymbolV2* sym );
180 
185  QgsVectorColorRampV2* sourceColorRamp();
186 
192  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
193 
195  bool invertedColorRamp() { return mInvertedColorRamp; }
196  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
197 
203  void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );
204 
205  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
206  Q_DECL_DEPRECATED QString rotationField() const override;
207 
208  void setSizeScaleField( const QString& fieldOrExpression );
209  QString sizeScaleField() const;
210 
211  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
212  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
213 
216  virtual bool legendSymbolItemsCheckable() const override;
217 
219  // @note added in 2.5
220  virtual bool legendSymbolItemChecked( const QString& key ) override;
221 
222  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
223 
225  // @note added in 2.5
226  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
227 
230  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
231 
235  static QgsCategorizedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
236 
237  protected:
247 
249  int mAttrNum;
250 
253  bool mCounting;
254 
257 
258  void rebuildHash();
259 
260  QgsSymbolV2* skipRender();
261 
262  QgsSymbolV2* symbolForValue( const QVariant& value );
263 
264  private:
265 
267  QVariant valueForFeature( QgsFeature& feature, QgsRenderContext &context ) const;
268 
269 };
271 
272 
273 #endif // QGSCATEGORIZEDSYMBOLRENDERERV2_H
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context)
Return legend keys matching a specified feature.
const QgsCategoryList & categories() const
virtual QString dump() const
for debugging
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:209
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...
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.
virtual QgsFeatureRendererV2 * clone() const =0
QList< QgsRendererCategoryV2 > QgsCategoryList
virtual Q_DECL_DEPRECATED QgsSymbolV2 * symbolForFeature(QgsFeature &feature)
To be overridden.
QScopedPointer< QgsVectorColorRampV2 > mSourceColorRamp
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="")
return a list of item text / symbol
virtual Q_DECL_DEPRECATED QString rotationField() const
return rotation field name (or empty string if not set or not supported by renderer) ...
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
For symbol levels.
QHash< QgsSymbolV2 *, QgsSymbolV2 * > mTempSymbols
temporary symbols, used for data-defined rotation and scaling
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
#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
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const
Return a list of symbology items for the legend.
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
int mAttrNum
attribute index (derived from attribute name in startRender)
QgsSymbolV2::ScaleMethod scaleMethod() const
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.