QGIS API Documentation 3.33.0-Master (01a999a9a0)
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
192 QgsLegendSymbolList legendSymbolItems( int currentLevel = -1 ) const;
193
201 bool isFilterOK( const QgsFeature &f, QgsRenderContext *context = nullptr ) const;
202
210 bool isScaleOK( double scale ) const;
211
212 QgsSymbol *symbol() { return mSymbol.get(); }
213 QString label() const { return mLabel; }
214 bool dependsOnScale() const { return mMaximumScale != 0 || mMinimumScale != 0; }
215
224 double maximumScale() const { return mMaximumScale; }
225
234 double minimumScale() const { return mMinimumScale; }
235
240 QgsExpression *filter() const { return mFilter.get(); }
241
246 QString filterExpression() const { return mFilterExp; }
247
253 QString description() const { return mDescription; }
254
260 bool active() const { return mIsActive; }
261
266 QString ruleKey() const { return mRuleKey; }
267
272 void setRuleKey( const QString &key ) { mRuleKey = key; }
273
275 void setSymbol( QgsSymbol *sym SIP_TRANSFER );
276 void setLabel( const QString &label ) { mLabel = label; }
277
285 void setMinimumScale( double scale ) { mMinimumScale = scale; }
286
294 void setMaximumScale( double scale ) { mMaximumScale = scale; }
295
301 void setFilterExpression( const QString &filterExp );
302
308 void setDescription( const QString &description ) { mDescription = description; }
309
314 void setActive( bool state ) { mIsActive = state; }
315
318
320 void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
321
325 static QgsRuleBasedRenderer::Rule *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
326
327 QDomElement save( QDomDocument &doc, QgsSymbolMap &symbolMap ) const;
328
330 bool startRender( QgsRenderContext &context, const QgsFields &fields, QString &filter );
331
339 bool hasActiveChildren() const;
340
342 QSet<int> collectZLevels();
343
348 void setNormZLevels( const QMap<int, int> &zLevelsToNormLevels ) SIP_SKIP;
349
359 QgsRuleBasedRenderer::Rule::RenderResult renderFeature( QgsRuleBasedRenderer::FeatureToRender &featToRender, QgsRenderContext &context, QgsRuleBasedRenderer::RenderQueue &renderQueue );
360
362 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
363
365 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
366
371 QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr );
372
381 QgsRuleBasedRenderer::RuleList rulesForFeature( const QgsFeature &feature, QgsRenderContext *context = nullptr, bool onlyActive = true );
382
388 void stopRender( QgsRenderContext &context );
389
399 static QgsRuleBasedRenderer::Rule *create( QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId = true ) SIP_FACTORY;
400
406 const QgsRuleBasedRenderer::RuleList &children() const { return mChildren; }
407
413 QgsRuleBasedRenderer::RuleList descendants() const;
414
420 QgsRuleBasedRenderer::Rule *parent() { return mParent; }
421
423 void appendChild( QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
424
426 void insertChild( int i, QgsRuleBasedRenderer::Rule *rule SIP_TRANSFER );
427
429 void removeChild( QgsRuleBasedRenderer::Rule *rule );
430
432 void removeChildAt( int i );
433
436
438 QgsRuleBasedRenderer::Rule *takeChildAt( int i ) SIP_TRANSFERBACK;
439
444 QgsRuleBasedRenderer::Rule *findRuleByKey( const QString &key );
445
451 void setIsElse( bool iselse );
452
458 bool isElse() const { return mElseRule; }
459
469 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
470
471 protected:
472 void initFilter();
473
474 private:
475#ifdef SIP_RUN
476 Rule( const QgsRuleBasedRenderer::Rule &rh );
477#endif
478
479 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
480 std::unique_ptr< QgsSymbol > mSymbol;
481 double mMaximumScale = 0;
482 double mMinimumScale = 0;
483 QString mFilterExp, mLabel, mDescription;
484 bool mElseRule = false;
485 RuleList mChildren;
486 RuleList mElseRules;
487 bool mIsActive = true; // whether it is enabled or not
488
489 QString mRuleKey; // string used for unique identification of rule within renderer
490
491 // temporary
492 std::unique_ptr< QgsExpression > mFilter;
493 // temporary while rendering
494 QSet<int> mSymbolNormZLevels;
495 RuleList mActiveChildren;
496
501 void updateElseRules();
502 };
503
505
507 static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
508
513
514 ~QgsRuleBasedRenderer() override;
515
517 QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
518
519 bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
520
521 void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
522 bool canSkipRender() override;
523 void stopRender( QgsRenderContext &context ) override;
524
525 QString filter( const QgsFields &fields = QgsFields() ) override;
526
527 QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
528
529 bool filterNeedsGeometry() const override;
530
531 QgsRuleBasedRenderer *clone() const override SIP_FACTORY;
532
533 void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const override;
534
538 static QgsFeatureRenderer *createFromSld( QDomElement &element, Qgis::GeometryType geomType ) SIP_FACTORY;
539
540 QgsSymbolList symbols( QgsRenderContext &context ) const override;
541
542 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
543 bool legendSymbolItemsCheckable() const override;
544 bool legendSymbolItemChecked( const QString &key ) override;
545 void checkLegendSymbolItem( const QString &key, bool state = true ) override;
546 QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok ) const override;
547
548 void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
549 QgsLegendSymbolList legendSymbolItems() const override;
550 QString dump() const override;
551 bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
552 QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
553 QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
554 QSet<QString> legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
555 QgsFeatureRenderer::Capabilities capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
556 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
557
559
560 QgsRuleBasedRenderer::Rule *rootRule() { return mRootRule; }
561
563
565 static void refineRuleCategories( QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r );
567 static void refineRuleRanges( QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r );
569 static void refineRuleScales( QgsRuleBasedRenderer::Rule *initialRule, QList<int> scales );
570
579 static QgsRuleBasedRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
580
582 static void convertToDataDefinedSymbology( QgsSymbol *symbol, const QString &sizeScaleField, const QString &rotationField = QString() );
583
584 protected:
586 Rule *mRootRule = nullptr;
587
588 // temporary
589 RenderQueue mRenderQueue;
590 QList<FeatureToRender> mCurrentFeatures;
591
592 QString mFilter;
593
594 private:
595#ifdef SIP_RUN
597 QgsRuleBasedRenderer &operator=( const QgsRuleBasedRenderer & );
598#endif
599};
600
601#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:198
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)