Skip to content
Permalink
Browse files

Fix referenced columns from QgsDataDefined so that columns are

returned even if expression was not sucessfully parsed (column
names are still inferred during the expression preparation).
  • Loading branch information
nyalldawson committed May 6, 2015
1 parent 6ad2f9f commit ad7fad513d302cdebd86355c837d1f0864135c6a
Showing with 60 additions and 3 deletions.
  1. +14 −1 src/core/qgsdatadefined.cpp
  2. +2 −2 src/core/qgsdatadefined.h
  3. +44 −0 tests/src/core/testqgsdatadefined.cpp
@@ -98,10 +98,17 @@ bool QgsDataDefined::hasDefaultValues() const
return ( !mActive && !mUseExpression && mExpressionString.isEmpty() && mField.isEmpty() );
}

void QgsDataDefined::setUseExpression( bool use )
{
mUseExpression = use;
mExprRefColumns.clear();
}

void QgsDataDefined::setExpressionString( const QString &expr )
{
mExpressionString = expr;
mExpressionPrepared = false;
mExprRefColumns.clear();
}

bool QgsDataDefined::prepareExpression( QgsVectorLayer* layer )
@@ -145,6 +152,7 @@ bool QgsDataDefined::prepareExpression( const QgsFields &fields )
}

mExpression->prepare( fields );
mExprRefColumns = mExpression->referencedColumns();

if ( mExpression->hasEvalError() )
{
@@ -153,7 +161,6 @@ bool QgsDataDefined::prepareExpression( const QgsFields &fields )
}

mExpressionPrepared = true;
mExprRefColumns = mExpression->referencedColumns();

return true;
}
@@ -192,6 +199,12 @@ QStringList QgsDataDefined::referencedColumns( const QgsFields &fields )
return mExprRefColumns;
}

void QgsDataDefined::setField( const QString &field )
{
mField = field;
mExprRefColumns.clear();
}

void QgsDataDefined::insertExpressionParam( QString key, QVariant param )
{
mExpressionParams.insert( key, param );
@@ -88,7 +88,7 @@ class CORE_EXPORT QgsDataDefined
void setActive( bool active ) { mActive = active; }

bool useExpression() const { return mUseExpression; }
void setUseExpression( bool use ) { mUseExpression = use; }
void setUseExpression( bool use );

QString expressionString() const { return mExpressionString; }
void setExpressionString( const QString& expr );
@@ -131,7 +131,7 @@ class CORE_EXPORT QgsDataDefined
QStringList referencedColumns( const QgsFields& fields = QgsFields() );

QString field() const { return mField; }
void setField( const QString& field ) { mField = field; }
void setField( const QString& field );

/** Encodes the QgsDataDefined into a string map.
* @param baseName optional base name for values in the string map. Can be used
@@ -40,6 +40,7 @@ class TestQgsDataDefined: public QObject
void equality(); //test equality operators
void xmlMethods(); //test saving and reading from xml
void mapMethods(); //test saving and reading from a string map
void referencedColumns(); //test referenced columns method

private:
};
@@ -249,5 +250,48 @@ void TestQgsDataDefined::mapMethods()
delete dd4;
}

void TestQgsDataDefined::referencedColumns()
{
QgsDataDefined dd;
dd.setActive( true );
dd.setUseExpression( true );

QStringList cols = dd.referencedColumns();
QVERIFY( cols.isEmpty() );

//set as expression
dd.setExpressionString( "1+col1+col2" );
cols = dd.referencedColumns();
QCOMPARE( cols.length(), 2 );
QVERIFY( cols.contains( QString( "col1" ) ) );
QVERIFY( cols.contains( QString( "col2" ) ) );

//alter expression and check that referenced columns is updated
dd.setExpressionString( "1+col1+col2+col3" );
cols = dd.referencedColumns();
QCOMPARE( cols.length(), 3 );
QVERIFY( cols.contains( QString( "col1" ) ) );
QVERIFY( cols.contains( QString( "col2" ) ) );
QVERIFY( cols.contains( QString( "col3" ) ) );

//switch to field
dd.setUseExpression( false );
cols = dd.referencedColumns();
QVERIFY( cols.isEmpty() );

dd.setField( "field" );
cols = dd.referencedColumns();
QCOMPARE( cols.length(), 1 );
QVERIFY( cols.contains( QString( "field" ) ) );

//switch back to expression
dd.setUseExpression( true );
cols = dd.referencedColumns();
QCOMPARE( cols.length(), 3 );
QVERIFY( cols.contains( QString( "col1" ) ) );
QVERIFY( cols.contains( QString( "col2" ) ) );
QVERIFY( cols.contains( QString( "col3" ) ) );
}

QTEST_MAIN( TestQgsDataDefined )
#include "testqgsdatadefined.moc"

0 comments on commit ad7fad5

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