QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
23 #include "qgsvectorlayerlabeling.h"
25 
26 class QDomDocument;
27 class QDomElement;
28 
29 class QgsExpression;
30 class QgsFeature;
32 class QgsRenderContext;
33 class QgsGeometry;
35 
43 {
44  public:
45  class Rule;
46  typedef QList<QgsRuleBasedLabeling::Rule *> RuleList;
47  typedef QMap<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *> RuleToProviderMap;
48 
55  class CORE_EXPORT Rule
56  {
57  public:
59  Rule( QgsPalLayerSettings *settings SIP_TRANSFER, double maximumScale = 0, double minimumScale = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
60  ~Rule();
61 
63  Rule( const Rule &rh ) = delete;
65  Rule &operator=( const Rule &rh ) = delete;
66 
69  {
70  Filtered = 0,
72  Registered
73  };
74 
78  QgsPalLayerSettings *settings() const { return mSettings.get(); }
79 
85  bool dependsOnScale() const { return !qgsDoubleNear( mMinimumScale, 0.0 ) || !qgsDoubleNear( mMaximumScale, 0 ); }
86 
95  double maximumScale() const { return mMaximumScale; }
96 
105  double minimumScale() const { return mMinimumScale; }
106 
111  QString filterExpression() const { return mFilterExp; }
112 
118  QString description() const { return mDescription; }
119 
125  bool active() const { return mIsActive; }
126 
132  bool isElse() const { return mElseRule; }
133 
135  QString ruleKey() const { return mRuleKey; }
136 
138  void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER );
139 
147  void setMinimumScale( double scale ) { mMinimumScale = scale; }
148 
156  void setMaximumScale( double scale ) { mMaximumScale = scale; }
157 
163  void setFilterExpression( const QString &filterExp ) { mFilterExp = filterExp; initFilter(); }
164 
170  void setDescription( const QString &description ) { mDescription = description; }
171 
176  void setActive( bool state ) { mIsActive = state; }
177 
183  void setIsElse( bool iselse ) { mElseRule = iselse; }
184 
186  void setRuleKey( const QString &key ) { mRuleKey = key; }
187 
188  // parent / child operations
189 
195  const QgsRuleBasedLabeling::RuleList &children() const { return mChildren; }
196 
203 
209  QgsRuleBasedLabeling::RuleList descendants() const;
210 
216  const QgsRuleBasedLabeling::Rule *parent() const SIP_SKIP { return mParent; }
217 
223  QgsRuleBasedLabeling::Rule *parent() { return mParent; }
224 
226  void appendChild( QgsRuleBasedLabeling::Rule *rule SIP_TRANSFER );
227 
229  void insertChild( int i, QgsRuleBasedLabeling::Rule *rule SIP_TRANSFER );
230 
232  void removeChildAt( int i );
233 
235  const QgsRuleBasedLabeling::Rule *findRuleByKey( const QString &key ) const;
236 
246  QgsRuleBasedLabeling::Rule *findRuleByKey( const QString &key ) SIP_SKIP;
247 
250 
251  // load / save
252 
259  static QgsRuleBasedLabeling::Rule *create( const QDomElement &ruleElem, const QgsReadWriteContext &context ) SIP_FACTORY;
260 
262  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
263 
264  // evaluation
265 
270  void createSubProviders( QgsVectorLayer *layer, RuleToProviderMap &subProviders, QgsRuleBasedLabelProvider *provider ) SIP_SKIP;
271 
276  void subProviderIds( QStringList &list ) const SIP_SKIP;
277 
282  void prepare( QgsRenderContext &context, QSet<QString> &attributeNames, RuleToProviderMap &subProviders ) SIP_SKIP;
283 
293  std::tuple< RegisterResult, QList< QgsLabelFeature * > > registerFeature( const QgsFeature &feature, QgsRenderContext &context, RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry = QgsGeometry(), const QgsSymbol *symbol = nullptr ) SIP_SKIP;
294 
299  bool requiresAdvancedEffects() const;
300 
310  bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
311 
312  private:
313 #ifdef SIP_RUN
314  Rule( const QgsRuleBasedLabeling::Rule &rh );
315 #endif
316 
324  bool isFilterOK( const QgsFeature &f, QgsRenderContext &context ) const;
325 
333  bool isScaleOK( double scale ) const;
334 
338  void initFilter();
339 
343  void updateElseRules();
344 
345  private:
346  Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
347  std::unique_ptr<QgsPalLayerSettings> mSettings;
348  double mMaximumScale = 0;
349  double mMinimumScale = 0;
350  QString mFilterExp;
351  QString mDescription;
352  bool mElseRule = false;
353  RuleList mChildren;
354  RuleList mElseRules;
355  bool mIsActive = true; // whether it is enabled or not
356 
357  QString mRuleKey = QUuid::createUuid().toString(); // string used for unique identification of rule within labeling
358 
359  std::unique_ptr<QgsExpression> mFilter;
360 
361  };
362 
363 
366  ~QgsRuleBasedLabeling() override;
367 
368  QgsRuleBasedLabeling::Rule *rootRule();
369  const Rule *rootRule() const SIP_SKIP;
370 
372  static QgsRuleBasedLabeling *create( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
373 
374  // implementation of parent interface
375 
376  QString type() const override;
377  QgsRuleBasedLabeling *clone() const override SIP_FACTORY;
378  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const override;
380  QgsVectorLayerLabelProvider *provider( QgsVectorLayer *layer ) const override SIP_SKIP;
381  QStringList subProviders() const override;
382  QgsPalLayerSettings settings( const QString &providerId = QString() ) const override;
383  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
384 
393  void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER, const QString &providerId = QString() ) override;
394  bool requiresAdvancedEffects() const override;
395  void toSld( QDomNode &parent, const QVariantMap &props ) const override;
396 
397  protected:
398  std::unique_ptr<Rule> mRootRule;
399 };
400 
401 #ifndef SIP_RUN
402 
411 {
412  public:
413  QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop = true );
414 
415  // reimplemented
416 
417  bool prepare( QgsRenderContext &context, QSet<QString> &attributeNames ) override;
418 
419  QList< QgsLabelFeature * > registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry(), const QgsSymbol *symbol = nullptr ) override;
420 
422  virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );
423 
425  QList<QgsAbstractLabelProvider *> subProviders() override;
426 
427  protected:
429  std::unique_ptr<QgsRuleBasedLabeling> mRules;
432 };
433 
434 #endif
435 
436 #endif // QGSRULEBASEDLABELING_H
QgsRuleBasedLabeling::Rule::setRuleKey
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based labeling)
Definition: qgsrulebasedlabeling.h:186
QgsRuleBasedLabeling::Rule
A child rule for QgsRuleBasedLabeling.
Definition: qgsrulebasedlabeling.h:55
QgsRuleBasedLabeling::Rule::setMinimumScale
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
Definition: qgsrulebasedlabeling.h:147
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsPalLayerSettings
Contains settings for how a map layer will be labeled.
Definition: qgspallabeling.h:86
QgsRuleBasedLabelProvider::mRules
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
Definition: qgsrulebasedlabeling.h:429
QgsRuleBasedLabeling::Rule::Inactive
@ Inactive
The rule is inactive.
Definition: qgsrulebasedlabeling.h:71
QgsRuleBasedLabeling::Rule::setActive
void setActive(bool state)
Sets if this rule is active.
Definition: qgsrulebasedlabeling.h:176
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsRuleBasedLabeling::Rule::parent
QgsRuleBasedLabeling::Rule * parent()
The parent rule.
Definition: qgsrulebasedlabeling.h:223
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
Definition: qgsstyleentityvisitor.h:33
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsVectorLayerLabelProvider
The QgsVectorLayerLabelProvider class implements a label provider for vector layers....
Definition: qgsvectorlayerlabelprovider.h:41
QgsRuleBasedLabeling
Rule based labeling for a vector layer.
Definition: qgsrulebasedlabeling.h:42
QgsRuleBasedLabeling::Rule::setDescription
void setDescription(const QString &description)
Set a human readable description for this rule.
Definition: qgsrulebasedlabeling.h:170
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsRuleBasedLabeling::Rule::filterExpression
QString filterExpression() const
A filter that will check if this rule applies.
Definition: qgsrulebasedlabeling.h:111
QgsRuleBasedLabeling::Rule::active
bool active() const
Returns if this rule is active.
Definition: qgsrulebasedlabeling.h:125
QgsRuleBasedLabeling::Rule::setMaximumScale
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
Definition: qgsrulebasedlabeling.h:156
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
QgsRuleBasedLabeling::Rule::parent
const QgsRuleBasedLabeling::Rule * parent() const
The parent rule.
Definition: qgsrulebasedlabeling.h:216
QgsRuleBasedLabeling::RuleToProviderMap
QMap< QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider * > RuleToProviderMap
Definition: qgsrulebasedlabeling.h:47
QgsRuleBasedLabeling::Rule::settings
QgsPalLayerSettings * settings() const
Returns the labeling settings.
Definition: qgsrulebasedlabeling.h:78
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsRuleBasedLabelProvider::mSubProviders
QgsRuleBasedLabeling::RuleToProviderMap mSubProviders
label providers are owned by labeling engine
Definition: qgsrulebasedlabeling.h:431
QgsRuleBasedLabeling::Rule::setIsElse
void setIsElse(bool iselse)
Sets if this rule is an ELSE rule.
Definition: qgsrulebasedlabeling.h:183
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsRuleBasedLabeling::Rule::maximumScale
double maximumScale() const
Returns the maximum map scale (i.e.
Definition: qgsrulebasedlabeling.h:95
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsRuleBasedLabeling::RuleList
QList< QgsRuleBasedLabeling::Rule * > RuleList
Definition: qgsrulebasedlabeling.h:45
QgsRuleBasedLabeling::Rule::setFilterExpression
void setFilterExpression(const QString &filterExp)
Set the expression used to check if a given feature shall be rendered with this rule.
Definition: qgsrulebasedlabeling.h:163
QgsAbstractLabelProvider::subProviders
virtual QList< QgsAbstractLabelProvider * > subProviders()
Returns list of child providers - useful if the provider needs to put labels into more layers with di...
Definition: qgslabelingengine.h:125
QgsRuleBasedLabeling::Rule::RegisterResult
RegisterResult
The result of registering a rule.
Definition: qgsrulebasedlabeling.h:68
QgsRuleBasedLabeling::Rule::isElse
bool isElse() const
Check if this rule is an ELSE rule.
Definition: qgsrulebasedlabeling.h:132
QgsRuleBasedLabeling::Rule::dependsOnScale
bool dependsOnScale() const
Determines if scale based labeling is active.
Definition: qgsrulebasedlabeling.h:85
QgsAbstractVectorLayerLabeling
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
Definition: qgsvectorlayerlabeling.h:41
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsRuleBasedLabeling::Rule::children
const QgsRuleBasedLabeling::RuleList & children() const
Returns all children rules of this rule.
Definition: qgsrulebasedlabeling.h:195
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsRuleBasedLabeling::Rule::description
QString description() const
A human readable description for this rule.
Definition: qgsrulebasedlabeling.h:118
QgsVectorLayerLabelProvider::registerFeature
virtual QList< QgsLabelFeature * > registerFeature(const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr)
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.
Definition: qgsvectorlayerlabelprovider.cpp:196
qgsvectorlayerlabelprovider.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgsvectorlayerlabeling.h
QgsLabelFeature
The QgsLabelFeature class describes a feature that should be used within the labeling engine....
Definition: qgslabelfeature.h:57
QgsVectorLayerLabelProvider::prepare
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
Definition: qgsvectorlayerlabelprovider.cpp:114
QgsRuleBasedLabeling::Rule::minimumScale
double minimumScale() const
Returns the minimum map scale (i.e.
Definition: qgsrulebasedlabeling.h:105
QgsRuleBasedLabeling::Rule::children
QgsRuleBasedLabeling::RuleList & children()
Returns all children rules of this rule.
Definition: qgsrulebasedlabeling.h:202
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsRuleBasedLabeling::Rule::ruleKey
QString ruleKey() const
Unique rule identifier (for identification of rule within labeling, used as provider ID)
Definition: qgsrulebasedlabeling.h:135
QgsRuleBasedLabelProvider
Label provider for rule based labeling.
Definition: qgsrulebasedlabeling.h:410