QGIS API Documentation  2.17.0-Master (f49f7ce)
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 
265  void toSld( QDomDocument& doc, QDomElement &element, const QgsStringMap& props ) const;
266  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
267 
268  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap ) const;
269 
273  Q_DECL_DEPRECATED bool startRender( QgsRenderContext& context, const QgsFields& fields );
274 
276  bool startRender( QgsRenderContext& context, const QgsFields& fields, QString& filter );
277 
279  QSet<int> collectZLevels();
280 
283  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
284 
294  RenderResult renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
295 
297  bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
298 
300  QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
301 
305  QSet< QString > legendKeysForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
306 
308  RuleList rulesForFeature( QgsFeature& feat, QgsRenderContext* context = nullptr );
309 
315  void stopRender( QgsRenderContext& context );
316 
325  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
326 
332  RuleList& children() { return mChildren; }
333 
339  RuleList descendants() const { RuleList l; Q_FOREACH ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
340 
346  Rule* parent() { return mParent; }
347 
349  void appendChild( Rule* rule );
350 
352  void insertChild( int i, Rule* rule );
353 
355  void removeChild( Rule* rule );
356 
358  void removeChildAt( int i );
359 
361  Rule* takeChild( Rule* rule );
362 
364  Rule* takeChildAt( int i );
365 
368  Rule* findRuleByKey( const QString& key );
369 
375  void updateElseRules();
376 
382  void setIsElse( bool iselse );
383 
389  bool isElse() { return mElseRule; }
390 
391  protected:
392  void initFilter();
393 
394  Rule* mParent; // parent rule (NULL only for root rule)
396  int mScaleMinDenom, mScaleMaxDenom;
397  QString mFilterExp, mLabel, mDescription;
398  bool mElseRule;
399  RuleList mChildren;
400  RuleList mElseRules;
401  bool mIsActive; // whether it is enabled or not
402 
403  QString mRuleKey; // string used for unique identification of rule within renderer
404 
405  // temporary
407  // temporary while rendering
409  RuleList mActiveChildren;
410 
411  private:
412 
413  Rule( const Rule& rh );
414  Rule& operator=( const Rule& rh );
415  };
416 
418 
419  static QgsFeatureRendererV2* create( QDomElement& element );
420 
424  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
425 
427 
429  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
430 
431  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
432 
433  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
434 
435  virtual void stopRender( QgsRenderContext& context ) override;
436 
437  virtual QString filter( const QgsFields& fields = QgsFields() ) override;
438 
439  virtual QList<QString> usedAttributes() override;
440 
441  virtual bool filterNeedsGeometry() const override;
442 
443  virtual QgsRuleBasedRendererV2* clone() const override;
444 
446  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
448  virtual void toSld( QDomDocument& doc, QDomElement &element, const QgsStringMap& props ) const override;
449 
450  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
451 
452  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
453 
455  virtual QDomElement save( QDomDocument& doc ) override;
456 
458  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
459 
462  virtual bool legendSymbolItemsCheckable() const override;
463 
466  virtual bool legendSymbolItemChecked( const QString& key ) override;
467 
470  virtual void checkLegendSymbolItem( const QString& key, bool state = true ) override;
471 
472  virtual void setLegendSymbolItem( const QString& key, QgsSymbolV2* symbol ) override;
473 
476  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, const QString& rule = "" ) override;
477 
481  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
482 
484  virtual QString dump() const override;
485 
488  virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context ) override;
489 
493  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
494 
495  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
496 
497  virtual QSet<QString> legendKeysForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
498 
500  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
501 
503 
504  Rule* rootRule() { return mRootRule; }
505 
507 
509  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
511  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
513  static void refineRuleScales( Rule* initialRule, QList<int> scales );
514 
518  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
519 
521  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, const QString& sizeScaleField, const QString& rotationField = QString() );
522 
523  protected:
526 
527  // temporary
528  RenderQueue mRenderQueue;
530 
532 };
533 
534 #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:252
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