Skip to content
Permalink
Browse files

Port field calculator to contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent cf3cf19 commit c1adf623afb138bd2038dae36140854fb4767c6f
Showing with 26 additions and 10 deletions.
  1. +2 −2 python/core/qgsexpression.sip
  2. +15 −5 src/app/qgsfieldcalculator.cpp
  3. +7 −1 src/core/qgsexpression.cpp
  4. +2 −2 src/core/qgsexpression.h
@@ -77,9 +77,9 @@ class QgsExpression
void setEvalErrorString( const QString& str );

//! Set the number for $rownum special column
void setCurrentRowNumber( int rowNumber );
void setCurrentRowNumber( int rowNumber ) /Deprecated/;
//! Return the number used for $rownum special column
int currentRowNumber();
int currentRowNumber() /Deprecated/;

//! Assign a special column
static void setSpecialColumn( const QString& name, QVariant value );
@@ -21,6 +21,7 @@
#include "qgsproject.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsexpressioncontext.h"

#include <QMessageBox>
#include <QSettings>
@@ -147,12 +148,16 @@ void QgsFieldCalculator::accept()
myDa.setEllipsoidalMode( QgisApp::instance()->mapCanvas()->mapSettings().hasCrsTransformEnabled() );
myDa.setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );


QString calcString = builder->expressionText();
QgsExpression exp( calcString );
exp.setGeomCalculator( myDa );

if ( ! exp.prepare( mVectorLayer->fields() ) )
QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mVectorLayer );

if ( !exp.prepare( &expContext ) )
{
QMessageBox::critical( 0, tr( "Evaluation error" ), exp.evalErrorString() );
return;
@@ -209,7 +214,9 @@ void QgsFieldCalculator::accept()
}
}

if ( ! exp.prepare( mVectorLayer->fields() ) )
//update expression context with new fields
expContext.setFields( mVectorLayer->fields() );
if ( ! exp.prepare( &expContext ) )
{
QApplication::restoreOverrideCursor();
QMessageBox::critical( 0, tr( "Evaluation error" ), exp.evalErrorString() );
@@ -252,8 +259,11 @@ void QgsFieldCalculator::accept()
continue;
}
}
exp.setCurrentRowNumber( rownum );
QVariant value = exp.evaluate( &feature );

expContext.setFeature( feature );
expContext.lastScope()->setVariable( QString( "_rownum_" ), rownum );

QVariant value = exp.evaluate( &expContext );
field.convertCompatible( value );
if ( exp.hasEvalError() )
{
@@ -848,9 +848,15 @@ static QVariant fcnSubstr( const QVariantList& values, const QgsExpressionContex
return QVariant( str.mid( from -1, len ) );
}

static QVariant fcnRowNumber( const QVariantList&, const QgsExpressionContext*, QgsExpression* parent )
static QVariant fcnRowNumber( const QVariantList&, const QgsExpressionContext* context, QgsExpression* parent )
{
if ( context && context->hasVariable( "_rownum_" ) )
return context->variable( "_rownum_" );

Q_NOWARN_DEPRECATED_PUSH
return QVariant( parent->currentRowNumber() );
Q_NOWARN_DEPRECATED_POP
//when above is removed - return QVariant()
}

#define FEAT_FROM_CONTEXT(c, f) if (!c || !c->hasVariable(QgsExpressionContext::EXPR_FEATURE)) return QVariant(); \
@@ -162,9 +162,9 @@ class CORE_EXPORT QgsExpression
void setEvalErrorString( const QString& str ) { mEvalErrorString = str; }

//! Set the number for $rownum special column
void setCurrentRowNumber( int rowNumber ) { mRowNumber = rowNumber; }
Q_DECL_DEPRECATED void setCurrentRowNumber( int rowNumber ) { mRowNumber = rowNumber; }
//! Return the number used for $rownum special column
int currentRowNumber() { return mRowNumber; }
Q_DECL_DEPRECATED int currentRowNumber() { return mRowNumber; }

//! Assign a special column
static void setSpecialColumn( const QString& name, QVariant value );

0 comments on commit c1adf62

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