QGIS API Documentation  2.0.1-Dufour
 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() );
90  //Rule( const Rule& other );
91  ~Rule();
92  QString dump( int offset = 0 ) const;
93  QSet<QString> usedAttributes();
94  QgsSymbolV2List symbols();
96  QgsLegendSymbolList legendSymbolItems();
97  bool isFilterOK( QgsFeature& f ) const;
98  bool isScaleOK( double scale ) const;
99 
100  QgsSymbolV2* symbol() { return mSymbol; }
101  QString label() const { return mLabel; }
102  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
103  int scaleMinDenom() const { return mScaleMinDenom; }
104  int scaleMaxDenom() const { return mScaleMaxDenom; }
105  QgsExpression* filter() const { return mFilter; }
106  QString filterExpression() const { return mFilterExp; }
107  QString description() const { return mDescription; }
108 
110  void setSymbol( QgsSymbolV2* sym );
111  void setLabel( QString label ) { mLabel = label; }
112  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
113  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
114  void setFilterExpression( QString filterExp ) { mFilterExp = filterExp; initFilter(); }
115  void setDescription( QString description ) { mDescription = description; }
116 
118  Rule* clone() const;
119 
120  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props );
121  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
122 
123  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );
124 
126  bool startRender( QgsRenderContext& context, const QgsVectorLayer *vlayer );
128  QSet<int> collectZLevels();
131  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
132 
133  bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
134 
137  bool willRenderFeature( QgsFeature& feat );
138 
141  QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
142 
144  RuleList rulesForFeature( QgsFeature& feat );
145 
146  void stopRender( QgsRenderContext& context );
147 
148  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
149 
150  RuleList& children() { return mChildren; }
151  RuleList descendants() const { RuleList l; foreach ( Rule *c, mChildren ) { l += c; l += c->children(); } return l; }
152  Rule* parent() { return mParent; }
153 
155  void appendChild( Rule* rule ) { mChildren.append( rule ); rule->mParent = this; }
157  void insertChild( int i, Rule* rule ) { mChildren.insert( i, rule ); rule->mParent = this; }
159  void removeChild( Rule* rule ) { mChildren.removeAll( rule ); delete rule; }
161  void removeChildAt( int i ) { Rule* rule = mChildren[i]; mChildren.removeAt( i ); delete rule; }
163  void takeChild( Rule* rule ) { mChildren.removeAll( rule ); rule->mParent = NULL; }
165  Rule* takeChildAt( int i ) { Rule* rule = mChildren.takeAt( i ); rule->mParent = NULL; return rule; }
166 
167  protected:
168  void initFilter();
169 
170  Rule* mParent; // parent rule (NULL only for root rule)
172  int mScaleMinDenom, mScaleMaxDenom;
173  QString mFilterExp, mLabel, mDescription;
174  bool mElseRule;
176 
177  // temporary
179  // temporary while rendering
180  QList<int> mSymbolNormZLevels;
182  };
183 
185 
186  static QgsFeatureRendererV2* create( QDomElement& element );
187 
191  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
192 
194 
196  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
197 
198  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );
199 
200  virtual void startRender( QgsRenderContext& context, const QgsVectorLayer *vlayer );
201 
202  virtual void stopRender( QgsRenderContext& context );
203 
204  virtual QList<QString> usedAttributes();
205 
206  virtual QgsFeatureRendererV2* clone();
207 
208  virtual void toSld( QDomDocument& doc, QDomElement &element ) const;
209 
210  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
211 
212  virtual QgsSymbolV2List symbols();
213 
215  virtual QDomElement save( QDomDocument& doc );
216 
218  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );
219 
223  virtual QgsLegendSymbolList legendSymbolItems();
224 
226  virtual QString dump() const;
227 
231  virtual bool willRenderFeature( QgsFeature& feat );
232 
237  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
238 
241  virtual int capabilities() { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
242 
244 
245  Rule* rootRule() { return mRootRule; }
246 
248 
250  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
252  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
254  static void refineRuleScales( Rule* initialRule, QList<int> scales );
255 
256  protected:
259 
260  // temporary
262  QList<FeatureToRender> mCurrentFeatures;
263 };
264 
265 #endif // QGSRULEBASEDRENDERERV2_H