QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrulebasedrendererv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrulebasedrendererv2.h - Rule-based renderer (symbology-ng)
3  ---------------------
4  begin : May 2010
5  copyright : (C) 2010 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 
16 #ifndef QGSRULEBASEDRENDERERV2_H
17 #define QGSRULEBASEDRENDERERV2_H
18 
19 #include "qgsfield.h"
20 #include "qgsfeature.h"
21 #include "qgis.h"
22 
23 #include "qgsrendererv2.h"
24 
25 class QgsExpression;
26 
29 
34 class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
35 {
36  public:
37 
38 
39  // TODO: use QVarLengthArray instead of QList
40 
41  enum FeatureFlags { FeatIsSelected = 1, FeatDrawMarkers = 2 };
42 
43  // feature for rendering: QgsFeature and some flags
45  {
46  FeatureToRender( QgsFeature& _f, int _flags ) : feat( _f ), flags( _flags ) {}
48  int flags; // selected and/or draw markers
49  };
50 
51  // rendering job: a feature to be rendered with a particular symbol
52  // (both f, symbol are _not_ owned by this class)
53  struct RenderJob
54  {
55  RenderJob( FeatureToRender& _ftr, QgsSymbolV2* _s ) : ftr( _ftr ), symbol( _s ) {}
58  };
59 
60  // render level: a list of jobs to be drawn at particular level
61  // (jobs are owned by this class)
62  struct RenderLevel
63  {
64  RenderLevel( int z ): zIndex( z ) {}
65  ~RenderLevel() { foreach ( RenderJob* j, jobs ) delete j; }
66  int zIndex;
67  QList<RenderJob*> jobs;
68  };
69 
70  // rendering queue: a list of rendering levels
71  typedef QList<RenderLevel> RenderQueue;
72 
73  class Rule;
74  typedef QList<Rule*> RuleList;
75 
84  class CORE_EXPORT Rule
85  {
86  public:
88  Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString(),
89  QString label = QString(), QString description = QString(), bool elseRule = false );
90  ~Rule();
91  QString dump( int offset = 0 ) const;
92  QSet<QString> usedAttributes();
93  QgsSymbolV2List symbols();
95  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );
97  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
98  bool isFilterOK( QgsFeature& f ) const;
99  bool isScaleOK( double scale ) const;
100 
101  QgsSymbolV2* symbol() { return mSymbol; }
102  QString label() const { return mLabel; }
103  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
104  int scaleMinDenom() const { return mScaleMinDenom; }
105  int scaleMaxDenom() const { return mScaleMaxDenom; }
106  QgsExpression* filter() const { return mFilter; }
107  QString filterExpression() const { return mFilterExp; }
108  QString description() const { return mDescription; }
110  bool checkState() const { return mCheckState; }
111 
114  QString ruleKey() const { return mRuleKey; }
117  void setRuleKey( const QString& key ) { mRuleKey = key; }
118 
120  void setSymbol( QgsSymbolV2* sym );
121  void setLabel( QString label ) { mLabel = label; }
122  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
123  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
124  void setFilterExpression( QString filterExp ) { mFilterExp = filterExp; initFilter(); }
125  void setDescription( QString description ) { mDescription = description; }
127  void setCheckState( bool state ) { mCheckState = state; }
128 
130  Rule* clone() const;
131 
132  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props );
133  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
134 
135  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );
136 
138  bool startRender( QgsRenderContext& context, const QgsFields& fields );
140  QSet<int> collectZLevels();
143  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
144 
145  bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
146 
148  bool willRenderFeature( QgsFeature& feat );
149 
151  QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
152 
154  RuleList rulesForFeature( QgsFeature& feat );
155 
156  void stopRender( QgsRenderContext& context );
157 
158  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
159 
160  RuleList& children() { return mChildren; }
161  RuleList descendants() const { RuleList l; foreach ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
162  Rule* parent() { return mParent; }
163 
165  void appendChild( Rule* rule );
167  void insertChild( int i, Rule* rule );
169  void removeChild( Rule* rule );
171  void removeChildAt( int i );
173  void takeChild( Rule* rule );
175  Rule* takeChildAt( int i );
176 
179  Rule* findRuleByKey( QString key );
180 
181  void updateElseRules();
182 
183  void setIsElse( bool iselse ) { mElseRule = iselse; }
184  bool isElse() { return mElseRule; }
185 
186  protected:
187  void initFilter();
188 
189  Rule* mParent; // parent rule (NULL only for root rule)
191  int mScaleMinDenom, mScaleMaxDenom;
192  QString mFilterExp, mLabel, mDescription;
193  bool mElseRule;
196  bool mCheckState; // whether it is enabled or not
197 
198  QString mRuleKey; // string used for unique identification of rule within renderer
199 
200  // temporary
202  // temporary while rendering
203  QList<int> mSymbolNormZLevels;
205  };
206 
208 
209  static QgsFeatureRendererV2* create( QDomElement& element );
210 
214  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
215 
217 
219  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature ) override;
220 
221  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
222 
223  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
224 
225  virtual void stopRender( QgsRenderContext& context ) override;
226 
227  virtual QList<QString> usedAttributes() override;
228 
229  virtual QgsFeatureRendererV2* clone() const override;
230 
231  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
232 
233  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
234 
235  virtual QgsSymbolV2List symbols() override;
236 
238  virtual QDomElement save( QDomDocument& doc ) override;
239 
241  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
242 
245  virtual bool legendSymbolItemsCheckable() const override;
246 
249  virtual bool legendSymbolItemChecked( QString key ) override;
250 
253  virtual void checkLegendSymbolItem( QString key, bool state = true ) override;
254 
257  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" ) override;
258 
262  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
263 
265  virtual QString dump() const override;
266 
269  virtual bool willRenderFeature( QgsFeature& feat ) override;
270 
274  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat ) override;
275 
276  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat ) override;
277 
279  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
280 
282 
283  Rule* rootRule() { return mRootRule; }
284 
286 
288  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
290  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
292  static void refineRuleScales( Rule* initialRule, QList<int> scales );
293 
297  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
298 
300  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, QString sizeScaleField, QString rotationField );
301 
302  protected:
305 
306  // temporary
308  QList<FeatureToRender> mCurrentFeatures;
309 };
310 
311 #endif // QGSRULEBASEDRENDERERV2_H