Deprecate $rownum, replace with @row_number variable which is only

available when it is usable
This commit is contained in:
Nyall Dawson 2015-09-07 19:19:37 +10:00
parent 97096e2d6b
commit 435aeeda66
7 changed files with 27 additions and 9 deletions

View File

@ -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 );
}

View File

@ -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 );

View File

@ -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 );

View File

@ -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() );

View File

@ -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;

View File

@ -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::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" )
@ -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 )

View File

@ -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 );
}