QGIS API Documentation  2.15.0-Master (972fc9f)
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 
29 class CORE_EXPORT QgsRendererCategoryV2
30 {
31  public:
33 
35  QgsRendererCategoryV2( const QVariant& value, QgsSymbolV2* symbol, const QString& label, bool render = true );
36 
39 
41 
43 
44  QVariant value() const;
45  QgsSymbolV2* symbol() const;
46  QString label() const;
47 
48  void setValue( const QVariant &value );
49  void setSymbol( QgsSymbolV2* s );
50  void setLabel( const QString &label );
51 
52  // @note added in 2.5
53  bool renderState() const;
54  void setRenderState( bool render );
55 
56  // debugging
57  QString dump() const;
58 
59  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
60 
61  protected:
65  bool mRender;
66 
67  void swap( QgsRendererCategoryV2 & other );
68 };
69 
71 
74 {
75  public:
76 
77  QgsCategorizedSymbolRendererV2( const QString& attrName = QString(), const QgsCategoryList& categories = QgsCategoryList() );
78 
80 
82  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
83 
85  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
86 
87  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
88 
89  virtual void stopRender( QgsRenderContext& context ) override;
90 
91  virtual QList<QString> usedAttributes() override;
92 
93  virtual QString dump() const override;
94 
95  virtual QgsCategorizedSymbolRendererV2* clone() const override;
96 
97  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
98 
100  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
101 
102  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
103 
105  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
106 
112  void updateSymbols( QgsSymbolV2 * sym );
113 
114  const QgsCategoryList& categories() const { return mCategories; }
115 
117  int categoryIndexForValue( const QVariant& val );
118 
121  int categoryIndexForLabel( const QString& val );
122 
123  bool updateCategoryValue( int catIndex, const QVariant &value );
124  bool updateCategorySymbol( int catIndex, QgsSymbolV2* symbol );
125  bool updateCategoryLabel( int catIndex, const QString& label );
126 
128  bool updateCategoryRenderState( int catIndex, bool render );
129 
130  void addCategory( const QgsRendererCategoryV2 &category );
131  bool deleteCategory( int catIndex );
132  void deleteAllCategories();
133 
135  void moveCategory( int from, int to );
136 
137  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
138  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
139 
140  QString classAttribute() const { return mAttrName; }
141  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
142 
144  static QgsFeatureRendererV2* create( QDomElement& element );
145 
147  virtual QDomElement save( QDomDocument& doc ) override;
148 
150  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
151 
154  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
155 
158 
159  virtual QSet< QString > legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
160 
166  QgsSymbolV2* sourceSymbol();
167 
174  void setSourceSymbol( QgsSymbolV2* sym );
175 
180  QgsVectorColorRampV2* sourceColorRamp();
181 
187  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
188 
190  bool invertedColorRamp() { return mInvertedColorRamp; }
191  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
192 
198  void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );
199 
200  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
201  Q_DECL_DEPRECATED QString rotationField() const override;
202 
203  void setSizeScaleField( const QString& fieldOrExpression );
204  QString sizeScaleField() const;
205 
206  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
207  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
208 
211  virtual bool legendSymbolItemsCheckable() const override;
212 
214  // @note added in 2.5
215  virtual bool legendSymbolItemChecked( const QString& key ) override;
216 
217  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
218 
220  // @note added in 2.5
221  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
222 
225  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
226 
230  static QgsCategorizedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
231 
232  protected:
242 
244  int mAttrNum;
245 
248  bool mCounting;
249 
252 
253  void rebuildHash();
254 
255  QgsSymbolV2* skipRender();
256 
257  QgsSymbolV2* symbolForValue( const QVariant& value );
258 
259  private:
260 
262  QVariant valueForFeature( QgsFeature& feature, QgsRenderContext &context ) const;
263 
264 };
266 
267 
268 #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:514
QScopedPointer< QgsExpression > mRotation
Container of fields for a vector layer.
Definition: qgsfield.h:193
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:515
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:87
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.