Skip to content

Commit ea32391

Browse files
committedJul 25, 2017
Add methods for handling page size to QgsLayoutItemPage
1 parent 534f7ab commit ea32391

File tree

8 files changed

+297
-2
lines changed

8 files changed

+297
-2
lines changed
 

‎python/core/layout/qgslayout.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
2222

2323
enum ZValues
2424
{
25+
ZPage,
2526
ZMapTool,
2627
};
2728

‎python/core/layout/qgslayoutitempage.sip

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,61 @@ class QgsLayoutItemPage : QgsLayoutItem
2020
%End
2121
public:
2222

23+
enum Orientation
24+
{
25+
Portrait,
26+
Landscape
27+
};
28+
2329
explicit QgsLayoutItemPage( QgsLayout *layout /TransferThis/ );
2430
%Docstring
2531
Constructor for QgsLayoutItemPage, with the specified parent ``layout``.
2632
%End
2733
virtual int type() const;
2834
virtual QString stringType() const;
2935

36+
void setPageSize( const QgsLayoutSize &size );
37+
%Docstring
38+
Sets the ``size`` of the page.
39+
.. seealso:: pageSize()
40+
%End
41+
42+
bool setPageSize( const QString &size, Orientation orientation = Portrait );
43+
%Docstring
44+
Sets the page size to a known page ``size``, e.g. "A4" and ``orientation``.
45+
The known page sizes are managed by QgsPageSizeRegistry. Valid page sizes
46+
can be retrieved via QgsPageSizeRegistry.entries().
47+
The function returns true if ``size`` was a valid page size and the page
48+
size was changed. If false is returned then ``size`` could not be matched
49+
to a known page size.
50+
.. seealso:: pageSize()
51+
:rtype: bool
52+
%End
53+
54+
QgsLayoutSize pageSize() const;
55+
%Docstring
56+
Returns the size of the page.
57+
.. seealso:: setPageSize()
58+
:rtype: QgsLayoutSize
59+
%End
60+
61+
Orientation orientation() const;
62+
%Docstring
63+
Returns the page orientiation.
64+
.. note::
65+
66+
There is no direct setter for page orientation - use setPageSize() instead.
67+
:rtype: Orientation
68+
%End
69+
70+
static QgsLayoutItemPage::Orientation decodePageOrientation( const QString &string, bool *ok /Out/ = 0 );
71+
%Docstring
72+
Decodes a ``string`` representing a page orientation. If specified, ``ok``
73+
will be set to true if string could be successfully interpreted as a
74+
page orientation.
75+
:rtype: QgsLayoutItemPage.Orientation
76+
%End
77+
3078
protected:
3179

3280
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );

‎src/core/layout/qgslayout.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
3939
//! Preset item z-values, to ensure correct stacking
4040
enum ZValues
4141
{
42-
ZMapTool = 10000, //!< Z-Value for temporary map tool items
42+
ZPage = 0, //!< Z-value for page (paper) items
43+
ZMapTool = 10000, //!< Z-value for temporary map tool items
4344
};
4445

4546
/**

‎src/core/layout/qgslayoutitem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
7878
}
7979

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

8383
if ( useImageCache )
8484
{

‎src/core/layout/qgslayoutitempage.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,85 @@
1717
#include "qgslayoutitempage.h"
1818
#include "qgslayout.h"
1919
#include "qgslayoututils.h"
20+
#include "qgspagesizeregistry.h"
2021
#include "qgssymbollayerutils.h"
2122
#include <QPainter>
2223

2324
#define SHADOW_WIDTH_PIXELS 5
2425
QgsLayoutItemPage::QgsLayoutItemPage( QgsLayout *layout )
2526
: QgsLayoutItem( layout )
2627
{
28+
setFlag( QGraphicsItem::ItemIsSelectable, false );
29+
setFlag( QGraphicsItem::ItemIsMovable, false );
30+
setZValue( QgsLayout::ZPage );
31+
}
32+
33+
void QgsLayoutItemPage::setPageSize( const QgsLayoutSize &size )
34+
{
35+
attemptResize( size );
36+
}
37+
38+
bool QgsLayoutItemPage::setPageSize( const QString &size, Orientation orientation )
39+
{
40+
QgsPageSize newSize;
41+
if ( QgsApplication::pageSizeRegistry()->decodePageSize( size, newSize ) )
42+
{
43+
switch ( orientation )
44+
{
45+
case Portrait:
46+
break; // nothing to do
47+
48+
case Landscape:
49+
{
50+
// flip height and width
51+
double x = newSize.size.width();
52+
newSize.size.setWidth( newSize.size.height() );
53+
newSize.size.setHeight( x );
54+
break;
55+
}
56+
}
57+
58+
setPageSize( newSize.size );
59+
return true;
60+
}
61+
else
62+
{
63+
return false;
64+
}
65+
}
2766

67+
QgsLayoutSize QgsLayoutItemPage::pageSize() const
68+
{
69+
return sizeWithUnits();
70+
}
71+
72+
QgsLayoutItemPage::Orientation QgsLayoutItemPage::orientation() const
73+
{
74+
if ( sizeWithUnits().width() >= sizeWithUnits().height() )
75+
return Landscape;
76+
else
77+
return Portrait;
78+
}
79+
80+
QgsLayoutItemPage::Orientation QgsLayoutItemPage::decodePageOrientation( const QString &string, bool *ok )
81+
{
82+
if ( ok )
83+
*ok = false;
84+
85+
QString trimmedString = string.trimmed();
86+
if ( trimmedString.compare( QStringLiteral( "portrait" ), Qt::CaseInsensitive ) == 0 )
87+
{
88+
if ( ok )
89+
*ok = true;
90+
return Portrait;
91+
}
92+
else if ( trimmedString.compare( QStringLiteral( "landscape" ), Qt::CaseInsensitive ) == 0 )
93+
{
94+
if ( ok )
95+
*ok = true;
96+
return Landscape;
97+
}
98+
return Landscape;
2899
}
29100

30101
void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )

‎src/core/layout/qgslayoutitempage.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgis_core.h"
2121
#include "qgslayoutitem.h"
2222
#include "qgslayoutitemregistry.h"
23+
#include "qgis_sip.h"
2324

2425
/**
2526
* \ingroup core
@@ -34,13 +35,56 @@ class CORE_EXPORT QgsLayoutItemPage : public QgsLayoutItem
3435

3536
public:
3637

38+
//! Page orientiation
39+
enum Orientation
40+
{
41+
Portrait, //!< Portrait orientation
42+
Landscape //!< Landscape orientation
43+
};
44+
3745
/**
3846
* Constructor for QgsLayoutItemPage, with the specified parent \a layout.
3947
*/
4048
explicit QgsLayoutItemPage( QgsLayout *layout SIP_TRANSFERTHIS );
4149
int type() const override { return QgsLayoutItemRegistry::LayoutPage; }
4250
QString stringType() const override { return QStringLiteral( "ItemPaper" ); }
4351

52+
/**
53+
* Sets the \a size of the page.
54+
* \see pageSize()
55+
*/
56+
void setPageSize( const QgsLayoutSize &size );
57+
58+
/**
59+
* Sets the page size to a known page \a size, e.g. "A4" and \a orientation.
60+
* The known page sizes are managed by QgsPageSizeRegistry. Valid page sizes
61+
* can be retrieved via QgsPageSizeRegistry::entries().
62+
* The function returns true if \a size was a valid page size and the page
63+
* size was changed. If false is returned then \a size could not be matched
64+
* to a known page size.
65+
* \see pageSize()
66+
*/
67+
bool setPageSize( const QString &size, Orientation orientation = Portrait );
68+
69+
/**
70+
* Returns the size of the page.
71+
* \see setPageSize()
72+
*/
73+
QgsLayoutSize pageSize() const;
74+
75+
/**
76+
* Returns the page orientiation.
77+
* \note There is no direct setter for page orientation - use setPageSize() instead.
78+
*/
79+
Orientation orientation() const;
80+
81+
/**
82+
* Decodes a \a string representing a page orientation. If specified, \a ok
83+
* will be set to true if string could be successfully interpreted as a
84+
* page orientation.
85+
*/
86+
static QgsLayoutItemPage::Orientation decodePageOrientation( const QString &string, bool *ok SIP_OUT = nullptr );
87+
4488
protected:
4589

4690
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ SET(TESTS
130130
testqgslayoutcontext.cpp
131131
testqgslayoutitem.cpp
132132
testqgslayoutobject.cpp
133+
testqgslayoutpage.cpp
133134
testqgslayoutunits.cpp
134135
testqgslayoututils.cpp
135136
testqgslegendrenderer.cpp

‎tests/src/core/testqgslayoutpage.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/***************************************************************************
2+
testqgslayoutpage.cpp
3+
---------------------
4+
begin : November 2014
5+
copyright : (C) 2014 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgslayout.h"
19+
#include "qgslayoutitempage.h"
20+
#include "qgslayoutitemregistry.h"
21+
#include "qgis.h"
22+
#include "qgsproject.h"
23+
#include <QObject>
24+
#include "qgstest.h"
25+
26+
class TestQgsLayoutPage : public QObject
27+
{
28+
Q_OBJECT
29+
30+
private slots:
31+
void initTestCase();// will be called before the first testfunction is executed.
32+
void cleanupTestCase();// will be called after the last testfunction was executed.
33+
void init();// will be called before each testfunction is executed.
34+
void cleanup();// will be called after every testfunction.
35+
void itemType();
36+
void pageSize();
37+
void decodePageOrientation();
38+
39+
private:
40+
QString mReport;
41+
42+
};
43+
44+
void TestQgsLayoutPage::initTestCase()
45+
{
46+
mReport = "<h1>Layout Page Tests</h1>\n";
47+
}
48+
49+
void TestQgsLayoutPage::cleanupTestCase()
50+
{
51+
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
52+
QFile myFile( myReportFile );
53+
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
54+
{
55+
QTextStream myQTextStream( &myFile );
56+
myQTextStream << mReport;
57+
myFile.close();
58+
}
59+
}
60+
61+
void TestQgsLayoutPage::init()
62+
{
63+
64+
}
65+
66+
void TestQgsLayoutPage::cleanup()
67+
{
68+
69+
}
70+
71+
void TestQgsLayoutPage::itemType()
72+
{
73+
QgsProject p;
74+
QgsLayout l( &p );
75+
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
76+
QCOMPARE( page->type(), static_cast< int >( QgsLayoutItemRegistry::LayoutPage ) );
77+
}
78+
79+
void TestQgsLayoutPage::pageSize()
80+
{
81+
QgsProject p;
82+
QgsLayout l( &p );
83+
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
84+
page->setPageSize( QgsLayoutSize( 270, 297, QgsUnitTypes::LayoutMeters ) );
85+
QCOMPARE( page->pageSize().width(), 270.0 );
86+
QCOMPARE( page->pageSize().height(), 297.0 );
87+
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMeters );
88+
QCOMPARE( page->orientation(), QgsLayoutItemPage::Portrait );
89+
page->setPageSize( QgsLayoutSize( 297, 270, QgsUnitTypes::LayoutMeters ) );
90+
QCOMPARE( page->orientation(), QgsLayoutItemPage::Landscape );
91+
92+
// from registry
93+
QVERIFY( !page->setPageSize( "hoooyeah" ) );
94+
// should be unchanged
95+
QCOMPARE( page->pageSize().width(), 297.0 );
96+
QCOMPARE( page->pageSize().height(), 270.0 );
97+
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMeters );
98+
99+
// good size
100+
QVERIFY( page->setPageSize( "A5" ) );
101+
QCOMPARE( page->pageSize().width(), 148.0 );
102+
QCOMPARE( page->pageSize().height(), 210.0 );
103+
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMillimeters );
104+
QCOMPARE( page->orientation(), QgsLayoutItemPage::Portrait );
105+
106+
QVERIFY( page->setPageSize( "A5", QgsLayoutItemPage::Landscape ) );
107+
QCOMPARE( page->pageSize().width(), 210.0 );
108+
QCOMPARE( page->pageSize().height(), 148.0 );
109+
QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMillimeters );
110+
QCOMPARE( page->orientation(), QgsLayoutItemPage::Landscape );
111+
112+
}
113+
114+
void TestQgsLayoutPage::decodePageOrientation()
115+
{
116+
//test good string
117+
bool ok = false;
118+
QCOMPARE( QgsLayoutItemPage::decodePageOrientation( QString( " porTrait " ), &ok ), QgsLayoutItemPage::Portrait );
119+
QVERIFY( ok );
120+
QCOMPARE( QgsLayoutItemPage::decodePageOrientation( QString( "landscape" ), &ok ), QgsLayoutItemPage::Landscape );
121+
QVERIFY( ok );
122+
123+
//test bad string
124+
QgsLayoutItemPage::decodePageOrientation( QString(), &ok );
125+
QVERIFY( !ok );
126+
}
127+
128+
QGSTEST_MAIN( TestQgsLayoutPage )
129+
#include "testqgslayoutpage.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.