From 435aeeda66f48533a0860ff13d9dd82bcd56e7e0 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 7 Sep 2015 19:19:37 +1000 Subject: [PATCH] Deprecate $rownum, replace with @row_number variable which is only available when it is usable --- src/app/composer/qgsattributeselectiondialog.cpp | 2 ++ src/app/qgsattributetabledialog.cpp | 6 +++++- src/app/qgsfieldcalculator.cpp | 12 +++++++++++- src/core/composer/qgscomposerattributetable.cpp | 2 +- src/core/composer/qgscomposerattributetablev2.cpp | 2 +- src/core/qgsexpression.cpp | 10 ++++++---- tests/src/core/testqgsexpression.cpp | 2 +- 7 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/app/composer/qgsattributeselectiondialog.cpp b/src/app/composer/qgsattributeselectiondialog.cpp index 5a5fe6c9f91..76fc372ec85 100644 --- a/src/app/composer/qgsattributeselectiondialog.cpp +++ b/src/app/composer/qgsattributeselectiondialog.cpp @@ -107,6 +107,8 @@ static QgsExpressionContext _getExpressionContext( const void* context ) } QScopedPointer< QgsExpressionContext > expContext( object->createExpressionContext() ); + expContext->lastScope()->setVariable( "row_number", 1 ); + expContext->setHighlightedVariables( QStringList() << "row_number" ); return QgsExpressionContext( *expContext ); } diff --git a/src/app/qgsattributetabledialog.cpp b/src/app/qgsattributetabledialog.cpp index fd9b5513d68..331fa091401 100644 --- a/src/app/qgsattributetabledialog.cpp +++ b/src/app/qgsattributetabledialog.cpp @@ -71,6 +71,10 @@ static QgsExpressionContext _getExpressionContext( const void* context ) if ( layer ) expContext << QgsExpressionContextUtils::layerScope( layer ); + expContext.lastScope()->setVariable( "row_number", 1 ); + + expContext.setHighlightedVariables( QStringList() << "row_number" ); + return expContext; } @@ -398,7 +402,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin } context.setFeature( feature ); - context.lastScope()->setVariable( QString( "_rownum_" ), rownum ); + context.lastScope()->setVariable( QString( "row_number" ), rownum ); QVariant value = exp.evaluate( &context ); fld.convertCompatible( value ); diff --git a/src/app/qgsfieldcalculator.cpp b/src/app/qgsfieldcalculator.cpp index 01d509c0be9..b766cc92964 100644 --- a/src/app/qgsfieldcalculator.cpp +++ b/src/app/qgsfieldcalculator.cpp @@ -36,8 +36,18 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl ) if ( !vl ) return; + + QgsExpressionContext expContext; + expContext << QgsExpressionContextUtils::globalScope() + << QgsExpressionContextUtils::projectScope() + << QgsExpressionContextUtils::layerScope( mVectorLayer ); + + expContext.lastScope()->setVariable( "row_number", 1 ); + expContext.setHighlightedVariables( QStringList() << "row_number" ); + builder->setLayer( vl ); builder->loadFieldNames(); + builder->setExpressionContext( expContext ); populateFields(); populateOutputFieldTypes(); @@ -261,7 +271,7 @@ void QgsFieldCalculator::accept() } expContext.setFeature( feature ); - expContext.lastScope()->setVariable( QString( "_rownum_" ), rownum ); + expContext.lastScope()->setVariable( QString( "row_number" ), rownum ); QVariant value = exp.evaluate( &expContext ); field.convertCompatible( value ); diff --git a/src/core/composer/qgscomposerattributetable.cpp b/src/core/composer/qgscomposerattributetable.cpp index d0fe2f4e95b..411854b92be 100644 --- a/src/core/composer/qgscomposerattributetable.cpp +++ b/src/core/composer/qgscomposerattributetable.cpp @@ -451,7 +451,7 @@ bool QgsComposerAttributeTable::getFeatureAttributes( QList &at { // Lets assume it's an expression QgsExpression* expression = new QgsExpression(( *columnIt )->attribute() ); - context->lastScope()->setVariable( QString( "_rownum_" ), counter + 1 ); + context->lastScope()->setVariable( QString( "row_number" ), counter + 1 ); expression->prepare( context.data() ); QVariant value = expression->evaluate( context.data() ); attributeMaps.last().insert( i, value.toString() ); diff --git a/src/core/composer/qgscomposerattributetablev2.cpp b/src/core/composer/qgscomposerattributetablev2.cpp index 0b6eec7c12b..33b9ba68528 100644 --- a/src/core/composer/qgscomposerattributetablev2.cpp +++ b/src/core/composer/qgscomposerattributetablev2.cpp @@ -555,7 +555,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co { // Lets assume it's an expression QgsExpression* expression = new QgsExpression(( *columnIt )->attribute() ); - context->lastScope()->setVariable( QString( "_rownum_" ), counter + 1 ); + context->lastScope()->setVariable( QString( "row_number" ), counter + 1 ); expression->prepare( context.data() ); QVariant value = expression->evaluate( context.data() ); currentRow << value; diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index ecf6124c06b..16d21fd50c8 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -850,8 +850,8 @@ static QVariant fcnSubstr( const QVariantList& values, const QgsExpressionContex static QVariant fcnRowNumber( const QVariantList&, const QgsExpressionContext* context, QgsExpression* parent ) { - if ( context && context->hasVariable( "_rownum_" ) ) - return context->variable( "_rownum_" ); + if ( context && context->hasVariable( "row_number" ) ) + return context->variable( "row_number" ); Q_NOWARN_DEPRECATED_PUSH return QVariant( parent->currentRowNumber() ); @@ -1925,7 +1925,7 @@ const QStringList& QgsExpression::BuiltinFunctions() << "levenshtein" << "longest_common_substring" << "hamming_distance" << "soundex" << "attribute" << "var" << "layer_property" - << "$rownum" << "$id" << "$scale" << "_specialcol_"; + << "$id" << "$scale" << "_specialcol_"; } return gmBuiltinFunctions; } @@ -2050,7 +2050,7 @@ const QList& QgsExpression::Functions() << new StaticFunction( "geom_to_wkt", -1, fcnGeomToWKT, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomToWKT" ) << new StaticFunction( "geometry", 1, fcnGetGeometry, "GeometryGroup" ) << new StaticFunction( "transform", 3, fcnTransformGeometry, "GeometryGroup" ) - << new StaticFunction( "$rownum", 0, fcnRowNumber, "Record" ) + << new StaticFunction( "$rownum", 0, fcnRowNumber, "deprecated" ) << new StaticFunction( "$id", 0, fcnFeatureId, "Record" ) << new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" ) << new StaticFunction( "$scale", 0, fcnScale, "Record" ) @@ -3243,6 +3243,8 @@ void QgsExpression::initVariableHelp() gVariableHelpTexts.insert( "map_id", QCoreApplication::translate( "variable_help", "ID of current map destination. This will be 'canvas' for canvas renders, and the item ID for composer map renders." ) ); gVariableHelpTexts.insert( "map_rotation", QCoreApplication::translate( "variable_help", "Current rotation of map." ) ); gVariableHelpTexts.insert( "map_scale", QCoreApplication::translate( "variable_help", "Current scale of map." ) ); + + gVariableHelpTexts.insert( "row_number", QCoreApplication::translate( "variable_help", "Stores the number of the current row." ) ); } QString QgsExpression::variableHelpText( const QString &variableName, bool showValue, const QVariant &value ) diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index e2d406dcdce..0c265524b9f 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -647,7 +647,7 @@ class TestQgsExpression: public QObject QgsExpressionContext context; context << new QgsExpressionContextScope(); - context.lastScope()->setVariable( "_rownum_", 101 ); + context.lastScope()->setVariable( "row_number", 101 ); QVariant v3 = exp.evaluate(); QCOMPARE( v3.toInt(), 101 ); }