Skip to content
Permalink
Browse files

Use @row_number, @column_number to unify variable names

  • Loading branch information
nyalldawson committed Jul 14, 2020
1 parent 1878f2e commit 8e3151a1598af01e78ccb828ab2be9e5b238146d
@@ -793,6 +793,7 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts()->insert( QStringLiteral( "row_number" ), QCoreApplication::translate( "variable_help", "Stores the number of the current row." ) );
sVariableHelpTexts()->insert( QStringLiteral( "grid_number" ), QCoreApplication::translate( "variable_help", "Current grid annotation value." ) );
sVariableHelpTexts()->insert( QStringLiteral( "grid_axis" ), QCoreApplication::translate( "variable_help", "Current grid annotation axis (e.g., 'x' for longitude, 'y' for latitude)." ) );
sVariableHelpTexts()->insert( QStringLiteral( "column_number" ), QCoreApplication::translate( "variable_help", "Stores the number of the current column." ) );

// map canvas item variables
sVariableHelpTexts()->insert( QStringLiteral( "canvas_cursor_point" ), QCoreApplication::translate( "variable_help", "Last cursor position on the canvas in the project's geographical coordinates." ) );
@@ -992,8 +992,8 @@ QMap<int, QString> QgsLayoutTable::headerLabels() const
QgsExpressionContextScope *QgsLayoutTable::scopeForCell( int row, int column ) const
{
std::unique_ptr< QgsExpressionContextScope > cellScope = qgis::make_unique< QgsExpressionContextScope >();
cellScope->setVariable( QStringLiteral( "table_row" ), row + 1, true );
cellScope->setVariable( QStringLiteral( "table_column" ), column + 1, true );
cellScope->setVariable( QStringLiteral( "row_number" ), row + 1, true );
cellScope->setVariable( QStringLiteral( "column_number" ), column + 1, true );
return cellScope.release();
}

@@ -284,8 +284,8 @@ void TestQgsLayoutManualTable::scopeForCell()
std::unique_ptr< QgsExpressionContextScope > scope( table->scopeForCell( 1, 2 ) );

// variable values for row/col should start at 1, not 0!
QCOMPARE( scope->variable( QStringLiteral( "table_row" ) ).toInt(), 2 );
QCOMPARE( scope->variable( QStringLiteral( "table_column" ) ).toInt(), 3 );
QCOMPARE( scope->variable( QStringLiteral( "row_number" ) ).toInt(), 2 );
QCOMPARE( scope->variable( QStringLiteral( "column_number" ) ).toInt(), 3 );
}

void TestQgsLayoutManualTable::expressionContents()
@@ -307,7 +307,7 @@ void TestQgsLayoutManualTable::expressionContents()
expectedRows.append( row );

table->setTableContents(
QgsTableContents() << ( QgsTableRow() << QgsTableCell( QStringLiteral( "Jet" ) ) << QgsTableCell( QgsProperty::fromExpression( QStringLiteral( "@table_row || ',' || @table_column" ) ) ) << QgsTableCell( QgsProperty::fromExpression( QStringLiteral( "@table_row || ',' || @table_column" ) ) ) )
QgsTableContents() << ( QgsTableRow() << QgsTableCell( QStringLiteral( "Jet" ) ) << QgsTableCell( QgsProperty::fromExpression( QStringLiteral( "@row_number || ',' || @column_number" ) ) ) << QgsTableCell( QgsProperty::fromExpression( QStringLiteral( "@row_number || ',' || @column_number" ) ) ) )
<< ( QgsTableRow() << QgsTableCell( QgsProperty::fromExpression( QStringLiteral( "@layout_name" ) ) ) << QgsTableCell( QStringLiteral( "Helicopter" ) ) << QgsTableCell( QStringLiteral( "Plane" ) ) ) );
compareTable( table, expectedRows );

@@ -1651,16 +1651,16 @@ void TestQgsLayoutTable::testScopeForCell()
std::unique_ptr< QgsExpressionContextScope > scope( table->scopeForCell( 0, 0 ) );

// variable values for row/col should start at 1, not 0!
QCOMPARE( scope->variable( QStringLiteral( "table_row" ) ).toInt(), 1 );
QCOMPARE( scope->variable( QStringLiteral( "table_column" ) ).toInt(), 1 );
QCOMPARE( scope->variable( QStringLiteral( "row_number" ) ).toInt(), 1 );
QCOMPARE( scope->variable( QStringLiteral( "column_number" ) ).toInt(), 1 );
QCOMPARE( scope->feature().attribute( 0 ).toString(), QStringLiteral( "Jet" ) );
scope.reset( table->scopeForCell( 0, 1 ) );
QCOMPARE( scope->variable( QStringLiteral( "table_row" ) ).toInt(), 1 );
QCOMPARE( scope->variable( QStringLiteral( "table_column" ) ).toInt(), 2 );
QCOMPARE( scope->variable( QStringLiteral( "row_number" ) ).toInt(), 1 );
QCOMPARE( scope->variable( QStringLiteral( "column_number" ) ).toInt(), 2 );
QCOMPARE( scope->feature().attribute( 0 ).toString(), QStringLiteral( "Jet" ) );
scope.reset( table->scopeForCell( 1, 2 ) );
QCOMPARE( scope->variable( QStringLiteral( "table_row" ) ).toInt(), 2 );
QCOMPARE( scope->variable( QStringLiteral( "table_column" ) ).toInt(), 3 );
QCOMPARE( scope->variable( QStringLiteral( "row_number" ) ).toInt(), 2 );
QCOMPARE( scope->variable( QStringLiteral( "column_number" ) ).toInt(), 3 );
QCOMPARE( scope->feature().attribute( 0 ).toString(), QStringLiteral( "Biplane" ) );

// make sure fields are set

0 comments on commit 8e3151a

Please sign in to comment.
You can’t perform that action at this time.