Skip to content

Commit

Permalink
Added willRenderFeature() and symbolsForFeature() utility methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Mar 1, 2012
1 parent ccb5ed0 commit ad955e2
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 64 deletions.
115 changes: 108 additions & 7 deletions python/core/symbology-ng-core.sip
Expand Up @@ -77,6 +77,17 @@ public:

virtual QString dump();

enum Capabilities
{
SymbolLevels = 1, // rendering with symbol levels (i.e. implements symbols(), symbolForFeature())
RotationField = 2, // rotate symbols by attribute value
MoreSymbolsPerFeature = 4 // may use more than one symbol to render a feature: symbolsForFeature() will return them
};

//! returns bitwise OR-ed capabilities of the renderer
//! \note added in 2.0
virtual int capabilities();

virtual QgsFeatureRendererV2* clone()=0 /Factory/;

virtual QgsSymbolV2List symbols()=0;
Expand Down Expand Up @@ -105,6 +116,18 @@ public:
//! @note added in 1.9
virtual void setRotationField( QString fieldName );

//! return whether the renderer will render a feature or not.
//! Must be called between startRender() and stopRender() calls.
//! Default implementation uses symbolForFeature().
//! @note added in 1.9
virtual bool willRenderFeature( QgsFeature& feat );

//! return list of symbols used for rendering the feature.
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
//! to use symbolForFeature()
//! @note added in 1.9
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );

protected:
QgsFeatureRendererV2(QString type);

Expand Down Expand Up @@ -154,6 +177,10 @@ public:

virtual QString dump();

//! returns bitwise OR-ed capabilities of the renderer
//! \note added in 2.0
virtual int capabilities();

virtual QgsFeatureRendererV2* clone() /Factory/;

virtual QgsSymbolV2List symbols();
Expand Down Expand Up @@ -221,6 +248,10 @@ public:

virtual QString dump();

//! returns bitwise OR-ed capabilities of the renderer
//! \note added in 2.0
virtual int capabilities();

virtual QgsFeatureRendererV2* clone() /Factory/;

virtual QgsSymbolV2List symbols();
Expand Down Expand Up @@ -320,6 +351,10 @@ public:

virtual QString dump();

//! returns bitwise OR-ed capabilities of the renderer
//! \note added in 2.0
virtual int capabilities();

virtual QgsFeatureRendererV2* clone() /Factory/;

virtual QgsSymbolV2List symbols();
Expand Down Expand Up @@ -410,27 +445,76 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
class Rule
{
public:
//! Constructor takes ownership of the symbol
Rule( QgsSymbolV2* symbol /Transfer/, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString() );
//Rule( const QgsRuleBasedRendererV2::Rule& other );

Rule( QgsSymbolV2* symbol /Transfer/, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString(),
QString label = QString(), QString description = QString() );
~Rule();
QString dump() const;
//QStringList needsFields() const;
QString dump( int offset = 0 ) const;
QSet<QString> usedAttributes();
QgsSymbolV2List symbols();
// TODO QgsLegendSymbolList legendSymbolItems();
bool isFilterOK( QgsFeature& f ) const;
bool isScaleOK( double scale ) const;

QgsSymbolV2* symbol();
QString label() const;
bool dependsOnScale() const;
int scaleMinDenom() const;
int scaleMaxDenom() const;
QString filterExpression() const;
QgsExpression* filter() const;
QString filterExpression() const;
QString description() const;

//! set a new symbol (or NULL). Deletes old symbol.
void setSymbol( QgsSymbolV2* sym /Transfer/ );
void setLabel( QString label );
void setScaleMinDenom( int scaleMinDenom );
void setScaleMaxDenom( int scaleMaxDenom );
void setFilterExpression( QString filterExp );
void setDescription( QString description );

//! clone this rule, return new instance
QgsRuleBasedRendererV2::Rule* clone() const /Factory/;

QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );

//! prepare the rule for rendering and its children (build active children array)
bool startRender( QgsRenderContext& context, const QgsVectorLayer *vlayer );
//! get all used z-levels from this rule and children
QSet<int> collectZLevels();
//! assign normalized z-levels [0..N-1] for this rule's symbol for quick access during rendering
// TODO void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );

// TODO bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );

//! only tell whether a feature will be rendered without actually rendering it
//! @note added in 1.9
bool willRenderFeature( QgsFeature& feat );

//! tell which symbols will be used to render the feature
//! @note added in 1.9
QgsSymbolV2List symbolsForFeature( QgsFeature& feat );

void stopRender( QgsRenderContext& context );

static QgsRuleBasedRendererV2::Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap ) /Factory/;

QList<QgsRuleBasedRendererV2::Rule*>& children();
QgsRuleBasedRendererV2::Rule* parent();

//! add child rule, take ownership, sets this as parent
void appendChild( QgsRuleBasedRendererV2::Rule* rule /Transfer/ );
//! add child rule, take ownership, sets this as parent
void insertChild( int i, QgsRuleBasedRendererV2::Rule* rule /Transfer/ );
//! delete child rule
void removeChild( QgsRuleBasedRendererV2::Rule* rule );
//! delete child rule
void removeChildAt( int i );
//! take child rule out, set parent as null
void takeChild( QgsRuleBasedRendererV2::Rule* rule );
//! take child rule out, set parent as null
QgsRuleBasedRendererV2::Rule* takeChildAt( int i );

//Rule& operator=( const Rule& other );
};

/////
Expand All @@ -453,6 +537,12 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2

virtual QList<QString> usedAttributes();

virtual QString dump();

//! returns bitwise OR-ed capabilities of the renderer
//! \note added in 2.0
virtual int capabilities();

virtual QgsFeatureRendererV2* clone() /Factory/;

virtual QgsSymbolV2List symbols();
Expand All @@ -463,6 +553,17 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );

//! return whether the renderer will render a feature or not.
//! Must be called between startRender() and stopRender() calls.
//! @note added in 1.9
virtual bool willRenderFeature( QgsFeature& feat );

//! return list of symbols used for rendering the feature.
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
//! to use symbolForFeature()
//! @note added in 1.9
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );

/////


Expand Down
8 changes: 8 additions & 0 deletions src/core/symbology-ng/qgsrendererv2.cpp
Expand Up @@ -417,3 +417,11 @@ void QgsFeatureRendererV2::renderVertexMarkerPolygon( QPolygonF& pts, QList<QPol
}
}
}

QgsSymbolV2List QgsFeatureRendererV2::symbolsForFeature( QgsFeature& feat )
{
QgsSymbolV2List lst;
QgsSymbolV2* s = symbolForFeature( feat );
if ( s ) lst.append( s );
return lst;
}
13 changes: 12 additions & 1 deletion src/core/symbology-ng/qgsrendererv2.h
Expand Up @@ -81,7 +81,8 @@ class CORE_EXPORT QgsFeatureRendererV2
enum Capabilities
{
SymbolLevels = 1, // rendering with symbol levels (i.e. implements symbols(), symbolForFeature())
RotationField = 1 << 1 // rotate symbols by attribute value
RotationField = 1 << 1, // rotate symbols by attribute value
MoreSymbolsPerFeature = 1 << 2 // may use more than one symbol to render a feature: symbolsForFeature() will return them
};

//! returns bitwise OR-ed capabilities of the renderer
Expand Down Expand Up @@ -117,7 +118,17 @@ class CORE_EXPORT QgsFeatureRendererV2
//! @note added in 1.9
virtual void setRotationField( QString fieldName ) { Q_UNUSED( fieldName ); }

//! return whether the renderer will render a feature or not.
//! Must be called between startRender() and stopRender() calls.
//! Default implementation uses symbolForFeature().
//! @note added in 1.9
virtual bool willRenderFeature( QgsFeature& feat ) { return symbolForFeature( feat ) != NULL; }

//! return list of symbols used for rendering the feature.
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
//! to use symbolForFeature()
//! @note added in 1.9
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );

protected:
QgsFeatureRendererV2( QString type );
Expand Down
42 changes: 42 additions & 0 deletions src/core/symbology-ng/qgsrulebasedrendererv2.cpp
Expand Up @@ -288,6 +288,38 @@ bool QgsRuleBasedRendererV2::Rule::renderFeature( QgsRuleBasedRendererV2::Featur
return rendered;
}

bool QgsRuleBasedRendererV2::Rule::willRenderFeature( QgsFeature& feat )
{
if ( !isFilterOK( feat ) )
return false;
if ( mSymbol )
return true;

for ( QList<Rule*>::iterator it = mActiveChildren.begin(); it != mActiveChildren.end(); ++it )
{
Rule* rule = *it;
if ( rule->willRenderFeature( feat ) )
return true;
}
return false;
}

QgsSymbolV2List QgsRuleBasedRendererV2::Rule::symbolsForFeature( QgsFeature& feat )
{
QgsSymbolV2List lst;
if ( !isFilterOK( feat ) )
return lst;
if ( mSymbol )
lst.append( mSymbol );

for ( QList<Rule*>::iterator it = mActiveChildren.begin(); it != mActiveChildren.end(); ++it )
{
Rule* rule = *it;
lst += rule->symbolsForFeature( feat );
}
return lst;
}


void QgsRuleBasedRendererV2::Rule::stopRender( QgsRenderContext& context )
{
Expand Down Expand Up @@ -581,3 +613,13 @@ QString QgsRuleBasedRendererV2::dump()
msg += mRootRule->dump();
return msg;
}

bool QgsRuleBasedRendererV2::willRenderFeature( QgsFeature& feat )
{
return mRootRule->willRenderFeature( feat );
}

QgsSymbolV2List QgsRuleBasedRendererV2::symbolsForFeature( QgsFeature& feat )
{
return mRootRule->symbolsForFeature( feat );
}
23 changes: 23 additions & 0 deletions src/core/symbology-ng/qgsrulebasedrendererv2.h
Expand Up @@ -127,6 +127,14 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2

bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );

//! only tell whether a feature will be rendered without actually rendering it
//! @note added in 1.9
bool willRenderFeature( QgsFeature& feat );

//! tell which symbols will be used to render the feature
//! @note added in 1.9
QgsSymbolV2List symbolsForFeature( QgsFeature& feat );

void stopRender( QgsRenderContext& context );

static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
Expand Down Expand Up @@ -204,6 +212,21 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
//! for debugging
virtual QString dump();

//! return whether the renderer will render a feature or not.
//! Must be called between startRender() and stopRender() calls.
//! @note added in 1.9
virtual bool willRenderFeature( QgsFeature& feat );

//! return list of symbols used for rendering the feature.
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
//! to use symbolForFeature()
//! @note added in 1.9
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );

//! returns bitwise OR-ed capabilities of the renderer
//! \note added in 2.0
virtual int capabilities() { return MoreSymbolsPerFeature; }

/////

Rule* rootRule() { return mRootRule; }
Expand Down

0 comments on commit ad955e2

Please sign in to comment.