Skip to content

Commit

Permalink
Add methods for handling page size to QgsLayoutItemPage
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent 534f7ab commit ea32391
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/core/layout/qgslayout.sip
Expand Up @@ -22,6 +22,7 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator

enum ZValues
{
ZPage,
ZMapTool,
};

Expand Down
48 changes: 48 additions & 0 deletions python/core/layout/qgslayoutitempage.sip
Expand Up @@ -20,13 +20,61 @@ class QgsLayoutItemPage : QgsLayoutItem
%End
public:

enum Orientation
{
Portrait,
Landscape
};

explicit QgsLayoutItemPage( QgsLayout *layout /TransferThis/ );
%Docstring
Constructor for QgsLayoutItemPage, with the specified parent ``layout``.
%End
virtual int type() const;
virtual QString stringType() const;

void setPageSize( const QgsLayoutSize &size );
%Docstring
Sets the ``size`` of the page.
.. seealso:: pageSize()
%End

bool setPageSize( const QString &size, Orientation orientation = Portrait );
%Docstring
Sets the page size to a known page ``size``, e.g. "A4" and ``orientation``.
The known page sizes are managed by QgsPageSizeRegistry. Valid page sizes
can be retrieved via QgsPageSizeRegistry.entries().
The function returns true if ``size`` was a valid page size and the page
size was changed. If false is returned then ``size`` could not be matched
to a known page size.
.. seealso:: pageSize()
:rtype: bool
%End

QgsLayoutSize pageSize() const;
%Docstring
Returns the size of the page.
.. seealso:: setPageSize()
:rtype: QgsLayoutSize
%End

Orientation orientation() const;
%Docstring
Returns the page orientiation.
.. note::

There is no direct setter for page orientation - use setPageSize() instead.
:rtype: Orientation
%End

static QgsLayoutItemPage::Orientation decodePageOrientation( const QString &string, bool *ok /Out/ = 0 );
%Docstring
Decodes a ``string`` representing a page orientation. If specified, ``ok``
will be set to true if string could be successfully interpreted as a
page orientation.
:rtype: QgsLayoutItemPage.Orientation
%End

protected:

virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
Expand Down
3 changes: 2 additions & 1 deletion src/core/layout/qgslayout.h
Expand Up @@ -39,7 +39,8 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
//! Preset item z-values, to ensure correct stacking
enum ZValues
{
ZMapTool = 10000, //!< Z-Value for temporary map tool items
ZPage = 0, //!< Z-value for page (paper) items
ZMapTool = 10000, //!< Z-value for temporary map tool items
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitem.cpp
Expand Up @@ -78,7 +78,7 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
}

double destinationDpi = itemStyle->matrix.m11() * 25.4;
bool useImageCache = false;
bool useImageCache = true;

if ( useImageCache )
{
Expand Down
71 changes: 71 additions & 0 deletions src/core/layout/qgslayoutitempage.cpp
Expand Up @@ -17,14 +17,85 @@
#include "qgslayoutitempage.h"
#include "qgslayout.h"
#include "qgslayoututils.h"
#include "qgspagesizeregistry.h"
#include "qgssymbollayerutils.h"
#include <QPainter>

#define SHADOW_WIDTH_PIXELS 5
QgsLayoutItemPage::QgsLayoutItemPage( QgsLayout *layout )
: QgsLayoutItem( layout )
{
setFlag( QGraphicsItem::ItemIsSelectable, false );
setFlag( QGraphicsItem::ItemIsMovable, false );
setZValue( QgsLayout::ZPage );
}

void QgsLayoutItemPage::setPageSize( const QgsLayoutSize &size )
{
attemptResize( size );
}

bool QgsLayoutItemPage::setPageSize( const QString &size, Orientation orientation )
{
QgsPageSize newSize;
if ( QgsApplication::pageSizeRegistry()->decodePageSize( size, newSize ) )
{
switch ( orientation )
{
case Portrait:
break; // nothing to do

case Landscape:
{
// flip height and width
double x = newSize.size.width();
newSize.size.setWidth( newSize.size.height() );
newSize.size.setHeight( x );
break;
}
}

setPageSize( newSize.size );
return true;
}
else
{
return false;
}
}

QgsLayoutSize QgsLayoutItemPage::pageSize() const
{
return sizeWithUnits();
}

QgsLayoutItemPage::Orientation QgsLayoutItemPage::orientation() const
{
if ( sizeWithUnits().width() >= sizeWithUnits().height() )
return Landscape;
else
return Portrait;
}

QgsLayoutItemPage::Orientation QgsLayoutItemPage::decodePageOrientation( const QString &string, bool *ok )
{
if ( ok )
*ok = false;

QString trimmedString = string.trimmed();
if ( trimmedString.compare( QStringLiteral( "portrait" ), Qt::CaseInsensitive ) == 0 )
{
if ( ok )
*ok = true;
return Portrait;
}
else if ( trimmedString.compare( QStringLiteral( "landscape" ), Qt::CaseInsensitive ) == 0 )
{
if ( ok )
*ok = true;
return Landscape;
}
return Landscape;
}

void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
Expand Down
44 changes: 44 additions & 0 deletions src/core/layout/qgslayoutitempage.h
Expand Up @@ -20,6 +20,7 @@
#include "qgis_core.h"
#include "qgslayoutitem.h"
#include "qgslayoutitemregistry.h"
#include "qgis_sip.h"

/**
* \ingroup core
Expand All @@ -34,13 +35,56 @@ class CORE_EXPORT QgsLayoutItemPage : public QgsLayoutItem

public:

//! Page orientiation
enum Orientation
{
Portrait, //!< Portrait orientation
Landscape //!< Landscape orientation
};

/**
* Constructor for QgsLayoutItemPage, with the specified parent \a layout.
*/
explicit QgsLayoutItemPage( QgsLayout *layout SIP_TRANSFERTHIS );
int type() const override { return QgsLayoutItemRegistry::LayoutPage; }
QString stringType() const override { return QStringLiteral( "ItemPaper" ); }

/**
* Sets the \a size of the page.
* \see pageSize()
*/
void setPageSize( const QgsLayoutSize &size );

/**
* Sets the page size to a known page \a size, e.g. "A4" and \a orientation.
* The known page sizes are managed by QgsPageSizeRegistry. Valid page sizes
* can be retrieved via QgsPageSizeRegistry::entries().
* The function returns true if \a size was a valid page size and the page
* size was changed. If false is returned then \a size could not be matched
* to a known page size.
* \see pageSize()
*/
bool setPageSize( const QString &size, Orientation orientation = Portrait );

/**
* Returns the size of the page.
* \see setPageSize()
*/
QgsLayoutSize pageSize() const;

/**
* Returns the page orientiation.
* \note There is no direct setter for page orientation - use setPageSize() instead.
*/
Orientation orientation() const;

/**
* Decodes a \a string representing a page orientation. If specified, \a ok
* will be set to true if string could be successfully interpreted as a
* page orientation.
*/
static QgsLayoutItemPage::Orientation decodePageOrientation( const QString &string, bool *ok SIP_OUT = nullptr );

protected:

void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -130,6 +130,7 @@ SET(TESTS
testqgslayoutcontext.cpp
testqgslayoutitem.cpp
testqgslayoutobject.cpp
testqgslayoutpage.cpp
testqgslayoutunits.cpp
testqgslayoututils.cpp
testqgslegendrenderer.cpp
Expand Down
129 changes: 129 additions & 0 deletions tests/src/core/testqgslayoutpage.cpp
@@ -0,0 +1,129 @@
/***************************************************************************
testqgslayoutpage.cpp
---------------------
begin : November 2014
copyright : (C) 2014 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayout.h"
#include "qgslayoutitempage.h"
#include "qgslayoutitemregistry.h"
#include "qgis.h"
#include "qgsproject.h"
#include <QObject>
#include "qgstest.h"

class TestQgsLayoutPage : public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void itemType();
void pageSize();
void decodePageOrientation();

private:
QString mReport;

};

void TestQgsLayoutPage::initTestCase()
{
mReport = "<h1>Layout Page Tests</h1>\n";
}

void TestQgsLayoutPage::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}

void TestQgsLayoutPage::init()
{

}

void TestQgsLayoutPage::cleanup()
{

}

void TestQgsLayoutPage::itemType()
{
QgsProject p;
QgsLayout l( &p );
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
QCOMPARE( page->type(), static_cast< int >( QgsLayoutItemRegistry::LayoutPage ) );
}

void TestQgsLayoutPage::pageSize()
{
QgsProject p;
QgsLayout l( &p );
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
page->setPageSize( QgsLayoutSize( 270, 297, QgsUnitTypes::LayoutMeters ) );
QCOMPARE( page->pageSize().width(), 270.0 );
QCOMPARE( page->pageSize().height(), 297.0 );
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMeters );
QCOMPARE( page->orientation(), QgsLayoutItemPage::Portrait );
page->setPageSize( QgsLayoutSize( 297, 270, QgsUnitTypes::LayoutMeters ) );
QCOMPARE( page->orientation(), QgsLayoutItemPage::Landscape );

// from registry
QVERIFY( !page->setPageSize( "hoooyeah" ) );
// should be unchanged
QCOMPARE( page->pageSize().width(), 297.0 );
QCOMPARE( page->pageSize().height(), 270.0 );
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMeters );

// good size
QVERIFY( page->setPageSize( "A5" ) );
QCOMPARE( page->pageSize().width(), 148.0 );
QCOMPARE( page->pageSize().height(), 210.0 );
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMillimeters );
QCOMPARE( page->orientation(), QgsLayoutItemPage::Portrait );

QVERIFY( page->setPageSize( "A5", QgsLayoutItemPage::Landscape ) );
QCOMPARE( page->pageSize().width(), 210.0 );
QCOMPARE( page->pageSize().height(), 148.0 );
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMillimeters );
QCOMPARE( page->orientation(), QgsLayoutItemPage::Landscape );

}

void TestQgsLayoutPage::decodePageOrientation()
{
//test good string
bool ok = false;
QCOMPARE( QgsLayoutItemPage::decodePageOrientation( QString( " porTrait " ), &ok ), QgsLayoutItemPage::Portrait );
QVERIFY( ok );
QCOMPARE( QgsLayoutItemPage::decodePageOrientation( QString( "landscape" ), &ok ), QgsLayoutItemPage::Landscape );
QVERIFY( ok );

//test bad string
QgsLayoutItemPage::decodePageOrientation( QString(), &ok );
QVERIFY( !ok );
}

QGSTEST_MAIN( TestQgsLayoutPage )
#include "testqgslayoutpage.moc"

0 comments on commit ea32391

Please sign in to comment.