Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method to create a QgsDataDefined from a string.
Automatically analyses to determine whether the string refers to
a column or is an expression.
  • Loading branch information
vmora authored and nyalldawson committed Apr 27, 2015
1 parent d431962 commit 0c5063f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
17 changes: 12 additions & 5 deletions python/core/qgsdatadefined.sip
Expand Up @@ -23,20 +23,27 @@ 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
* 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
Expand Down
13 changes: 12 additions & 1 deletion src/core/qgsdatadefined.cpp
Expand Up @@ -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() : "" ) : "" )
{
Expand All @@ -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;
Expand Down
15 changes: 11 additions & 4 deletions src/core/qgsdatadefined.h
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/src/core/testqgsdatadefined.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 0c5063f

Please sign in to comment.