Skip to content
Permalink
Browse files
Add QgsLayoutItemPage::pageLayout
  • Loading branch information
manisandro authored and nyalldawson committed Jun 13, 2021
1 parent 898a03a commit 64a7234e33f8b378ad778c7ef07b298927509012
@@ -10,6 +10,7 @@




class QgsLayoutItemPage : QgsLayoutItem
{
%Docstring(signature="appended")
@@ -69,6 +70,13 @@ size was changed. If ``False`` is returned then ``size`` could not be matched
to a known page size.

.. seealso:: :py:func:`pageSize`
%End

QPageLayout pageLayout() const;
%Docstring
Returns the page layout for the page, suitable to pass to QPrinter.setPageLayout

.. versionadded:: 3.20
%End

QgsLayoutSize pageSize() const;
@@ -103,6 +103,27 @@ bool QgsLayoutItemPage::setPageSize( const QString &size, Orientation orientatio
}
}

QPageLayout QgsLayoutItemPage::pageLayout() const
{
QPageLayout pageLayout;
pageLayout.setMargins( {0, 0, 0, 0} );
pageLayout.setMode( QPageLayout::FullPageMode );
QSizeF size = layout()->renderContext().measurementConverter().convert( pageSize(), QgsUnitTypes::LayoutMillimeters ).toQSizeF();

if ( pageSize().width() > pageSize().height() )
{
pageLayout.setOrientation( QPageLayout::Landscape );
pageLayout.setPageSize( QPageSize( QSizeF( size.height(), size.width() ), QPageSize::Millimeter ) );
}
else
{
pageLayout.setOrientation( QPageLayout::Portrait );
pageLayout.setPageSize( QPageSize( size, QPageSize::Millimeter ) );
}
pageLayout.setUnits( QPageLayout::Millimeter );
return pageLayout;
}

QgsLayoutSize QgsLayoutItemPage::pageSize() const
{
return sizeWithUnits();
@@ -17,6 +17,8 @@
#ifndef QGSLAYOUTITEMPAGE_H
#define QGSLAYOUTITEMPAGE_H

#include <QPageLayout>

#include "qgis_core.h"
#include "qgslayoutitem.h"
#include "qgslayoutitemregistry.h"
@@ -106,6 +108,12 @@ class CORE_EXPORT QgsLayoutItemPage : public QgsLayoutItem
*/
bool setPageSize( const QString &size, Orientation orientation = Portrait );

/**
* Returns the page layout for the page, suitable to pass to QPrinter::setPageLayout
* \since QGIS 3.20
*/
QPageLayout pageLayout() const;

/**
* Returns the size of the page.
* \see setPageSize()
@@ -51,6 +51,8 @@ class TestQgsLayoutPage : public QObject

void hiddenPages(); //test hidden page boundaries

void pageLayout(); //test page layout

private:
QString mReport;

@@ -270,5 +272,30 @@ void TestQgsLayoutPage::hiddenPages()

}

void TestQgsLayoutPage::pageLayout()
{
QgsProject p;
QgsLayout l( &p );
std::unique_ptr< QgsLayoutItemPage > page1( new QgsLayoutItemPage( &l ) );
page1->setPageSize( QgsLayoutSize( 297, 210, QgsUnitTypes::LayoutMillimeters ) );

QPageLayout layout1 = page1->pageLayout();

QCOMPARE( layout1.orientation(), QPageLayout::Landscape );
QCOMPARE( layout1.units(), QPageLayout::Millimeter );
QCOMPARE( layout1.pageSize().size( QPageSize::Millimeter ).width(), 210 );
QCOMPARE( layout1.pageSize().size( QPageSize::Millimeter ).height(), 297 );

std::unique_ptr< QgsLayoutItemPage > page2( new QgsLayoutItemPage( &l ) );
page2->setPageSize( QgsLayoutSize( 210, 297, QgsUnitTypes::LayoutMillimeters ) );

QPageLayout layout2 = page2->pageLayout();

QCOMPARE( layout2.orientation(), QPageLayout::Portrait );
QCOMPARE( layout2.units(), QPageLayout::Millimeter );
QCOMPARE( layout2.pageSize().size( QPageSize::Millimeter ).width(), 210 );
QCOMPARE( layout2.pageSize().size( QPageSize::Millimeter ).height(), 297 );
}

QGSTEST_MAIN( TestQgsLayoutPage )
#include "testqgslayoutpage.moc"

0 comments on commit 64a7234

Please sign in to comment.