QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 QGSRULEBASEDRENDERER_H
17 #define QGSRULEBASEDRENDERER_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 
53  {
54  FeatureToRender( const QgsFeature &_f, int _flags )
55  : feat( _f )
56  , flags( _flags )
57  {}
59  int flags; // selected and/or draw markers
60  };
61 
66  struct RenderJob
67  {
69  : ftr( _ftr )
70  , symbol( _s )
71  {}
72 
75 
77  QgsSymbol *symbol = nullptr;
78 
79  private:
80 #ifdef SIP_RUN
81  RenderJob &operator=( const RenderJob & );
82 #endif
83  };
84 
89  struct RenderLevel
90  {
91  explicit RenderLevel( int z ): zIndex( z ) {}
92  ~RenderLevel() { qDeleteAll( jobs ); }
93  int zIndex;
94 
96  QList<QgsRuleBasedRenderer::RenderJob *> jobs;
97 
99  {
100  zIndex = rh.zIndex;
101  qDeleteAll( jobs );
102  jobs.clear();
103  for ( RenderJob *job : qgis::as_const( rh.jobs ) )
104  {
105  jobs << new RenderJob( *job );
106  }
107  return *this;
108  }
109 
111  : zIndex( other.zIndex ), jobs()
112  {
113  for ( RenderJob *job : qgis::as_const( other.jobs ) )
114  {
115  jobs << new RenderJob( *job );
116  }
117  }
118 
119  };
120 
122  typedef QList<QgsRuleBasedRenderer::RenderLevel> RenderQueue;
123 
124  class Rule;
125  typedef QList<QgsRuleBasedRenderer::Rule *> RuleList;
126 
136  class CORE_EXPORT Rule
137  {
138  public:
141  {
142  Filtered = 0,
144  Rendered
145  };
146 
148  Rule( QgsSymbol *symbol SIP_TRANSFER, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(),
149  const QString &label = QString(), const QString &description = QString(), bool elseRule = false );
150  ~Rule();
151 
153  Rule( const Rule &rh ) = delete;
155  Rule &operator=( const Rule &rh ) = delete;
156 
162  QString dump( int indent = 0 ) const;
163 
168  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
169 
173  bool needsGeometry() const;
174 
176  QgsSymbolList symbols( const QgsRenderContext &context = QgsRenderContext() ) const;
177 
179  QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
180 
188  bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
189 
197  bool isScaleOK( double scale ) const;
198 
199  QgsSymbol *symbol() { return mSymbol.get(); }
200  QString label() const { return mLabel; }
201  bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
202 
211  double maximumScale() const { return mMaximumScale; }
212 
221  double minimumScale() const { return mMinimumScale; }
222 
227  QgsExpression *filter() const { return mFilter.get(); }
228 
233  QString filterExpression() const { return mFilterExp; }
234 
240  QString description() const { return mDescription; }
241 
247  bool active() const { return mIsActive; }
248 
253  QString ruleKey() const { return mRuleKey; }
254 
259  void setRuleKey( const QString &key ) { mRuleKey = key; }
260 
262  void setSymbol( QgsSymbol *sym SIP_TRANSFER );
263  void setLabel( const QString &label ) { mLabel = label; }
264 
272  void setMinimumScale( double scale ) { mMinimumScale = scale; }
273 
281  void setMaximumScale( double scale ) { mMaximumScale = scale; }
282 
288  void setFilterExpression( const QString &filterExp );
289 
295  void setDescription( const QString &description ) { mDescription = description; }
296 
301  void setActive( bool state ) { mIsActive = state; }
302 
305 
306  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
307 
311  static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
312 
313  QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
314 
316  bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
317 
319  QSet<int> collectZLevels();
320 
325  void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
326 
336  QgsRuleBasedRenderer::Rule::RenderResult renderFeature( QgsRuleBasedRenderer::FeatureToRender &featToRender, QgsRenderContext &context, QgsRuleBasedRenderer::RenderQueue &renderQueue );
337 
339  bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
340 
342  QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
343 
348  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
349 
358  QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
359 
365  void stopRender( QgsRenderContext &context );
366 
375  static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap ) SIP_FACTORY;
376 
382  const QgsRuleBasedRenderer::RuleList &children() { return mChildren; }
383 
389  QgsRuleBasedRenderer::RuleList descendants() const;
390 
396  QgsRuleBasedRenderer::Rule *parent() { return mParent; }
397 
399  void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
400 
402  void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
403 
405  void removeChild( QgsRuleBasedRenderer::Rule *rule );
406 
408  void removeChildAt( int i );
409 
412 
414  QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
415 
420  QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
421 
427  void setIsElse( bool iselse );
428 
434  bool isElse() const { return mElseRule; }
435 
445  bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
446 
447  protected:
448  void initFilter();
449 
450  private:
451 #ifdef SIP_RUN
452  Rule( const QgsRuleBasedRenderer::Rule &rh );
453 #endif
454 
455  Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
456  std::unique_ptr< QgsSymbol > mSymbol;
457  double mMaximumScale = 0;
458  double mMinimumScale = 0;
459  QString mFilterExp, mLabel, mDescription;
460  bool mElseRule = false;
461  RuleList mChildren;
462  RuleList mElseRules;
463  bool mIsActive = true; // whether it is enabled or not
464 
465  QString mRuleKey; // string used for unique identification of rule within renderer
466 
467  // temporary
468  std::unique_ptr< QgsExpression > mFilter;
469  // temporary while rendering
470  QSet<int> mSymbolNormZLevels;
471  RuleList mActiveChildren;
472 
477  void updateElseRules();
478  };
479 
481 
483  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
484 
488  QgsRuleBasedRenderer( QgsSymbol *defaultSymbol SIP_TRANSFER );
489 
490  ~QgsRuleBasedRenderer() override;
491 
493  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
494 
495  bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
496 
497  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
498 
499  void stopRender( QgsRenderContext &context ) override;
500 
501  QString filter( const QgsFields &fields = QgsFields() ) override;
502 
503  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
504 
505  bool filterNeedsGeometry() const override;
506 
507  QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
508 
509  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
510 
511  static QgsFeatureRenderer *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
512 
513  QgsSymbolList symbols( QgsRenderContext &context ) const override;
514 
515  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
516  bool legendSymbolItemsCheckable() const override;
517  bool legendSymbolItemChecked( const QString &key ) override;
518  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
519 
520  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
521  QgsLegendSymbolList legendSymbolItems() const override;
522  QString dump() const override;
523  bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
524  QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
525  QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
526  QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
527  QgsFeatureRenderer::Capabilities capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
528  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
529 
531 
532  QgsRuleBasedRenderer::Rule *rootRule() { return mRootRule; }
533 
535 
537  static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
539  static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
541  static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
542 
548  static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
549 
551  static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
552 
553  protected:
555  Rule *mRootRule = nullptr;
556 
557  // temporary
559  QList<FeatureToRender> mCurrentFeatures;
560 
561  QString mFilter;
562 
563  private:
564 #ifdef SIP_RUN
566  QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
567 #endif
568 };
569 
570 #endif // QGSRULEBASEDRENDERER_H
qgsfields.h
QgsRuleBasedRenderer
Definition: qgsrulebasedrenderer.h:37
QgsRuleBasedRenderer::RenderLevel::jobs
QList< QgsRuleBasedRenderer::RenderJob * > jobs
List of jobs to render, owned by this object.
Definition: qgsrulebasedrenderer.h:96
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsSymbolMap
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:46
QgsFields
Definition: qgsfields.h:44
qgsfeature.h
qgis.h
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsStyleEntityVisitorInterface
Definition: qgsstyleentityvisitor.h:33
QgsRuleBasedRenderer::Rule::ruleKey
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
Definition: qgsrulebasedrenderer.h:253
QgsRuleBasedRenderer::RuleList
QList< QgsRuleBasedRenderer::Rule * > RuleList
Definition: qgsrulebasedrenderer.h:124
QgsSymbol
Definition: qgssymbol.h:63
QgsRuleBasedRenderer::Rule::setMaximumScale
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
Definition: qgsrulebasedrenderer.h:281
QgsRuleBasedRenderer::RenderQueue
QList< QgsRuleBasedRenderer::RenderLevel > RenderQueue
Rendering queue: a list of rendering levels.
Definition: qgsrulebasedrenderer.h:122
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
QgsRuleBasedRenderer::Rule::setDescription
void setDescription(const QString &description)
Set a human readable description for this rule.
Definition: qgsrulebasedrenderer.h:295
QgsRuleBasedRenderer::Rule::minimumScale
double minimumScale() const
Returns the minimum map scale (i.e.
Definition: qgsrulebasedrenderer.h:221
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsRuleBasedRenderer::Rule::maximumScale
double maximumScale() const
Returns the maximum map scale (i.e.
Definition: qgsrulebasedrenderer.h:211
QgsRuleBasedRenderer::RenderJob::ftr
QgsRuleBasedRenderer::FeatureToRender & ftr
Feature to render.
Definition: qgsrulebasedrenderer.h:74
QgsRuleBasedRenderer::RenderLevel::zIndex
int zIndex
Definition: qgsrulebasedrenderer.h:93
QgsRuleBasedRenderer::Rule::dependsOnScale
bool dependsOnScale() const
Definition: qgsrulebasedrenderer.h:201
QgsRuleBasedRenderer::Rule::setRuleKey
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based renderer)
Definition: qgsrulebasedrenderer.h:259
QgsCsException
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsRuleBasedRenderer::FeatureFlags
FeatureFlags
Definition: qgsrulebasedrenderer.h:42
QgsRuleBasedRenderer::Rule::filter
QgsExpression * filter() const
A filter that will check if this rule applies.
Definition: qgsrulebasedrenderer.h:227
QgsRuleBasedRenderer::rootRule
QgsRuleBasedRenderer::Rule * rootRule()
Definition: qgsrulebasedrenderer.h:532
QgsRuleBasedRenderer::mCurrentFeatures
QList< FeatureToRender > mCurrentFeatures
Definition: qgsrulebasedrenderer.h:559
QgsRuleBasedRenderer::FeatureToRender
Feature for rendering by a QgsRuleBasedRenderer.
Definition: qgsrulebasedrenderer.h:52
QgsRuleBasedRenderer::Rule::description
QString description() const
A human readable description for this rule.
Definition: qgsrulebasedrenderer.h:240
QgsRuleBasedRenderer::mRenderQueue
RenderQueue mRenderQueue
Definition: qgsrulebasedrenderer.h:558
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:45
QgsRuleBasedRenderer::mFilter
QString mFilter
Definition: qgsrulebasedrenderer.h:561
QgsRuleBasedRenderer::FeatureToRender::flags
int flags
Definition: qgsrulebasedrenderer.h:59
QgsGraduatedSymbolRenderer
Definition: qgsgraduatedsymbolrenderer.h:35
qgis_sip.h
QgsRuleBasedRenderer::Rule::symbol
QgsSymbol * symbol()
Definition: qgsrulebasedrenderer.h:199
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsRuleBasedRenderer::RenderLevel::RenderLevel
RenderLevel(int z)
Definition: qgsrulebasedrenderer.h:91
QgsRuleBasedRenderer::Rule::label
QString label() const
Definition: qgsrulebasedrenderer.h:200
QgsRuleBasedRenderer::FeatureToRender::feat
QgsFeature feat
Definition: qgsrulebasedrenderer.h:58
qgsrenderer.h
QgsRuleBasedRenderer::Rule::RenderResult
RenderResult
The result of rendering a rule.
Definition: qgsrulebasedrenderer.h:140
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsFeatureRenderer
Definition: qgsrenderer.h:102
QgsRuleBasedRenderer::RenderJob
A QgsRuleBasedRenderer rendering job, consisting of a feature to be rendered with a particular symbol...
Definition: qgsrulebasedrenderer.h:66
QgsRuleBasedRenderer::Rule::isElse
bool isElse() const
Check if this rule is an ELSE rule.
Definition: qgsrulebasedrenderer.h:434
QgsRuleBasedRenderer::Rule::setMinimumScale
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
Definition: qgsrulebasedrenderer.h:272
QgsRuleBasedRenderer::Rule::setLabel
void setLabel(const QString &label)
Definition: qgsrulebasedrenderer.h:263
QgsRuleBasedRenderer::RenderLevel::~RenderLevel
~RenderLevel()
Definition: qgsrulebasedrenderer.h:92
QgsRuleBasedRenderer::Rule::parent
QgsRuleBasedRenderer::Rule * parent()
The parent rule.
Definition: qgsrulebasedrenderer.h:396
QgsRuleBasedRenderer::Rule::Inactive
@ Inactive
The rule is inactive.
Definition: qgsrulebasedrenderer.h:143
QgsCategorizedSymbolRenderer
Definition: qgscategorizedsymbolrenderer.h:151
QgsFeature
Definition: qgsfeature.h:55
QgsWkbTypes
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
QgsRuleBasedRenderer::RenderJob::RenderJob
RenderJob(QgsRuleBasedRenderer::FeatureToRender &_ftr, QgsSymbol *_s)
Definition: qgsrulebasedrenderer.h:68
QgsRuleBasedRenderer::Rule::setActive
void setActive(bool state)
Sets if this rule is active.
Definition: qgsrulebasedrenderer.h:301
QgsExpression
Definition: qgsexpression.h:113
QgsLegendSymbolList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition: qgslegendsymbolitem.h:144
QgsRuleBasedRenderer::RenderLevel::operator=
QgsRuleBasedRenderer::RenderLevel & operator=(const QgsRuleBasedRenderer::RenderLevel &rh)
Definition: qgsrulebasedrenderer.h:98
QgsRuleBasedRenderer::Rule
Definition: qgsrulebasedrenderer.h:136
QgsRuleBasedRenderer::FeatureToRender::FeatureToRender
FeatureToRender(const QgsFeature &_f, int _flags)
Definition: qgsrulebasedrenderer.h:54
QgsRuleBasedRenderer::RenderLevel
Render level: a list of jobs to be drawn at particular level for a QgsRuleBasedRenderer.
Definition: qgsrulebasedrenderer.h:89
QgsRuleBasedRenderer::Rule::active
bool active() const
Returns if this rule is active.
Definition: qgsrulebasedrenderer.h:247
QgsRuleBasedRenderer::RenderLevel::RenderLevel
RenderLevel(const QgsRuleBasedRenderer::RenderLevel &other)
Definition: qgsrulebasedrenderer.h:110
QgsRuleBasedRenderer::Rule::filterExpression
QString filterExpression() const
A filter that will check if this rule applies.
Definition: qgsrulebasedrenderer.h:233