QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsrendercontext.h"
27 
28 class QgsExpression;
29 
32 
40 class CORE_EXPORT QgsRuleBasedRenderer : public QgsFeatureRenderer
41 {
42  public:
43  // TODO: use QVarLengthArray instead of QList
44 
46  {
47  FeatIsSelected = 1,
48  FeatDrawMarkers = 2
49  };
50 
56  {
57  FeatureToRender( const QgsFeature &_f, int _flags )
58  : feat( _f )
59  , flags( _flags )
60  {}
62  int flags; // selected and/or draw markers
63  };
64 
69  struct RenderJob
70  {
71 
78  : ftr( _ftr )
79  , symbol( _s )
80  {}
81 
84 
86  QgsSymbol *symbol = nullptr;
87 
88  private:
89 #ifdef SIP_RUN
90  RenderJob &operator=( const RenderJob & );
91 #endif
92  };
93 
98  struct RenderLevel
99  {
100  explicit RenderLevel( int z )
101  : zIndex( z )
102  {}
103 
104  ~RenderLevel() { qDeleteAll( jobs ); }
105  int zIndex;
106 
108  QList<QgsRuleBasedRenderer::RenderJob *> jobs;
109 
111  {
112  zIndex = rh.zIndex;
113  qDeleteAll( jobs );
114  jobs.clear();
115  for ( auto it = rh.jobs.constBegin(); it != rh.jobs.constEnd(); ++it )
116  {
117  jobs << new RenderJob( *( *it ) );
118  }
119  return *this;
120  }
121 
123  : zIndex( other.zIndex ), jobs()
124  {
125  for ( auto it = other.jobs.constBegin(); it != other.jobs.constEnd(); ++it )
126  {
127  jobs << new RenderJob( * ( *it ) );
128  }
129  }
130 
131  };
132 
134  typedef QList<QgsRuleBasedRenderer::RenderLevel> RenderQueue;
135 
136  class Rule;
137  typedef QList<QgsRuleBasedRenderer::Rule *> RuleList;
138 
149  class CORE_EXPORT Rule
150  {
151  public:
154  {
155  Filtered = 0,
157  Rendered
158  };
159 
161  Rule( QgsSymbol *symbol SIP_TRANSFER, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(),
162  const QString &label = QString(), const QString &description = QString(), bool elseRule = false );
163  ~Rule();
164 
166  Rule( const Rule &rh ) = delete;
168  Rule &operator=( const Rule &rh ) = delete;
169 
175  QString dump( int indent = 0 ) const;
176 
181  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
182 
186  bool needsGeometry() const;
187 
189  QgsSymbolList symbols( const QgsRenderContext &context = QgsRenderContext() ) const;
190 
192  QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
193 
201  bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
202 
210  bool isScaleOK( double scale ) const;
211 
212  QgsSymbol *symbol() { return mSymbol.get(); }
213  QString label() const { return mLabel; }
214  bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
215 
224  double maximumScale() const { return mMaximumScale; }
225 
234  double minimumScale() const { return mMinimumScale; }
235 
240  QgsExpression *filter() const { return mFilter.get(); }
241 
246  QString filterExpression() const { return mFilterExp; }
247 
253  QString description() const { return mDescription; }
254 
260  bool active() const { return mIsActive; }
261 
266  QString ruleKey() const { return mRuleKey; }
267 
272  void setRuleKey( const QString &key ) { mRuleKey = key; }
273 
275  void setSymbol( QgsSymbol *sym SIP_TRANSFER );
276  void setLabel( const QString &label ) { mLabel = label; }
277 
285  void setMinimumScale( double scale ) { mMinimumScale = scale; }
286 
294  void setMaximumScale( double scale ) { mMaximumScale = scale; }
295 
301  void setFilterExpression( const QString &filterExp );
302 
308  void setDescription( const QString &description ) { mDescription = description; }
309 
314  void setActive( bool state ) { mIsActive = state; }
315 
318 
320  void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
321 
325  static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
326 
327  QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
328 
330  bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
331 
333  QSet<int> collectZLevels();
334 
339  void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
340 
350  QgsRuleBasedRenderer::Rule::RenderResult renderFeature( QgsRuleBasedRenderer::FeatureToRender &featToRender, QgsRenderContext &context, QgsRuleBasedRenderer::RenderQueue &renderQueue );
351 
353  bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
354 
356  QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
357 
362  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
363 
372  QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
373 
379  void stopRender( QgsRenderContext &context );
380 
389  static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap ) SIP_FACTORY;
390 
396  const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
397 
403  QgsRuleBasedRenderer::RuleList descendants() const;
404 
410  QgsRuleBasedRenderer::Rule *parent() { return mParent; }
411 
413  void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
414 
416  void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
417 
419  void removeChild( QgsRuleBasedRenderer::Rule *rule );
420 
422  void removeChildAt( int i );
423 
426 
428  QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
429 
434  QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
435 
441  void setIsElse( bool iselse );
442 
448  bool isElse() const { return mElseRule; }
449 
459  bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
460 
461  protected:
462  void initFilter();
463 
464  private:
465 #ifdef SIP_RUN
466  Rule( const QgsRuleBasedRenderer::Rule &rh );
467 #endif
468 
469  Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
470  std::unique_ptr< QgsSymbol > mSymbol;
471  double mMaximumScale = 0;
472  double mMinimumScale = 0;
473  QString mFilterExp, mLabel, mDescription;
474  bool mElseRule = false;
475  RuleList mChildren;
476  RuleList mElseRules;
477  bool mIsActive = true; // whether it is enabled or not
478 
479  QString mRuleKey; // string used for unique identification of rule within renderer
480 
481  // temporary
482  std::unique_ptr< QgsExpression > mFilter;
483  // temporary while rendering
484  QSet<int> mSymbolNormZLevels;
485  RuleList mActiveChildren;
486 
491  void updateElseRules();
492  };
493 
495 
497  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
498 
502  QgsRuleBasedRenderer( QgsSymbol *defaultSymbol SIP_TRANSFER );
503 
504  ~QgsRuleBasedRenderer() override;
505 
507  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
508 
509  bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
510 
511  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
512 
513  void stopRender( QgsRenderContext &context ) override;
514 
515  QString filter( const QgsFields &fields = QgsFields() ) override;
516 
517  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
518 
519  bool filterNeedsGeometry() const override;
520 
521  QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
522 
523  void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
524 
525  static QgsFeatureRenderer *createFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
526 
527  QgsSymbolList symbols( QgsRenderContext &context ) const override;
528 
529  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
530  bool legendSymbolItemsCheckable() const override;
531  bool legendSymbolItemChecked( const QString &key ) override;
532  void checkLegendSymbolItem( const QString &key, bool state = true ) override;
533  QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
534 
535  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
536  QgsLegendSymbolList legendSymbolItems() const override;
537  QString dump() const override;
538  bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
539  QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
540  QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
541  QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
542  QgsFeatureRenderer::Capabilities capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
543  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
544 
546 
547  QgsRuleBasedRenderer::Rule *rootRule() { return mRootRule; }
548 
550 
552  static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
554  static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
556  static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
557 
566  static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
567 
569  static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
570 
571  protected:
573  Rule *mRootRule = nullptr;
574 
575  // temporary
576  RenderQueue mRenderQueue;
577  QList<FeatureToRender> mCurrentFeatures;
578 
579  QString mFilter;
580 
581  private:
582 #ifdef SIP_RUN
584  QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
585 #endif
586 };
587 
588 #endif // QGSRULEBASEDRENDERER_H
qgsfields.h
QgsRuleBasedRenderer
Rule based renderer.
Definition: qgsrulebasedrenderer.h:40
QgsRuleBasedRenderer::RenderLevel::jobs
QList< QgsRuleBasedRenderer::RenderJob * > jobs
List of jobs to render, owned by this object.
Definition: qgsrulebasedrenderer.h:108
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsSymbolMap
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:45
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
qgsfeature.h
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
Definition: qgsstyleentityvisitor.h:33
QgsRuleBasedRenderer::Rule::ruleKey
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
Definition: qgsrulebasedrenderer.h:266
QgsRuleBasedRenderer::RuleList
QList< QgsRuleBasedRenderer::Rule * > RuleList
Definition: qgsrulebasedrenderer.h:136
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsRuleBasedRenderer::Rule::setMaximumScale
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
Definition: qgsrulebasedrenderer.h:294
QgsRuleBasedRenderer::RenderQueue
QList< QgsRuleBasedRenderer::RenderLevel > RenderQueue
Rendering queue: a list of rendering levels.
Definition: qgsrulebasedrenderer.h:134
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:308
QgsRuleBasedRenderer::Rule::minimumScale
double minimumScale() const
Returns the minimum map scale (i.e.
Definition: qgsrulebasedrenderer.h:234
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:224
QgsRuleBasedRenderer::RenderLevel::zIndex
int zIndex
Definition: qgsrulebasedrenderer.h:105
QgsRuleBasedRenderer::Rule::dependsOnScale
bool dependsOnScale() const
Definition: qgsrulebasedrenderer.h:214
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:272
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsRuleBasedRenderer::FeatureFlags
FeatureFlags
Definition: qgsrulebasedrenderer.h:45
QgsRuleBasedRenderer::Rule::filter
QgsExpression * filter() const
A filter that will check if this rule applies.
Definition: qgsrulebasedrenderer.h:240
QgsRuleBasedRenderer::rootRule
QgsRuleBasedRenderer::Rule * rootRule()
Definition: qgsrulebasedrenderer.h:547
QgsRuleBasedRenderer::FeatureToRender
Feature for rendering by a QgsRuleBasedRenderer.
Definition: qgsrulebasedrenderer.h:55
QgsRuleBasedRenderer::Rule::description
QString description() const
A human readable description for this rule.
Definition: qgsrulebasedrenderer.h:253
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
QgsRuleBasedRenderer::FeatureToRender::flags
int flags
Definition: qgsrulebasedrenderer.h:62
QgsGraduatedSymbolRenderer
Definition: qgsgraduatedsymbolrenderer.h:35
qgis_sip.h
QgsRuleBasedRenderer::Rule::symbol
QgsSymbol * symbol()
Definition: qgsrulebasedrenderer.h:212
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
qgsrendercontext.h
QgsRuleBasedRenderer::RenderLevel::RenderLevel
RenderLevel(int z)
Definition: qgsrulebasedrenderer.h:100
QgsRuleBasedRenderer::Rule::label
QString label() const
Definition: qgsrulebasedrenderer.h:213
QgsRuleBasedRenderer::FeatureToRender::feat
QgsFeature feat
Definition: qgsrulebasedrenderer.h:61
QgsRuleBasedRenderer::RenderJob::RenderJob
RenderJob(const QgsRuleBasedRenderer::FeatureToRender &_ftr, QgsSymbol *_s)
Constructor for a render job, with the specified feature to render and symbol.
Definition: qgsrulebasedrenderer.h:77
qgsrenderer.h
QgsRuleBasedRenderer::Rule::RenderResult
RenderResult
The result of rendering a rule.
Definition: qgsrulebasedrenderer.h:153
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsRuleBasedRenderer::RenderJob
A QgsRuleBasedRenderer rendering job, consisting of a feature to be rendered with a particular symbol...
Definition: qgsrulebasedrenderer.h:69
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsRuleBasedRenderer::Rule::isElse
bool isElse() const
Check if this rule is an ELSE rule.
Definition: qgsrulebasedrenderer.h:448
QgsRuleBasedRenderer::Rule::setMinimumScale
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
Definition: qgsrulebasedrenderer.h:285
QgsRuleBasedRenderer::Rule::setLabel
void setLabel(const QString &label)
Definition: qgsrulebasedrenderer.h:276
QgsRuleBasedRenderer::RenderLevel::~RenderLevel
~RenderLevel()
Definition: qgsrulebasedrenderer.h:104
QgsRuleBasedRenderer::Rule::parent
QgsRuleBasedRenderer::Rule * parent()
The parent rule.
Definition: qgsrulebasedrenderer.h:410
QgsRuleBasedRenderer::Rule::Inactive
@ Inactive
The rule is inactive.
Definition: qgsrulebasedrenderer.h:156
QgsCategorizedSymbolRenderer
Definition: qgscategorizedsymbolrenderer.h:164
QgsRuleBasedRenderer::RenderJob::ftr
QgsRuleBasedRenderer::FeatureToRender ftr
Feature to render.
Definition: qgsrulebasedrenderer.h:83
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsWkbTypes
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:41
QgsRuleBasedRenderer::Rule::setActive
void setActive(bool state)
Sets if this rule is active.
Definition: qgsrulebasedrenderer.h:314
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsLegendSymbolList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition: qgslegendsymbolitem.h:144
QgsRuleBasedRenderer::RenderLevel::operator=
QgsRuleBasedRenderer::RenderLevel & operator=(const QgsRuleBasedRenderer::RenderLevel &rh)
Definition: qgsrulebasedrenderer.h:110
QgsRuleBasedRenderer::Rule
This class keeps data about a rules for rule-based renderer.
Definition: qgsrulebasedrenderer.h:149
QgsRuleBasedRenderer::FeatureToRender::FeatureToRender
FeatureToRender(const QgsFeature &_f, int _flags)
Definition: qgsrulebasedrenderer.h:57
QgsRuleBasedRenderer::RenderLevel
Render level: a list of jobs to be drawn at particular level for a QgsRuleBasedRenderer.
Definition: qgsrulebasedrenderer.h:98
QgsRuleBasedRenderer::Rule::active
bool active() const
Returns if this rule is active.
Definition: qgsrulebasedrenderer.h:260
QgsRuleBasedRenderer::RenderLevel::RenderLevel
RenderLevel(const QgsRuleBasedRenderer::RenderLevel &other)
Definition: qgsrulebasedrenderer.h:122
QgsRuleBasedRenderer::Rule::filterExpression
QString filterExpression() const
A filter that will check if this rule applies.
Definition: qgsrulebasedrenderer.h:246