QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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_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
191 QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
192
200 bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
201
209 bool isScaleOK( double scale ) const;
210
211 QgsSymbol *symbol() { return mSymbol.get(); }
212 QString label() const { return mLabel; }
213 bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
214
222 double maximumScale() const { return mMaximumScale; }
223
231 double minimumScale() const { return mMinimumScale; }
232
237 QgsExpression *filter() const { return mFilter.get(); }
238
243 QString filterExpression() const { return mFilterExp; }
244
250 QString description() const { return mDescription; }
251
257 bool active() const { return mIsActive; }
258
262 QString ruleKey() const { return mRuleKey; }
263
267 void setRuleKey( const QString &key ) { mRuleKey = key; }
268
270 void setSymbol( QgsSymbol *sym SIP_TRANSFER );
271 void setLabel( const QString &label ) { mLabel = label; }
272
280 void setMinimumScale( double scale ) { mMinimumScale = scale; }
281
289 void setMaximumScale( double scale ) { mMaximumScale = scale; }
290
296 void setFilterExpression( const QString &filterExp );
297
303 void setDescription( const QString &description ) { mDescription = description; }
304
309 void setActive( bool state ) { mIsActive = state; }
310
313
315 void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
316
320 static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
321
322 QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
323
325 bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
326
334 bool hasActiveChildren() const;
335
337 QSet<int> collectZLevels();
338
343 void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
344
354 QgsRuleBasedRenderer::Rule::RenderResult renderFeature( QgsRuleBasedRenderer::FeatureToRender &featToRender, QgsRenderContext &context, QgsRuleBasedRenderer::RenderQueue &renderQueue );
355
357 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
358
360 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
361
365 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
366
375 QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
376
382 void stopRender( QgsRenderContext &context );
383
393 static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId = true ) SIP_FACTORY;
394
400 const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
401
407 QgsRuleBasedRenderer::RuleList descendants() const;
408
414 QgsRuleBasedRenderer::Rule *parent() { return mParent; }
415
417 void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
418
420 void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
421
423 void removeChild( QgsRuleBasedRenderer::Rule *rule );
424
426 void removeChildAt( int i );
427
430
432 QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
433
437 QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
438
444 void setIsElse( bool iselse );
445
451 bool isElse() const { return mElseRule; }
452
462 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
463
464 protected:
465 void initFilter();
466
467 private:
468#ifdef SIP_RUN
469 Rule( const QgsRuleBasedRenderer::Rule &rh );
470#endif
471
472 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
473 std::unique_ptr< QgsSymbol > mSymbol;
474 double mMaximumScale = 0;
475 double mMinimumScale = 0;
476 QString mFilterExp, mLabel, mDescription;
477 bool mElseRule = false;
478 RuleList mChildren;
479 RuleList mElseRules;
480 bool mIsActive = true; // whether it is enabled or not
481
482 QString mRuleKey; // string used for unique identification of rule within renderer
483
484 // temporary
485 std::unique_ptr< QgsExpression > mFilter;
486 // temporary while rendering
487 QSet<int> mSymbolNormZLevels;
488 RuleList mActiveChildren;
489
494 void updateElseRules();
495 };
496
498
500 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
501
506
507 ~QgsRuleBasedRenderer() override;
508
510 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
511
512 bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
513
514 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
515 bool canSkipRender() override;
516 void stopRender( QgsRenderContext &context ) override;
517
518 QString filter( const QgsFields &fields = QgsFields() ) override;
519
520 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
521
522 bool filterNeedsGeometry() const override;
523
524 QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
525
526 void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
527
531 static QgsFeatureRenderer *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
532
533 QgsSymbolList symbols( QgsRenderContext &context ) const override;
534
535 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
536 bool legendSymbolItemsCheckable() const override;
537 bool legendSymbolItemChecked( const QString &key ) override;
538 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
539 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
540
541 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
542 QgsLegendSymbolList legendSymbolItems() const override;
543 QString dump() const override;
544 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
545 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
546 QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
547 QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
548 QgsFeatureRenderer::Capabilities capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
549 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
550
552
553 QgsRuleBasedRenderer::Rule *rootRule() { return mRootRule; }
554
556
558 static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
560 static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
562 static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
563
571 static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
572
574 static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
575
576 protected:
578 Rule *mRootRule = nullptr;
579
580 // temporary
581 RenderQueue mRenderQueue;
582 QList<FeatureToRender> mCurrentFeatures;
583
584 QString mFilter;
585
586 private:
587#ifdef SIP_RUN
589 QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
590#endif
591};
592
593#endif // QGSRULEBASEDRENDERER_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
Custom exception class for Coordinate Reference System related exceptions.
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 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.
@ ScaleDependent
Depends on scale if feature will be rendered (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: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:94
Represents a vector layer which manages a vector based data sets.
#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:203
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)