QGIS API Documentation  3.6.0-Noosa (5873452)
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( const QgsRenderContext &context, QSet<QString> &attributeNames, RuleToProviderMap &subProviders ) SIP_SKIP;
280 
285  RegisterResult registerFeature( const QgsFeature &feature, QgsRenderContext &context, RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry = QgsGeometry() ) SIP_SKIP;
286 
291  bool requiresAdvancedEffects() const;
292 
293  private:
294 #ifdef SIP_RUN
295  Rule( const QgsRuleBasedLabeling::Rule &rh );
296 #endif
297 
305  bool isFilterOK( const QgsFeature &f, QgsRenderContext &context ) const;
306 
314  bool isScaleOK( double scale ) const;
315 
319  void initFilter();
320 
324  void updateElseRules();
325 
326  private:
327  Rule *mParent = nullptr; // parent rule (NULL only for root rule)
328  std::unique_ptr<QgsPalLayerSettings> mSettings;
329  double mMaximumScale = 0;
330  double mMinimumScale = 0;
331  QString mFilterExp;
332  QString mDescription;
333  bool mElseRule = false;
334  RuleList mChildren;
335  RuleList mElseRules;
336  bool mIsActive = true; // whether it is enabled or not
337 
338  QString mRuleKey = QUuid::createUuid().toString(); // string used for unique identification of rule within labeling
339 
340  std::unique_ptr<QgsExpression> mFilter;
341 
342  };
343 
344 
347  ~QgsRuleBasedLabeling() override;
348 
349  QgsRuleBasedLabeling::Rule *rootRule() { return mRootRule; }
350  const Rule *rootRule() const SIP_SKIP { return mRootRule; }
351 
353  static QgsRuleBasedLabeling *create( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
354 
355  // implementation of parent interface
356 
357  QString type() const override;
358  QgsRuleBasedLabeling *clone() const override SIP_FACTORY;
359  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const override;
361  QgsVectorLayerLabelProvider *provider( QgsVectorLayer *layer ) const override SIP_SKIP;
362  QStringList subProviders() const override;
363  QgsPalLayerSettings settings( const QString &providerId = QString() ) const override;
364 
373  void setSettings( QgsPalLayerSettings *settings SIP_TRANSFER, const QString &providerId = QString() ) override;
374  bool requiresAdvancedEffects() const override;
375  void toSld( QDomNode &parent, const QgsStringMap &props ) const override;
376 
377  protected:
378  Rule *mRootRule = nullptr;
379 };
380 
381 #ifndef SIP_RUN
382 
390 {
391  public:
392  QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop = true );
393 
394  // reimplemented
395 
396  bool prepare( const QgsRenderContext &context, QSet<QString> &attributeNames ) override;
397 
398  void registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry() ) override;
399 
401  virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );
402 
404  QList<QgsAbstractLabelProvider *> subProviders() override;
405 
406  protected:
408  std::unique_ptr<QgsRuleBasedLabeling> mRules;
411 };
412 
413 #endif
414 
415 #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
Gets 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
virtual void registerFeature(const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry=QgsGeometry())
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels...
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:265
const Rule * rootRule() const
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:106
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.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
QList< QgsRuleBasedLabeling::Rule * > RuleList
virtual bool prepare(const QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsRuleBasedLabeling::Rule * rootRule()
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:69
void setActive(bool state)
Sets if this rule is active.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
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.