QGIS API Documentation  2.14.0-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"
22 
23 class QDomDocument;
24 class QDomElement;
25 
26 class QgsExpression;
27 class QgsFeature;
29 class QgsRenderContext;
30 class QgsGeometry;
31 
39 {
40  public:
41  class Rule;
42  typedef QList<Rule*> RuleList;
44 
45 
51  class CORE_EXPORT Rule
52  {
53  public:
55  Rule( QgsPalLayerSettings* settings, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(), const QString& description = QString(), bool elseRule = false );
56  ~Rule();
57 
60  {
61  Filtered = 0,
63  Registered
64  };
65 
69  QgsPalLayerSettings* settings() const { return mSettings; }
70 
76  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
77 
86  int scaleMinDenom() const { return mScaleMinDenom; }
87 
96  int scaleMaxDenom() const { return mScaleMaxDenom; }
101  QString filterExpression() const { return mFilterExp; }
107  QString description() const { return mDescription; }
113  bool active() const { return mIsActive; }
119  bool isElse() const { return mElseRule; }
120 
122  void setSettings( QgsPalLayerSettings* settings );
123 
130  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
137  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
143  void setFilterExpression( const QString& filterExp ) { mFilterExp = filterExp; initFilter(); }
149  void setDescription( const QString& description ) { mDescription = description; }
154  void setActive( bool state ) { mIsActive = state; }
160  void setIsElse( bool iselse ) { mElseRule = iselse; }
161 
162 
163  // parent / child operations
164 
170  const RuleList& children() const { return mChildren; }
176  RuleList& children() { return mChildren; }
182  const Rule* parent() const { return mParent; }
188  Rule* parent() { return mParent; }
189 
191  void appendChild( Rule* rule );
192 
194  void insertChild( int i, Rule* rule );
195 
197  void removeChildAt( int i );
198 
200  Rule* clone() const;
201 
202  // load / save
203 
209  static Rule* create( const QDomElement& ruleElem );
210 
212  QDomElement save( QDomDocument& doc ) const;
213 
214  // evaluation
215 
217  void createSubProviders( QgsVectorLayer* layer, RuleToProviderMap& subProviders );
218 
220  void prepare( const QgsRenderContext& context, QStringList& attributeNames, RuleToProviderMap& subProviders );
221 
223  RegisterResult registerFeature( QgsFeature& feature, QgsRenderContext& context, RuleToProviderMap& subProviders, QgsGeometry* obstacleGeometry = nullptr );
224 
225  protected:
233  bool isFilterOK( QgsFeature& f, QgsRenderContext& context ) const;
240  bool isScaleOK( double scale ) const;
241 
245  void initFilter();
246 
250  void updateElseRules();
251 
252  protected:
253  Rule* mParent; // parent rule (NULL only for root rule)
255  int mScaleMinDenom, mScaleMaxDenom;
256  QString mFilterExp, mDescription;
257  bool mElseRule;
258  RuleList mChildren;
259  RuleList mElseRules;
260  bool mIsActive; // whether it is enabled or not
261 
262  // temporary
264 
265  private:
266 
267  Rule( const Rule& rh );
268  Rule& operator=( const Rule& rh );
269  };
270 
271 
277 
278  Rule* rootRule() { return mRootRule; }
279  const Rule* rootRule() const { return mRootRule; }
280 
282  static QgsRuleBasedLabeling* create( const QDomElement& element );
283 
284  // implementation of parent interface
285 
286  virtual QString type() const override;
287  virtual QDomElement save( QDomDocument& doc ) const override;
288  virtual QgsVectorLayerLabelProvider* provider( QgsVectorLayer* layer ) const override;
289 
290  protected:
292 };
293 
294 
296 
303 {
304  public:
305  QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling& rules, QgsVectorLayer* layer, bool withFeatureLoop = true );
307 
308  // reimplemented
309 
310  virtual bool prepare( const QgsRenderContext& context, QStringList& attributeNames ) override;
311 
312  virtual void registerFeature( QgsFeature& feature, QgsRenderContext& context, QgsGeometry* obstacleGeometry = nullptr ) override;
313 
314  // new methods
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.
bool isElse() const
Check if this rule is an ELSE rule.
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
QString description() const
A human readable description for this rule.
RegisterResult
The result of registering a rule.
QString filterExpression() const
A filter that will check if this rule applies.
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.
int scaleMinDenom() const
The minimum scale at which this label rule should be applied.
const Rule * parent() const
The parent rule.
const RuleList & children() const
Return all children rules of this rule.
bool dependsOnScale() const
Determines if scale based labeling is active.
void setScaleMinDenom(int scaleMinDenom)
Set the minimum denominator for which this rule shall apply.
bool active() const
Returns if this rule is active.
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
const Rule * rootRule() const
Contains information about the context of a rendering operation.
QgsRuleBasedLabeling mRules
owned copy
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
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.