Add method to create a QgsDataDefined from a string.

Automatically analyses to determine whether the string refers to
a column or is an expression.
This commit is contained in:
vmora 2015-04-27 09:45:13 +10:00 committed by Nyall Dawson
parent d4319621a5
commit 0c5063fee6
4 changed files with 51 additions and 10 deletions

View File

@ -23,12 +23,19 @@ class QgsDataDefined
const QString& field = QString() );
/**
* Construct a new data defined object, analyse the expression to determine
* if it's a simple field
*
* Construct a new data defined object, analysing the expression to determine
* if it's a simple field reference or an expression.
* @param expression can be null
*/
QgsDataDefined( const QgsExpression * expression );
explicit QgsDataDefined( const QgsExpression * expression );
/**
* Construct a new data defined object, analysing the string to determine
* if it's a simple field reference or an expression
* @param string field reference or an expression, can be empty
* @note added in QGIS 2.9
*/
explicit QgsDataDefined( const QString& string );
/**
* Copy constructor. Note that copies of data defined objects with expressions
@ -36,7 +43,7 @@ class QgsDataDefined
*/
QgsDataDefined( const QgsDataDefined& other );
~QgsDataDefined();
virtual ~QgsDataDefined();
/**Returns whether the data defined container is set to all the default
* values, ie, disabled, with empty expression and no assigned field

View File

@ -35,7 +35,7 @@ QgsDataDefined::QgsDataDefined( bool active,
QgsDataDefined::QgsDataDefined( const QgsExpression * expression )
: mActive( bool( expression ) )
, mUseExpression( expression && expression->rootNode() && !dynamic_cast<const QgsExpression::NodeColumnRef*>( expression->rootNode() ) )
, mUseExpression( expression && ! expression->isField() )
, mExpressionString( mUseExpression ? expression->expression() : "" )
, mField( !mUseExpression ? ( expression ? expression->expression() : "" ) : "" )
{
@ -55,6 +55,17 @@ QgsDataDefined::QgsDataDefined( const QgsDataDefined &other )
}
QgsDataDefined::QgsDataDefined( const QString & string )
{
QgsExpression expression( string );
mActive = expression.rootNode();
mUseExpression = mActive && ! expression.isField();
mExpressionString = mUseExpression ? expression.expression() : QString();
mField = expression.isField() ? expression.rootNode()->dump() : QString();
mExpression = 0;
mExpressionPrepared = false;
}
QgsDataDefined::~QgsDataDefined()
{
delete mExpression;

View File

@ -45,20 +45,27 @@ class CORE_EXPORT QgsDataDefined
const QString& field = QString() );
/**
* Construct a new data defined object, analyse the expression to determine
* if it's a simple field
*
* Construct a new data defined object, analysing the expression to determine
* if it's a simple field reference or an expression.
* @param expression can be null
*/
explicit QgsDataDefined( const QgsExpression * expression );
/**
* Construct a new data defined object, analysing the string to determine
* if it's a simple field reference or an expression
* @param string field reference or an expression, can be empty
* @note added in QGIS 2.9
*/
explicit QgsDataDefined( const QString& string );
/**
* Copy constructor. Note that copies of data defined objects with expressions
* will not be prepared.
*/
QgsDataDefined( const QgsDataDefined& other );
~QgsDataDefined();
virtual ~QgsDataDefined();
/**Returns whether the data defined container is set to all the default
* values, ie, disabled, with empty expression and no assigned field

View File

@ -70,6 +70,22 @@ void TestQgsDataDefined::create()
QVERIFY( dd->useExpression() );
QCOMPARE( dd->expressionString(), QString( "exp" ) );
QCOMPARE( dd->field(), QString( "field" ) );
//test with string constructor
QScopedPointer<QgsDataDefined> stringConstructorField( new QgsDataDefined( QString( "\"col1\"" ) ) );
QVERIFY( stringConstructorField->isActive() );
QVERIFY( ! stringConstructorField->useExpression() );
QVERIFY( stringConstructorField->expressionString().isEmpty() );
QCOMPARE( stringConstructorField->field(), QString( "col1" ) );
QScopedPointer<QgsDataDefined> stringConstructorExp( new QgsDataDefined( QString( "1 + 2" ) ) );
QVERIFY( stringConstructorExp->isActive() );
QVERIFY( stringConstructorExp->useExpression() );
QCOMPARE( stringConstructorExp->expressionString(), QString( "1 + 2" ) );
QVERIFY( stringConstructorExp->field().isEmpty() );
QScopedPointer<QgsDataDefined> stringConstructorEmpty( new QgsDataDefined( QString( "" ) ) );
QVERIFY( ! stringConstructorEmpty->isActive() );
}
void TestQgsDataDefined::copy()