QGIS API Documentation  2.12.0-Lyon
qgsrulebasedlabeling.h
Go to the documentation of this file.
1 #ifndef QGSRULEBASEDLABELING_H
2 #define QGSRULEBASEDLABELING_H
3 
4 #include <QStringList>
5 #include <QMap>
6 
8 
9 class QDomDocument;
10 class QDomElement;
11 
12 class QgsExpression;
13 class QgsFeature;
15 class QgsRenderContext;
16 
17 
19 {
20  public:
21  class Rule;
22  typedef QList<Rule*> RuleList;
24 
25  class CORE_EXPORT Rule
26  {
27  public:
29  Rule( QgsPalLayerSettings* settings, int scaleMinDenom = 0, int scaleMaxDenom = 0, const QString& filterExp = QString(), const QString& description = QString(), bool elseRule = false );
30  ~Rule();
31 
34  {
35  Filtered = 0,
37  Registered
38  };
39 
40  QgsPalLayerSettings* settings() const { return mSettings; }
41  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
42  int scaleMinDenom() const { return mScaleMinDenom; }
43  int scaleMaxDenom() const { return mScaleMaxDenom; }
48  QString filterExpression() const { return mFilterExp; }
54  QString description() const { return mDescription; }
60  bool active() const { return mIsActive; }
66  bool isElse() const { return mElseRule; }
67 
69  void setSettings( QgsPalLayerSettings* settings );
70 
77  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
84  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
90  void setFilterExpression( const QString& filterExp ) { mFilterExp = filterExp; initFilter(); }
96  void setDescription( const QString& description ) { mDescription = description; }
101  void setActive( bool state ) { mIsActive = state; }
107  void setIsElse( bool iselse ) { mElseRule = iselse; }
108 
109 
110  // parent / child operations
111 
117  const RuleList& children() const { return mChildren; }
123  RuleList& children() { return mChildren; }
129  const Rule* parent() const { return mParent; }
135  Rule* parent() { return mParent; }
136 
138  void appendChild( Rule* rule );
139 
141  void insertChild( int i, Rule* rule );
142 
144  void removeChildAt( int i );
145 
147  Rule* clone() const;
148 
149  // load / save
150 
156  static Rule* create( const QDomElement& ruleElem );
157 
159  QDomElement save( QDomDocument& doc ) const;
160 
161  // evaluation
162 
164  void createSubProviders( QgsVectorLayer* layer, RuleToProviderMap& subProviders );
165 
167  void prepare( const QgsRenderContext& context, QStringList& attributeNames, RuleToProviderMap& subProviders );
168 
170  RegisterResult registerFeature( QgsFeature& feature, QgsRenderContext& context, RuleToProviderMap& subProviders );
171 
172  protected:
180  bool isFilterOK( QgsFeature& f, QgsRenderContext& context ) const;
187  bool isScaleOK( double scale ) const;
188 
189  void initFilter();
190 
194  void updateElseRules();
195 
196  protected:
197  Rule* mParent; // parent rule (NULL only for root rule)
199  int mScaleMinDenom, mScaleMaxDenom;
200  QString mFilterExp, mDescription;
201  bool mElseRule;
202  RuleList mChildren;
203  RuleList mElseRules;
204  bool mIsActive; // whether it is enabled or not
205 
206  // temporary
208  };
209 
210 
216 
217  Rule* rootRule() { return mRootRule; }
218  const Rule* rootRule() const { return mRootRule; }
219 
221  static QgsRuleBasedLabeling* create( const QDomElement& element );
222 
223  // implementation of parent interface
224 
225  virtual QString type() const override;
226  virtual QDomElement save( QDomDocument& doc ) const override;
227  virtual QgsVectorLayerLabelProvider* provider( QgsVectorLayer* layer ) const override;
228 
229  protected:
231 };
232 
233 
235 
236 
238 {
239  public:
240  QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling& rules, QgsVectorLayer* layer, bool withFeatureLoop = true );
242 
243  // reimplemented
244 
245  virtual bool prepare( const QgsRenderContext& context, QStringList& attributeNames ) override;
246 
247  virtual void registerFeature( QgsFeature& feature, QgsRenderContext& context ) override;
248 
249  // new methods
250 
252 
253  protected:
258 };
259 
260 
261 #endif // QGSRULEBASEDLABELING_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:92
QgsPalLayerSettings * settings() const
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
virtual void registerFeature(QgsFeature &feature, QgsRenderContext &context)
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels...
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.
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:176
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.
const Rule * parent() const
The parent rule.
const RuleList & children() const
Return all children rules of this rule.
void setScaleMinDenom(int scaleMinDenom)
Set the minimum denominator for which this rule shall apply.
bool active() const
Returns if this rule is active.
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.
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.