QGIS API Documentation 4.3.0-Master (0ff9723465c)
Loading...
Searching...
No Matches
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"
21
22#include <QMap>
23#include <QStringList>
24#include <QUuid>
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
47 private:
48 typedef std::vector<std::pair<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *>> RuleToProviderVec;
49
50 public:
56 class CORE_EXPORT Rule
57 {
58 public:
60 Rule(
61 QgsPalLayerSettings *settings SIP_TRANSFER, double maximumScale = 0, double minimumScale = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false
62 );
63 ~Rule();
64
65 Rule( const Rule &rh ) = delete;
66 Rule &operator=( const Rule &rh ) = delete;
67
75
79 QgsPalLayerSettings *settings() const { return mSettings.get(); }
80
86 bool dependsOnScale() const { return !qgsDoubleNear( mMinimumScale, 0.0 ) || !qgsDoubleNear( mMaximumScale, 0 ); }
87
95 double maximumScale() const { return mMaximumScale; }
96
104 double minimumScale() const { return mMinimumScale; }
105
110 QString filterExpression() const { return mFilterExp; }
111
117 QString description() const { return mDescription; }
118
124 bool active() const { return mIsActive; }
125
131 bool isElse() const { return mElseRule; }
132
134 QString ruleKey() const { return mRuleKey; }
135
138
146 void setMinimumScale( double scale ) { mMinimumScale = scale; }
147
155 void setMaximumScale( double scale ) { mMaximumScale = scale; }
156
162 void setFilterExpression( const QString &filterExp )
163 {
164 mFilterExp = filterExp;
165 initFilter();
166 }
167
173 void setDescription( const QString &description ) { mDescription = description; }
174
179 void setActive( bool state ) { mIsActive = state; }
180
186 void setIsElse( bool iselse ) { mElseRule = iselse; }
187
189 void setRuleKey( const QString &key ) { mRuleKey = key; }
190
191 // parent / child operations
192
198 const QgsRuleBasedLabeling::RuleList &children() const { return mChildren; }
199
206
212 QgsRuleBasedLabeling::RuleList descendants() const;
213
219 const QgsRuleBasedLabeling::Rule *parent() const SIP_SKIP { return mParent; }
220
226 QgsRuleBasedLabeling::Rule *parent() { return mParent; }
227
229 void appendChild( QgsRuleBasedLabeling::Rule *rule SIP_TRANSFER );
230
232 void insertChild( int i, QgsRuleBasedLabeling::Rule *rule SIP_TRANSFER );
233
235 void removeChildAt( int i );
236
238 const QgsRuleBasedLabeling::Rule *findRuleByKey( const QString &key ) const;
239
248 QgsRuleBasedLabeling::Rule *findRuleByKey( const QString &key ) SIP_SKIP;
249
255 QgsRuleBasedLabeling::Rule *clone( bool resetRuleKey = true ) const SIP_FACTORY;
256
257 // load / save
258
266 static QgsRuleBasedLabeling::Rule *create( const QDomElement &ruleElem, const QgsReadWriteContext &context, bool reuseId = true ) SIP_FACTORY;
267
269 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
270
271 // evaluation
272
277 void createSubProviders( QgsVectorLayer *layer, RuleToProviderVec &subProviders, QgsRuleBasedLabelProvider *provider ) SIP_SKIP;
278
283 void subProviderIds( QStringList &list ) const SIP_SKIP;
284
289 void prepare( QgsRenderContext &context, QSet<QString> &attributeNames, RuleToProviderVec &subProviders ) SIP_SKIP;
290
300 std::tuple< RegisterResult, QList< QgsLabelFeature * > > registerFeature( const QgsFeature &feature, QgsRenderContext &context, RuleToProviderVec &subProviders, const QgsLabelFeatureDetails &details )
301 SIP_SKIP;
302
307 bool requiresAdvancedEffects() const;
308
320 bool hasNonDefaultCompositionMode() const;
321
331 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
332
333 private:
334#ifdef SIP_RUN
335 Rule( const QgsRuleBasedLabeling::Rule &rh );
336#endif
337
345 bool isFilterOK( const QgsFeature &f, QgsRenderContext &context ) const;
346
354 bool isScaleOK( double scale ) const;
355
359 void initFilter();
360
364 void updateElseRules();
365
366 private:
367 Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
368 std::unique_ptr<QgsPalLayerSettings> mSettings;
369 double mMaximumScale = 0;
370 double mMinimumScale = 0;
371 QString mFilterExp;
372 QString mDescription;
373 bool mElseRule = false;
374 RuleList mChildren;
375 RuleList mElseRules;
376 bool mIsActive = true; // whether it is enabled or not
377
378 QString mRuleKey = QUuid::createUuid().toString(); // string used for unique identification of rule within labeling
379
380 std::unique_ptr<QgsExpression> mFilter;
381 };
382
383
386 ~QgsRuleBasedLabeling() override;
387
388 QgsRuleBasedLabeling::Rule *rootRule();
389 const Rule *rootRule() const SIP_SKIP;
390
392 static QgsRuleBasedLabeling *create( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
393
394 // implementation of parent interface
395
396 QString type() const override;
397 QgsRuleBasedLabeling *clone() const override SIP_FACTORY;
398 QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const override;
400 QgsVectorLayerLabelProvider *provider( QgsVectorLayer *layer ) const override SIP_SKIP;
401 QStringList subProviders() const override;
402 QgsPalLayerSettings settings( const QString &providerId = QString() ) const override;
403 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
404
412 void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER, const QString &providerId = QString() ) override;
413 bool requiresAdvancedEffects() const override;
414 bool hasNonDefaultCompositionMode() const override;
415 Q_DECL_DEPRECATED void toSld( QDomNode &parent, const QVariantMap &properties ) const override SIP_DEPRECATED;
416 bool toSld( QDomNode &parent, QgsSldExportContext &context ) const override;
417 void multiplyOpacity( double opacityFactor ) override;
418
419
420 protected:
421 std::unique_ptr<Rule> mRootRule;
422};
423
424#ifndef SIP_RUN
425
434{
435 public:
436 QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop = true );
437
438 // reimplemented
439
440 bool prepare( QgsRenderContext &context, QSet<QString> &attributeNames ) override;
441
442 QList< QgsLabelFeature * > registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsLabelFeatureDetails &details = QgsLabelFeatureDetails() ) override;
443
445 virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );
446
448 QList<QgsAbstractLabelProvider *> subProviders() override;
449
450 protected:
452 std::unique_ptr<QgsRuleBasedLabeling> mRules;
454 std::vector<std::pair<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *>> mSubProviders;
455};
456
457#endif
458
459#endif // QGSRULEBASEDLABELING_H
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
QString providerId() const
Returns provider ID - useful in case there is more than one label provider within a layer (e....
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:60
A geometry is the spatial representation of a feature.
Encapsulates details for an individual feature to label.
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.
bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames) override
Prepare for registration of features.
QList< QgsLabelFeature * > registerFeature(const QgsFeature &feature, QgsRenderContext &context, const QgsLabelFeatureDetails &details=QgsLabelFeatureDetails()) override
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
QgsRuleBasedLabelProvider(const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop=true)
std::vector< std::pair< QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider * > > mSubProviders
label providers are owned by labeling engine
virtual QgsVectorLayerLabelProvider * createProvider(QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings)
create a label provider
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.
@ Filtered
The rule does not apply.
@ Registered
Something was registered.
void setActive(bool state)
Sets if this rule is active.
bool isElse() const
Check if this rule is an ELSE rule.
Rule(QgsPalLayerSettings *settings, double maximumScale=0, double minimumScale=0, const QString &filterExp=QString(), const QString &description=QString(), bool elseRule=false)
takes ownership of settings, settings may be nullptr
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
std::unique_ptr< Rule > mRootRule
QStringList subProviders() const override
Gets list of sub-providers within the layer's labeling.
QgsRuleBasedLabeling(QgsRuleBasedLabeling::Rule *root)
Constructs the labeling from given tree of rules (takes ownership).
QgsVectorLayerLabelProvider * provider(QgsVectorLayer *layer) const override
static QgsRuleBasedLabeling * create(const QDomElement &element, const QgsReadWriteContext &context)
Create the instance from a DOM element with saved configuration.
QgsPalLayerSettings settings(const QString &providerId=QString()) const override
Gets associated label settings.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the labeling...
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Returns labeling configuration as XML element.
bool hasNonDefaultCompositionMode() const override
Returns true the labeling requires a non-default composition mode.
bool requiresAdvancedEffects() const override
Returns true if drawing labels requires advanced effects like composition modes, which could prevent ...
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.
Implements a label provider for vector layers.
QgsVectorLayerLabelProvider(QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings, const QString &layerName=QString())
Convenience constructor to initialize the provider from given vector layer.
const QgsPalLayerSettings & settings() const
Returns the layer's settings.
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:7693
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_FACTORY
Definition qgis_sip.h:83