Skip to content
Permalink
Browse files

Add ELSE to rule based renderer

  • Loading branch information
NathanW2 committed Sep 12, 2013
1 parent ebc1573 commit 02ed712b03de18a3c26fbac485b7bc6791c7d7cd
@@ -29,11 +29,11 @@



QgsRuleBasedRendererV2::Rule::Rule( QgsSymbolV2* symbol, int scaleMinDenom, int scaleMaxDenom, QString filterExp, QString label, QString description )
QgsRuleBasedRendererV2::Rule::Rule(QgsSymbolV2* symbol, int scaleMinDenom, int scaleMaxDenom, QString filterExp, QString label, QString description , bool elseRule)
: mParent( NULL ), mSymbol( symbol ),
mScaleMinDenom( scaleMinDenom ), mScaleMaxDenom( scaleMaxDenom ),
mFilterExp( filterExp ), mLabel( label ), mDescription( description ),
mFilter( NULL )
mFilter( NULL ), mElseRule( elseRule )
{
initFilter();
}
@@ -48,7 +48,12 @@ QgsRuleBasedRendererV2::Rule::~Rule()

void QgsRuleBasedRendererV2::Rule::initFilter()
{
if ( !mFilterExp.isEmpty() )
if ( mElseRule || mFilterExp.compare("ELSE", Qt::CaseInsensitive) == 0 )
{
mElseRule = true;
mFilter = NULL;
}
else if ( !mFilterExp.isEmpty() )
{
delete mFilter;
mFilter = new QgsExpression( mFilterExp );
@@ -132,7 +137,7 @@ QgsLegendSymbolList QgsRuleBasedRendererV2::Rule::legendSymbolItems()

bool QgsRuleBasedRendererV2::Rule::isFilterOK( QgsFeature& f ) const
{
if ( ! mFilter )
if ( ! mFilter || mElseRule )
return true;

QVariant res = mFilter->evaluate( &f );
@@ -373,12 +378,31 @@ bool QgsRuleBasedRendererV2::Rule::renderFeature( QgsRuleBasedRendererV2::Featur
rendered = true;
}

QList<Rule*> elserules;
bool willrendersomething = false;

// process children
for ( QList<Rule*>::iterator it = mActiveChildren.begin(); it != mActiveChildren.end(); ++it )
{
Rule* rule = *it;
rendered |= rule->renderFeature( featToRender, context, renderQueue );
if ( rule->isElse())
{
elserules << rule;
continue;
}
willrendersomething |= rule->renderFeature( featToRender, context, renderQueue );
rendered |= willrendersomething;
}

// If none of the rules passed then we jump into the else rules and process them.
if ( !willrendersomething )
{
foreach(Rule* rule, elserules)
{
rendered |= rule->renderFeature( featToRender, context, renderQueue );
}
}

return rendered;
}

@@ -86,7 +86,7 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
public:
//! Constructor takes ownership of the symbol
Rule( QgsSymbolV2* symbol, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString(),
QString label = QString(), QString description = QString() );
QString label = QString(), QString description = QString(), bool elseRule = false );
//Rule( const Rule& other );
~Rule();
QString dump( int offset = 0 ) const;
@@ -164,6 +164,9 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
//! take child rule out, set parent as null
Rule* takeChildAt( int i ) { Rule* rule = mChildren.takeAt( i ); rule->mParent = NULL; return rule; }

void setIsElse( bool iselse ) { mElseRule = iselse; }
bool isElse() { return mElseRule; }

protected:
void initFilter();

@@ -636,7 +636,15 @@ QVariant QgsRuleBasedRendererV2Model::data( const QModelIndex &index, int role )
switch ( index.column() )
{
case 0: return rule->label();
case 1: return rule->filterExpression().isEmpty() ? tr( "(no filter)" ) : rule->filterExpression();
case 1:
if ( rule->isElse() )
{
return "ELSE";
}
else
{
return rule->filterExpression().isEmpty() ? tr( "(no filter)" ) : rule->filterExpression();
}
case 2: return rule->dependsOnScale() ? _formatScale( rule->scaleMinDenom() ) : QVariant();
case 3: return rule->dependsOnScale() ? _formatScale( rule->scaleMaxDenom() ) : QVariant();
case 4:
@@ -683,6 +691,16 @@ QVariant QgsRuleBasedRendererV2Model::data( const QModelIndex &index, int role )
{
return ( index.column() == 2 || index.column() == 3 ) ? Qt::AlignRight : Qt::AlignLeft;
}
else if (role == Qt::FontRole && index.column() == 1)
{
if ( rule->isElse() )
{
QFont italicFont;
italicFont.setItalic(true);
return italicFont;
}
return QVariant();
}
else if ( role == Qt::EditRole )
{
switch ( index.column() )

0 comments on commit 02ed712

Please sign in to comment.
You can’t perform that action at this time.