diff --git a/src/core/expression/qgsexpression.cpp b/src/core/expression/qgsexpression.cpp index ca0e5521bf91..a55f28ba16b1 100644 --- a/src/core/expression/qgsexpression.cpp +++ b/src/core/expression/qgsexpression.cpp @@ -736,6 +736,7 @@ void QgsExpression::initVariableHelp() sVariableHelpTexts()->insert( QStringLiteral( "layout_page" ), QCoreApplication::translate( "variable_help", "Current page number in composition." ) ); sVariableHelpTexts()->insert( QStringLiteral( "layout_pageheight" ), QCoreApplication::translate( "variable_help", "Composition page height in mm." ) ); sVariableHelpTexts()->insert( QStringLiteral( "layout_pagewidth" ), QCoreApplication::translate( "variable_help", "Composition page width in mm." ) ); + sVariableHelpTexts()->insert( QStringLiteral( "layout_pageoffsets" ), QCoreApplication::translate( "variable_help", "Array of Y coordinate of the top of each page." ) ); sVariableHelpTexts()->insert( QStringLiteral( "layout_dpi" ), QCoreApplication::translate( "variable_help", "Composition resolution (DPI)." ) ); //atlas variables diff --git a/src/core/expression/qgsexpressioncontextutils.cpp b/src/core/expression/qgsexpressioncontextutils.cpp index fa15cb01ef79..6e1b9a73596b 100644 --- a/src/core/expression/qgsexpressioncontextutils.cpp +++ b/src/core/expression/qgsexpressioncontextutils.cpp @@ -510,6 +510,15 @@ QgsExpressionContextScope *QgsExpressionContextUtils::layoutScope( const QgsLayo scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) ); } + + QVariantList offsets; + for ( int i = 0; i < layout->pageCollection()->pageCount(); i++ ) + { + QPointF p = layout->pageCollection()->pagePositionToLayoutPosition( i, QgsLayoutPoint( 0, 0 ) ); + offsets << p.y(); + } + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageoffsets" ), offsets, true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_dpi" ), layout->renderContext().dpi(), true ) ); scope->addFunction( QStringLiteral( "item_variables" ), new GetLayoutItemVariables( layout ) ); diff --git a/tests/src/core/testqgslayoutlabel.cpp b/tests/src/core/testqgslayoutlabel.cpp index c057e64608ec..6a398e14e0e2 100644 --- a/tests/src/core/testqgslayoutlabel.cpp +++ b/tests/src/core/testqgslayoutlabel.cpp @@ -50,6 +50,7 @@ class TestQgsLayoutLabel : public QObject void featureEvaluationUsingContext(); // test page expressions void pageEvaluation(); + void pageSizeEvaluation(); void marginMethods(); //tests getting/setting margins void render(); void renderAsHtml(); @@ -235,6 +236,34 @@ void TestQgsLayoutLabel::pageEvaluation() } } +void TestQgsLayoutLabel::pageSizeEvaluation() +{ + QgsLayout l( QgsProject::instance() ); + l.initializeDefaults(); + + QgsLayoutItemLabel *label = new QgsLayoutItemLabel( &l ); + label->setMargin( 1 ); + label->setText( QStringLiteral( "[%array_to_string(@layout_pageoffsets)%]" ) ); + l.addLayoutItem( label ); + + { + QString evaluated = label->currentText(); + QString expected = QStringLiteral( "0" ); + QCOMPARE( evaluated, expected ); + } + + // add a page and re-evaluate + QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l ); + page2->setPageSize( "A4", QgsLayoutItemPage::Landscape ); + l.pageCollection()->addPage( page2 ); + + { + QString evaluated = label->currentText(); + QString expected = QStringLiteral( "0,220" ); + QCOMPARE( evaluated, expected ); + } +} + void TestQgsLayoutLabel::marginMethods() { QgsLayout l( QgsProject::instance() );