Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix NULL field in size expression beeing drawn
the expression generated by the assistant was causing symbol with size
expr evaluated to NULL to be drawn with default size, wich is not what
the default should be.

The generated size expression is now composed with coalesce(...,0)
so the symbol is not drawn when the size expression is NULL

The expression is still recognized without the coalesce to allow the
legend to be drawn even in the absence of coalesce: this can be used
to have the default size for NULL expression and use a conditional expr
fro color to highlight symbols where value is NULL.
  • Loading branch information
vmora authored and nyalldawson committed Jun 24, 2015
1 parent 4fb22b4 commit 61fbe1c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/core/qgsscaleexpression.cpp
Expand Up @@ -44,6 +44,8 @@ QgsScaleExpression::QgsScaleExpression( Type type, const QString& baseExpression
void QgsScaleExpression::init()
{
bool ok;
mType = Unknown;

if ( !rootNode() )
return;

Expand All @@ -53,6 +55,16 @@ void QgsScaleExpression::init()

QList<Node*> args = f->args()->list();

// the scale function may be enclosed in a coalesce(expr, 0) to avoid NULL value
// to be drawn with the default size
if ( "coalesce" == Functions()[f->fnIndex()]->name() )
{
f = dynamic_cast<const NodeFunction*>( args[0] );
if ( !f )
return;
args = f->args()->list();
}

if ( "scale_linear" == Functions()[f->fnIndex()]->name() )
{
mType = Linear;
Expand All @@ -68,13 +80,11 @@ void QgsScaleExpression::init()
mType = Area;
else
{
mType = Unknown;
return;
}
}
else
{
mType = Unknown;
return;
}

Expand Down Expand Up @@ -106,13 +116,13 @@ QString QgsScaleExpression::createExpression( Type type, const QString & baseExp
switch ( type )
{
case Linear:
return QString( "scale_linear(%1,%2,%3,%4,%5)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_linear(%1, %2, %3, %4, %5), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );

case Area:
return QString( "scale_exp(%1,%2,%3,%4,%5, 0.5)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.5), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );

case Flannery:
return QString( "scale_exp(%1,%2,%3,%4,%5, 0.57)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.57), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );

case Unknown:
break;
Expand Down
26 changes: 23 additions & 3 deletions tests/src/core/testqgsscaleexpression.cpp
Expand Up @@ -29,6 +29,26 @@ class TestQgsScaleExpression: public QObject

void parsing()
{
{
QgsScaleExpression exp( "coalesce(scale_linear(column, 1, 7, 2, 10), 0)" );
QCOMPARE( bool( exp ), true );
QCOMPARE( exp.type(), QgsScaleExpression::Linear );
QCOMPARE( exp.baseExpression(), QString( "column" ) );
QCOMPARE( exp.minValue(), 1. );
QCOMPARE( exp.maxValue(), 7. );
QCOMPARE( exp.minSize(), 2. );
QCOMPARE( exp.maxSize(), 10. );
}
{
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.5), 0)" );
QCOMPARE( bool( exp ), true );
QCOMPARE( exp.type(), QgsScaleExpression::Area );
}
{
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.57), 0)" );
QCOMPARE( bool( exp ), true );
QCOMPARE( exp.type(), QgsScaleExpression::Flannery );
}
{
QgsScaleExpression exp( "scale_linear(column, 1, 7, 2, 10)" );
QCOMPARE( bool( exp ), true );
Expand All @@ -50,17 +70,17 @@ class TestQgsScaleExpression: public QObject
QCOMPARE( exp.type(), QgsScaleExpression::Flannery );
}
{
QgsScaleExpression exp( "scale_exp(column, 1, 7, 2, 10, 0.51)" );
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, 2, 10, 0.51), 0)" );
QCOMPARE( bool( exp ), false );
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
}
{
QgsScaleExpression exp( "scale_exp(column, 1, 7, a, 10, 0.5)" );
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7, a, 10, 0.5), 0)" );
QCOMPARE( bool( exp ), false );
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
}
{
QgsScaleExpression exp( "scale_exp(column, 1, 7)" );
QgsScaleExpression exp( "coalesce(scale_exp(column, 1, 7), 0)" );
QCOMPARE( bool( exp ), false );
QCOMPARE( exp.type(), QgsScaleExpression::Unknown );
}
Expand Down

0 comments on commit 61fbe1c

Please sign in to comment.