QGIS API Documentation  master-3f58142
src/core/symbology-ng/qgsrulebasedrendererv2.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsrulebasedrendererv2.h - Rule-based renderer (symbology-ng)
00003     ---------------------
00004     begin                : May 2010
00005     copyright            : (C) 2010 by Martin Dobias
00006     email                : wonder dot sk at gmail dot com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #ifndef QGSRULEBASEDRENDERERV2_H
00017 #define QGSRULEBASEDRENDERERV2_H
00018 
00019 #include "qgsfield.h"
00020 #include "qgsfeature.h"
00021 #include "qgis.h"
00022 
00023 #include "qgsrendererv2.h"
00024 
00025 class QgsExpression;
00026 
00027 class QgsCategorizedSymbolRendererV2;
00028 class QgsGraduatedSymbolRendererV2;
00029 
00034 class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
00035 {
00036   public:
00037 
00038 
00039     // TODO: use QVarLengthArray instead of QList
00040 
00041     enum FeatureFlags { FeatIsSelected = 1, FeatDrawMarkers = 2 };
00042 
00043     // feature for rendering: QgsFeature and some flags
00044     struct FeatureToRender
00045     {
00046       FeatureToRender( QgsFeature& _f, int _flags ) : feat( _f ), flags( _flags ) {}
00047       QgsFeature feat;
00048       int flags; // selected and/or draw markers
00049     };
00050 
00051     // rendering job: a feature to be rendered with a particular symbol
00052     // (both f, symbol are _not_ owned by this class)
00053     struct RenderJob
00054     {
00055       RenderJob( FeatureToRender& _ftr, QgsSymbolV2* _s ) : ftr( _ftr ), symbol( _s ) {}
00056       FeatureToRender& ftr;
00057       QgsSymbolV2* symbol;
00058     };
00059 
00060     // render level: a list of jobs to be drawn at particular level
00061     // (jobs are owned by this class)
00062     struct RenderLevel
00063     {
00064       RenderLevel( int z ): zIndex( z ) {}
00065       ~RenderLevel() { foreach ( RenderJob* j, jobs ) delete j; }
00066       int zIndex;
00067       QList<RenderJob*> jobs;
00068     };
00069 
00070     // rendering queue: a list of rendering levels
00071     typedef QList<RenderLevel> RenderQueue;
00072 
00073     class Rule;
00074     typedef QList<Rule*> RuleList;
00075 
00084     class CORE_EXPORT Rule
00085     {
00086       public:
00088         Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString(),
00089               QString label = QString(), QString description = QString() );
00090         //Rule( const Rule& other );
00091         ~Rule();
00092         QString dump( int offset = 0 ) const;
00093         QSet<QString> usedAttributes();
00094         QgsSymbolV2List symbols();
00096         QgsLegendSymbolList legendSymbolItems();
00097         bool isFilterOK( QgsFeature& f ) const;
00098         bool isScaleOK( double scale ) const;
00099 
00100         QgsSymbolV2* symbol() { return mSymbol; }
00101         QString label() const { return mLabel; }
00102         bool dependsOnScale() const { return mScaleMinDenom != 0 || mScaleMaxDenom != 0; }
00103         int scaleMinDenom() const { return mScaleMinDenom; }
00104         int scaleMaxDenom() const { return mScaleMaxDenom; }
00105         QgsExpression* filter() const { return mFilter; }
00106         QString filterExpression() const { return mFilterExp; }
00107         QString description() const { return mDescription; }
00108 
00110         void setSymbol( QgsSymbolV2* sym );
00111         void setLabel( QString label ) { mLabel = label; }
00112         void setScaleMinDenom( int scaleMinDenom ) { mScaleMinDenom = scaleMinDenom; }
00113         void setScaleMaxDenom( int scaleMaxDenom ) { mScaleMaxDenom = scaleMaxDenom; }
00114         void setFilterExpression( QString filterExp ) { mFilterExp = filterExp; initFilter(); }
00115         void setDescription( QString description ) { mDescription = description; }
00116 
00118         Rule* clone() const;
00119 
00120         void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props );
00121         static Rule* createFromSld( QDomElement& element, QGis::GeometryType geomType );
00122 
00123         QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );
00124 
00126         bool startRender( QgsRenderContext& context, const QgsVectorLayer *vlayer );
00128         QSet<int> collectZLevels();
00131         void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
00132 
00133         bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
00134 
00137         bool willRenderFeature( QgsFeature& feat );
00138 
00141         QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
00142 
00144         RuleList rulesForFeature( QgsFeature& feat );
00145 
00146         void stopRender( QgsRenderContext& context );
00147 
00148         static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
00149 
00150         RuleList& children() { return mChildren; }
00151         RuleList descendants() const { RuleList l; foreach ( Rule *c, mChildren ) { l += c; l += c->children(); } return l; }
00152         Rule* parent() { return mParent; }
00153 
00155         void appendChild( Rule* rule ) { mChildren.append( rule ); rule->mParent = this; }
00157         void insertChild( int i, Rule* rule ) { mChildren.insert( i, rule ); rule->mParent = this; }
00159         void removeChild( Rule* rule ) { mChildren.removeAll( rule ); delete rule; }
00161         void removeChildAt( int i ) { Rule* rule = mChildren[i]; mChildren.removeAt( i ); delete rule; }
00163         void takeChild( Rule* rule ) { mChildren.removeAll( rule ); rule->mParent = NULL; }
00165         Rule* takeChildAt( int i ) { Rule* rule = mChildren.takeAt( i ); rule->mParent = NULL; return rule; }
00166 
00167       protected:
00168         void initFilter();
00169 
00170         Rule* mParent; // parent rule (NULL only for root rule)
00171         QgsSymbolV2* mSymbol;
00172         int mScaleMinDenom, mScaleMaxDenom;
00173         QString mFilterExp, mLabel, mDescription;
00174         bool mElseRule;
00175         RuleList mChildren;
00176 
00177         // temporary
00178         QgsExpression* mFilter;
00179         // temporary while rendering
00180         QList<int> mSymbolNormZLevels;
00181         RuleList mActiveChildren;
00182     };
00183 
00185 
00186     static QgsFeatureRendererV2* create( QDomElement& element );
00187 
00189     QgsRuleBasedRendererV2( QgsRuleBasedRendererV2::Rule* root );
00191     QgsRuleBasedRendererV2( QgsSymbolV2* defaultSymbol );
00192 
00193     ~QgsRuleBasedRendererV2();
00194 
00196     virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
00197 
00198     virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );
00199 
00200     virtual void startRender( QgsRenderContext& context, const QgsVectorLayer *vlayer );
00201 
00202     virtual void stopRender( QgsRenderContext& context );
00203 
00204     virtual QList<QString> usedAttributes();
00205 
00206     virtual QgsFeatureRendererV2* clone();
00207 
00208     virtual void toSld( QDomDocument& doc, QDomElement &element ) const;
00209 
00210     static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
00211 
00212     virtual QgsSymbolV2List symbols();
00213 
00215     virtual QDomElement save( QDomDocument& doc );
00216 
00218     virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );
00219 
00223     virtual QgsLegendSymbolList legendSymbolItems();
00224 
00226     virtual QString dump();
00227 
00231     virtual bool willRenderFeature( QgsFeature& feat );
00232 
00237     virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
00238 
00241     virtual int capabilities() { return MoreSymbolsPerFeature | Filter | ScaleDependent; }
00242 
00244 
00245     Rule* rootRule() { return mRootRule; }
00246 
00248 
00250     static void refineRuleCategories( Rule* initialRule, QgsCategorizedSymbolRendererV2* r );
00252     static void refineRuleRanges( Rule* initialRule, QgsGraduatedSymbolRendererV2* r );
00254     static void refineRuleScales( Rule* initialRule, QList<int> scales );
00255 
00256   protected:
00258     Rule* mRootRule;
00259 
00260     // temporary
00261     RenderQueue mRenderQueue;
00262     QList<FeatureToRender> mCurrentFeatures;
00263 };
00264 
00265 #endif // QGSRULEBASEDRENDERERV2_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines