QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsgraduatedsymbolrendererv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgraduatedsymbolrendererv2.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 QGSGRADUATEDSYMBOLRENDERERV2_H
16 #define QGSGRADUATEDSYMBOLRENDERERV2_H
17 
18 #include "qgssymbolv2.h"
19 #include "qgsrendererv2.h"
20 #include "qgsexpression.h"
21 #include <QScopedPointer>
22 #include <QRegExp>
23 
24 class CORE_EXPORT QgsRendererRangeV2
25 {
26  public:
28  QgsRendererRangeV2( double lowerValue, double upperValue, QgsSymbolV2* symbol, QString label, bool render = true );
29  QgsRendererRangeV2( const QgsRendererRangeV2& range );
30 
31  // default dtor is ok
32  QgsRendererRangeV2& operator=( QgsRendererRangeV2 range );
33 
34  bool operator<( const QgsRendererRangeV2 &other ) const;
35 
36  double lowerValue() const;
37  double upperValue() const;
38 
39  QgsSymbolV2* symbol() const;
40  QString label() const;
41 
42  void setSymbol( QgsSymbolV2* s );
43  void setLabel( QString label );
44  void setLowerValue( double lowerValue );
45  void setUpperValue( double upperValue );
46 
47  // @note added in 2.5
48  bool renderState() const;
49  void setRenderState( bool render );
50 
51  // debugging
52  QString dump() const;
53 
54  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
55 
56  protected:
57  double mLowerValue, mUpperValue;
58  QScopedPointer<QgsSymbolV2> mSymbol;
59  QString mLabel;
60  bool mRender;
61 
62  // for cpy+swap idiom
63  void swap( QgsRendererRangeV2 & other );
64 };
65 
66 typedef QList<QgsRendererRangeV2> QgsRangeList;
67 
68 
69 // @note added in 2.6
71 {
72  public:
74  QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );
75 
76  bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
77  bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;
78 
79  QString format() const { return mFormat; }
80  void setFormat( QString format ) { mFormat = format; }
81 
82  int precision() const { return mPrecision; }
83  void setPrecision( int precision );
84 
85  bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
86  void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
87 
89  QString labelForRange( double lower, double upper ) const;
90  QString labelForRange( const QgsRendererRangeV2 &range ) const;
91  QString formatNumber( double value ) const;
92 
93  void setFromDomElement( QDomElement &element );
94  void saveToDomElement( QDomElement &element );
95 
96  static int MaxPrecision;
97  static int MinPrecision;
98 
99  protected:
100  QString mFormat;
103  // values used to manage number formatting - precision and trailing zeroes
104  double mNumberScale;
105  QString mNumberSuffix;
108 };
109 
110 class QgsVectorLayer;
112 
114 {
115  public:
116  QgsGraduatedSymbolRendererV2( QString attrName = QString(), QgsRangeList ranges = QgsRangeList() );
118 
119  virtual ~QgsGraduatedSymbolRendererV2();
120 
121  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
122 
123  virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature );
124 
125  virtual void startRender( QgsRenderContext& context, const QgsFields& fields );
126 
127  virtual void stopRender( QgsRenderContext& context );
128 
129  virtual QList<QString> usedAttributes();
130 
131  virtual QString dump() const;
132 
133  virtual QgsFeatureRendererV2* clone() const;
134 
135  virtual void toSld( QDomDocument& doc, QDomElement &element ) const;
136 
138  virtual int capabilities() { return SymbolLevels | RotationField | Filter; }
139 
140  virtual QgsSymbolV2List symbols();
141 
142  QString classAttribute() const { return mAttrName; }
143  void setClassAttribute( QString attr ) { mAttrName = attr; }
144 
145  const QgsRangeList& ranges() const { return mRanges; }
146 
147  bool updateRangeSymbol( int rangeIndex, QgsSymbolV2* symbol );
148  bool updateRangeLabel( int rangeIndex, QString label );
149  bool updateRangeUpperValue( int rangeIndex, double value );
150  bool updateRangeLowerValue( int rangeIndex, double value );
152  bool updateRangeRenderState( int rangeIndex, bool render );
153 
154  void addClass( QgsSymbolV2* symbol );
156  void addClass( QgsRendererRangeV2 range );
158  void addClass( double lower, double upper );
159  void deleteClass( int idx );
160  void deleteAllClasses();
161 
163  void moveClass( int from, int to );
164 
165  void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
166  void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );
167 
168  enum Mode
169  {
175  Custom
176  };
177 
178  Mode mode() const { return mMode; }
179  void setMode( Mode mode ) { mMode = mode; }
185  void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
189  QList<double> getDataValues( QgsVectorLayer *vlayer );
190 
193  const QgsRendererRangeV2LabelFormat &labelFormat() const { return mLabelFormat; }
198  void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = false );
199 
203  void calculateLabelPrecision( bool updateRanges = true );
204 
205  static QgsGraduatedSymbolRendererV2* createRenderer(
206  QgsVectorLayer* vlayer,
207  QString attrName,
208  int classes,
209  Mode mode,
210  QgsSymbolV2* symbol,
211  QgsVectorColorRampV2* ramp,
212  bool inverted = false,
214  );
215 
217  static QgsFeatureRendererV2* create( QDomElement& element );
218 
220  virtual QDomElement save( QDomDocument& doc );
221 
223  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );
224 
227  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = QString() );
228 
229  QgsSymbolV2* sourceSymbol();
230  void setSourceSymbol( QgsSymbolV2* sym );
231 
232  QgsVectorColorRampV2* sourceColorRamp();
233  void setSourceColorRamp( QgsVectorColorRampV2* ramp );
235  bool invertedColorRamp() { return mInvertedColorRamp; }
236  void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }
237 
241  void updateColorRamp( QgsVectorColorRampV2* ramp = 0, bool inverted = false );
242 
244  void updateSymbols( QgsSymbolV2* sym );
245 
246  void setRotationField( QString fieldOrExpression );
247  QString rotationField() const;
248 
249  void setSizeScaleField( QString fieldOrExpression );
250  QString sizeScaleField() const;
251 
252  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
253  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
254 
257  virtual bool legendSymbolItemsCheckable() const;
258 
261  virtual bool legendSymbolItemChecked( QString key );
262 
265  virtual void checkLegendSymbolItem( QString key, bool state = true );
266 
269  virtual QString legendClassificationAttribute() const { return classAttribute(); }
270 
274  static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
275 
276  protected:
277  QString mAttrName;
280  QScopedPointer<QgsSymbolV2> mSourceSymbol;
281  QScopedPointer<QgsVectorColorRampV2> mSourceColorRamp;
284  QScopedPointer<QgsExpression> mRotation;
285  QScopedPointer<QgsExpression> mSizeScale;
287  QScopedPointer<QgsExpression> mExpression;
289  int mAttrNum;
290  bool mCounting;
291 
293  QHash<QgsSymbolV2*, QgsSymbolV2*> mTempSymbols;
294 
295  QgsSymbolV2* symbolForValue( double value );
296 
297 };
298 
299 #endif // QGSGRADUATEDSYMBOLRENDERERV2_H
QList< QgsRendererRangeV2 > QgsRangeList
virtual QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature)
Return symbol for feature.
Definition: qgsrendererv2.h:95
virtual void setRotationField(QString fieldName)
sets rotation field of renderer (if supported by the renderer)
virtual void checkLegendSymbolItem(QString key, bool state=true)
item in symbology was checked
QList< QgsSymbolV2 * > QgsSymbolV2List
Definition: qgsrendererv2.h:38
QScopedPointer< QgsVectorColorRampV2 > mSourceColorRamp
virtual QString dump() const
for debugging
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual QDomElement save(QDomDocument &doc)
store renderer info to XML element
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, QString rule="")
return a list of item text / symbol
Container of fields for a vector layer.
Definition: qgsfield.h:172
QHash< QgsSymbolV2 *, QgsSymbolV2 * > mTempSymbols
temporary symbols, used for data-defined rotation and scaling
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:113
QMap< QString, QString > QgsStringMap
Definition: qgis.h:416
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize)
return a list of symbology items for the legend
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
virtual void stopRender(QgsRenderContext &context)=0
virtual QString rotationField() const
return rotation field name (or empty string if not set or not supported by renderer) ...
QScopedPointer< QgsSymbolV2 > mSymbol
virtual QgsSymbolV2List symbols()=0
for symbol levels
QScopedPointer< QgsExpression > mExpression
virtual QgsFeatureRendererV2 * clone() const =0
QgsSymbolV2::ScaleMethod scaleMethod() const
virtual int capabilities()
returns bitwise OR-ed capabilities of the renderer
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
virtual bool legendSymbolItemChecked(QString key)
items of symbology items in legend is checked
QgsRendererRangeV2LabelFormat mLabelFormat
QList< QPair< QString, QPixmap > > QgsLegendSymbologyList
const QgsRendererRangeV2LabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
int ANALYSIS_EXPORT lower(int n, int i)
lower function
Contains information about the context of a rendering operation.
int mAttrNum
attribute index (derived from attribute name in startRender)
const QgsRangeList & ranges() const
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
QScopedPointer< QgsSymbolV2 > mSourceSymbol
void setTrimTrailingZeroes(bool trimTrailingZeroes)
QScopedPointer< QgsExpression > mRotation
QScopedPointer< QgsExpression > mSizeScale
Represents a vector layer which manages a vector based data sets.
QList< QPair< QString, QgsSymbolV2 * > > QgsLegendSymbolList
Definition: qgsrendererv2.h:42
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature)=0
to be overridden