QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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.h"
20#include "qgis_core.h"
21#include "qgis_sip.h"
22#include "qgsfeature.h"
23#include "qgsfields.h"
24#include "qgsrendercontext.h"
25#include "qgsrenderer.h"
26
27class QgsExpression;
28
31
39class CORE_EXPORT QgsRuleBasedRenderer : public QgsFeatureRenderer
40{
41 public:
42 // TODO: use QVarLengthArray instead of QList
43
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 {
70
77 : ftr( _ftr )
78 , symbol( _s )
79 {}
80
83
85 QgsSymbol *symbol = nullptr;
86
87 private:
88#ifdef SIP_RUN
89 RenderJob &operator=( const RenderJob & );
90#endif
91 };
92
98 {
99 explicit RenderLevel( int z )
100 : zIndex( z )
101 {}
102
103 ~RenderLevel() { qDeleteAll( jobs ); }
105
107 QList<QgsRuleBasedRenderer::RenderJob *> jobs;
108
110 {
111 if ( &rh == this )
112 return *this;
113
114 zIndex = rh.zIndex;
115 qDeleteAll( jobs );
116 jobs.clear();
117 for ( auto it = rh.jobs.constBegin(); it != rh.jobs.constEnd(); ++it )
118 {
119 jobs << new RenderJob( *( *it ) );
120 }
121 return *this;
122 }
123
125 : zIndex( other.zIndex ), jobs()
126 {
127 for ( auto it = other.jobs.constBegin(); it != other.jobs.constEnd(); ++it )
128 {
129 jobs << new RenderJob( * ( *it ) );
130 }
131 }
132
133 };
134
136 typedef QList<QgsRuleBasedRenderer::RenderLevel> RenderQueue;
137
138 class Rule;
139 typedef QList<QgsRuleBasedRenderer::Rule *> RuleList;
140
151 class CORE_EXPORT Rule
152 {
153 public:
161
163 Rule( QgsSymbol *symbol SIP_TRANSFER, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(),
164 const QString &label = QString(), const QString &description = QString(), bool elseRule = false );
165 ~Rule();
166
167 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
191 QgsSymbolList symbols( const QgsRenderContext &context = QgsRenderContext() ) const;
192
193 QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
194
202 bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
203
211 bool isScaleOK( double scale ) const;
212
213 QgsSymbol *symbol() { return mSymbol.get(); }
214 QString label() const { return mLabel; }
215 bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
216
224 double maximumScale() const { return mMaximumScale; }
225
233 double minimumScale() const { return mMinimumScale; }
234
239 QgsExpression *filter() const { return mFilter.get(); }
240
245 QString filterExpression() const { return mFilterExp; }
246
252 QString description() const { return mDescription; }
253
259 bool active() const { return mIsActive; }
260
264 QString ruleKey() const { return mRuleKey; }
265
269 void setRuleKey( const QString &key ) { mRuleKey = key; }
270
272 void setSymbol( QgsSymbol *sym SIP_TRANSFER );
273 void setLabel( const QString &label ) { mLabel = label; }
274
282 void setMinimumScale( double scale ) { mMinimumScale = scale; }
283
291 void setMaximumScale( double scale ) { mMaximumScale = scale; }
292
298 void setFilterExpression( const QString &filterExp );
299
305 void setDescription( const QString &description ) { mDescription = description; }
306
311 void setActive( bool state ) { mIsActive = state; }
312
315
321 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const SIP_DEPRECATED;
322
328 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const;
329
333 static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
334
335 QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
336
338 bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
339
347 bool hasActiveChildren() const;
348
350 QSet<int> collectZLevels();
351
356 void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
357
368
370 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
371
373 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
374
378 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
379
388 QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
389
395 void stopRender( QgsRenderContext &context );
396
407 static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId = true, const QgsReadWriteContext &context = QgsReadWriteContext() ) SIP_FACTORY;
408
414 const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
415
421 QgsRuleBasedRenderer::RuleList descendants() const;
422
428 QgsRuleBasedRenderer::Rule *parent() { return mParent; }
429
431 void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
432
434 void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
435
437 void removeChild( QgsRuleBasedRenderer::Rule *rule );
438
440 void removeChildAt( int i );
441
444
446 QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
447
451 QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
452
458 void setIsElse( bool iselse );
459
465 bool isElse() const { return mElseRule; }
466
476 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
477
478 protected:
479 void initFilter();
480
481 private:
482#ifdef SIP_RUN
483 Rule( const QgsRuleBasedRenderer::Rule &rh );
484#endif
485
486 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
487 std::unique_ptr< QgsSymbol > mSymbol;
488 double mMaximumScale = 0;
489 double mMinimumScale = 0;
490 QString mFilterExp, mLabel, mDescription;
491 bool mElseRule = false;
492 RuleList mChildren;
493 RuleList mElseRules;
494 bool mIsActive = true; // whether it is enabled or not
495
496 QString mRuleKey; // string used for unique identification of rule within renderer
497
498 // temporary
499 std::unique_ptr< QgsExpression > mFilter;
500 // temporary while rendering
501 QSet<int> mSymbolNormZLevels;
502 RuleList mActiveChildren;
503
508 void updateElseRules();
509 };
510
512
514 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
515
520
521 ~QgsRuleBasedRenderer() override;
522
524 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
525
526 Qgis::FeatureRendererFlags flags() const override;
527 bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
528
529 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
530 bool canSkipRender() override;
531 void stopRender( QgsRenderContext &context ) override;
532
533 QString filter( const QgsFields &fields = QgsFields() ) override;
534
535 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
536
537 bool filterNeedsGeometry() const override;
538
539 QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
540
541 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
542 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
543
547 static QgsFeatureRenderer *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
548
549 QgsSymbolList symbols( QgsRenderContext &context ) const override;
550
551 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
552 bool legendSymbolItemsCheckable() const override;
553 bool legendSymbolItemChecked( const QString &key ) override;
554 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
555 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
556
557 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
558 QgsLegendSymbolList legendSymbolItems() const override;
559 QString dump() const override;
560 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
561 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
562 QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
563 QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
565 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
566
568
570
572
574 static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
576 static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
578 static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
579
587 static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
588
590 static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
591
592 protected:
594 Rule *mRootRule = nullptr;
595
596 // temporary
599
600 QString mFilter;
601
602 private:
603#ifdef SIP_RUN
605 QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
606#endif
607};
608
609#endif // QGSRULEBASEDRENDERER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:857
A feature renderer which represents features using a list of renderer categories.
Custom exception class for Coordinate Reference System related exceptions.
Handles parsing and evaluation of expressions (formerly called "search strings").
Abstract base class for all 2D vector feature renderers.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
QgsFeatureRenderer(const QString &type)
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
virtual Qgis::FeatureRendererFlags flags() const
Returns flags associated with the renderer.
QFlags< Capability > Capabilities
virtual bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false)
Render a feature using this renderer in the given context.
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.
@ MoreSymbolsPerFeature
May use more than one symbol to render a feature: symbolsForFeature() will return them.
@ ScaleDependent
Depends on scale if feature will be rendered (rule based ).
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
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:60
Container of fields for a vector layer.
Definition qgsfields.h:46
A vector feature renderer which uses numeric attributes to classify features into different ranges.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
Represents an individual rule for a 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.
bool needsGeometry() const
Returns true if this rule or one of its children needs the geometry to be applied.
const QgsRuleBasedRenderer::RuleList & children() const
Returns all children rules of this rule.
RenderResult
The result of rendering a rule.
@ Inactive
The rule is inactive.
@ Filtered
The rule does not apply.
@ Rendered
Something was rendered.
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(QgsSymbol *symbol, int maximumScale=0, int minimumScale=0, const QString &filterExp=QString(), const QString &label=QString(), const QString &description=QString(), bool elseRule=false)
Constructor takes ownership of the symbol.
Rule(const Rule &rh)=delete
Rule & operator=(const Rule &rh)=delete
bool isFilterOK(const QgsFeature &f, QgsRenderContext *context=nullptr) const
Check if a given feature shall be rendered by this rule.
bool isScaleOK(double scale) const
Check if this rule applies for a given scale.
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.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
Stores renderer properties to an XML element.
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
QList< QgsRuleBasedRenderer::RenderLevel > RenderQueue
Rendering queue: a list of rendering levels.
QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns legend keys matching a specified feature.
QgsSymbolList symbolsForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns list of symbols used for rendering the feature.
QString filter(const QgsFields &fields=QgsFields()) override
If a renderer does not require all the features this method may be overridden and return an expressio...
bool willRenderFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns whether the renderer will render a feature or not.
QList< QgsRuleBasedRenderer::Rule * > RuleList
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
Rule * mRootRule
the root node with hierarchical list of rules
static QgsFeatureRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates a new rule-based renderer instance from XML.
QgsRuleBasedRenderer::Rule * rootRule()
static QgsFeatureRenderer * createFromSld(QDomElement &element, Qgis::GeometryType geomType)
Creates a new rule based renderer from an SLD XML element.
QList< FeatureToRender > mCurrentFeatures
bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) override
Render a feature using this renderer in the given context.
QgsRuleBasedRenderer(QgsRuleBasedRenderer::Rule *root)
Constructs the renderer from given tree of rules (takes ownership).
Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const override
Used from subclasses to create SLD Rule elements following SLD v1.1 specs.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition qgis_sip.h:48
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_THROW(name,...)
Definition qgis_sip.h:211
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition qgsrenderer.h:52
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:51
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.
QgsSymbol * symbol
Symbol to render feature with (not owned by this object).
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)