QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 QString label() const { return mLabel; }
221 bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
222
230 double maximumScale() const { return mMaximumScale; }
231
239 double minimumScale() const { return mMinimumScale; }
240
245 QgsExpression *filter() const { return mFilter.get(); }
246
251 QString filterExpression() const { return mFilterExp; }
252
258 QString description() const { return mDescription; }
259
265 bool active() const { return mIsActive; }
266
270 QString ruleKey() const { return mRuleKey; }
271
275 void setRuleKey( const QString &key ) { mRuleKey = key; }
276
278 void setSymbol( QgsSymbol *sym SIP_TRANSFER );
279 void setLabel( const QString &label ) { mLabel = label; }
280
288 void setMinimumScale( double scale ) { mMinimumScale = scale; }
289
297 void setMaximumScale( double scale ) { mMaximumScale = scale; }
298
304 void setFilterExpression( const QString &filterExp );
305
311 void setDescription( const QString &description ) { mDescription = description; }
312
317 void setActive( bool state ) { mIsActive = state; }
318
321
327 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const SIP_DEPRECATED;
328
334 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const;
335
339 static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
340
341 QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
342
344 bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
345
353 bool hasActiveChildren() const;
354
356 QSet<int> collectZLevels();
357
362 void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
363
374
376 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
377
379 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
380
384 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
385
394 QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
395
401 void stopRender( QgsRenderContext &context );
402
413 static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId = true, const QgsReadWriteContext &context = QgsReadWriteContext() ) SIP_FACTORY;
414
420 const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
421
427 QgsRuleBasedRenderer::RuleList descendants() const;
428
434 QgsRuleBasedRenderer::Rule *parent() { return mParent; }
435
437 void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
438
440 void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
441
443 void removeChild( QgsRuleBasedRenderer::Rule *rule );
444
446 void removeChildAt( int i );
447
450
452 QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
453
457 QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
458
464 void setIsElse( bool iselse );
465
471 bool isElse() const { return mElseRule; }
472
482 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
483
484 protected:
485 void initFilter();
486
487 private:
488#ifdef SIP_RUN
489 Rule( const QgsRuleBasedRenderer::Rule &rh );
490#endif
491
492 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
493 std::unique_ptr< QgsSymbol > mSymbol;
494 double mMaximumScale = 0;
495 double mMinimumScale = 0;
496 QString mFilterExp, mLabel, mDescription;
497 bool mElseRule = false;
498 RuleList mChildren;
499 RuleList mElseRules;
500 bool mIsActive = true; // whether it is enabled or not
501
502 QString mRuleKey; // string used for unique identification of rule within renderer
503
504 // temporary
505 std::unique_ptr< QgsExpression > mFilter;
506 // temporary while rendering
507 QSet<int> mSymbolNormZLevels;
508 RuleList mActiveChildren;
509
514 void updateElseRules();
515 };
516
518
520 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
521
526
527 ~QgsRuleBasedRenderer() override;
528
530 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
531
532 Qgis::FeatureRendererFlags flags() const override;
533 bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
534
535 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
536 bool canSkipRender() override;
537 void stopRender( QgsRenderContext &context ) override;
538
539 QString filter( const QgsFields &fields = QgsFields() ) override;
540
541 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
542
543 bool filterNeedsGeometry() const override;
544
545 QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
546
547 Q_DECL_DEPRECATED void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override SIP_DEPRECATED;
548 bool toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const override;
549
553 static QgsFeatureRenderer *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
554
555 QgsSymbolList symbols( QgsRenderContext &context ) const override;
556
557 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
558 bool legendSymbolItemsCheckable() const override;
559 bool legendSymbolItemChecked( const QString &key ) override;
560 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
561 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
562
563 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
564 QgsLegendSymbolList legendSymbolItems() const override;
565 QString dump() const override;
566 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
567 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
568 QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
569 QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
571 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
572
574
576
578
580 static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
582 static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
584 static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
585
593 static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
594
596 static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
597
598 protected:
600 Rule *mRootRule = nullptr;
601
602 // temporary
605
606 QString mFilter;
607
608 private:
609#ifdef SIP_RUN
611 QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
612#endif
613};
614
615#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:864
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: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:133
#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:210
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)