Skip to content

Commit

Permalink
Deprecate $rownum, replace with @row_number variable which is only
Browse files Browse the repository at this point in the history
available when it is usable
  • Loading branch information
nyalldawson committed Sep 7, 2015
1 parent 97096e2 commit 435aeed
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/app/composer/qgsattributeselectiondialog.cpp
Expand Up @@ -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 );
}

Expand Down
6 changes: 5 additions & 1 deletion src/app/qgsattributetabledialog.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 );
Expand Down
12 changes: 11 additions & 1 deletion src/app/qgsfieldcalculator.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerattributetable.cpp
Expand Up @@ -451,7 +451,7 @@ bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributeMap> &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() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerattributetablev2.cpp
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/core/qgsexpression.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -2050,7 +2050,7 @@ const QList<QgsExpression::Function*>& 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" )
Expand Down Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsexpression.cpp
Expand Up @@ -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 );
}
Expand Down

0 comments on commit 435aeed

Please sign in to comment.