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 )
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 
140  QgsSymbolV2List symbols( const QgsRenderContext& context = QgsRenderContext() ) const;
141 
143  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) const;
144 
146  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
147 
155  bool isFilterOK( QgsFeature& f, QgsRenderContext *context = nullptr ) const;
156 
163  bool isScaleOK( double scale ) const;
164 
165  QgsSymbolV2* symbol() { return mSymbol; }
166  QString label() const { return mLabel; }
167  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
168  int scaleMinDenom() const { return mScaleMinDenom; }
169  int scaleMaxDenom() const { return mScaleMaxDenom; }
170 
175  QgsExpression* filter() const { return mFilter; }
176 
181  QString filterExpression() const { return mFilterExp; }
182 
188  QString description() const { return mDescription; }
189 
192  Q_DECL_DEPRECATED bool checkState() const { return mIsActive; }
193 
199  bool active() const { return mIsActive; }
200 
203  QString ruleKey() const { return mRuleKey; }
206  void setRuleKey( const QString& key ) { mRuleKey = key; }
207 
209  void setSymbol( QgsSymbolV2* sym );
210  void setLabel( const QString& label ) { mLabel = label; }
211 
218  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
219 
226  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
227 
233  void setFilterExpression( const QString& filterExp );
234 
240  void setDescription( const QString& description ) { mDescription = description; }
241 
244  Q_DECL_DEPRECATED void setCheckState( bool state ) { mIsActive = state; }
245 
250  void setActive( bool state ) { mIsActive = state; }
251 
253  Rule* clone() const;
254 
255  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
256  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
257 
258  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap ) const;
259 
263  Q_DECL_DEPRECATED bool startRender( QgsRenderContext& context, const QgsFields& fields );
264 
266  bool startRender( QgsRenderContext& context, const QgsFields& fields, QString& filter );
267 
269  QSet<int> collectZLevels();
270 
273  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
274 
284  RenderResult renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
285 
287  bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
288 
290  QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
291 
295  QSet< QString > legendKeysForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
296 
298  RuleList rulesForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
299 
305  void stopRender( QgsRenderContext& context );
306 
315  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
316 
322  RuleList& children() { return mChildren; }
323 
329  RuleList descendants() const { RuleList l; Q_FOREACH ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
330 
336  Rule* parent() { return mParent; }
337 
339  void appendChild( Rule* rule );
340 
342  void insertChild( int i, Rule* rule );
343 
345  void removeChild( Rule* rule );
346 
348  void removeChildAt( int i );
349 
351  Rule* takeChild( Rule* rule );
352 
354  Rule* takeChildAt( int i );
355 
358  Rule* findRuleByKey( const QString& key );
359 
365  void updateElseRules();
366 
372  void setIsElse( bool iselse );
373 
379  bool isElse() { return mElseRule; }
380 
381  protected:
382  void initFilter();
383 
384  Rule* mParent; // parent rule (NULL only for root rule)
386  int mScaleMinDenom, mScaleMaxDenom;
387  QString mFilterExp, mLabel, mDescription;
388  bool mElseRule;
389  RuleList mChildren;
390  RuleList mElseRules;
391  bool mIsActive; // whether it is enabled or not
392 
393  QString mRuleKey; // string used for unique identification of rule within renderer
394 
395  // temporary
397  // temporary while rendering
399  RuleList mActiveChildren;
400 
401  private:
402 
403  Rule( const Rule& rh );
404  Rule& operator=( const Rule& rh );
405  };
406 
408 
409  static QgsFeatureRendererV2* create( QDomElement& element );
410 
414  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
415 
417 
419  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
420 
421  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
422 
423  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
424 
425  virtual void stopRender( QgsRenderContext& context ) override;
426 
427  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
428 
429  virtual QList<QString> usedAttributes() override;
430 
431  virtual QgsRuleBasedRendererV2* clone() const override;
432 
433  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
434 
435  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
436 
437  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
438 
440  virtual QDomElement save( QDomDocument& doc ) override;
441 
443  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
444 
447  virtual bool legendSymbolItemsCheckable() const override;
448 
451  virtual bool legendSymbolItemChecked( const QString& key ) override;
452 
455  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
456 
457  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
458 
461  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) override;
462 
466  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
467 
469  virtual QString dump() const override;
470 
473  virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context ) override;
474 
478  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
479 
480  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
481 
482  virtual QSet<QString> legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
483 
485  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
486 
488 
489  Rule* rootRule() { return mRootRule; }
490 
492 
494  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
496  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
498  static void refineRuleScales( Rule* initialRule, QList<int> scales );
499 
503  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
504 
506  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, const QString& sizeScaleField, const QString& rotationField = QString() );
507 
508  protected:
511 
512  // temporary
513  RenderQueue mRenderQueue;
515 
517 };
518 
519 #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