Skip to content

Commit 0c5063f

Browse files
vmoranyalldawson
authored andcommitted
Add method to create a QgsDataDefined from a string.
Automatically analyses to determine whether the string refers to a column or is an expression.
1 parent d431962 commit 0c5063f

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

python/core/qgsdatadefined.sip

+12-5
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,27 @@ class QgsDataDefined
2323
const QString& field = QString() );
2424

2525
/**
26-
* Construct a new data defined object, analyse the expression to determine
27-
* if it's a simple field
28-
*
26+
* Construct a new data defined object, analysing the expression to determine
27+
* if it's a simple field reference or an expression.
2928
* @param expression can be null
3029
*/
31-
QgsDataDefined( const QgsExpression * expression );
30+
explicit QgsDataDefined( const QgsExpression * expression );
31+
32+
/**
33+
* Construct a new data defined object, analysing the string to determine
34+
* if it's a simple field reference or an expression
35+
* @param string field reference or an expression, can be empty
36+
* @note added in QGIS 2.9
37+
*/
38+
explicit QgsDataDefined( const QString& string );
3239

3340
/**
3441
* Copy constructor. Note that copies of data defined objects with expressions
3542
* will not be prepared.
3643
*/
3744
QgsDataDefined( const QgsDataDefined& other );
3845

39-
~QgsDataDefined();
46+
virtual ~QgsDataDefined();
4047

4148
/**Returns whether the data defined container is set to all the default
4249
* values, ie, disabled, with empty expression and no assigned field

src/core/qgsdatadefined.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ QgsDataDefined::QgsDataDefined( bool active,
3535

3636
QgsDataDefined::QgsDataDefined( const QgsExpression * expression )
3737
: mActive( bool( expression ) )
38-
, mUseExpression( expression && expression->rootNode() && !dynamic_cast<const QgsExpression::NodeColumnRef*>( expression->rootNode() ) )
38+
, mUseExpression( expression && ! expression->isField() )
3939
, mExpressionString( mUseExpression ? expression->expression() : "" )
4040
, mField( !mUseExpression ? ( expression ? expression->expression() : "" ) : "" )
4141
{
@@ -55,6 +55,17 @@ QgsDataDefined::QgsDataDefined( const QgsDataDefined &other )
5555

5656
}
5757

58+
QgsDataDefined::QgsDataDefined( const QString & string )
59+
{
60+
QgsExpression expression( string );
61+
mActive = expression.rootNode();
62+
mUseExpression = mActive && ! expression.isField();
63+
mExpressionString = mUseExpression ? expression.expression() : QString();
64+
mField = expression.isField() ? expression.rootNode()->dump() : QString();
65+
mExpression = 0;
66+
mExpressionPrepared = false;
67+
}
68+
5869
QgsDataDefined::~QgsDataDefined()
5970
{
6071
delete mExpression;

src/core/qgsdatadefined.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,27 @@ class CORE_EXPORT QgsDataDefined
4545
const QString& field = QString() );
4646

4747
/**
48-
* Construct a new data defined object, analyse the expression to determine
49-
* if it's a simple field
50-
*
48+
* Construct a new data defined object, analysing the expression to determine
49+
* if it's a simple field reference or an expression.
5150
* @param expression can be null
5251
*/
5352
explicit QgsDataDefined( const QgsExpression * expression );
5453

54+
/**
55+
* Construct a new data defined object, analysing the string to determine
56+
* if it's a simple field reference or an expression
57+
* @param string field reference or an expression, can be empty
58+
* @note added in QGIS 2.9
59+
*/
60+
explicit QgsDataDefined( const QString& string );
61+
5562
/**
5663
* Copy constructor. Note that copies of data defined objects with expressions
5764
* will not be prepared.
5865
*/
5966
QgsDataDefined( const QgsDataDefined& other );
6067

61-
~QgsDataDefined();
68+
virtual ~QgsDataDefined();
6269

6370
/**Returns whether the data defined container is set to all the default
6471
* values, ie, disabled, with empty expression and no assigned field

tests/src/core/testqgsdatadefined.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ void TestQgsDataDefined::create()
7070
QVERIFY( dd->useExpression() );
7171
QCOMPARE( dd->expressionString(), QString( "exp" ) );
7272
QCOMPARE( dd->field(), QString( "field" ) );
73+
74+
//test with string constructor
75+
QScopedPointer<QgsDataDefined> stringConstructorField( new QgsDataDefined( QString( "\"col1\"" ) ) );
76+
QVERIFY( stringConstructorField->isActive() );
77+
QVERIFY( ! stringConstructorField->useExpression() );
78+
QVERIFY( stringConstructorField->expressionString().isEmpty() );
79+
QCOMPARE( stringConstructorField->field(), QString( "col1" ) );
80+
81+
QScopedPointer<QgsDataDefined> stringConstructorExp( new QgsDataDefined( QString( "1 + 2" ) ) );
82+
QVERIFY( stringConstructorExp->isActive() );
83+
QVERIFY( stringConstructorExp->useExpression() );
84+
QCOMPARE( stringConstructorExp->expressionString(), QString( "1 + 2" ) );
85+
QVERIFY( stringConstructorExp->field().isEmpty() );
86+
87+
QScopedPointer<QgsDataDefined> stringConstructorEmpty( new QgsDataDefined( QString( "" ) ) );
88+
QVERIFY( ! stringConstructorEmpty->isActive() );
7389
}
7490

7591
void TestQgsDataDefined::copy()

0 commit comments

Comments
 (0)