QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
22 #include "qgsvectorlayerlabeling.h"
24 
25 class QDomDocument;
26 class QDomElement;
27 
28 class QgsExpression;
29 class QgsFeature;
31 class QgsRenderContext;
32 class QgsGeometry;
34 
41 {
42  public:
43  class Rule;
44  typedef QList<QgsRuleBasedLabeling::Rule *> RuleList;
45  typedef QMap<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *> RuleToProviderMap;
46 
52  class CORE_EXPORT Rule
53  {
54  public:
56  Rule( QgsPalLayerSettings *settings SIP_TRANSFER, double maximumScale = 0, double minimumScale = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
57  ~Rule();
58 
60  Rule( const Rule &rh ) = delete;
62  Rule &operator=( const Rule &rh ) = delete;
63 
66  {
67  Filtered = 0,
69  Registered
70  };
71 
75  QgsPalLayerSettings *settings() const { return mSettings.get(); }
76 
82  bool dependsOnScale() const { return !qgsDoubleNear( mMinimumScale, 0.0 ) || !qgsDoubleNear( mMaximumScale, 0 ); }
83 
92  double maximumScale() const { return mMaximumScale; }
93 
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 
135  void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER );
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 
243  QgsRuleBasedLabeling::Rule *findRuleByKey( const QString &key ) SIP_SKIP;
244 
247 
248  // load / save
249 
256  static QgsRuleBasedLabeling::Rule *create( const QDomElement &ruleElem, const QgsReadWriteContext &context ) SIP_FACTORY;
257 
259  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
260 
261  // evaluation
262 
267  void createSubProviders( QgsVectorLayer *layer, RuleToProviderMap &subProviders, QgsRuleBasedLabelProvider *provider ) SIP_SKIP;
268 
273  void subProviderIds( QStringList &list ) const SIP_SKIP;
274 
279  void prepare( QgsRenderContext &context, QSet<QString> &attributeNames, RuleToProviderMap &subProviders ) SIP_SKIP;
280 
285  RegisterResult registerFeature( const QgsFeature &feature, QgsRenderContext &context, RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry = QgsGeometry(), const QgsSymbol *symbol = nullptr ) SIP_SKIP;
286 
291  bool requiresAdvancedEffects() const;
292 
302  bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
303 
304  private:
305 #ifdef SIP_RUN
306  Rule( const QgsRuleBasedLabeling::Rule &rh );
307 #endif
308 
316  bool isFilterOK( const QgsFeature &f, QgsRenderContext &context ) const;
317 
325  bool isScaleOK( double scale ) const;
326 
330  void initFilter();
331 
335  void updateElseRules();
336 
337  private:
338  Rule *mParent = nullptr; // parent rule (nullptr only for root rule)
339  std::unique_ptr<QgsPalLayerSettings> mSettings;
340  double mMaximumScale = 0;
341  double mMinimumScale = 0;
342  QString mFilterExp;
343  QString mDescription;
344  bool mElseRule = false;
345  RuleList mChildren;
346  RuleList mElseRules;
347  bool mIsActive = true; // whether it is enabled or not
348 
349  QString mRuleKey = QUuid::createUuid().toString(); // string used for unique identification of rule within labeling
350 
351  std::unique_ptr<QgsExpression> mFilter;
352 
353  };
354 
355 
358  ~QgsRuleBasedLabeling() override;
359 
360  QgsRuleBasedLabeling::Rule *rootRule();
361  const Rule *rootRule() const SIP_SKIP;
362 
364  static QgsRuleBasedLabeling *create( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
365 
366  // implementation of parent interface
367 
368  QString type() const override;
369  QgsRuleBasedLabeling *clone() const override SIP_FACTORY;
370  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const override;
372  QgsVectorLayerLabelProvider *provider( QgsVectorLayer *layer ) const override SIP_SKIP;
373  QStringList subProviders() const override;
374  QgsPalLayerSettings settings( const QString &providerId = QString() ) const override;
375  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
376 
385  void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER, const QString &providerId = QString() ) override;
386  bool requiresAdvancedEffects() const override;
387  void toSld( QDomNode &parent, const QgsStringMap &props ) const override;
388 
389  protected:
390  std::unique_ptr<Rule> mRootRule;
391 };
392 
393 #ifndef SIP_RUN
394 
402 {
403  public:
404  QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop = true );
405 
406  // reimplemented
407 
408  bool prepare( QgsRenderContext &context, QSet<QString> &attributeNames ) override;
409 
410  void registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry(), const QgsSymbol *symbol = nullptr ) override;
411 
413  virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );
414 
416  QList<QgsAbstractLabelProvider *> subProviders() override;
417 
418  protected:
420  std::unique_ptr<QgsRuleBasedLabeling> mRules;
423 };
424 
425 #endif
426 
427 #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:183
QgsRuleBasedLabeling::Rule::operator=
Rule & operator=(const Rule &rh)=delete
Rules cannot be copied.
QgsRuleBasedLabeling::Rule
Definition: qgsrulebasedlabeling.h:53
QgsRuleBasedLabeling::Rule::setMinimumScale
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
Definition: qgsrulebasedlabeling.h:144
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsPalLayerSettings
Definition: qgspallabeling.h:207
QgsRuleBasedLabelProvider::mRules
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
Definition: qgsrulebasedlabeling.h:420
QgsRuleBasedLabeling::Rule::Inactive
@ Inactive
The rule is inactive.
Definition: qgsrulebasedlabeling.h:68
QgsRuleBasedLabeling::Rule::setActive
void setActive(bool state)
Sets if this rule is active.
Definition: qgsrulebasedlabeling.h:173
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsRuleBasedLabeling::Rule::parent
QgsRuleBasedLabeling::Rule * parent()
The parent rule.
Definition: qgsrulebasedlabeling.h:220
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g.
Definition: qgsstyleentityvisitor.h:34
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsVectorLayerLabelProvider
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
Definition: qgsvectorlayerlabelprovider.h:41
QgsRuleBasedLabeling
Definition: qgsrulebasedlabeling.h:41
QgsRuleBasedLabeling::Rule::setDescription
void setDescription(const QString &description)
Set a human readable description for this rule.
Definition: qgsrulebasedlabeling.h:167
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:108
QgsRuleBasedLabeling::Rule::active
bool active() const
Returns if this rule is active.
Definition: qgsrulebasedlabeling.h:122
QgsRuleBasedLabeling::Rule::setMaximumScale
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
Definition: qgsrulebasedlabeling.h:153
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:213
QgsRuleBasedLabeling::RuleToProviderMap
QMap< QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider * > RuleToProviderMap
Definition: qgsrulebasedlabeling.h:45
QgsRuleBasedLabeling::Rule::settings
QgsPalLayerSettings * settings() const
Returns the labeling settings.
Definition: qgsrulebasedlabeling.h:75
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:422
QgsRuleBasedLabeling::Rule::setIsElse
void setIsElse(bool iselse)
Sets if this rule is an ELSE rule.
Definition: qgsrulebasedlabeling.h:180
QgsRuleBasedLabeling::Rule::Rule
Rule(const Rule &rh)=delete
Rules cannot be copied.
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:315
QgsRuleBasedLabeling::Rule::maximumScale
double maximumScale() const
Returns the maximum map scale (i.e.
Definition: qgsrulebasedlabeling.h:92
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsRuleBasedLabeling::RuleList
QList< QgsRuleBasedLabeling::Rule * > RuleList
Definition: qgsrulebasedlabeling.h:43
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:160
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:121
QgsRuleBasedLabeling::Rule::RegisterResult
RegisterResult
The result of registering a rule.
Definition: qgsrulebasedlabeling.h:66
QgsRuleBasedLabeling::Rule::isElse
bool isElse() const
Check if this rule is an ELSE rule.
Definition: qgsrulebasedlabeling.h:129
QgsRuleBasedLabeling::Rule::dependsOnScale
bool dependsOnScale() const
Determines if scale based labeling is active.
Definition: qgsrulebasedlabeling.h:82
QgsAbstractVectorLayerLabeling
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
Definition: qgsvectorlayerlabeling.h:42
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:758
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:192
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsRuleBasedLabeling::Rule::description
QString description() const
A human readable description for this rule.
Definition: qgsrulebasedlabeling.h:115
QgsVectorLayerLabelProvider::registerFeature
virtual void 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:190
qgsvectorlayerlabelprovider.h
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
qgsvectorlayerlabeling.h
QgsVectorLayerLabelProvider::prepare
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
Definition: qgsvectorlayerlabelprovider.cpp:112
QgsRuleBasedLabeling::Rule::minimumScale
double minimumScale() const
Returns the minimum map scale (i.e.
Definition: qgsrulebasedlabeling.h:102
QgsRuleBasedLabeling::Rule::children
QgsRuleBasedLabeling::RuleList & children()
Returns all children rules of this rule.
Definition: qgsrulebasedlabeling.h:199
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsRuleBasedLabeling::Rule::ruleKey
QString ruleKey() const
Unique rule identifier (for identification of rule within labeling, used as provider ID)
Definition: qgsrulebasedlabeling.h:132
QgsRuleBasedLabelProvider
Definition: qgsrulebasedlabeling.h:402