QGIS API Documentation  2.14.11-Essen
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 
52  class CORE_EXPORT Rule
53  {
54  public:
56  Rule( QgsPalLayerSettings* settings, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(), const QString& description = QString(), bool elseRule = false );
57  ~Rule();
58 
61  {
62  Filtered = 0,
64  Registered
65  };
66 
70  QgsPalLayerSettings* settings() const { return mSettings; }
71 
77  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
78 
87  int scaleMinDenom() const { return mScaleMinDenom; }
88 
97  int scaleMaxDenom() const { return mScaleMaxDenom; }
102  QString filterExpression() const { return mFilterExp; }
108  QString description() const { return mDescription; }
114  bool active() const { return mIsActive; }
120  bool isElse() const { return mElseRule; }
121 
123  void setSettings( QgsPalLayerSettings* settings );
124 
131  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
138  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
144  void setFilterExpression( const QString& filterExp ) { mFilterExp = filterExp; initFilter(); }
150  void setDescription( const QString& description ) { mDescription = description; }
155  void setActive( bool state ) { mIsActive = state; }
161  void setIsElse( bool iselse ) { mElseRule = iselse; }
162 
163 
164  // parent / child operations
165 
171  const RuleList& children() const { return mChildren; }
177  RuleList& children() { return mChildren; }
183  const Rule* parent() const { return mParent; }
189  Rule* parent() { return mParent; }
190 
192  void appendChild( Rule* rule );
193 
195  void insertChild( int i, Rule* rule );
196 
198  void removeChildAt( int i );
199 
201  Rule* clone() const;
202 
203  // load / save
204 
210  static Rule* create( const QDomElement& ruleElem );
211 
213  QDomElement save( QDomDocument& doc ) const;
214 
215  // evaluation
216 
218  void createSubProviders( QgsVectorLayer* layer, RuleToProviderMap& subProviders, QgsRuleBasedLabelProvider *provider );
219 
221  void prepare( const QgsRenderContext& context, QStringList& attributeNames, RuleToProviderMap& subProviders );
222 
224  RegisterResult registerFeature( QgsFeature& feature, QgsRenderContext& context, RuleToProviderMap& subProviders, QgsGeometry* obstacleGeometry = nullptr );
225 
226  protected:
234  bool isFilterOK( QgsFeature& f, QgsRenderContext& context ) const;
241  bool isScaleOK( double scale ) const;
242 
246  void initFilter();
247 
251  void updateElseRules();
252 
253  protected:
254  Rule* mParent; // parent rule (NULL only for root rule)
256  int mScaleMinDenom, mScaleMaxDenom;
257  QString mFilterExp, mDescription;
258  bool mElseRule;
259  RuleList mChildren;
260  RuleList mElseRules;
261  bool mIsActive; // whether it is enabled or not
262 
263  // temporary
265 
266  private:
267 
268  Rule( const Rule& rh );
269  Rule& operator=( const Rule& rh );
270  };
271 
272 
278 
279  Rule* rootRule() { return mRootRule; }
280  const Rule* rootRule() const { return mRootRule; }
281 
283  static QgsRuleBasedLabeling* create( const QDomElement& element );
284 
285  // implementation of parent interface
286 
287  virtual QString type() const override;
288  virtual QDomElement save( QDomDocument& doc ) const override;
289  virtual QgsVectorLayerLabelProvider *provider( QgsVectorLayer* layer ) const override;
290 
291  protected:
293 };
294 
295 
302 {
303  public:
304  QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling& rules, QgsVectorLayer* layer, bool withFeatureLoop = true );
306 
307  // reimplemented
308 
309  virtual bool prepare( const QgsRenderContext& context, QStringList& attributeNames ) override;
310 
311  virtual void registerFeature( QgsFeature& feature, QgsRenderContext& context, QgsGeometry* obstacleGeometry = nullptr ) override;
312 
313  // new methods
314  virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, bool withFeatureLoop, const QgsPalLayerSettings *settings );
315 
317 
318  protected:
323 };
324 
325 
326 #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.
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
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.
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.