Skip to content

Commit

Permalink
Update QgsDataDefined for expression contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 2bb1c8a commit 5e03016
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
21 changes: 17 additions & 4 deletions python/core/qgsdatadefined.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 19 additions & 7 deletions src/core/qgsdatadefined.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void QgsDataDefined::setUseExpression( bool use )

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

Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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() )
{
Expand All @@ -247,7 +259,7 @@ QStringList QgsDataDefined::referencedColumns( const QgsFields &fields )
{
if ( !d->expression || !d->expressionPrepared )
{
prepareExpression( fields );
prepareExpression( context );
}
}
else if ( !d->field.isEmpty() )
Expand Down
22 changes: 18 additions & 4 deletions src/core/qgsdatadefined.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QExplicitlySharedDataPointer>
#include "qgis.h"
#include "qgsfield.h"
#include "qgsexpressioncontext.h"

class QgsExpression;
class QgsVectorLayer;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsdatadefined.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down

0 comments on commit 5e03016

Please sign in to comment.