QGIS API Documentation  3.17.0-Master (a035f434f4)
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
Class for parsing and evaluation of expressions (formerly called "search strings").
The class is used as a container of context for various read/write operations on other objects...
double maximumScale() const
Returns the maximum map scale (i.e.
QgsPalLayerSettings * settings() const
Returns the labeling settings.
void setFilterExpression(const QString &filterExp)
Set the expression used to check if a given feature shall be rendered with this rule.
QgsRuleBasedLabeling::RuleToProviderMap mSubProviders
label providers are owned by labeling engine
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
bool isElse() const
Check if this rule is an ELSE rule.
RegisterResult
The result of registering a rule.
bool dependsOnScale() const
Determines if scale based labeling is active.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:316
QString description() const
A human readable description for this rule.
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:123
QString ruleKey() const
Unique rule identifier (for identification of rule within labeling, used as provider ID) ...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QMap< QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider * > RuleToProviderMap
const QgsRuleBasedLabeling::RuleList & children() const
Returns all children rules of this rule.
An interface for classes which can visit style entity (e.g.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:759
QList< QgsRuleBasedLabeling::Rule * > RuleList
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
#define SIP_SKIP
Definition: qgis_sip.h:126
std::unique_ptr< Rule > mRootRule
#define SIP_TRANSFER
Definition: qgis_sip.h:36
double minimumScale() const
Returns the minimum map scale (i.e.
QgsRuleBasedLabeling::RuleList & children()
Returns all children rules of this rule.
#define SIP_FACTORY
Definition: qgis_sip.h:76
void setActive(bool state)
Sets if this rule is active.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
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...
QgsRuleBasedLabeling::Rule * parent()
The parent rule.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based labeling) ...
virtual QList< QgsAbstractLabelProvider * > subProviders()
Returns list of child providers - useful if the provider needs to put labels into more layers with di...
Contains information about the context of a rendering operation.
QString filterExpression() const
A filter that will check if this rule applies.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
bool active() const
Returns if this rule is active.
const QgsRuleBasedLabeling::Rule * parent() const
The parent rule.
void setDescription(const QString &description)
Set a human readable description for this rule.
void setIsElse(bool iselse)
Sets if this rule is an ELSE rule.
Represents a vector layer which manages a vector based data sets.
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.