QGIS API Documentation  2.12.0-Lyon
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  explicit RenderLevel( int z ): zIndex( z ) {}
65  ~RenderLevel() { Q_FOREACH ( RenderJob* j, jobs ) delete j; }
66  int zIndex;
68  };
69 
70  // rendering queue: a list of rendering levels
72 
73  class Rule;
74  typedef QList<Rule*> RuleList;
75 
84  class CORE_EXPORT Rule
85  {
86  public:
89  {
90  Filtered = 0,
92  Rendered
93  };
94 
96  Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(),
97  const QString& label = QString(), const QString& description = QString(), bool elseRule = false );
98  ~Rule();
99 
105  QString dump( int indent = 0 ) const;
106 
111  QSet<QString> usedAttributes();
112 
113  QgsSymbolV2List symbols( const QgsRenderContext& context = QgsRenderContext() );
114 
116  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" );
117 
119  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
120 
128  bool isFilterOK( QgsFeature& f, QgsRenderContext *context = 0 ) const;
129 
136  bool isScaleOK( double scale ) const;
137 
138  QgsSymbolV2* symbol() { return mSymbol; }
139  QString label() const { return mLabel; }
140  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
141  int scaleMinDenom() const { return mScaleMinDenom; }
142  int scaleMaxDenom() const { return mScaleMaxDenom; }
143 
148  QgsExpression* filter() const { return mFilter; }
149 
154  QString filterExpression() const { return mFilterExp; }
155 
161  QString description() const { return mDescription; }
162 
165  bool checkState() const { return mIsActive; }
171  bool active() const { return mIsActive; }
172 
175  QString ruleKey() const { return mRuleKey; }
178  void setRuleKey( const QString& key ) { mRuleKey = key; }
179 
181  void setSymbol( QgsSymbolV2* sym );
182  void setLabel( const QString& label ) { mLabel = label; }
183 
190  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
191 
198  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
199 
205  void setFilterExpression( const QString& filterExp ) { mFilterExp = filterExp; initFilter(); }
206 
212  void setDescription( const QString& description ) { mDescription = description; }
213 
216  void setCheckState( bool state ) { mIsActive = state; }
217 
222  void setActive( bool state ) { mIsActive = state; }
223 
225  Rule* clone() const;
226 
227  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props );
228  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
229 
230  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );
231 
233  Q_DECL_DEPRECATED bool startRender( QgsRenderContext& context, const QgsFields& fields );
234 
236  bool startRender( QgsRenderContext& context, const QgsFields& fields, QString& filter );
237 
239  QSet<int> collectZLevels();
240 
243  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
244 
254  RenderResult renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
255 
257  bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = 0 );
258 
260  QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = 0 );
261 
263  RuleList rulesForFeature( QgsFeature& feat, QgsRenderContext* context = 0 );
264 
270  void stopRender( QgsRenderContext& context );
271 
280  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
281 
287  RuleList& children() { return mChildren; }
288 
294  RuleList descendants() const { RuleList l; Q_FOREACH ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
295 
301  Rule* parent() { return mParent; }
302 
304  void appendChild( Rule* rule );
305 
307  void insertChild( int i, Rule* rule );
308 
310  void removeChild( Rule* rule );
311 
313  void removeChildAt( int i );
314 
316  void takeChild( Rule* rule );
317 
319  Rule* takeChildAt( int i );
320 
323  Rule* findRuleByKey( const QString& key );
324 
330  void updateElseRules();
331 
337  void setIsElse( bool iselse ) { mElseRule = iselse; }
338 
344  bool isElse() { return mElseRule; }
345 
346  protected:
347  void initFilter();
348 
349  Rule* mParent; // parent rule (NULL only for root rule)
351  int mScaleMinDenom, mScaleMaxDenom;
352  QString mFilterExp, mLabel, mDescription;
353  bool mElseRule;
354  RuleList mChildren;
355  RuleList mElseRules;
356  bool mIsActive; // whether it is enabled or not
357 
358  QString mRuleKey; // string used for unique identification of rule within renderer
359 
360  // temporary
362  // temporary while rendering
364  RuleList mActiveChildren;
365  };
366 
368 
369  static QgsFeatureRendererV2* create( QDomElement& element );
370 
374  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
375 
377 
379  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
380 
381  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
382 
383  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
384 
385  virtual void stopRender( QgsRenderContext& context ) override;
386 
387  virtual QString filter() override;
388 
389  virtual QList<QString> usedAttributes() override;
390 
391  virtual QgsFeatureRendererV2* clone() const override;
392 
393  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
394 
395  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
396 
397  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
398 
400  virtual QDomElement save( QDomDocument& doc ) override;
401 
403  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
404 
407  virtual bool legendSymbolItemsCheckable() const override;
408 
411  virtual bool legendSymbolItemChecked( const QString& key ) override;
412 
415  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
416 
419  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) override;
420 
424  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
425 
427  virtual QString dump() const override;
428 
431  virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context ) override;
432 
436  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
437 
438  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
439 
441  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
442 
444 
445  Rule* rootRule() { return mRootRule; }
446 
448 
450  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
452  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
454  static void refineRuleScales( Rule* initialRule, QList<int> scales );
455 
459  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
460 
462  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, const QString& sizeScaleField, const QString& rotationField = QString() );
463 
464  protected:
467 
468  // temporary
469  RenderQueue mRenderQueue;
471 
473 };
474 
475 #endif // QGSRULEBASEDRENDERERV2_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:92
void setLabel(const QString &label)
QString description() const
A human readable description for this rule.
void setActive(bool state)
Sets if this rule is active.
void setScaleMaxDenom(int scaleMaxDenom)
Set the maximum denominator for which this rule shall apply.
Container of fields for a vector layer.
Definition: qgsfield.h:177
GeometryType
Definition: qgis.h:104
RenderJob(FeatureToRender &_ftr, QgsSymbolV2 *_s)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
void setIsElse(bool iselse)
Sets if this rule is an ELSE rule.
bool isElse()
Check if this rule is an ELSE rule.
void setDescription(const QString &description)
Set a human readable description for this rule.
virtual int capabilities() override
returns bitwise OR-ed capabilities of the renderer
This class keeps data about a rules for rule-based renderer.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based renderer) ...
QgsExpression * filter() const
A filter that will check if this rule applies.
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
Rule * mRootRule
the root node with hierarchical list of rules
RuleList descendants() const
Returns all children, grand-children, grand-grand-children, grand-gra...
Contains information about the context of a rendering operation.
QString filterExpression() const
A filter that will check if this rule applies.
RenderResult
The result of rendering a rule.
Rule * parent()
The parent rule.
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features wi...
RuleList & children()
Return all children rules of this rule.
QList< RenderLevel > RenderQueue
void setFilterExpression(const QString &filterExp)
Set the expression used to check if a given feature shall be rendered with this rule.
void setScaleMinDenom(int scaleMinDenom)
Set the minimum denominator for which this rule shall apply.
bool active() const
Returns if this rule is active.
QList< FeatureToRender > mCurrentFeatures