QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
39 class CORE_EXPORT QgsRuleBasedRenderer : public QgsFeatureRenderer
40 {
41  public:
42  // TODO: use QVarLengthArray instead of QList
43 
45  {
46  FeatIsSelected = 1,
47  FeatDrawMarkers = 2
48  };
49 
55  {
56  FeatureToRender( const QgsFeature &_f, int _flags )
57  : feat( _f )
58  , flags( _flags )
59  {}
61  int flags; // selected and/or draw markers
62  };
63 
68  struct RenderJob
69  {
71  : ftr( _ftr )
72  , symbol( _s )
73  {}
74 
77 
79  QgsSymbol *symbol = nullptr;
80 
81  private:
82 #ifdef SIP_RUN
83  RenderJob &operator=( const RenderJob & );
84 #endif
85  };
86 
91  struct RenderLevel
92  {
93  explicit RenderLevel( int z ): zIndex( z ) {}
94  ~RenderLevel() { qDeleteAll( jobs ); }
95  int zIndex;
96 
98  QList<QgsRuleBasedRenderer::RenderJob *> jobs;
99 
101  {
102  zIndex = rh.zIndex;
103  qDeleteAll( jobs );
104  jobs.clear();
105  for ( auto it = rh.jobs.constBegin(); it != rh.jobs.constEnd(); ++it )
106  {
107  jobs << new RenderJob( *( *it ) );
108  }
109  return *this;
110  }
111 
113  : zIndex( other.zIndex ), jobs()
114  {
115  for ( auto it = other.jobs.constBegin(); it != other.jobs.constEnd(); ++it )
116  {
117  jobs << new RenderJob( * ( *it ) );
118  }
119  }
120 
121  };
122 
124  typedef QList<QgsRuleBasedRenderer::RenderLevel> RenderQueue;
125 
126  class Rule;
127  typedef QList<QgsRuleBasedRenderer::Rule *> RuleList;
128 
139  class CORE_EXPORT Rule
140  {
141  public:
144  {
145  Filtered = 0,
147  Rendered
148  };
149 
151  Rule( QgsSymbol *symbol SIP_TRANSFER, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(),
152  const QString &label = QString(), const QString &description = QString(), bool elseRule = false );
153  ~Rule();
154 
156  Rule( const Rule &rh ) = delete;
158  Rule &operator=( const Rule &rh ) = delete;
159 
165  QString dump( int indent = 0 ) const;
166 
171  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
172 
176  bool needsGeometry() const;
177 
179  QgsSymbolList symbols( const QgsRenderContext &context = QgsRenderContext() ) const;
180 
182  QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
183 
191  bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
192 
200  bool isScaleOK( double scale ) const;
201 
202  QgsSymbol *symbol() { return mSymbol.get(); }
203  QString label() const { return mLabel; }
204  bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
205 
214  double maximumScale() const { return mMaximumScale; }
215 
224  double minimumScale() const { return mMinimumScale; }
225 
230  QgsExpression *filter() const { return mFilter.get(); }
231 
236  QString filterExpression() const { return mFilterExp; }
237 
243  QString description() const { return mDescription; }
244 
250  bool active() const { return mIsActive; }
251 
256  QString ruleKey() const { return mRuleKey; }
257 
262  void setRuleKey( const QString &key ) { mRuleKey = key; }
263 
265  void setSymbol( QgsSymbol *sym SIP_TRANSFER );
266  void setLabel( const QString &label ) { mLabel = label; }
267 
275  void setMinimumScale( double scale ) { mMinimumScale = scale; }
276 
284  void setMaximumScale( double scale ) { mMaximumScale = scale; }
285 
291  void setFilterExpression( const QString &filterExp );
292 
298  void setDescription( const QString &description ) { mDescription = description; }
299 
304  void setActive( bool state ) { mIsActive = state; }
305 
308 
310  void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
311 
315  static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
316 
317  QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
318 
320  bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
321 
323  QSet<int> collectZLevels();
324 
329  void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
330 
340  QgsRuleBasedRenderer::Rule::RenderResult renderFeature( QgsRuleBasedRenderer::FeatureToRender &featToRender, QgsRenderContext &context, QgsRuleBasedRenderer::RenderQueue &renderQueue );
341 
343  bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
344 
346  QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
347 
352  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
353 
362  QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
363 
369  void stopRender( QgsRenderContext &context );
370 
379  static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap ) SIP_FACTORY;
380 
386  const QgsRuleBasedRenderer::RuleList &children() { return mChildren; }
387 
393  QgsRuleBasedRenderer::RuleList descendants() const;
394 
400  QgsRuleBasedRenderer::Rule *parent() { return mParent; }
401 
403  void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
404 
406  void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
407 
409  void removeChild( QgsRuleBasedRenderer::Rule *rule );
410 
412  void removeChildAt( int i );
413 
416 
418  QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
419 
424  QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
425 
431  void setIsElse( bool iselse );
432 
438  bool isElse() const { return mElseRule; }
439 
449  bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
450 
451  protected:
452  void initFilter();
453 
454  private:
455 #ifdef SIP_RUN
456  Rule( const QgsRuleBasedRenderer::Rule &rh );
457 #endif
458 
459  Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
460  std::unique_ptr< QgsSymbol > mSymbol;
461  double mMaximumScale = 0;
462  double mMinimumScale = 0;
463  QString mFilterExp, mLabel, mDescription;
464  bool mElseRule = false;
465  RuleList mChildren;
466  RuleList mElseRules;
467  bool mIsActive = true; // whether it is enabled or not
468 
469  QString mRuleKey; // string used for unique identification of rule within renderer
470 
471  // temporary
472  std::unique_ptr< QgsExpression > mFilter;
473  // temporary while rendering
474  QSet<int> mSymbolNormZLevels;
475  RuleList mActiveChildren;
476 
481  void updateElseRules();
482  };
483 
485 
487  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
488 
492  QgsRuleBasedRenderer( QgsSymbol *defaultSymbol SIP_TRANSFER );
493 
494  ~QgsRuleBasedRenderer() override;
495 
497  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
498 
499  bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
500 
501  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
502 
503  void stopRender( QgsRenderContext &context ) override;
504 
505  QString filter( const QgsFields &fields = QgsFields() ) override;
506 
507  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
508 
509  bool filterNeedsGeometry() const override;
510 
511  QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
512 
513  void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
514 
515  static QgsFeatureRenderer *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
516 
517  QgsSymbolList symbols( QgsRenderContext &context ) const override;
518 
519  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
520  bool legendSymbolItemsCheckable() const override;
521  bool legendSymbolItemChecked( const QString &key ) override;
522  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
523 
524  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
525  QgsLegendSymbolList legendSymbolItems() const override;
526  QString dump() const override;
527  bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
528  QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
529  QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
530  QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
531  QgsFeatureRenderer::Capabilities capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
532  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
533 
535 
536  QgsRuleBasedRenderer::Rule *rootRule() { return mRootRule; }
537 
539 
541  static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
543  static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
545  static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
546 
555  static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
556 
558  static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
559 
560  protected:
562  Rule *mRootRule = nullptr;
563 
564  // temporary
565  RenderQueue mRenderQueue;
566  QList<FeatureToRender> mCurrentFeatures;
567 
568  QString mFilter;
569 
570  private:
571 #ifdef SIP_RUN
573  QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
574 #endif
575 };
576 
577 #endif // QGSRULEBASEDRENDERER_H
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Class for parsing and evaluation of expressions (formerly called "search strings").
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
virtual QString dump() const
Returns debug information about this renderer.
@ ScaleDependent
Depends on scale if feature will be rendered (rule based )
Definition: qgsrenderer.h:265
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
virtual bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) SIP_THROW(QgsCsException)
Render a feature using this renderer in the given context.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
This class keeps data about a rules for rule-based renderer.
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
void setDescription(const QString &description)
Set a human readable description for this rule.
RenderResult
The result of rendering a rule.
@ Inactive
The rule is inactive.
double maximumScale() const
Returns the maximum map scale (i.e.
bool isElse() const
Check if this rule is an ELSE rule.
QgsRuleBasedRenderer::Rule * parent()
The parent rule.
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
void setActive(bool state)
Sets if this rule is active.
Rule(const Rule &rh)=delete
Rules cannot be copied.
QgsExpression * filter() const
A filter that will check if this rule applies.
double minimumScale() const
Returns the minimum map scale (i.e.
QString description() const
A human readable description for this rule.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
void setLabel(const QString &label)
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based renderer)
QString filterExpression() const
A filter that will check if this rule applies.
bool active() const
Returns if this rule is active.
Rule & operator=(const Rule &rh)=delete
Rules cannot be copied.
Rule based renderer.
QList< QgsRuleBasedRenderer::RenderLevel > RenderQueue
Rendering queue: a list of rendering levels.
QgsRuleBasedRenderer::Rule * rootRule()
QList< QgsRuleBasedRenderer::Rule * > RuleList
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
Represents a vector layer which manages a vector based data sets.
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
#define SIP_THROW(name)
Definition: qgis_sip.h:189
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
#define SIP_FACTORY
Definition: qgis_sip.h:76
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:44
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
Feature for rendering by a QgsRuleBasedRenderer.
FeatureToRender(const QgsFeature &_f, int _flags)
A QgsRuleBasedRenderer rendering job, consisting of a feature to be rendered with a particular symbol...
QgsRuleBasedRenderer::FeatureToRender & ftr
Feature to render.
RenderJob(QgsRuleBasedRenderer::FeatureToRender &_ftr, QgsSymbol *_s)
Render level: a list of jobs to be drawn at particular level for a QgsRuleBasedRenderer.
QList< QgsRuleBasedRenderer::RenderJob * > jobs
List of jobs to render, owned by this object.
RenderLevel(const QgsRuleBasedRenderer::RenderLevel &other)
QgsRuleBasedRenderer::RenderLevel & operator=(const QgsRuleBasedRenderer::RenderLevel &rh)