QGIS API Documentation 4.3.0-Master (16eddb2670e)
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 {
76 : ftr( _ftr )
77 , symbol( _s )
78 {}
79
82
84 QgsSymbol *symbol = nullptr;
85
86 private:
87#ifdef SIP_RUN
88 RenderJob &operator=( const RenderJob & );
89#endif
90 };
91
97 {
98 explicit RenderLevel( int z )
99 : zIndex( z )
100 {}
101
102 ~RenderLevel() { qDeleteAll( jobs ); }
104
106 QList<QgsRuleBasedRenderer::RenderJob *> jobs;
107
109 {
110 if ( &rh == this )
111 return *this;
112
113 zIndex = rh.zIndex;
114 qDeleteAll( jobs );
115 jobs.clear();
116 for ( auto it = rh.jobs.constBegin(); it != rh.jobs.constEnd(); ++it )
117 {
118 jobs << new RenderJob( *( *it ) );
119 }
120 return *this;
121 }
122
124 : zIndex( other.zIndex )
125 , jobs()
126 {
127 for ( auto it = other.jobs.constBegin(); it != other.jobs.constEnd(); ++it )
128 {
129 jobs << new RenderJob( *( *it ) );
130 }
131 }
132 };
133
135 typedef QList<QgsRuleBasedRenderer::RenderLevel> RenderQueue;
136
137 class Rule;
138 typedef QList<QgsRuleBasedRenderer::Rule *> RuleList;
139
150 class CORE_EXPORT Rule
151 {
152 public:
160
162 Rule(
163 QgsSymbol *symbol SIP_TRANSFER,
164 int maximumScale = 0,
165 int minimumScale = 0,
166 const QString &filterExp = QString(),
167 const QString &label = QString(),
168 const QString &description = QString(),
169 bool elseRule = false
170 );
171 ~Rule();
172
173 Rule( const Rule &rh ) = delete;
174 Rule &operator=( const Rule &rh ) = delete;
175
181 QString dump( int indent = 0 ) const;
182
187 QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
188
192 bool needsGeometry() const;
193
197 QgsSymbolList symbols( const QgsRenderContext &context = QgsRenderContext() ) const;
198
199 QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
200
208 bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
209
217 bool isScaleOK( double scale ) const;
218
219 QgsSymbol *symbol() { return mSymbol.get(); }
220
226 const QgsSymbol *symbol() const SIP_SKIP { return mSymbol.get(); }
227
228 QString label() const { return mLabel; }
229 bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
230
238 double maximumScale() const { return mMaximumScale; }
239
247 double minimumScale() const { return mMinimumScale; }
248
253 QgsExpression *filter() const { return mFilter.get(); }
254
259 QString filterExpression() const { return mFilterExp; }
260
266 QString description() const { return mDescription; }
267
273 bool active() const { return mIsActive; }
274
278 QString ruleKey() const { return mRuleKey; }
279
283 void setRuleKey( const QString &key ) { mRuleKey = key; }
284
286 void setSymbol( QgsSymbol *sym SIP_TRANSFER );
287 void setLabel( const QString &label ) { mLabel = label; }
288
296 void setMinimumScale( double scale ) { mMinimumScale = scale; }
297
305 void setMaximumScale( double scale ) { mMaximumScale = scale; }
306
312 void setFilterExpression( const QString &filterExp );
313
319 void setDescription( const QString &description ) { mDescription = description; }
320
325 void setActive( bool state ) { mIsActive = state; }
326
328 std::unique_ptr<QgsRuleBasedRenderer::Rule> clone() const;
329
335 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const SIP_DEPRECATED;
336
342 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const;
343
347 static std::unique_ptr<QgsRuleBasedRenderer::Rule> createFromSld( QDomElement &element, Qgis::GeometryType geomType );
348
349 QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
350
352 bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
353
361 bool hasActiveChildren() const;
362
364 QSet<int> collectZLevels();
365
370 void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
371
382
384 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
385
387 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
388
392 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
393
402 QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
403
409 void stopRender( QgsRenderContext &context );
410
421 static std::unique_ptr<QgsRuleBasedRenderer::Rule> create( QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId = true, const QgsReadWriteContext &context = QgsReadWriteContext() );
422
428 const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
429
435 QgsRuleBasedRenderer::RuleList descendants() const;
436
442 QgsRuleBasedRenderer::Rule *parent() { return mParent; }
443
445 void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
446
448 void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
449
451 void removeChild( QgsRuleBasedRenderer::Rule *rule );
452
454 void removeChildAt( int i );
455
458
460 QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
461
465 QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
466
472 void setIsElse( bool iselse );
473
479 bool isElse() const { return mElseRule; }
480
490 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
491
492 protected:
493 void initFilter();
494
495 private:
496#ifdef SIP_RUN
497 Rule( const QgsRuleBasedRenderer::Rule &rh );
498#endif
499
500 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
501 std::unique_ptr< QgsSymbol > mSymbol;
502 double mMaximumScale = 0;
503 double mMinimumScale = 0;
504 QString mFilterExp, mLabel, mDescription;
505 bool mElseRule = false;
506 RuleList mChildren;
507 RuleList mElseRules;
508 bool mIsActive = true; // whether it is enabled or not
509
510 QString mRuleKey; // string used for unique identification of rule within renderer
511
512 // temporary
513 std::unique_ptr< QgsExpression > mFilter;
514 // temporary while rendering
515 QSet<int> mSymbolNormZLevels;
516 RuleList mActiveChildren;
517
522 void updateElseRules();
523 };
524
526
528 static std::unique_ptr<QgsFeatureRenderer> create( QDomElement &element, const QgsReadWriteContext &context );
529
534
535 ~QgsRuleBasedRenderer() override;
536
538 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
539
540 Qgis::FeatureRendererFlags flags() const override;
541 bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
542
543 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
544 bool canSkipRender() override;
545 void stopRender( QgsRenderContext &context ) override;
546
547 QString filter( const QgsFields &fields = QgsFields() ) override;
548
549 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
550
551 bool filterNeedsGeometry() const override;
552
553 QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
554
555 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
556 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
557
561 static std::unique_ptr<QgsFeatureRenderer> createFromSld( QDomElement &element, Qgis::GeometryType geomType );
562
563 QgsSymbolList symbols( QgsRenderContext &context ) const override;
564
565 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
566 bool legendSymbolItemsCheckable() const override;
567 bool legendSymbolItemChecked( const QString &key ) override;
568 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
569 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
570
571 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
572 void setLegendSymbolItemLabel( const QString &key, const QString &label ) override;
573 QgsLegendSymbolList legendSymbolItems() const override;
574 QString dump() const override;
575 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
576 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
577 QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
578 QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
580 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
581
583
585
586
594 const QgsRuleBasedRenderer::Rule *rootRule() const SIP_SKIP { return mRootRule.get(); }
595
597
599 static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
601 static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
603 static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
604
612 static std::unique_ptr<QgsRuleBasedRenderer> convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr );
613
615 static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
616
617 protected:
619 std::unique_ptr<Rule> mRootRule;
620
621 // temporary
623 QList<FeatureToRender> mCurrentFeatures;
624
625 QString mFilter;
626
627 private:
628#ifdef SIP_RUN
630 QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
631#endif
632};
633
634#endif // QGSRULEBASEDRENDERER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:920
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:399
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 symbolsForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns list of symbols used for rendering the feature.
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.
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
QFlags< Capability > Capabilities
virtual QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns legend keys matching a specified feature.
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 QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
Stores renderer properties to an XML element.
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 Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const
Used from subclasses to create SLD Rule elements following SLD v1.1 specs.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
virtual bool willRenderFeature(const QgsFeature &feature, QgsRenderContext &context) const
Returns whether the renderer will render a feature or not.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
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:45
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)
const QgsSymbol * symbol() const
Returns the symbol associated with the rule.
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.
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
QList< QgsRuleBasedRenderer::RenderLevel > RenderQueue
Rendering queue: a list of rendering levels.
const QgsRuleBasedRenderer::Rule * rootRule() const
Returns the root rule of the rule-based renderer.
static std::unique_ptr< QgsFeatureRenderer > create(QDomElement &element, const QgsReadWriteContext &context)
Creates a new rule-based renderer instance from XML.
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...
std::unique_ptr< Rule > mRootRule
the root node with hierarchical list of rules
static std::unique_ptr< QgsFeatureRenderer > createFromSld(QDomElement &element, Qgis::GeometryType geomType)
Creates a new rule based renderer from an SLD XML element.
QList< QgsRuleBasedRenderer::Rule * > RuleList
QgsRuleBasedRenderer::Rule * rootRule()
QList< FeatureToRender > mCurrentFeatures
QgsRuleBasedRenderer(QgsRuleBasedRenderer::Rule *root)
Constructs the renderer from given tree of rules (takes ownership).
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:227
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_TRANSFERBACK
Definition qgis_sip.h:47
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_THROW(name,...)
Definition qgis_sip.h:215
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)