QGIS API Documentation  2.17.0-Master (bf77d09)
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  // TODO: use QVarLengthArray instead of QList
38 
39  enum FeatureFlags { FeatIsSelected = 1, FeatDrawMarkers = 2 };
40 
41  // feature for rendering: QgsFeature and some flags
43  {
44  FeatureToRender( QgsFeature& _f, int _flags )
45  : feat( _f )
46  , flags( _flags )
47  {}
49  int flags; // selected and/or draw markers
50  };
51 
52  // rendering job: a feature to be rendered with a particular symbol
53  // (both f, symbol are _not_ owned by this class)
54  struct RenderJob
55  {
57  : ftr( _ftr )
58  , symbol( _s )
59  {}
62  };
63 
64  // render level: a list of jobs to be drawn at particular level
65  // (jobs are owned by this class)
66  struct RenderLevel
67  {
68  explicit RenderLevel( int z ): zIndex( z ) {}
69  ~RenderLevel() { Q_FOREACH ( RenderJob* j, jobs ) delete j; }
70  int zIndex;
72 
74  {
75  zIndex = rh.zIndex;
76  qDeleteAll( jobs );
77  jobs.clear();
78  Q_FOREACH ( RenderJob* job, rh.jobs )
79  {
80  jobs << new RenderJob( *job );
81  }
82  return *this;
83  }
84 
85  RenderLevel( const RenderLevel& other )
86  : zIndex( other.zIndex )
87  {
88  Q_FOREACH ( RenderJob* job, other.jobs )
89  {
90  jobs << new RenderJob( *job );
91  }
92  }
93 
94  };
95 
96  // rendering queue: a list of rendering levels
98 
99  class Rule;
100  typedef QList<Rule*> RuleList;
101 
110  class CORE_EXPORT Rule
111  {
112  public:
115  {
116  Filtered = 0,
118  Rendered
119  };
120 
122  Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(),
123  const QString& label = QString(), const QString& description = QString(), bool elseRule = false );
124  ~Rule();
125 
131  QString dump( int indent = 0 ) const;
132 
137  QSet<QString> usedAttributes() const;
138 
142  bool needsGeometry() const;
143 
145  QgsSymbolV2List symbols( const QgsRenderContext& context = QgsRenderContext() ) const;
146 
148  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) const;
149 
151  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
152 
160  bool isFilterOK( QgsFeature& f, QgsRenderContext *context = nullptr ) const;
161 
168  bool isScaleOK( double scale ) const;
169 
170  QgsSymbolV2* symbol() { return mSymbol; }
171  QString label() const { return mLabel; }
172  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
173  int scaleMinDenom() const { return mScaleMinDenom; }
174  int scaleMaxDenom() const { return mScaleMaxDenom; }
175 
180  QgsExpression* filter() const { return mFilter; }
181 
186  QString filterExpression() const { return mFilterExp; }
187 
193  QString description() const { return mDescription; }
194 
197  Q_DECL_DEPRECATED bool checkState() const { return mIsActive; }
198 
204  bool active() const { return mIsActive; }
205 
208  QString ruleKey() const { return mRuleKey; }
211  void setRuleKey( const QString& key ) { mRuleKey = key; }
212 
214  void setSymbol( QgsSymbolV2* sym );
215  void setLabel( const QString& label ) { mLabel = label; }
216 
223  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
224 
231  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
232 
238  void setFilterExpression( const QString& filterExp );
239 
245  void setDescription( const QString& description ) { mDescription = description; }
246 
249  Q_DECL_DEPRECATED void setCheckState( bool state ) { mIsActive = state; }
250 
255  void setActive( bool state ) { mIsActive = state; }
256 
258  Rule* clone() const;
259 
260  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
261  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
262 
263  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap ) const;
264 
268  Q_DECL_DEPRECATED bool startRender( QgsRenderContext& context, const QgsFields& fields );
269 
271  bool startRender( QgsRenderContext& context, const QgsFields& fields, QString& filter );
272 
274  QSet<int> collectZLevels();
275 
278  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
279 
289  RenderResult renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
290 
292  bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
293 
295  QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
296 
300  QSet< QString > legendKeysForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
301 
303  RuleList rulesForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
304 
310  void stopRender( QgsRenderContext& context );
311 
320  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
321 
327  RuleList& children() { return mChildren; }
328 
334  RuleList descendants() const { RuleList l; Q_FOREACH ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
335 
341  Rule* parent() { return mParent; }
342 
344  void appendChild( Rule* rule );
345 
347  void insertChild( int i, Rule* rule );
348 
350  void removeChild( Rule* rule );
351 
353  void removeChildAt( int i );
354 
356  Rule* takeChild( Rule* rule );
357 
359  Rule* takeChildAt( int i );
360 
363  Rule* findRuleByKey( const QString& key );
364 
370  void updateElseRules();
371 
377  void setIsElse( bool iselse );
378 
384  bool isElse() { return mElseRule; }
385 
386  protected:
387  void initFilter();
388 
389  Rule* mParent; // parent rule (NULL only for root rule)
391  int mScaleMinDenom, mScaleMaxDenom;
392  QString mFilterExp, mLabel, mDescription;
393  bool mElseRule;
394  RuleList mChildren;
395  RuleList mElseRules;
396  bool mIsActive; // whether it is enabled or not
397 
398  QString mRuleKey; // string used for unique identification of rule within renderer
399 
400  // temporary
402  // temporary while rendering
404  RuleList mActiveChildren;
405 
406  private:
407 
408  Rule( const Rule& rh );
409  Rule& operator=( const Rule& rh );
410  };
411 
413 
414  static QgsFeatureRendererV2* create( QDomElement& element );
415 
419  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
420 
422 
424  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
425 
426  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
427 
428  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
429 
430  virtual void stopRender( QgsRenderContext& context ) override;
431 
432  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
433 
434  virtual QList<QString> usedAttributes() override;
435 
436  virtual bool filterNeedsGeometry() const override;
437 
438  virtual QgsRuleBasedRendererV2* clone() const override;
439 
440  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
441 
442  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
443 
444  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
445 
447  virtual QDomElement save( QDomDocument& doc ) override;
448 
450  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
451 
454  virtual bool legendSymbolItemsCheckable() const override;
455 
458  virtual bool legendSymbolItemChecked( const QString& key ) override;
459 
462  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
463 
464  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
465 
468  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) override;
469 
473  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
474 
476  virtual QString dump() const override;
477 
480  virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context ) override;
481 
485  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
486 
487  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
488 
489  virtual QSet<QString> legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
490 
492  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
493 
495 
496  Rule* rootRule() { return mRootRule; }
497 
499 
501  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
503  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
505  static void refineRuleScales( Rule* initialRule, QList<int> scales );
506 
510  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
511 
513  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, const QString& sizeScaleField, const QString& rotationField = QString() );
514 
515  protected:
518 
519  // temporary
520  RenderQueue mRenderQueue;
522 
524 };
525 
526 #endif // QGSRULEBASEDRENDERERV2_H
Class for parsing and evaluation of expressions (formerly called "search strings").
void setLabel(const QString &label)
QString description() const
A human readable description for this rule.
void clear()
GeometryType
Definition: qgis.h:115
void setActive(bool state)
Sets if this rule is active.
void setScaleMaxDenom(int scaleMaxDenom)
Set the maximum denominator for which this rule shall apply.
Q_DECL_DEPRECATED bool checkState() const
Container of fields for a vector layer.
Definition: qgsfield.h:193
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:187
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
RenderLevel & operator=(const RenderLevel &rh)
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
Q_DECL_DEPRECATED void setCheckState(bool state)
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