Skip to content

Commit c1adf62

Browse files
committedAug 22, 2015
Port field calculator to contexts
1 parent cf3cf19 commit c1adf62

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed
 

‎python/core/qgsexpression.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ class QgsExpression
7777
void setEvalErrorString( const QString& str );
7878

7979
//! Set the number for $rownum special column
80-
void setCurrentRowNumber( int rowNumber );
80+
void setCurrentRowNumber( int rowNumber ) /Deprecated/;
8181
//! Return the number used for $rownum special column
82-
int currentRowNumber();
82+
int currentRowNumber() /Deprecated/;
8383

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

‎src/app/qgsfieldcalculator.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsproject.h"
2222
#include "qgsvectordataprovider.h"
2323
#include "qgsvectorlayer.h"
24+
#include "qgsexpressioncontext.h"
2425

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

150-
151151
QString calcString = builder->expressionText();
152152
QgsExpression exp( calcString );
153153
exp.setGeomCalculator( myDa );
154154

155-
if ( ! exp.prepare( mVectorLayer->fields() ) )
155+
QgsExpressionContext expContext;
156+
expContext << QgsExpressionContextUtils::globalScope()
157+
<< QgsExpressionContextUtils::projectScope()
158+
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
159+
160+
if ( !exp.prepare( &expContext ) )
156161
{
157162
QMessageBox::critical( 0, tr( "Evaluation error" ), exp.evalErrorString() );
158163
return;
@@ -209,7 +214,9 @@ void QgsFieldCalculator::accept()
209214
}
210215
}
211216

212-
if ( ! exp.prepare( mVectorLayer->fields() ) )
217+
//update expression context with new fields
218+
expContext.setFields( mVectorLayer->fields() );
219+
if ( ! exp.prepare( &expContext ) )
213220
{
214221
QApplication::restoreOverrideCursor();
215222
QMessageBox::critical( 0, tr( "Evaluation error" ), exp.evalErrorString() );
@@ -252,8 +259,11 @@ void QgsFieldCalculator::accept()
252259
continue;
253260
}
254261
}
255-
exp.setCurrentRowNumber( rownum );
256-
QVariant value = exp.evaluate( &feature );
262+
263+
expContext.setFeature( feature );
264+
expContext.lastScope()->setVariable( QString( "_rownum_" ), rownum );
265+
266+
QVariant value = exp.evaluate( &expContext );
257267
field.convertCompatible( value );
258268
if ( exp.hasEvalError() )
259269
{

‎src/core/qgsexpression.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,15 @@ static QVariant fcnSubstr( const QVariantList& values, const QgsExpressionContex
848848
return QVariant( str.mid( from -1, len ) );
849849
}
850850

851-
static QVariant fcnRowNumber( const QVariantList&, const QgsExpressionContext*, QgsExpression* parent )
851+
static QVariant fcnRowNumber( const QVariantList&, const QgsExpressionContext* context, QgsExpression* parent )
852852
{
853+
if ( context && context->hasVariable( "_rownum_" ) )
854+
return context->variable( "_rownum_" );
855+
856+
Q_NOWARN_DEPRECATED_PUSH
853857
return QVariant( parent->currentRowNumber() );
858+
Q_NOWARN_DEPRECATED_POP
859+
//when above is removed - return QVariant()
854860
}
855861

856862
#define FEAT_FROM_CONTEXT(c, f) if (!c || !c->hasVariable(QgsExpressionContext::EXPR_FEATURE)) return QVariant(); \

‎src/core/qgsexpression.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ class CORE_EXPORT QgsExpression
162162
void setEvalErrorString( const QString& str ) { mEvalErrorString = str; }
163163

164164
//! Set the number for $rownum special column
165-
void setCurrentRowNumber( int rowNumber ) { mRowNumber = rowNumber; }
165+
Q_DECL_DEPRECATED void setCurrentRowNumber( int rowNumber ) { mRowNumber = rowNumber; }
166166
//! Return the number used for $rownum special column
167-
int currentRowNumber() { return mRowNumber; }
167+
Q_DECL_DEPRECATED int currentRowNumber() { return mRowNumber; }
168168

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

0 commit comments

Comments
 (0)
Please sign in to comment.