QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
406 static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId = true ) SIP_FACTORY;
407
413 const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
414
420 QgsRuleBasedRenderer::RuleList descendants() const;
421
427 QgsRuleBasedRenderer::Rule *parent() { return mParent; }
428
430 void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
431
433 void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
434
436 void removeChild( QgsRuleBasedRenderer::Rule *rule );
437
439 void removeChildAt( int i );
440
443
445 QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
446
450 QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
451
457 void setIsElse( bool iselse );
458
464 bool isElse() const { return mElseRule; }
465
475 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
476
477 protected:
478 void initFilter();
479
480 private:
481#ifdef SIP_RUN
482 Rule( const QgsRuleBasedRenderer::Rule &rh );
483#endif
484
485 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
486 std::unique_ptr< QgsSymbol > mSymbol;
487 double mMaximumScale = 0;
488 double mMinimumScale = 0;
489 QString mFilterExp, mLabel, mDescription;
490 bool mElseRule = false;
491 RuleList mChildren;
492 RuleList mElseRules;
493 bool mIsActive = true; // whether it is enabled or not
494
495 QString mRuleKey; // string used for unique identification of rule within renderer
496
497 // temporary
498 std::unique_ptr< QgsExpression > mFilter;
499 // temporary while rendering
500 QSet<int> mSymbolNormZLevels;
501 RuleList mActiveChildren;
502
507 void updateElseRules();
508 };
509
511
513 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
514
519
520 ~QgsRuleBasedRenderer() override;
521
523 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
524
525 Qgis::FeatureRendererFlags flags() const override;
526 bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
527
528 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
529 bool canSkipRender() override;
530 void stopRender( QgsRenderContext &context ) override;
531
532 QString filter( const QgsFields &fields = QgsFields() ) override;
533
534 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
535
536 bool filterNeedsGeometry() const override;
537
538 QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
539
540 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
541 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
542
546 static QgsFeatureRenderer *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
547
548 QgsSymbolList symbols( QgsRenderContext &context ) const override;
549
550 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
551 bool legendSymbolItemsCheckable() const override;
552 bool legendSymbolItemChecked( const QString &key ) override;
553 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
554 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
555
556 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
557 QgsLegendSymbolList legendSymbolItems() const override;
558 QString dump() const override;
559 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
560 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
561 QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
562 QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
564 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
565
567
569
571
573 static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
575 static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
577 static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
578
586 static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
587
589 static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
590
591 protected:
593 Rule *mRootRule = nullptr;
594
595 // temporary
598
599 QString mFilter;
600
601 private:
602#ifdef SIP_RUN
604 QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
605#endif
606};
607
608#endif // QGSRULEBASEDRENDERER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:838
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:58
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:50
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:49
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)