QGIS API Documentation  2.13.0-Master
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 
70  {
71  zIndex = rh.zIndex;
72  qDeleteAll( jobs );
73  jobs.clear();
74  Q_FOREACH ( RenderJob* job, rh.jobs )
75  {
76  jobs << new RenderJob( *job );
77  }
78  return *this;
79  }
80 
81  RenderLevel( const RenderLevel& other )
82  : zIndex( other.zIndex )
83  {
84  Q_FOREACH ( RenderJob* job, other.jobs )
85  {
86  jobs << new RenderJob( *job );
87  }
88  }
89 
90  };
91 
92  // rendering queue: a list of rendering levels
94 
95  class Rule;
96  typedef QList<Rule*> RuleList;
97 
106  class CORE_EXPORT Rule
107  {
108  public:
111  {
112  Filtered = 0,
114  Rendered
115  };
116 
118  Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(),
119  const QString& label = QString(), const QString& description = QString(), bool elseRule = false );
120  ~Rule();
121 
127  QString dump( int indent = 0 ) const;
128 
133  QSet<QString> usedAttributes() const;
134 
136  QgsSymbolV2List symbols( const QgsRenderContext& context = QgsRenderContext() ) const;
137 
139  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) const;
140 
142  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
143 
151  bool isFilterOK( QgsFeature& f, QgsRenderContext *context = nullptr ) const;
152 
159  bool isScaleOK( double scale ) const;
160 
161  QgsSymbolV2* symbol() { return mSymbol; }
162  QString label() const { return mLabel; }
163  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
164  int scaleMinDenom() const { return mScaleMinDenom; }
165  int scaleMaxDenom() const { return mScaleMaxDenom; }
166 
171  QgsExpression* filter() const { return mFilter; }
172 
177  QString filterExpression() const { return mFilterExp; }
178 
184  QString description() const { return mDescription; }
185 
188  Q_DECL_DEPRECATED bool checkState() const { return mIsActive; }
189 
195  bool active() const { return mIsActive; }
196 
199  QString ruleKey() const { return mRuleKey; }
202  void setRuleKey( const QString& key ) { mRuleKey = key; }
203 
205  void setSymbol( QgsSymbolV2* sym );
206  void setLabel( const QString& label ) { mLabel = label; }
207 
214  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
215 
222  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
223 
229  void setFilterExpression( const QString& filterExp );
230 
236  void setDescription( const QString& description ) { mDescription = description; }
237 
240  Q_DECL_DEPRECATED void setCheckState( bool state ) { mIsActive = state; }
241 
246  void setActive( bool state ) { mIsActive = state; }
247 
249  Rule* clone() const;
250 
251  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const;
252  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
253 
254  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap ) const;
255 
259  Q_DECL_DEPRECATED bool startRender( QgsRenderContext& context, const QgsFields& fields );
260 
262  bool startRender( QgsRenderContext& context, const QgsFields& fields, QString& filter );
263 
265  QSet<int> collectZLevels();
266 
269  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
270 
280  RenderResult renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
281 
283  bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
284 
286  QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
287 
291  QSet< QString > legendKeysForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
292 
294  RuleList rulesForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
295 
301  void stopRender( QgsRenderContext& context );
302 
311  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
312 
318  RuleList& children() { return mChildren; }
319 
325  RuleList descendants() const { RuleList l; Q_FOREACH ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
326 
332  Rule* parent() { return mParent; }
333 
335  void appendChild( Rule* rule );
336 
338  void insertChild( int i, Rule* rule );
339 
341  void removeChild( Rule* rule );
342 
344  void removeChildAt( int i );
345 
347  Rule* takeChild( Rule* rule );
348 
350  Rule* takeChildAt( int i );
351 
354  Rule* findRuleByKey( const QString& key );
355 
361  void updateElseRules();
362 
368  void setIsElse( bool iselse );
369 
375  bool isElse() { return mElseRule; }
376 
377  protected:
378  void initFilter();
379 
380  Rule* mParent; // parent rule (NULL only for root rule)
382  int mScaleMinDenom, mScaleMaxDenom;
383  QString mFilterExp, mLabel, mDescription;
384  bool mElseRule;
385  RuleList mChildren;
386  RuleList mElseRules;
387  bool mIsActive; // whether it is enabled or not
388 
389  QString mRuleKey; // string used for unique identification of rule within renderer
390 
391  // temporary
393  // temporary while rendering
395  RuleList mActiveChildren;
396 
397  private:
398 
399  Rule( const Rule& rh );
400  Rule& operator=( const Rule& rh );
401  };
402 
404 
405  static QgsFeatureRendererV2* create( QDomElement& element );
406 
410  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
411 
413 
415  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
416 
417  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
418 
419  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
420 
421  virtual void stopRender( QgsRenderContext& context ) override;
422 
423  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
424 
425  virtual QList<QString> usedAttributes() override;
426 
427  virtual QgsRuleBasedRendererV2* clone() const override;
428 
429  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
430 
431  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
432 
433  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
434 
436  virtual QDomElement save( QDomDocument& doc ) override;
437 
439  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
440 
443  virtual bool legendSymbolItemsCheckable() const override;
444 
447  virtual bool legendSymbolItemChecked( const QString& key ) override;
448 
451  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
452 
453  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
454 
457  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) override;
458 
462  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
463 
465  virtual QString dump() const override;
466 
469  virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context ) override;
470 
474  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
475 
476  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
477 
478  virtual QSet<QString> legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
479 
481  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
482 
484 
485  Rule* rootRule() { return mRootRule; }
486 
488 
490  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
492  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
494  static void refineRuleScales( Rule* initialRule, QList<int> scales );
495 
499  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
500 
502  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, const QString& sizeScaleField, const QString& rotationField = QString() );
503 
504  protected:
507 
508  // temporary
509  RenderQueue mRenderQueue;
511 
513 };
514 
515 #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:189
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