Skip to content
Permalink
Browse files

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 0c5063fee6dc8c62a31185fec836f81ff3957d8f
Showing with 51 additions and 10 deletions.
  1. +12 −5 python/core/qgsdatadefined.sip
  2. +12 −1 src/core/qgsdatadefined.cpp
  3. +11 −4 src/core/qgsdatadefined.h
  4. +16 −0 tests/src/core/testqgsdatadefined.cpp
@@ -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
@@ -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;
@@ -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
@@ -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()

0 comments on commit 0c5063f

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