QGIS API Documentation  2.14.11-Essen
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 
64  void toSld( QDomDocument& doc, QDomElement &element, const QgsStringMap& props ) const;
65 
66  protected:
70  bool mRender;
71 
72  void swap( QgsRendererCategoryV2 & other );
73 };
74 
76 
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 
103  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
105  virtual void toSld( QDomDocument& doc, QDomElement &element, const QgsStringMap& props ) const override;
106 
108  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
109 
110  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
111 
113  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
114 
120  void updateSymbols( QgsSymbolV2 * sym );
121 
122  const QgsCategoryList& categories() const { return mCategories; }
123 
125  int categoryIndexForValue( const QVariant& val );
126 
129  int categoryIndexForLabel( const QString& val );
130 
131  bool updateCategoryValue( int catIndex, const QVariant &value );
132  bool updateCategorySymbol( int catIndex, QgsSymbolV2* symbol );
133  bool updateCategoryLabel( int catIndex, const QString& label );
134 
136  bool updateCategoryRenderState( int catIndex, bool render );
137 
138  void addCategory( const QgsRendererCategoryV2 &category );
139  bool deleteCategory( int catIndex );
140  void deleteAllCategories();
141 
143  void moveCategory( int from, int to );
144 
145  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
146  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
147 
148  QString classAttribute() const { return mAttrName; }
149  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
150 
152  static QgsFeatureRendererV2* create( QDomElement& element );
153 
155  virtual QDomElement save( QDomDocument& doc ) override;
156 
158  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
159 
162  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
163 
166 
167  virtual QSet< QString > legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
168 
174  QgsSymbolV2* sourceSymbol();
175 
182  void setSourceSymbol( QgsSymbolV2* sym );
183 
188  QgsVectorColorRampV2* sourceColorRamp();
189 
195  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
196 
198  bool invertedColorRamp() { return mInvertedColorRamp; }
199  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
200 
206  void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );
207 
208  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
209  Q_DECL_DEPRECATED QString rotationField() const override;
210 
211  void setSizeScaleField( const QString& fieldOrExpression );
212  QString sizeScaleField() const;
213 
214  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
215  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
216 
219  virtual bool legendSymbolItemsCheckable() const override;
220 
222  // @note added in 2.5
223  virtual bool legendSymbolItemChecked( const QString& key ) override;
224 
225  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
226 
228  // @note added in 2.5
229  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
230 
233  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
234 
238  static QgsCategorizedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
239 
240  protected:
250 
252  int mAttrNum;
253 
256  bool mCounting;
257 
260 
261  void rebuildHash();
262 
263  QgsSymbolV2* skipRender();
264 
265  QgsSymbolV2* symbolForValue( const QVariant& value );
266 
267  private:
268 
270  QVariant valueForFeature( QgsFeature& feature, QgsRenderContext &context ) const;
271 
272 };
274 
275 
276 #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:415
QScopedPointer< QgsExpression > mRotation
Container of fields for a vector layer.
Definition: qgsfield.h:187
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:416
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:87
int mAttrNum
attribute index (derived from attribute name in startRender)
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
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.
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.