QGIS API Documentation  2.12.0-Lyon
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 
42  QVariant value() const;
43  QgsSymbolV2* symbol() const;
44  QString label() const;
45 
46  void setValue( const QVariant &value );
47  void setSymbol( QgsSymbolV2* s );
48  void setLabel( const QString &label );
49 
50  // @note added in 2.5
51  bool renderState() const;
52  void setRenderState( bool render );
53 
54  // debugging
55  QString dump() const;
56 
57  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
58 
59  protected:
63  bool mRender;
64 
65  void swap( QgsRendererCategoryV2 & other );
66 };
67 
69 
72 {
73  public:
74 
75  QgsCategorizedSymbolRendererV2( const QString& attrName = QString(), const QgsCategoryList& categories = QgsCategoryList() );
76 
78 
79  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
80 
81  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
82 
83  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
84 
85  virtual void stopRender( QgsRenderContext& context ) override;
86 
87  virtual QList<QString> usedAttributes() override;
88 
89  virtual QString dump() const override;
90 
91  virtual QgsFeatureRendererV2* clone() const override;
92 
93  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
94 
96  virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }
97 
98  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
99  void updateSymbols( QgsSymbolV2 * sym );
100 
101  const QgsCategoryList& categories() const { return mCategories; }
102 
104  int categoryIndexForValue( const QVariant& val );
105 
108  int categoryIndexForLabel( const QString& val );
109 
110  bool updateCategoryValue( int catIndex, const QVariant &value );
111  bool updateCategorySymbol( int catIndex, QgsSymbolV2* symbol );
112  bool updateCategoryLabel( int catIndex, const QString& label );
113 
115  bool updateCategoryRenderState( int catIndex, bool render );
116 
117  void addCategory( const QgsRendererCategoryV2 &category );
118  bool deleteCategory( int catIndex );
119  void deleteAllCategories();
120 
122  void moveCategory( int from, int to );
123 
124  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
125  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
126 
127  QString classAttribute() const { return mAttrName; }
128  void setClassAttribute( const QString& attr ) { mAttrName = attr; }
129 
131  static QgsFeatureRendererV2* create( QDomElement& element );
132 
134  virtual QDomElement save( QDomDocument& doc ) override;
135 
137  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
138 
141  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = QString() ) override;
142 
145 
146  QgsSymbolV2* sourceSymbol();
147  void setSourceSymbol( QgsSymbolV2* sym );
148 
149  QgsVectorColorRampV2* sourceColorRamp();
150 
154  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
155 
157  bool invertedColorRamp() { return mInvertedColorRamp; }
158  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
159 
165  void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );
166 
167  Q_DECL_DEPRECATED void setRotationField( const QString& fieldOrExpression ) override;
168  Q_DECL_DEPRECATED QString rotationField() const override;
169 
170  void setSizeScaleField( const QString& fieldOrExpression );
171  QString sizeScaleField() const;
172 
173  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
174  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
175 
178  virtual bool legendSymbolItemsCheckable() const override;
179 
181  // @note added in 2.5
182  virtual bool legendSymbolItemChecked( const QString& key ) override;
183 
185  // @note added in 2.5
186  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
187 
190  virtual QString legendClassificationAttribute() const override { return classAttribute(); }
191 
195  static QgsCategorizedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
196 
197  protected:
207 
209  int mAttrNum;
210 
213  bool mCounting;
214 
217 
218  void rebuildHash();
219 
220  QgsSymbolV2* symbolForValue( const QVariant& value );
221 
223 };
225 
226 
227 #endif // QGSCATEGORIZEDSYMBOLRENDERERV2_H
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:390
QScopedPointer< QgsExpression > mRotation
Container of fields for a vector layer.
Definition: qgsfield.h:177
virtual int capabilities() override
returns bitwise OR-ed capabilities of the renderer
virtual QList< QString > usedAttributes()=0
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
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
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:391
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:78
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
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.
QScopedPointer< QgsExpression > mExpression
virtual QString legendClassificationAttribute() const override
If supported by the renderer, return classification attribute for the use in legend.