Skip to content

Commit 435aeed

Browse files
committedSep 7, 2015
Deprecate $rownum, replace with @row_number variable which is only
available when it is usable
1 parent 97096e2 commit 435aeed

File tree

7 files changed

+27
-9
lines changed

7 files changed

+27
-9
lines changed
 

‎src/app/composer/qgsattributeselectiondialog.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ static QgsExpressionContext _getExpressionContext( const void* context )
107107
}
108108

109109
QScopedPointer< QgsExpressionContext > expContext( object->createExpressionContext() );
110+
expContext->lastScope()->setVariable( "row_number", 1 );
111+
expContext->setHighlightedVariables( QStringList() << "row_number" );
110112
return QgsExpressionContext( *expContext );
111113
}
112114

‎src/app/qgsattributetabledialog.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ static QgsExpressionContext _getExpressionContext( const void* context )
7171
if ( layer )
7272
expContext << QgsExpressionContextUtils::layerScope( layer );
7373

74+
expContext.lastScope()->setVariable( "row_number", 1 );
75+
76+
expContext.setHighlightedVariables( QStringList() << "row_number" );
77+
7478
return expContext;
7579
}
7680

@@ -398,7 +402,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
398402
}
399403

400404
context.setFeature( feature );
401-
context.lastScope()->setVariable( QString( "_rownum_" ), rownum );
405+
context.lastScope()->setVariable( QString( "row_number" ), rownum );
402406

403407
QVariant value = exp.evaluate( &context );
404408
fld.convertCompatible( value );

‎src/app/qgsfieldcalculator.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,18 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl )
3636
if ( !vl )
3737
return;
3838

39+
40+
QgsExpressionContext expContext;
41+
expContext << QgsExpressionContextUtils::globalScope()
42+
<< QgsExpressionContextUtils::projectScope()
43+
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
44+
45+
expContext.lastScope()->setVariable( "row_number", 1 );
46+
expContext.setHighlightedVariables( QStringList() << "row_number" );
47+
3948
builder->setLayer( vl );
4049
builder->loadFieldNames();
50+
builder->setExpressionContext( expContext );
4151

4252
populateFields();
4353
populateOutputFieldTypes();
@@ -261,7 +271,7 @@ void QgsFieldCalculator::accept()
261271
}
262272

263273
expContext.setFeature( feature );
264-
expContext.lastScope()->setVariable( QString( "_rownum_" ), rownum );
274+
expContext.lastScope()->setVariable( QString( "row_number" ), rownum );
265275

266276
QVariant value = exp.evaluate( &expContext );
267277
field.convertCompatible( value );

‎src/core/composer/qgscomposerattributetable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributeMap> &at
451451
{
452452
// Lets assume it's an expression
453453
QgsExpression* expression = new QgsExpression(( *columnIt )->attribute() );
454-
context->lastScope()->setVariable( QString( "_rownum_" ), counter + 1 );
454+
context->lastScope()->setVariable( QString( "row_number" ), counter + 1 );
455455
expression->prepare( context.data() );
456456
QVariant value = expression->evaluate( context.data() );
457457
attributeMaps.last().insert( i, value.toString() );

‎src/core/composer/qgscomposerattributetablev2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
555555
{
556556
// Lets assume it's an expression
557557
QgsExpression* expression = new QgsExpression(( *columnIt )->attribute() );
558-
context->lastScope()->setVariable( QString( "_rownum_" ), counter + 1 );
558+
context->lastScope()->setVariable( QString( "row_number" ), counter + 1 );
559559
expression->prepare( context.data() );
560560
QVariant value = expression->evaluate( context.data() );
561561
currentRow << value;

‎src/core/qgsexpression.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,8 @@ static QVariant fcnSubstr( const QVariantList& values, const QgsExpressionContex
850850

851851
static QVariant fcnRowNumber( const QVariantList&, const QgsExpressionContext* context, QgsExpression* parent )
852852
{
853-
if ( context && context->hasVariable( "_rownum_" ) )
854-
return context->variable( "_rownum_" );
853+
if ( context && context->hasVariable( "row_number" ) )
854+
return context->variable( "row_number" );
855855

856856
Q_NOWARN_DEPRECATED_PUSH
857857
return QVariant( parent->currentRowNumber() );
@@ -1925,7 +1925,7 @@ const QStringList& QgsExpression::BuiltinFunctions()
19251925
<< "levenshtein" << "longest_common_substring" << "hamming_distance"
19261926
<< "soundex"
19271927
<< "attribute" << "var" << "layer_property"
1928-
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
1928+
<< "$id" << "$scale" << "_specialcol_";
19291929
}
19301930
return gmBuiltinFunctions;
19311931
}
@@ -2050,7 +2050,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
20502050
<< new StaticFunction( "geom_to_wkt", -1, fcnGeomToWKT, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomToWKT" )
20512051
<< new StaticFunction( "geometry", 1, fcnGetGeometry, "GeometryGroup" )
20522052
<< new StaticFunction( "transform", 3, fcnTransformGeometry, "GeometryGroup" )
2053-
<< new StaticFunction( "$rownum", 0, fcnRowNumber, "Record" )
2053+
<< new StaticFunction( "$rownum", 0, fcnRowNumber, "deprecated" )
20542054
<< new StaticFunction( "$id", 0, fcnFeatureId, "Record" )
20552055
<< new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" )
20562056
<< new StaticFunction( "$scale", 0, fcnScale, "Record" )
@@ -3243,6 +3243,8 @@ void QgsExpression::initVariableHelp()
32433243
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." ) );
32443244
gVariableHelpTexts.insert( "map_rotation", QCoreApplication::translate( "variable_help", "Current rotation of map." ) );
32453245
gVariableHelpTexts.insert( "map_scale", QCoreApplication::translate( "variable_help", "Current scale of map." ) );
3246+
3247+
gVariableHelpTexts.insert( "row_number", QCoreApplication::translate( "variable_help", "Stores the number of the current row." ) );
32463248
}
32473249

32483250
QString QgsExpression::variableHelpText( const QString &variableName, bool showValue, const QVariant &value )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ class TestQgsExpression: public QObject
647647

648648
QgsExpressionContext context;
649649
context << new QgsExpressionContextScope();
650-
context.lastScope()->setVariable( "_rownum_", 101 );
650+
context.lastScope()->setVariable( "row_number", 101 );
651651
QVariant v3 = exp.evaluate();
652652
QCOMPARE( v3.toInt(), 101 );
653653
}

0 commit comments

Comments
 (0)
Please sign in to comment.