mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
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:
parent
d4319621a5
commit
0c5063fee6
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user