Skip to content

Commit

Permalink
[layout expressions] add Y page offset expression
Browse files Browse the repository at this point in the history
this is useful for dynamically position items
relative to pages in multi-pages layouts
  • Loading branch information
olivierdalang authored and nyalldawson committed Aug 21, 2020
1 parent a462a17 commit 6ac8f61
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/core/expression/qgsexpressioncontextutils.cpp
Expand Up @@ -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 ) );
Expand Down

0 comments on commit 6ac8f61

Please sign in to comment.