QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QStringList>
19 #include <QMap>
20 
21 #include "qgsvectorlayerlabeling.h"
23 
24 class QDomDocument;
25 class QDomElement;
26 
27 class QgsExpression;
28 class QgsFeature;
30 class QgsRenderContext;
31 class QgsGeometry;
33 
41 {
42  public:
43  class Rule;
44  typedef QList<Rule*> RuleList;
46 
53  class CORE_EXPORT Rule
54  {
55  public:
57  Rule( QgsPalLayerSettings* settings, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(), const QString& description = QString(), bool elseRule = false );
58  ~Rule();
59 
62  {
63  Filtered = 0,
65  Registered
66  };
67 
71  QgsPalLayerSettings* settings() const { return mSettings; }
72 
78  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
79 
88  int scaleMinDenom() const { return mScaleMinDenom; }
89 
98  int scaleMaxDenom() const { return mScaleMaxDenom; }
103  QString filterExpression() const { return mFilterExp; }
109  QString description() const { return mDescription; }
115  bool active() const { return mIsActive; }
121  bool isElse() const { return mElseRule; }
122 
124  QString ruleKey() const { return mRuleKey; }
125 
127  void setSettings( QgsPalLayerSettings* settings );
128 
135  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
142  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
148  void setFilterExpression( const QString& filterExp ) { mFilterExp = filterExp; initFilter(); }
154  void setDescription( const QString& description ) { mDescription = description; }
159  void setActive( bool state ) { mIsActive = state; }
165  void setIsElse( bool iselse ) { mElseRule = iselse; }
166 
168  void setRuleKey( const QString& key ) { mRuleKey = key; }
169 
170  // parent / child operations
171 
177  const RuleList& children() const { return mChildren; }
183  RuleList& children() { return mChildren; }
184 
190  RuleList descendants() const { RuleList l; Q_FOREACH ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
191 
197  const Rule* parent() const { return mParent; }
203  Rule* parent() { return mParent; }
204 
206  void appendChild( Rule* rule );
207 
209  void insertChild( int i, Rule* rule );
210 
212  void removeChildAt( int i );
213 
215  const Rule* findRuleByKey( const QString& key ) const;
216 
218  Rule* clone() const;
219 
220  // load / save
221 
227  static Rule* create( const QDomElement& ruleElem );
228 
230  QDomElement save( QDomDocument& doc ) const;
231 
232  // evaluation
233 
235  void createSubProviders( QgsVectorLayer* layer, RuleToProviderMap& subProviders, QgsRuleBasedLabelProvider *provider );
236 
238  void subProviderIds( QStringList& list ) const;
239 
241  void prepare( const QgsRenderContext& context, QStringList& attributeNames, RuleToProviderMap& subProviders );
242 
244  RegisterResult registerFeature( QgsFeature& feature, QgsRenderContext& context, RuleToProviderMap& subProviders, QgsGeometry* obstacleGeometry = nullptr );
245 
246  protected:
254  bool isFilterOK( QgsFeature& f, QgsRenderContext& context ) const;
261  bool isScaleOK( double scale ) const;
262 
266  void initFilter();
267 
271  void updateElseRules();
272 
273  protected:
274  Rule* mParent; // parent rule (NULL only for root rule)
276  int mScaleMinDenom, mScaleMaxDenom;
277  QString mFilterExp, mDescription;
278  bool mElseRule;
279  RuleList mChildren;
280  RuleList mElseRules;
281  bool mIsActive; // whether it is enabled or not
282 
283  QString mRuleKey; // string used for unique identification of rule within labeling
284 
285  // temporary
287 
288  private:
289 
290  Rule( const Rule& rh );
291  Rule& operator=( const Rule& rh );
292  };
293 
294 
300 
301  Rule* rootRule() { return mRootRule; }
302  const Rule* rootRule() const { return mRootRule; }
303 
305  static QgsRuleBasedLabeling* create( const QDomElement& element );
306 
307  // implementation of parent interface
308 
309  virtual QString type() const override;
310  virtual QDomElement save( QDomDocument& doc ) const override;
311  virtual QgsVectorLayerLabelProvider *provider( QgsVectorLayer* layer ) const override;
312  virtual QStringList subProviders() const override;
313  virtual QgsPalLayerSettings settings( QgsVectorLayer* layer, const QString& providerId = QString() ) const override;
314 
315  protected:
317 };
318 
319 
326 {
327  public:
328  QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling& rules, QgsVectorLayer* layer, bool withFeatureLoop = true );
330 
331  // reimplemented
332 
333  virtual bool prepare( const QgsRenderContext& context, QStringList& attributeNames ) override;
334 
335  virtual void registerFeature( QgsFeature& feature, QgsRenderContext& context, QgsGeometry* obstacleGeometry = nullptr ) override;
336 
338  virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString& providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );
339 
342 
343  protected:
348 };
349 
350 
351 #endif // QGSRULEBASEDLABELING_H
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsPalLayerSettings * settings() const
Get 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
const Rule * parent() const
The parent rule.
RuleList descendants() const
Returns all children, grand-children, grand-grand-children, grand-gra...
int scaleMinDenom() const
The minimum scale at which this label rule should be applied.
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.
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:76
Rule * parent()
The parent rule.
QMap< Rule *, QgsVectorLayerLabelProvider * > RuleToProviderMap
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:187
virtual QList< QgsAbstractLabelProvider * > subProviders()
Return list of child providers - useful if the provider needs to put labels into more layers with dif...
virtual bool prepare(const QgsRenderContext &context, QStringList &attributeNames)
Prepare for registration of features.
void setScaleMinDenom(int scaleMinDenom)
Set the minimum denominator for which this rule shall apply.
virtual void registerFeature(QgsFeature &feature, QgsRenderContext &context, QgsGeometry *obstacleGeometry=nullptr)
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels...
void setActive(bool state)
Sets if this rule is active.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based labeling) ...
QgsPalLayerSettings * mSettings
Contains information about the context of a rendering operation.
QString filterExpression() const
A filter that will check if this rule applies.
const RuleList & children() const
Return all children rules of this rule.
QgsRuleBasedLabeling mRules
owned copy
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
bool active() const
Returns if this rule is active.
void setDescription(const QString &description)
Set a human readable description for this rule.
void setScaleMaxDenom(int scaleMaxDenom)
Set the maximum denominator for which this rule shall apply.
int scaleMaxDenom() const
The maximum scale denominator at which this label rule should be applied.
void setIsElse(bool iselse)
Sets if this rule is an ELSE rule.
RuleList & children()
Return all children rules of this rule.
Represents a vector layer which manages a vector based data sets.