QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrulebasedrendererv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrulebasedrendererv2.h - Rule-based renderer (symbology-ng)
3  ---------------------
4  begin : May 2010
5  copyright : (C) 2010 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 
16 #ifndef QGSRULEBASEDRENDERERV2_H
17 #define QGSRULEBASEDRENDERERV2_H
18 
19 #include "qgsfield.h"
20 #include "qgsfeature.h"
21 #include "qgis.h"
22 
23 #include "qgsrendererv2.h"
24 
25 class QgsExpression;
26 
29 
34 class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
35 {
36  public:
37 
38 
39  // TODO: use QVarLengthArray instead of QList
40 
41  enum FeatureFlags { FeatIsSelected = 1, FeatDrawMarkers = 2 };
42 
43  // feature for rendering: QgsFeature and some flags
45  {
46  FeatureToRender( QgsFeature& _f, int _flags ) : feat( _f ), flags( _flags ) {}
48  int flags; // selected and/or draw markers
49  };
50 
51  // rendering job: a feature to be rendered with a particular symbol
52  // (both f, symbol are _not_ owned by this class)
53  struct RenderJob
54  {
55  RenderJob( FeatureToRender& _ftr, QgsSymbolV2* _s ) : ftr( _ftr ), symbol( _s ) {}
58  };
59 
60  // render level: a list of jobs to be drawn at particular level
61  // (jobs are owned by this class)
62  struct RenderLevel
63  {
64  RenderLevel( int z ): zIndex( z ) {}
65  ~RenderLevel() { foreach ( RenderJob* j, jobs ) delete j; }
66  int zIndex;
68  };
69 
70  // rendering queue: a list of rendering levels
72 
73  class Rule;
74  typedef QList<Rule*> RuleList;
75 
84  class CORE_EXPORT Rule
85  {
86  public:
88  {
89  Filtered = 0,
91  Rendered
92  };
93 
95  Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString(),
96  QString label = QString(), QString description = QString(), bool elseRule = false );
97  ~Rule();
98  QString dump( int offset = 0 ) const;
99  QSet<QString> usedAttributes();
100  QgsSymbolV2List symbols( const QgsRenderContext& context = QgsRenderContext() );
102  QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );
104  QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
105  bool isFilterOK( QgsFeature& f, QgsRenderContext *context = 0 ) const;
106  bool isScaleOK( double scale ) const;
107 
108  QgsSymbolV2* symbol() { return mSymbol; }
109  QString label() const { return mLabel; }
110  bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
111  int scaleMinDenom() const { return mScaleMinDenom; }
112  int scaleMaxDenom() const { return mScaleMaxDenom; }
113  QgsExpression* filter() const { return mFilter; }
114  QString filterExpression() const { return mFilterExp; }
115  QString description() const { return mDescription; }
118  bool checkState() const { return mIsActive; }
124  bool active() const { return mIsActive; }
125 
128  QString ruleKey() const { return mRuleKey; }
131  void setRuleKey( const QString& key ) { mRuleKey = key; }
132 
134  void setSymbol( QgsSymbolV2* sym );
135  void setLabel( QString label ) { mLabel = label; }
136  void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
137  void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
138  void setFilterExpression( QString filterExp ) { mFilterExp = filterExp; initFilter(); }
139  void setDescription( QString description ) { mDescription = description; }
142  void setCheckState( bool state ) { mIsActive = state; }
147  void setActive( bool state ) { mIsActive = state; }
148 
150  Rule* clone() const;
151 
152  void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props );
153  static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
154 
155  QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );
156 
158  Q_DECL_DEPRECATED bool startRender( QgsRenderContext& context, const QgsFields& fields );
160  bool startRender( QgsRenderContext& context, const QgsFields& fields, QString& filter );
162  QSet<int> collectZLevels();
165  void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
166 
167  RenderResult renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
168 
170  bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = 0 );
171 
173  QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = 0 );
174 
176  RuleList rulesForFeature( QgsFeature& feat, QgsRenderContext* context = 0 );
177 
178  void stopRender( QgsRenderContext& context );
179 
180  static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
181 
182  RuleList& children() { return mChildren; }
183  RuleList descendants() const { RuleList l; foreach ( Rule *c, mChildren ) { l += c; l += c->descendants(); } return l; }
184  Rule* parent() { return mParent; }
185 
187  void appendChild( Rule* rule );
189  void insertChild( int i, Rule* rule );
191  void removeChild( Rule* rule );
193  void removeChildAt( int i );
195  void takeChild( Rule* rule );
197  Rule* takeChildAt( int i );
198 
201  Rule* findRuleByKey( QString key );
202 
203  void updateElseRules();
204 
205  void setIsElse( bool iselse ) { mElseRule = iselse; }
206  bool isElse() { return mElseRule; }
207 
208  protected:
209  void initFilter();
210 
211  Rule* mParent; // parent rule (NULL only for root rule)
213  int mScaleMinDenom, mScaleMaxDenom;
214  QString mFilterExp, mLabel, mDescription;
215  bool mElseRule;
218  bool mIsActive; // whether it is enabled or not
219 
220  QString mRuleKey; // string used for unique identification of rule within renderer
221 
222  // temporary
224  // temporary while rendering
227  };
228 
230 
231  static QgsFeatureRendererV2* create( QDomElement& element );
232 
236  QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
237 
239 
241  virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context ) override;
242 
243  virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
244 
245  virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) override;
246 
247  virtual void stopRender( QgsRenderContext& context ) override;
248 
249  virtual QString filter() override;
250 
251  virtual QList<QString> usedAttributes() override;
252 
253  virtual QgsFeatureRendererV2* clone() const override;
254 
255  virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
256 
257  static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
258 
259  virtual QgsSymbolV2List symbols( QgsRenderContext& context ) override;
260 
262  virtual QDomElement save( QDomDocument& doc ) override;
263 
265  virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize ) override;
266 
269  virtual bool legendSymbolItemsCheckable() const override;
270 
273  virtual bool legendSymbolItemChecked( QString key ) override;
274 
277  virtual void checkLegendSymbolItem( QString key, bool state = true ) override;
278 
281  virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" ) override;
282 
286  virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override;
287 
289  virtual QString dump() const override;
290 
293  virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context ) override;
294 
298  virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
299 
300  virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context ) override;
301 
303  virtual int capabilities() override { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
304 
306 
307  Rule* rootRule() { return mRootRule; }
308 
310 
312  static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
314  static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
316  static void refineRuleScales( Rule* initialRule, QList<int> scales );
317 
321  static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );
322 
324  static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, QString sizeScaleField, QString rotationField = QString() );
325 
326  protected:
329 
330  // temporary
333 
335 };
336 
337 #endif // QGSRULEBASEDRENDERERV2_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:88
GeometryType
Definition: qgis.h:155
void setActive(bool state)
Sets if this rule is active.
void setScaleMaxDenom(int scaleMaxDenom)
Container of fields for a vector layer.
Definition: qgsfield.h:177
RenderJob(FeatureToRender &_ftr, QgsSymbolV2 *_s)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:162
virtual int capabilities() override
returns bitwise OR-ed capabilities of the renderer
This class keeps data about a rules for rule-based renderer.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based renderer) ...
QgsExpression * filter() const
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
Rule * mRootRule
the root node with hierarchical list of rules
Contains information about the context of a rendering operation.
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features wi...
void setDescription(QString description)
QList< RenderLevel > RenderQueue
void setScaleMinDenom(int scaleMinDenom)
bool active() const
Returns if this rule is active.
void setFilterExpression(QString filterExp)
QList< FeatureToRender > mCurrentFeatures