Skip to content
Permalink
Browse files

Update QgsDataDefined for expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 2bb1c8a commit 5e03016b3ab2ea9704b9dfaf67371ff34542128b
Showing with 55 additions and 16 deletions.
  1. +17 −4 python/core/qgsdatadefined.sip
  2. +19 −7 src/core/qgsdatadefined.cpp
  3. +18 −4 src/core/qgsdatadefined.h
  4. +1 −1 tests/src/core/testqgsdatadefined.cpp
@@ -120,14 +120,21 @@ class QgsDataDefined
* @param layer vector layer
* @returns true if expression was successfully prepared
*/
bool prepareExpression( QgsVectorLayer* layer );
bool prepareExpression( QgsVectorLayer* layer ) /Deprecated/;

/** Prepares the expression using a fields collection
* @param fields
* @returns true if expression was successfully prepared
* @note added in QGIS 2.9
*/
bool prepareExpression( const QgsFields &fields = QgsFields() );
bool prepareExpression( const QgsFields &fields ) /Deprecated/;

/** Prepares the expression using an expression context.
* @param context expression context
* @returns true if expression was successfully prepared
* @note added in QGIS 2.12
*/
bool prepareExpression( const QgsExpressionContext &context = QgsExpressionContext() );

/** Returns whether the data defined object's expression is prepared
* @returns true if expression is prepared
@@ -139,13 +146,19 @@ class QgsDataDefined
/** Returns the columns referenced by the QgsDataDefined
* @param layer vector layer, used for preparing the expression if required
*/
QStringList referencedColumns( QgsVectorLayer* layer );
QStringList referencedColumns( QgsVectorLayer* layer ) /Deprecated/;

/** Returns the columns referenced by the QgsDataDefined
* @param fields vector layer, used for preparing the expression if required
* @note added in QGIS 2.9
*/
QStringList referencedColumns( const QgsFields& fields = QgsFields() );
QStringList referencedColumns( const QgsFields& fields ) /Deprecated/;

/** Returns the columns referenced by the QgsDataDefined
* @param context expression context, used for preparing the expression if required
* @note added in QGIS 2.12
*/
QStringList referencedColumns( const QgsExpressionContext& context = QgsExpressionContext() );

/**
* Get the field which this QgsDataDefined represents. Be aware that this may return
@@ -115,6 +115,7 @@ void QgsDataDefined::setUseExpression( bool use )

d.detach();
d->useExpression = use;
d->expressionPrepared = false;
d->exprRefColumns.clear();
}

@@ -156,17 +157,22 @@ bool QgsDataDefined::prepareExpression( QgsVectorLayer* layer )
{
if ( layer )
{
return prepareExpression( layer->fields() );
return prepareExpression( QgsExpressionContextUtils::createFeatureBasedContext( QgsFeature(), layer->fields() ) );
}
else
{
//preparing expression without a layer set, so pass empty field list
QgsFields empty;
//preparing expression without a layer set, so pass empty context
QgsExpressionContext empty;
return prepareExpression( empty );
}
}

bool QgsDataDefined::prepareExpression( const QgsFields &fields )
{
return prepareExpression( QgsExpressionContextUtils::createFeatureBasedContext( QgsFeature(), fields ) );
}

bool QgsDataDefined::prepareExpression( const QgsExpressionContext& context )
{
if ( !d->useExpression || d->expressionString.isEmpty() )
{
@@ -194,7 +200,7 @@ bool QgsDataDefined::prepareExpression( const QgsFields &fields )
}
}

d->expression->prepare( fields );
d->expression->prepare( &context );
d->exprRefColumns = d->expression->referencedColumns();

if ( d->expression->hasEvalError() )
@@ -227,15 +233,21 @@ QStringList QgsDataDefined::referencedColumns( QgsVectorLayer* layer )
{
if ( layer )
{
return referencedColumns( layer->fields() );
return referencedColumns( QgsExpressionContextUtils::createFeatureBasedContext( QgsFeature(), layer->fields() ) );
}
else
{
return referencedColumns( );
QgsExpressionContext empty;
return referencedColumns( empty );
}
}

QStringList QgsDataDefined::referencedColumns( const QgsFields &fields )
{
return referencedColumns( QgsExpressionContextUtils::createFeatureBasedContext( QgsFeature(), fields ) );
}

QStringList QgsDataDefined::referencedColumns( const QgsExpressionContext& context )
{
if ( !d->exprRefColumns.isEmpty() )
{
@@ -247,7 +259,7 @@ QStringList QgsDataDefined::referencedColumns( const QgsFields &fields )
{
if ( !d->expression || !d->expressionPrepared )
{
prepareExpression( fields );
prepareExpression( context );
}
}
else if ( !d->field.isEmpty() )
@@ -21,6 +21,7 @@
#include <QExplicitlySharedDataPointer>
#include "qgis.h"
#include "qgsfield.h"
#include "qgsexpressioncontext.h"

class QgsExpression;
class QgsVectorLayer;
@@ -146,14 +147,21 @@ class CORE_EXPORT QgsDataDefined
* @param layer vector layer
* @returns true if expression was successfully prepared
*/
bool prepareExpression( QgsVectorLayer* layer );
Q_DECL_DEPRECATED bool prepareExpression( QgsVectorLayer* layer );

/** Prepares the expression using a fields collection
* @param fields
* @returns true if expression was successfully prepared
* @note added in QGIS 2.9
*/
bool prepareExpression( const QgsFields &fields = QgsFields() );
Q_DECL_DEPRECATED bool prepareExpression( const QgsFields &fields );

/** Prepares the expression using an expression context.
* @param context expression context
* @returns true if expression was successfully prepared
* @note added in QGIS 2.12
*/
bool prepareExpression( const QgsExpressionContext &context = QgsExpressionContext() );

/** Returns whether the data defined object's expression is prepared
* @returns true if expression is prepared
@@ -165,13 +173,19 @@ class CORE_EXPORT QgsDataDefined
/** Returns the columns referenced by the QgsDataDefined
* @param layer vector layer, used for preparing the expression if required
*/
QStringList referencedColumns( QgsVectorLayer* layer );
Q_DECL_DEPRECATED QStringList referencedColumns( QgsVectorLayer* layer );

/** Returns the columns referenced by the QgsDataDefined
* @param fields vector layer, used for preparing the expression if required
* @note added in QGIS 2.9
*/
QStringList referencedColumns( const QgsFields& fields = QgsFields() );
Q_DECL_DEPRECATED QStringList referencedColumns( const QgsFields& fields );

/** Returns the columns referenced by the QgsDataDefined
* @param context expression context, used for preparing the expression if required
* @note added in QGIS 2.12
*/
QStringList referencedColumns( const QgsExpressionContext& context = QgsExpressionContext() );

/**
* Get the field which this QgsDataDefined represents. Be aware that this may return
@@ -95,7 +95,7 @@ void TestQgsDataDefined::create()
void TestQgsDataDefined::copy()
{
QgsDataDefined original( true, true, QString( "sqrt(2)" ), QString( "field" ) );
original.prepareExpression( NULL );
original.prepareExpression();
QgsDataDefined copy( original );
QVERIFY( copy == original );

0 comments on commit 5e03016

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