QGIS API Documentation
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 ) : feat( _f ), flags( _flags ) {}
46  int flags; // selected and/or draw markers
47  };
48 
49  // rendering job: a feature to be rendered with a particular symbol
50  // (both f, symbol are _not_ owned by this class)
51  struct RenderJob
52  {
53  RenderJob( FeatureToRender& _ftr, QgsSymbolV2* _s ) : ftr( _ftr ), symbol( _s ) {}
56  };
57 
58  // render level: a list of jobs to be drawn at particular level
59  // (jobs are owned by this class)
60  struct RenderLevel
61  {
62  explicit RenderLevel( int z ): zIndex( z ) {}
63  ~RenderLevel() { Q_FOREACH ( RenderJob* j, jobs ) delete j; }
64  int zIndex;
66 
68  {
69  zIndex = rh.zIndex;
70  qDeleteAll( jobs );
71  jobs.clear();
72  Q_FOREACH ( RenderJob* job, rh.jobs )
73  {
74  jobs << new RenderJob( *job );
75  }
76  return *this;
77  }
78 
79  RenderLevel( const RenderLevel& other )
80  : zIndex( other.zIndex )
81  {
82  Q_FOREACH ( RenderJob* job, other.jobs )
83  {
84  jobs << new RenderJob( *job );
85  }
86  }
87 
88  };
89 
90  // rendering queue: a list of rendering levels
92 
93  class Rule;
94  typedef QList<Rule*> RuleList;
95 
104  class CORE_EXPORT Rule
105  {
106  public:
109  {
110  Filtered = 0,
112  Rendered
113  };
114 
116  Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(),
117  const QString& label = QString(), const QString& description = QString(), bool elseRule = false );
118  ~Rule();
119 
125  QString dump( int indent = 0 ) const;
126 
131  QSet<QString> usedAttributes() const;
132 
134  QgsSymbolV2List symbols( const QgsRenderContext& context = QgsRenderContext() ) const;
135 
137  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) const;
138 
140  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
141 
149  bool isFilterOK( QgsFeature& f, QgsRenderContext *context = nullptr ) const;
150 
157  bool isScaleOK( double scale ) const;
158 
159  QgsSymbolV2* symbol() { return mSymbol; }
160  QString label() const { return mLabel; }
161  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
162  int scaleMinDenom() const { return mScaleMinDenom; }
163  int scaleMaxDenom() const { return mScaleMaxDenom; }
164 
169  QgsExpression* filter() const { return mFilter; }
170 
175  QString filterExpression() const { return mFilterExp; }
176 
182  QString description() const { return mDescription; }
183 
186  Q_DECL_DEPRECATED bool checkState() const { return mIsActive; }
187 
193  bool active() const { return mIsActive; }
194 
197  QString ruleKey() const { return mRuleKey; }
200  void setRuleKey( const QString& key ) { mRuleKey = key; }
201 
203  void setSymbol( QgsSymbolV2* sym );
204  void setLabel( const QString& label ) { mLabel = label; }
205 
212  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
213 
220  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
221 
227  void setFilterExpression( const QString& filterExp );
228 
234  void setDescription( const QString& description ) { mDescription = description; }
235 
238  Q_DECL_DEPRECATED void setCheckState( bool state ) { mIsActive = state; }
239 
244  void setActive( bool state ) { mIsActive = state; }
245 
247  Rule* clone() const;
248 
249  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
250  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
251 
252  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap ) const;
253 
257  Q_DECL_DEPRECATED bool startRender( QgsRenderContext& context, const QgsFields& fields );
258 
260  bool startRender( QgsRenderContext& context, const QgsFields& fields, QString& filter );
261 
263  QSet<int> collectZLevels();
264 
267  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
268 
278  RenderResult renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
279 
281  bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
282 
284  QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
285 
289  QSet< QString > legendKeysForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
290 
292  RuleList rulesForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
293 
299  void stopRender( QgsRenderContext& context );
300 
309  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
310 
316  RuleList& children() { return mChildren; }
317 
323  RuleList descendants() const { RuleList l; Q_FOREACH ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
324 
330  Rule* parent() { return mParent; }
331 
333  void appendChild( Rule* rule );
334 
336  void insertChild( int i, Rule* rule );
337 
339  void removeChild( Rule* rule );
340 
342  void removeChildAt( int i );
343 
345  Rule* takeChild( Rule* rule );
346 
348  Rule* takeChildAt( int i );
349 
352  Rule* findRuleByKey( const QString& key );
353 
359  void updateElseRules();
360 
366  void setIsElse( bool iselse );
367 
373  bool isElse() { return mElseRule; }
374 
375  protected:
376  void initFilter();
377 
378  Rule* mParent; // parent rule (NULL only for root rule)
380  int mScaleMinDenom, mScaleMaxDenom;
381  QString mFilterExp, mLabel, mDescription;
382  bool mElseRule;
383  RuleList mChildren;
384  RuleList mElseRules;
385  bool mIsActive; // whether it is enabled or not
386 
387  QString mRuleKey; // string used for unique identification of rule within renderer
388 
389  // temporary
391  // temporary while rendering
393  RuleList mActiveChildren;
394 
395  private:
396 
397  Rule( const Rule& rh );
398  Rule& operator=( const Rule& rh );
399  };
400 
402 
403  static QgsFeatureRendererV2* create( QDomElement& element );
404 
408  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
409 
411 
413  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
414 
415  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
416 
417  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
418 
419  virtual void stopRender( QgsRenderContext& context ) override;
420 
421  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
422 
423  virtual QList<QString> usedAttributes() override;
424 
425  virtual QgsRuleBasedRendererV2* clone() const override;
426 
427  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
428 
429  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
430 
431  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
432 
434  virtual QDomElement save( QDomDocument& doc ) override;
435 
437  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
438 
441  virtual bool legendSymbolItemsCheckable() const override;
442 
445  virtual bool legendSymbolItemChecked( const QString& key ) override;
446 
449  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
450 
451  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
452 
455  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) override;
456 
460  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
461 
463  virtual QString dump() const override;
464 
467  virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context ) override;
468 
472  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
473 
474  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
475 
476  virtual QSet<QString> legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
477 
479  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
480 
482 
483  Rule* rootRule() { return mRootRule; }
484 
486 
488  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
490  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
492  static void refineRuleScales( Rule* initialRule, QList<int> scales );
493 
497  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
498 
500  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, const QString& sizeScaleField, const QString& rotationField = QString() );
501 
502  protected:
505 
506  // temporary
507  RenderQueue mRenderQueue;
509 
511 };
512 
513 #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:111
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:187
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