Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsLayoutItemPage::pageLayout
  • Loading branch information
manisandro authored and nyalldawson committed Jun 13, 2021
1 parent 898a03a commit 64a7234
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/auto_generated/layout/qgslayoutitempage.sip.in
Expand Up @@ -10,6 +10,7 @@




class QgsLayoutItemPage : QgsLayoutItem
{
%Docstring(signature="appended")
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/core/layout/qgslayoutitempage.cpp
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutitempage.h
Expand Up @@ -17,6 +17,8 @@
#ifndef QGSLAYOUTITEMPAGE_H
#define QGSLAYOUTITEMPAGE_H

#include <QPageLayout>

#include "qgis_core.h"
#include "qgslayoutitem.h"
#include "qgslayoutitemregistry.h"
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions tests/src/core/testqgslayoutpage.cpp
Expand Up @@ -51,6 +51,8 @@ class TestQgsLayoutPage : public QObject

void hiddenPages(); //test hidden page boundaries

void pageLayout(); //test page layout

private:
QString mReport;

Expand Down Expand Up @@ -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.