QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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
165 Rule( const Rule &rh ) = delete;
166 Rule &operator=( const Rule &rh ) = delete;
167
173 QString dump( int indent = 0 ) const;
174
179 QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
180
184 bool needsGeometry() const;
185
187 QgsSymbolList symbols( const QgsRenderContext &context = QgsRenderContext() ) const;
188
189 QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
190
198 bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
199
207 bool isScaleOK( double scale ) const;
208
209 QgsSymbol *symbol() { return mSymbol.get(); }
210 QString label() const { return mLabel; }
211 bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
212
220 double maximumScale() const { return mMaximumScale; }
221
229 double minimumScale() const { return mMinimumScale; }
230
235 QgsExpression *filter() const { return mFilter.get(); }
236
241 QString filterExpression() const { return mFilterExp; }
242
248 QString description() const { return mDescription; }
249
255 bool active() const { return mIsActive; }
256
260 QString ruleKey() const { return mRuleKey; }
261
265 void setRuleKey( const QString &key ) { mRuleKey = key; }
266
268 void setSymbol( QgsSymbol *sym SIP_TRANSFER );
269 void setLabel( const QString &label ) { mLabel = label; }
270
278 void setMinimumScale( double scale ) { mMinimumScale = scale; }
279
287 void setMaximumScale( double scale ) { mMaximumScale = scale; }
288
294 void setFilterExpression( const QString &filterExp );
295
301 void setDescription( const QString &description ) { mDescription = description; }
302
307 void setActive( bool state ) { mIsActive = state; }
308
311
313 void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
314
318 static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
319
320 QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
321
323 bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
324
332 bool hasActiveChildren() const;
333
335 QSet<int> collectZLevels();
336
341 void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
342
352 QgsRuleBasedRenderer::Rule::RenderResult renderFeature( QgsRuleBasedRenderer::FeatureToRender &featToRender, QgsRenderContext &context, QgsRuleBasedRenderer::RenderQueue &renderQueue );
353
355 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
356
358 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
359
363 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
364
373 QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
374
380 void stopRender( QgsRenderContext &context );
381
391 static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId = true ) SIP_FACTORY;
392
398 const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
399
405 QgsRuleBasedRenderer::RuleList descendants() const;
406
412 QgsRuleBasedRenderer::Rule *parent() { return mParent; }
413
415 void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
416
418 void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
419
421 void removeChild( QgsRuleBasedRenderer::Rule *rule );
422
424 void removeChildAt( int i );
425
428
430 QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
431
435 QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
436
442 void setIsElse( bool iselse );
443
449 bool isElse() const { return mElseRule; }
450
460 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
461
462 protected:
463 void initFilter();
464
465 private:
466#ifdef SIP_RUN
467 Rule( const QgsRuleBasedRenderer::Rule &rh );
468#endif
469
470 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
471 std::unique_ptr< QgsSymbol > mSymbol;
472 double mMaximumScale = 0;
473 double mMinimumScale = 0;
474 QString mFilterExp, mLabel, mDescription;
475 bool mElseRule = false;
476 RuleList mChildren;
477 RuleList mElseRules;
478 bool mIsActive = true; // whether it is enabled or not
479
480 QString mRuleKey; // string used for unique identification of rule within renderer
481
482 // temporary
483 std::unique_ptr< QgsExpression > mFilter;
484 // temporary while rendering
485 QSet<int> mSymbolNormZLevels;
486 RuleList mActiveChildren;
487
492 void updateElseRules();
493 };
494
496
498 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
499
504
505 ~QgsRuleBasedRenderer() override;
506
508 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
509
510 Qgis::FeatureRendererFlags flags() const override;
511 bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
512
513 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
514 bool canSkipRender() override;
515 void stopRender( QgsRenderContext &context ) override;
516
517 QString filter( const QgsFields &fields = QgsFields() ) override;
518
519 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
520
521 bool filterNeedsGeometry() const override;
522
523 QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
524
525 void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
526
530 static QgsFeatureRenderer *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
531
532 QgsSymbolList symbols( QgsRenderContext &context ) const override;
533
534 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
535 bool legendSymbolItemsCheckable() const override;
536 bool legendSymbolItemChecked( const QString &key ) override;
537 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
538 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
539
540 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
541 QgsLegendSymbolList legendSymbolItems() const override;
542 QString dump() const override;
543 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
544 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
545 QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
546 QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
547 QgsFeatureRenderer::Capabilities capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
548 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
549
551
552 QgsRuleBasedRenderer::Rule *rootRule() { return mRootRule; }
553
555
557 static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
559 static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
561 static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
562
570 static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
571
573 static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
574
575 protected:
577 Rule *mRootRule = nullptr;
578
579 // temporary
580 RenderQueue mRenderQueue;
581 QList<FeatureToRender> mCurrentFeatures;
582
583 QString mFilter;
584
585 private:
586#ifdef SIP_RUN
588 QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
589#endif
590};
591
592#endif // QGSRULEBASEDRENDERER_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:772
Custom exception class for Coordinate Reference System related exceptions.
Class for 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.
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.
@ 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: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.
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
Rule & operator=(const Rule &rh)=delete
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:231
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:48
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:47
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)