QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
28class QgsExpression;
29
32
40class 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
99 {
100 explicit RenderLevel( int z )
101 : zIndex( z )
102 {}
103
104 ~RenderLevel() { qDeleteAll( jobs ); }
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
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
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 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 QString dump() const
Returns debug information about this renderer.
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
@ ScaleDependent
Depends on scale if feature will be rendered (rule based )
Definition: qgsrenderer.h:266
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.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
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.
QgsRuleBasedRenderer::Rule * parent()
The parent rule.
bool isElse() const
Check if this rule is an ELSE 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.
Rule & operator=(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 based renderer.
QList< QgsRuleBasedRenderer::RenderLevel > RenderQueue
Rendering queue: a list of rendering levels.
QList< QgsRuleBasedRenderer::Rule * > RuleList
QgsRuleBasedRenderer::Rule * rootRule()
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:93
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_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
#define SIP_THROW(name,...)
Definition: qgis_sip.h:198
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:45
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
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...
RenderJob(const QgsRuleBasedRenderer::FeatureToRender &_ftr, QgsSymbol *_s)
Constructor for a render job, with the specified feature to render and symbol.
QgsRuleBasedRenderer::FeatureToRender ftr
Feature to render.
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.
QgsRuleBasedRenderer::RenderLevel & operator=(const QgsRuleBasedRenderer::RenderLevel &rh)
RenderLevel(const QgsRuleBasedRenderer::RenderLevel &other)