Skip to content

Commit

Permalink
More misc porting to expression contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 000a62f commit cc6feae
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -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
Expand All @@ -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() )
Expand Down Expand Up @@ -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() );
}
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/app/qgslabelpropertydialog.cpp
Expand Up @@ -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 )
{
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/gui/qgshtmlannotationitem.cpp
Expand Up @@ -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 );
}

Expand Down
9 changes: 8 additions & 1 deletion src/gui/qgsmaptip.cpp
Expand Up @@ -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.