QGIS API Documentation  3.0.2-Girona (307d082)
qgsrulebasedrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrulebasedrenderer.h - Rule-based renderer (symbology)
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 "qgis_core.h"
20 #include "qgis_sip.h"
21 #include "qgsfields.h"
22 #include "qgsfeature.h"
23 #include "qgis.h"
24 
25 #include "qgsrenderer.h"
26 
27 class QgsExpression;
28 
31 
37 class CORE_EXPORT QgsRuleBasedRenderer : public QgsFeatureRenderer
38 {
39  public:
40  // TODO: use QVarLengthArray instead of QList
41 
43  {
44  FeatIsSelected = 1,
45  FeatDrawMarkers = 2
46  };
47 
48  // feature for rendering: QgsFeature and some flags
50  {
51  FeatureToRender( QgsFeature &_f, int _flags )
52  : feat( _f )
53  , flags( _flags )
54  {}
56  int flags; // selected and/or draw markers
57  };
58 
59  // rendering job: a feature to be rendered with a particular symbol
60  // (both f, symbol are _not_ owned by this class)
61  struct RenderJob
62  {
64  : ftr( _ftr )
65  , symbol( _s )
66  {}
68  QgsSymbol *symbol = nullptr;
69  };
70 
71  // render level: a list of jobs to be drawn at particular level
72  // (jobs are owned by this class)
73  struct RenderLevel
74  {
75  explicit RenderLevel( int z ): zIndex( z ) {}
76  ~RenderLevel() { Q_FOREACH ( RenderJob *j, jobs ) delete j; }
77  int zIndex;
78  QList<QgsRuleBasedRenderer::RenderJob *> jobs;
79 
81  {
82  zIndex = rh.zIndex;
83  qDeleteAll( jobs );
84  jobs.clear();
85  Q_FOREACH ( RenderJob *job, rh.jobs )
86  {
87  jobs << new RenderJob( *job );
88  }
89  return *this;
90  }
91 
93  : zIndex( other.zIndex )
94  {
95  Q_FOREACH ( RenderJob *job, other.jobs )
96  {
97  jobs << new RenderJob( *job );
98  }
99  }
100 
101  };
102 
103  // rendering queue: a list of rendering levels
104  typedef QList<QgsRuleBasedRenderer::RenderLevel> RenderQueue;
105 
106  class Rule;
107  typedef QList<QgsRuleBasedRenderer::Rule *> RuleList;
108 
118  class CORE_EXPORT Rule
119  {
120  public:
123  {
124  Filtered = 0,
126  Rendered
127  };
128 
130  Rule( QgsSymbol *symbol SIP_TRANSFER, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(),
131  const QString &label = QString(), const QString &description = QString(), bool elseRule = false );
132  ~Rule();
133 
135  Rule( const Rule &rh ) = delete;
137  Rule &operator=( const Rule &rh ) = delete;
138 
144  QString dump( int indent = 0 ) const;
145 
150  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
151 
155  bool needsGeometry() const;
156 
158  QgsSymbolList symbols( const QgsRenderContext &context = QgsRenderContext() ) const;
159 
161  QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
162 
170  bool isFilterOK( QgsFeature &f, QgsRenderContext *context = nullptr ) const;
171 
179  bool isScaleOK( double scale ) const;
180 
181  QgsSymbol *symbol() { return mSymbol; }
182  QString label() const { return mLabel; }
183  bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
184 
193  double maximumScale() const { return mMaximumScale; }
194 
203  double minimumScale() const { return mMinimumScale; }
204 
209  QgsExpression *filter() const { return mFilter; }
210 
215  QString filterExpression() const { return mFilterExp; }
216 
222  QString description() const { return mDescription; }
223 
229  bool active() const { return mIsActive; }
230 
235  QString ruleKey() const { return mRuleKey; }
236 
241  void setRuleKey( const QString &key ) { mRuleKey = key; }
242 
244  void setSymbol( QgsSymbol *sym SIP_TRANSFER );
245  void setLabel( const QString &label ) { mLabel = label; }
246 
254  void setMinimumScale( double scale ) { mMinimumScale = scale; }
255 
263  void setMaximumScale( double scale ) { mMaximumScale = scale; }
264 
270  void setFilterExpression( const QString &filterExp );
271 
277  void setDescription( const QString &description ) { mDescription = description; }
278 
283  void setActive( bool state ) { mIsActive = state; }
284 
287 
288  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
289 
293  static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
294 
295  QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
296 
298  bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
299 
301  QSet<int> collectZLevels();
302 
307  void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
308 
319 
321  bool willRenderFeature( QgsFeature &feat, QgsRenderContext *context = nullptr );
322 
324  QgsSymbolList symbolsForFeature( QgsFeature &feat, QgsRenderContext *context = nullptr );
325 
330  QSet< QString > legendKeysForFeature( QgsFeature &feat, QgsRenderContext *context = nullptr );
331 
333  QgsRuleBasedRenderer::RuleList rulesForFeature( QgsFeature &feat, QgsRenderContext *context = nullptr );
334 
340  void stopRender( QgsRenderContext &context );
341 
350  static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap ) SIP_FACTORY;
351 
357  QgsRuleBasedRenderer::RuleList &children() { return mChildren; }
358 
364  QgsRuleBasedRenderer::RuleList descendants() const { RuleList l; Q_FOREACH ( QgsRuleBasedRenderer::Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
365 
371  QgsRuleBasedRenderer::Rule *parent() { return mParent; }
372 
374  void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
375 
377  void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
378 
380  void removeChild( QgsRuleBasedRenderer::Rule *rule );
381 
383  void removeChildAt( int i );
384 
387 
389  QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
390 
395  QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
396 
402  void setIsElse( bool iselse );
403 
409  bool isElse() { return mElseRule; }
410 
411  protected:
412  void initFilter();
413 
414  Rule *mParent; // parent rule (NULL only for root rule)
415  QgsSymbol *mSymbol = nullptr;
416  double mMaximumScale = 0;
417  double mMinimumScale = 0;
418  QString mFilterExp, mLabel, mDescription;
419  bool mElseRule;
420  RuleList mChildren;
421  RuleList mElseRules;
422  bool mIsActive; // whether it is enabled or not
423 
424  QString mRuleKey; // string used for unique identification of rule within renderer
425 
426  // temporary
427  QgsExpression *mFilter = nullptr;
428  // temporary while rendering
430  RuleList mActiveChildren;
431 
432  private:
433 #ifdef SIP_RUN
434  Rule( const QgsRuleBasedRenderer::Rule &rh );
435 #endif
436 
441  void updateElseRules();
442  };
443 
445 
447  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
448 
450  QgsRuleBasedRenderer( QgsRuleBasedRenderer::Rule *root SIP_TRANSFER );
452  QgsRuleBasedRenderer( QgsSymbol *defaultSymbol SIP_TRANSFER );
453 
454  ~QgsRuleBasedRenderer() override;
455 
457  QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
458 
459  bool renderFeature( QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
460 
461  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
462 
463  void stopRender( QgsRenderContext &context ) override;
464 
465  QString filter( const QgsFields &fields = QgsFields() ) override;
466 
467  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
468 
469  bool filterNeedsGeometry() const override;
470 
471  QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
472 
473  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
474 
475  static QgsFeatureRenderer *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
476 
477  QgsSymbolList symbols( QgsRenderContext &context ) override;
478 
479  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
480  bool legendSymbolItemsCheckable() const override;
481  bool legendSymbolItemChecked( const QString &key ) override;
482  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
483 
484  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
485  QgsLegendSymbolList legendSymbolItems() const override;
486  QString dump() const override;
487  bool willRenderFeature( QgsFeature &feat, QgsRenderContext &context ) override;
488  QgsSymbolList symbolsForFeature( QgsFeature &feat, QgsRenderContext &context ) override;
489  QgsSymbolList originalSymbolsForFeature( QgsFeature &feat, QgsRenderContext &context ) override;
490  QSet<QString> legendKeysForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
491  QgsFeatureRenderer::Capabilities capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
492 
494 
495  QgsRuleBasedRenderer::Rule *rootRule() { return mRootRule; }
496 
498 
500  static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
502  static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
504  static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
505 
511  static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
512 
514  static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
515 
516  protected:
518  Rule *mRootRule = nullptr;
519 
520  // temporary
521  RenderQueue mRenderQueue;
522  QList<FeatureToRender> mCurrentFeatures;
523 
524  QString mFilter;
525 
526  private:
527 #ifdef SIP_RUN
529  QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
530 #endif
531 };
532 
533 #endif // QGSRULEBASEDRENDERERV2_H
The class is used as a container of context for various read/write operations on other objects...
RenderLevel(const QgsRuleBasedRenderer::RenderLevel &other)
void setDescription(const QString &description)
Set a human readable description for this rule.
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QgsRuleBasedRenderer::RuleList descendants() const
Returns all children, grand-children, grand-grand-children, grand-gra...
bool isElse()
Check if this rule is an ELSE rule.
QList< QgsRuleBasedRenderer::RenderLevel > RenderQueue
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
This class keeps data about a rules for rule-based renderer.
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based renderer) ...
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
QgsRuleBasedRenderer::FeatureToRender & ftr
QgsRuleBasedRenderer::RuleList & children()
Return all children rules of this rule.
Container of fields for a vector layer.
Definition: qgsfields.h:42
QList< FeatureToRender > mCurrentFeatures
QgsRuleBasedRenderer::RenderLevel & operator=(const QgsRuleBasedRenderer::RenderLevel &rh)
double maximumScale() const
Returns the maximum map scale (i.e.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
QString description() const
A human readable description for this rule.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:479
bool active() const
Returns if this rule is active.
QList< QgsRuleBasedRenderer::Rule * > RuleList
RenderResult
The result of rendering a rule.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsExpression * filter() const
A filter that will check if this rule applies.
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features wi...
QgsRuleBasedRenderer::Rule * rootRule()
QList< QgsRuleBasedRenderer::RenderJob * > jobs
double minimumScale() const
Returns the minimum map scale (i.e.
#define SIP_FACTORY
Definition: qgis_sip.h:69
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:137
void setActive(bool state)
Sets if this rule is active.
Contains information about the context of a rendering operation.
void setLabel(const QString &label)
QMap< QString, QgsSymbol *> QgsSymbolMap
Definition: qgsrenderer.h:44
RenderJob(QgsRuleBasedRenderer::FeatureToRender &_ftr, QgsSymbol *_s)
QString filterExpression() const
A filter that will check if this rule applies.
FeatureToRender(QgsFeature &_f, int _flags)
QgsRuleBasedRenderer::Rule * parent()
The parent rule.