QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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( const Rule& other );
91  ~Rule();
92  QString dump( int offset = 0 ) const;
93  QSet<QString> usedAttributes();
94  QgsSymbolV2List symbols();
96  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );
98  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
99  bool isFilterOK( QgsFeature& f ) const;
100  bool isScaleOK( double scale ) const;
101 
102  QgsSymbolV2* symbol() { return mSymbol; }
103  QString label() const { return mLabel; }
104  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
105  int scaleMinDenom() const { return mScaleMinDenom; }
106  int scaleMaxDenom() const { return mScaleMaxDenom; }
107  QgsExpression* filter() const { return mFilter; }
108  QString filterExpression() const { return mFilterExp; }
109  QString description() const { return mDescription; }
111  bool checkState() const { return mCheckState; }
112 
115  QString ruleKey() const { return mRuleKey; }
118  void setRuleKey( const QString& key ) { mRuleKey = key; }
119 
121  void setSymbol( QgsSymbolV2* sym );
122  void setLabel( QString label ) { mLabel = label; }
123  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
124  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
125  void setFilterExpression( QString filterExp ) { mFilterExp = filterExp; initFilter(); }
126  void setDescription( QString description ) { mDescription = description; }
128  void setCheckState( bool state ) { mCheckState = state; }
129 
131  Rule* clone() const;
132 
133  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props );
134  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
135 
136  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );
137 
139  bool startRender( QgsRenderContext& context, const QgsFields& fields );
141  QSet<int> collectZLevels();
144  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
145 
146  bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
147 
149  bool willRenderFeature( QgsFeature& feat );
150 
152  QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
153 
155  RuleList rulesForFeature( QgsFeature& feat );
156 
157  void stopRender( QgsRenderContext& context );
158 
159  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
160 
161  RuleList& children() { return mChildren; }
162  RuleList descendants() const { RuleList l; foreach ( Rule *c, mChildren ) { l += c; l += c->children(); } return l; }
163  Rule* parent() { return mParent; }
164 
166  void appendChild( Rule* rule );
168  void insertChild( int i, Rule* rule );
170  void removeChild( Rule* rule );
172  void removeChildAt( int i );
174  void takeChild( Rule* rule );
176  Rule* takeChildAt( int i );
177 
180  Rule* findRuleByKey( QString key );
181 
182  void updateElseRules();
183 
184  void setIsElse( bool iselse ) { mElseRule = iselse; }
185  bool isElse() { return mElseRule; }
186 
187  protected:
188  void initFilter();
189 
190  Rule* mParent; // parent rule (NULL only for root rule)
192  int mScaleMinDenom, mScaleMaxDenom;
193  QString mFilterExp, mLabel, mDescription;
194  bool mElseRule;
195  RuleList mChildren;
196  RuleList mElseRules;
197  bool mCheckState; // whether it is enabled or not
198 
199  QString mRuleKey; // string used for unique identification of rule within renderer
200 
201  // temporary
203  // temporary while rendering
204  QList<int> mSymbolNormZLevels;
205  RuleList mActiveChildren;
206  };
207 
209 
210  static QgsFeatureRendererV2* create( QDomElement& element );
211 
215  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
216 
218 
220  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
221 
222  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );
223 
224  virtual void startRender( QgsRenderContext& context, const QgsFields& fields );
225 
226  virtual void stopRender( QgsRenderContext& context );
227 
228  virtual QList<QString> usedAttributes();
229 
230  virtual QgsFeatureRendererV2* clone() const;
231 
232  virtual void toSld( QDomDocument& doc, QDomElement &element ) const;
233 
234  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
235 
236  virtual QgsSymbolV2List symbols();
237 
239  virtual QDomElement save( QDomDocument& doc );
240 
242  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );
243 
246  virtual bool legendSymbolItemsCheckable() const;
247 
250  virtual bool legendSymbolItemChecked( QString key );
251 
254  virtual void checkLegendSymbolItem( QString key, bool state = true );
255 
258  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );
259 
263  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const;
264 
266  virtual QString dump() const;
267 
270  virtual bool willRenderFeature( QgsFeature& feat );
271 
275  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
276 
277  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat );
278 
280  virtual int capabilities() { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
281 
283 
284  Rule* rootRule() { return mRootRule; }
285 
287 
289  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
291  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
293  static void refineRuleScales( Rule* initialRule, QList<int> scales );
294 
298  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
299 
301  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, QString sizeScaleField, QString rotationField );
302 
303  protected:
306 
307  // temporary
308  RenderQueue mRenderQueue;
309  QList<FeatureToRender> mCurrentFeatures;
310 };
311 
312 #endif // QGSRULEBASEDRENDERERV2_H
QMap< QString, QgsSymbolV2 * > QgsSymbolV2Map
Definition: qgsrendererv2.h:38
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:87
~RenderLevel()
Definition: qgsrulebasedrendererv2.h:65
QString description() const
Definition: qgsrulebasedrendererv2.h:109
QgsSymbolV2 * symbol
Definition: qgsrulebasedrendererv2.h:57
int flags
Definition: qgsrulebasedrendererv2.h:48
Rule * mParent
Definition: qgsrulebasedrendererv2.h:190
QList< QgsSymbolV2 * > QgsSymbolV2List
Definition: qgsrendererv2.h:37
Definition: qgsrulebasedrendererv2.h:62
void setScaleMaxDenom(int scaleMaxDenom)
Definition: qgsrulebasedrendererv2.h:124
void setLabel(QString label)
Definition: qgsrulebasedrendererv2.h:122
bool mElseRule
Definition: qgsrulebasedrendererv2.h:194
Container of fields for a vector layer.
Definition: qgsfield.h:172
GeometryType
Definition: qgis.h:155
RenderJob(FeatureToRender &_ftr, QgsSymbolV2 *_s)
Definition: qgsrulebasedrendererv2.h:55
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
void setIsElse(bool iselse)
Definition: qgsrulebasedrendererv2.h:184
bool isElse()
Definition: qgsrulebasedrendererv2.h:185
QMap< QString, QString > QgsStringMap
Definition: qgis.h:412
bool mCheckState
Definition: qgsrulebasedrendererv2.h:197
QString mRuleKey
Definition: qgsrulebasedrendererv2.h:199
Definition: qgsgraduatedsymbolrendererv2.h:113
bool checkState() const
Definition: qgsrulebasedrendererv2.h:111
void setCheckState(bool state)
Definition: qgsrulebasedrendererv2.h:128
RuleList mElseRules
Definition: qgsrulebasedrendererv2.h:196
int mScaleMinDenom
Definition: qgsrulebasedrendererv2.h:192
int scaleMinDenom() const
Definition: qgsrulebasedrendererv2.h:105
QgsFeature feat
Definition: qgsrulebasedrendererv2.h:47
QgsSymbolV2 * symbol()
Definition: qgsrulebasedrendererv2.h:102
Definition: qgscategorizedsymbolrendererv2.h:70
RuleList mChildren
Definition: qgsrulebasedrendererv2.h:195
This class keeps data about a rules for rule-based renderer.
Definition: qgsrulebasedrendererv2.h:84
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based renderer) ...
Definition: qgsrulebasedrendererv2.h:118
QgsExpression * filter() const
Definition: qgsrulebasedrendererv2.h:107
Definition: qgsrulebasedrendererv2.h:44
RuleList mActiveChildren
Definition: qgsrulebasedrendererv2.h:205
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
Definition: qgsrulebasedrendererv2.h:115
QList< RenderJob * > jobs
Definition: qgsrulebasedrendererv2.h:67
Rule * mRootRule
the root node with hierarchical list of rules
Definition: qgsrulebasedrendererv2.h:305
Definition: qgssymbolv2.h:43
RenderLevel(int z)
Definition: qgsrulebasedrendererv2.h:64
RuleList descendants() const
Definition: qgsrulebasedrendererv2.h:162
QList< QPair< QString, QPixmap > > QgsLegendSymbologyList
Definition: qgspluginlayer.h:20
QString mLabel
Definition: qgsrulebasedrendererv2.h:193
FeatureToRender(QgsFeature &_f, int _flags)
Definition: qgsrulebasedrendererv2.h:46
QgsSymbolV2 * mSymbol
Definition: qgsrulebasedrendererv2.h:191
QString label() const
Definition: qgsrulebasedrendererv2.h:103
int zIndex
Definition: qgsrulebasedrendererv2.h:66
bool dependsOnScale() const
Definition: qgsrulebasedrendererv2.h:104
QgsExpression * mFilter
Definition: qgsrulebasedrendererv2.h:202
QList< Rule * > RuleList
Definition: qgsrulebasedrendererv2.h:73
int scaleMaxDenom() const
Definition: qgsrulebasedrendererv2.h:106
Definition: qgsrulebasedrendererv2.h:53
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:39
QString filterExpression() const
Definition: qgsrulebasedrendererv2.h:108
QList< int > mSymbolNormZLevels
Definition: qgsrulebasedrendererv2.h:204
Rule * parent()
Definition: qgsrulebasedrendererv2.h:163
FeatureToRender & ftr
Definition: qgsrulebasedrendererv2.h:56
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features wi...
Definition: qgsrulebasedrendererv2.h:34
RuleList & children()
Definition: qgsrulebasedrendererv2.h:161
void setDescription(QString description)
Definition: qgsrulebasedrendererv2.h:126
Rule * rootRule()
Definition: qgsrulebasedrendererv2.h:284
virtual int capabilities()
returns bitwise OR-ed capabilities of the renderer
Definition: qgsrulebasedrendererv2.h:280
Definition: qgsrendererv2.h:72
QList< QgsLegendSymbolItemV2 > QgsLegendSymbolListV2
Definition: qgslegendsymbolitemv2.h:90
RenderQueue mRenderQueue
Definition: qgsrulebasedrendererv2.h:308
FeatureFlags
Definition: qgsrulebasedrendererv2.h:41
QList< RenderLevel > RenderQueue
Definition: qgsrulebasedrendererv2.h:71
void setScaleMinDenom(int scaleMinDenom)
Definition: qgsrulebasedrendererv2.h:123
QList< QPair< QString, QgsSymbolV2 * > > QgsLegendSymbolList
Definition: qgsrendererv2.h:41
void setFilterExpression(QString filterExp)
Definition: qgsrulebasedrendererv2.h:125
QList< FeatureToRender > mCurrentFeatures
Definition: qgsrulebasedrendererv2.h:309