Skip to content
Permalink
Browse files

More misc porting to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 000a62f commit cc6feae8355c3f42e6f0ccd68ce06e36378b0579
@@ -365,6 +365,11 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin

int rownum = 1;

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

QgsField fld = layer->fields()[ fieldindex ];

//go through all the features and change the new attributes
@@ -377,8 +382,10 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
continue;
}

exp.setCurrentRowNumber( rownum );
QVariant value = exp.evaluate( &feature );
context.setFeature( feature );
context.lastScope()->setVariable( QString( "_rownum_" ), rownum );

QVariant value = exp.evaluate( &context );
fld.convertCompatible( value );
// Bail if we have a update error
if ( exp.hasEvalError() )
@@ -768,7 +775,12 @@ void QgsAttributeTableDialog::setFilterExpression( QString filterString )
return;
}

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

if ( ! filterExpression.prepare( &context ) )
{
QgisApp::instance()->messageBar()->pushMessage( tr( "Evaluation error" ), filterExpression.evalErrorString(), QgsMessageBar::WARNING, QgisApp::instance()->messageTimeout() );
}
@@ -790,7 +802,8 @@ void QgsAttributeTableDialog::setFilterExpression( QString filterString )

while ( featIt.nextFeature( f ) )
{
if ( filterExpression.evaluate( &f ).toInt() != 0 )
context.setFeature( f );
if ( filterExpression.evaluate( &context ).toInt() != 0 )
filteredFeatures << f.id();

// check if there were errors during evaluating
@@ -204,6 +204,13 @@ void QgsLabelPropertyDialog::setDataDefinedValues( const QgsPalLayerSettings &la
//loop through data defined properties and set all the GUI widget values. We can do this
//even if the data defined property is set to an expression, as it's useful to show
//users what the evaluated property is...

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( vlayer );
context.setFeature( mCurLabelFeat );

QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* >::const_iterator propIt = mDataDefinedProperties.constBegin();
for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt )
{
@@ -215,11 +222,11 @@ void QgsLabelPropertyDialog::setDataDefinedValues( const QgsPalLayerSettings &la

if ( !dd->expressionIsPrepared() )
{
dd->prepareExpression( vlayer );
dd->prepareExpression( context );
}

//TODO - pass expression context
QVariant result = layerSettings.dataDefinedValue( propIt.key(), mCurLabelFeat, vlayer->fields() );
QVariant result = layerSettings.dataDefinedValue( propIt.key(), mCurLabelFeat, vlayer->fields(), &context );
if ( !result.isValid() || result.isNull() )
{
//could not evaluate data defined value
@@ -218,7 +218,12 @@ void QgsHtmlAnnotationItem::setFeatureForMapPosition()
mFeatureId = currentFeatureId;
mFeature = currentFeature;

QString newtext = QgsExpression::replaceExpressionText( mHtmlSource, &mFeature, vectorLayer() );
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
context.setFeature( mFeature );
QString newtext = QgsExpression::replaceExpressionText( mHtmlSource, &context );
mWebView->setHtml( newtext );
}

@@ -96,7 +96,14 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsM

int idx = vlayer->fieldNameIndex( vlayer->displayField() );
if ( idx < 0 )
return QgsExpression::replaceExpressionText( vlayer->displayField(), &feature, vlayer );
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( vlayer );
context.setFeature( feature );
return QgsExpression::replaceExpressionText( vlayer->displayField(), &context );
}
else
return feature.attribute( idx ).toString();
}

0 comments on commit cc6feae

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