QGIS API Documentation 3.43.0-Master (0cdc48caa8d)
qgsrulebasedlabeling.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsrulebasedlabeling.h
3 ---------------------
4 begin : September 2015
5 copyright : (C) 2015 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#ifndef QGSRULEBASEDLABELING_H
16#define QGSRULEBASEDLABELING_H
17
18#include "qgis_core.h"
19#include <QStringList>
20#include <QMap>
21#include <QUuid>
22
25
26class QDomDocument;
27class QDomElement;
28
29class QgsExpression;
30class QgsFeature;
33class QgsGeometry;
35
42{
43 public:
44 class Rule;
45 typedef QList<QgsRuleBasedLabeling::Rule *> RuleList;
46 private:
47 typedef std::vector<std::pair<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *>> RuleToProviderVec;
48
49 public:
50
56 class CORE_EXPORT Rule
57 {
58 public:
60 Rule( QgsPalLayerSettings *settings SIP_TRANSFER, double maximumScale = 0, double minimumScale = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
61 ~Rule();
62
63 Rule( const Rule &rh ) = delete;
64 Rule &operator=( const Rule &rh ) = delete;
65
68 {
69 Filtered = 0,
71 Registered
72 };
73
77 QgsPalLayerSettings *settings() const { return mSettings.get(); }
78
84 bool dependsOnScale() const { return !qgsDoubleNear( mMinimumScale, 0.0 ) || !qgsDoubleNear( mMaximumScale, 0 ); }
85
93 double maximumScale() const { return mMaximumScale; }
94
102 double minimumScale() const { return mMinimumScale; }
103
108 QString filterExpression() const { return mFilterExp; }
109
115 QString description() const { return mDescription; }
116
122 bool active() const { return mIsActive; }
123
129 bool isElse() const { return mElseRule; }
130
132 QString ruleKey() const { return mRuleKey; }
133
136
144 void setMinimumScale( double scale ) { mMinimumScale = scale; }
145
153 void setMaximumScale( double scale ) { mMaximumScale = scale; }
154
160 void setFilterExpression( const QString &filterExp ) { mFilterExp = filterExp; initFilter(); }
161
167 void setDescription( const QString &description ) { mDescription = description; }
168
173 void setActive( bool state ) { mIsActive = state; }
174
180 void setIsElse( bool iselse ) { mElseRule = iselse; }
181
183 void setRuleKey( const QString &key ) { mRuleKey = key; }
184
185 // parent / child operations
186
192 const QgsRuleBasedLabeling::RuleList &children() const { return mChildren; }
193
200
206 QgsRuleBasedLabeling::RuleList descendants() const;
207
213 const QgsRuleBasedLabeling::Rule *parent() const SIP_SKIP { return mParent; }
214
220 QgsRuleBasedLabeling::Rule *parent() { return mParent; }
221
223 void appendChild( QgsRuleBasedLabeling::Rule *rule SIP_TRANSFER );
224
226 void insertChild( int i, QgsRuleBasedLabeling::Rule *rule SIP_TRANSFER );
227
229 void removeChildAt( int i );
230
232 const QgsRuleBasedLabeling::Rule *findRuleByKey( const QString &key ) const;
233
242 QgsRuleBasedLabeling::Rule *findRuleByKey( const QString &key ) SIP_SKIP;
243
249 QgsRuleBasedLabeling::Rule *clone( bool resetRuleKey = true ) const SIP_FACTORY;
250
251 // load / save
252
260 static QgsRuleBasedLabeling::Rule *create( const QDomElement &ruleElem, const QgsReadWriteContext &context, bool reuseId = true ) SIP_FACTORY;
261
263 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
264
265 // evaluation
266
271 void createSubProviders( QgsVectorLayer *layer, RuleToProviderVec &subProviders, QgsRuleBasedLabelProvider *provider ) SIP_SKIP;
272
277 void subProviderIds( QStringList &list ) const SIP_SKIP;
278
283 void prepare( QgsRenderContext &context, QSet<QString> &attributeNames, RuleToProviderVec &subProviders ) SIP_SKIP;
284
294 std::tuple< RegisterResult, QList< QgsLabelFeature * > > registerFeature( const QgsFeature &feature, QgsRenderContext &context, RuleToProviderVec &subProviders, const QgsGeometry &obstacleGeometry = QgsGeometry(), const QgsSymbol *symbol = nullptr ) SIP_SKIP;
295
300 bool requiresAdvancedEffects() const;
301
313 bool hasNonDefaultCompositionMode() const;
314
324 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
325
326 private:
327#ifdef SIP_RUN
328 Rule( const QgsRuleBasedLabeling::Rule &rh );
329#endif
330
338 bool isFilterOK( const QgsFeature &f, QgsRenderContext &context ) const;
339
347 bool isScaleOK( double scale ) const;
348
352 void initFilter();
353
357 void updateElseRules();
358
359 private:
360 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
361 std::unique_ptr<QgsPalLayerSettings> mSettings;
362 double mMaximumScale = 0;
363 double mMinimumScale = 0;
364 QString mFilterExp;
365 QString mDescription;
366 bool mElseRule = false;
367 RuleList mChildren;
368 RuleList mElseRules;
369 bool mIsActive = true; // whether it is enabled or not
370
371 QString mRuleKey = QUuid::createUuid().toString(); // string used for unique identification of rule within labeling
372
373 std::unique_ptr<QgsExpression> mFilter;
374
375 };
376
377
380 ~QgsRuleBasedLabeling() override;
381
382 QgsRuleBasedLabeling::Rule *rootRule();
383 const Rule *rootRule() const SIP_SKIP;
384
386 static QgsRuleBasedLabeling *create( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
387
388 // implementation of parent interface
389
390 QString type() const override;
391 QgsRuleBasedLabeling *clone() const override SIP_FACTORY;
392 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const override;
394 QgsVectorLayerLabelProvider *provider( QgsVectorLayer *layer ) const override SIP_SKIP;
395 QStringList subProviders() const override;
396 QgsPalLayerSettings settings( const QString &providerId = QString() ) const override;
397 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
398
406 void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER, const QString &providerId = QString() ) override;
407 bool requiresAdvancedEffects() const override;
408 bool hasNonDefaultCompositionMode() const override;
409 Q_DECL_DEPRECATED void toSld( QDomNode &parent, const QVariantMap &properties ) const override SIP_DEPRECATED;
410 bool toSld( QDomNode &parent, QgsSldExportContext &context ) const override;
411 void multiplyOpacity( double opacityFactor ) override;
412
413
414 protected:
415 std::unique_ptr<Rule> mRootRule;
416};
417
418#ifndef SIP_RUN
419
428{
429 public:
430 QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop = true );
431
432 // reimplemented
433
434 bool prepare( QgsRenderContext &context, QSet<QString> &attributeNames ) override;
435
436 QList< QgsLabelFeature * > registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry(), const QgsSymbol *symbol = nullptr ) override;
437
439 virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );
440
442 QList<QgsAbstractLabelProvider *> subProviders() override;
443
444 protected:
446 std::unique_ptr<QgsRuleBasedLabeling> mRules;
448 std::vector<std::pair<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *>> mSubProviders;
449};
450
451#endif
452
453#endif // QGSRULEBASEDLABELING_H
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
virtual QStringList subProviders() const
Gets list of sub-providers within the layer's labeling.
virtual void setSettings(QgsPalLayerSettings *settings, const QString &providerId=QString())=0
Set pal settings for a specific provider (takes ownership).
virtual QgsAbstractVectorLayerLabeling * clone() const =0
Returns a new copy of the object.
Handles parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
A geometry is the spatial representation of a feature.
Describes a feature that should be used within the labeling engine.
Contains settings for how a map layer will be labeled.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
Label provider for rule based labeling.
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
std::vector< std::pair< QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider * > > mSubProviders
label providers are owned by labeling engine
A child rule for QgsRuleBasedLabeling.
QgsRuleBasedLabeling::RuleList & children()
Returns all children rules of this rule.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based labeling)
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
Rule(const Rule &rh)=delete
double maximumScale() const
Returns the maximum map scale (i.e.
void setDescription(const QString &description)
Set a human readable description for this rule.
bool dependsOnScale() const
Determines if scale based labeling is active.
const QgsRuleBasedLabeling::RuleList & children() const
Returns all children rules of this rule.
QString filterExpression() const
A filter that will check if this rule applies.
QgsRuleBasedLabeling::Rule * parent()
The parent rule.
const QgsRuleBasedLabeling::Rule * parent() const
The parent rule.
bool active() const
Returns if this rule is active.
QgsPalLayerSettings * settings() const
Returns the labeling settings.
RegisterResult
The result of registering a rule.
@ Inactive
The rule is inactive.
void setActive(bool state)
Sets if this rule is active.
bool isElse() const
Check if this rule is an ELSE rule.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
Rule & operator=(const Rule &rh)=delete
void setFilterExpression(const QString &filterExp)
Set the expression used to check if a given feature shall be rendered with this rule.
double minimumScale() const
Returns the minimum map scale (i.e.
void setIsElse(bool iselse)
Sets if this rule is an ELSE rule.
QString ruleKey() const
Unique rule identifier (for identification of rule within labeling, used as provider ID)
QString description() const
A human readable description for this rule.
Rule based labeling for a vector layer.
QList< QgsRuleBasedLabeling::Rule * > RuleList
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:231
Implements a label provider for vector layers.
Represents a vector layer which manages a vector based dataset.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6302
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:76