Skip to content
Permalink
Browse files

Port some GUI classes to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 89a362c commit fdfb804580429a9a79b47c4a901174f44ecc2ef0
@@ -263,7 +263,12 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
if ( mReadOnlySelector )
{
QgsExpression expr( mReferencedLayer->displayExpression() );
QString title = expr.evaluate( &mFeature ).toString();
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mReferencedLayer );
context.setFeature( mFeature );
QString title = expr.evaluate( &context ).toString();
if ( expr.hasEvalError() )
{
title = mFeature.attribute( mFkeyFieldIdx ).toString();
@@ -688,7 +693,12 @@ void QgsRelationReferenceWidget::featureIdentified( const QgsFeature& feature )
if ( mReadOnlySelector )
{
QgsExpression expr( mReferencedLayer->displayExpression() );
QString title = expr.evaluate( &feature ).toString();
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mReferencedLayer );
context.setFeature( feature );
QString title = expr.evaluate( &context ).toString();
if ( expr.hasEvalError() )
{
title = feature.attribute( mFkeyFieldIdx ).toString();
@@ -217,11 +217,16 @@ QgsValueRelationWidgetWrapper::ValueRelationCache QgsValueRelationWidgetWrapper:
int ki = layer->fieldNameIndex( config.value( "Key" ).toString() );
int vi = layer->fieldNameIndex( config.value( "Value" ).toString() );

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( layer );

QgsExpression *e = 0;
if ( !config.value( "FilterExpression" ).toString().isEmpty() )
{
e = new QgsExpression( config.value( "FilterExpression" ).toString() );
if ( e->hasParserError() || !e->prepare( layer->fields() ) )
if ( e->hasParserError() || !e->prepare( &context ) )
ki = -1;
}

@@ -263,7 +268,8 @@ QgsValueRelationWidgetWrapper::ValueRelationCache QgsValueRelationWidgetWrapper:
QgsFeature f;
while ( fit.nextFeature( f ) )
{
if ( e && !e->evaluate( &f ).toBool() )
context.setFeature( f );
if ( e && !e->evaluate( &context ).toBool() )
continue;

cache.append( ValueRelationItem( f.attribute( ki ), f.attribute( vi ).toString() ) );
@@ -56,6 +56,10 @@ QgsFieldExpressionWidget::QgsFieldExpressionWidget( QWidget *parent )
connect( mButton, SIGNAL( clicked() ), this, SLOT( editExpression() ) );
// NW TODO - Fix in 2.6
// connect( mCombo->lineEdit(), SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );

mExpressionContext.reset( new QgsExpressionContext() );
mExpressionContext->appendScope( QgsExpressionContextUtils::globalScope() );
mExpressionContext->appendScope( QgsExpressionContextUtils::projectScope() );
}

void QgsFieldExpressionWidget::setExpressionDialogTitle( QString title )
@@ -96,8 +100,7 @@ QString QgsFieldExpressionWidget::currentText() const
bool QgsFieldExpressionWidget::isValidExpression( QString *expressionError ) const
{
QString temp;
QgsVectorLayer* vl = layer();
return QgsExpression::isValid( currentText(), vl ? vl->fields() : QgsFields(), expressionError ? *expressionError : temp );
return QgsExpression::isValid( currentText(), mExpressionContext.data(), expressionError ? *expressionError : temp );
}

bool QgsFieldExpressionWidget::isExpression() const
@@ -135,6 +138,12 @@ void QgsFieldExpressionWidget::setLayer( QgsMapLayer *layer )

void QgsFieldExpressionWidget::setLayer( QgsVectorLayer *layer )
{
mExpressionContext.reset( new QgsExpressionContext() );
mExpressionContext->appendScope( QgsExpressionContextUtils::globalScope() );
mExpressionContext->appendScope( QgsExpressionContextUtils::projectScope() );
if ( layer )
mExpressionContext->appendScope( QgsExpressionContextUtils::layerScope( layer ) );

mFieldProxyModel->sourceFieldModel()->setLayer( layer );
}

@@ -269,9 +278,7 @@ void QgsFieldExpressionWidget::updateLineEditStyle( const QString expression )

bool QgsFieldExpressionWidget::isExpressionValid( const QString expressionStr )
{
QgsVectorLayer* vl = layer();

QgsExpression expression( expressionStr );
expression.prepare( vl ? vl->fields() : QgsFields() );
expression.prepare( mExpressionContext.data() );
return !expression.hasParserError();
}
@@ -137,6 +137,7 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
QgsFieldProxyModel* mFieldProxyModel;
QString mExpressionDialogTitle;
QSharedPointer<const QgsDistanceArea> mDa;
QScopedPointer< QgsExpressionContext > mExpressionContext;
};

#endif // QGSFIELDEXPRESSIONWIDGET_H
@@ -199,9 +199,13 @@ long QgsSearchQueryBuilder::countRecords( QString searchString )

int count = 0;
QgsFeature feat;
const QgsFields& fields = mLayer->fields();

if ( !search.prepare( fields ) )
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

if ( !search.prepare( &context ) )
{
QMessageBox::critical( this, tr( "Evaluation error" ), search.evalErrorString() );
return -1;
@@ -213,7 +217,8 @@ long QgsSearchQueryBuilder::countRecords( QString searchString )

while ( fit.nextFeature( feat ) )
{
QVariant value = search.evaluate( &feat );
context.setFeature( feat );
QVariant value = search.evaluate( &context );
if ( value.toInt() != 0 )
{
count++;
@@ -207,7 +207,13 @@ void QgsSizeScaleWidget::computeFromLayerTriggered()
return;

QgsExpression expression( mExpressionWidget->currentField() );
if ( ! expression.prepare( mLayer->fields() ) )

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

if ( ! expression.prepare( &context ) )
return;

QStringList lst( expression.referencedColumns() );
@@ -225,7 +231,8 @@ void QgsSizeScaleWidget::computeFromLayerTriggered()
while ( fit.nextFeature( f ) )
{
bool ok;
const double value = expression.evaluate( f ).toDouble( &ok );
context.setFeature( f );
const double value = expression.evaluate( &context ).toDouble( &ok );
if ( ok )
{
max = qMax( max, value );

0 comments on commit fdfb804

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