Skip to content

Commit 5cf36cd

Browse files
committedDec 17, 2017
Add method to detect whether layout has uniform page sizes
1 parent 069a0ba commit 5cf36cd

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
 

‎python/core/layout/qgslayoutpagecollection.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ Returns the maximum width of pages in the collection. The returned value is
198198
in layout units.
199199
%End
200200

201+
bool hasUniformPageSizes() const;
202+
%Docstring
203+
Returns true if the layout has uniform page sizes, e.g. all pages are the same size.
204+
205+
This method does not consider differing units as non-uniform sizes, only the actual
206+
physical size of the pages.
207+
:rtype: bool
208+
%End
209+
201210
int pageNumberForPoint( QPointF point ) const;
202211
%Docstring
203212
Returns the page number corresponding to a ``point`` in the layout (in layout units).

‎src/core/layout/qgslayoutpagecollection.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ QSizeF QgsLayoutPageCollection::maximumPageSize() const
9595
return maxSize;
9696
}
9797

98+
bool QgsLayoutPageCollection::hasUniformPageSizes() const
99+
{
100+
QSizeF size;
101+
for ( QgsLayoutItemPage *page : mPages )
102+
{
103+
QSizeF pageSize = mLayout->convertToLayoutUnits( page->pageSize() );
104+
if ( !size.isValid() )
105+
size = pageSize;
106+
else
107+
{
108+
if ( !qgsDoubleNear( pageSize.width(), size.width(), 0.01 )
109+
|| !qgsDoubleNear( pageSize.height(), size.height(), 0.01 ) )
110+
return false;
111+
}
112+
}
113+
return true;
114+
}
115+
98116
int QgsLayoutPageCollection::pageNumberForPoint( QPointF point ) const
99117
{
100118
int pageNumber = 0;

‎src/core/layout/qgslayoutpagecollection.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
242242
*/
243243
QSizeF maximumPageSize() const;
244244

245+
/**
246+
* Returns true if the layout has uniform page sizes, e.g. all pages are the same size.
247+
*
248+
* This method does not consider differing units as non-uniform sizes, only the actual
249+
* physical size of the pages.
250+
*/
251+
bool hasUniformPageSizes() const;
252+
245253
/**
246254
* Returns the page number corresponding to a \a point in the layout (in layout units).
247255
*

‎tests/src/python/test_qgslayoutpagecollection.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,34 @@ def testMaxPageWidthAndSize(self):
282282
self.assertEqual(collection.maximumPageSize().width(), 100000.0)
283283
self.assertEqual(collection.maximumPageSize().height(), 100000.0)
284284

285+
def testUniformPageSizes(self):
286+
"""
287+
Test detection of uniform page sizes
288+
"""
289+
p = QgsProject()
290+
l = QgsLayout(p)
291+
collection = l.pageCollection()
292+
293+
self.assertTrue(collection.hasUniformPageSizes())
294+
295+
# add a page
296+
page = QgsLayoutItemPage(l)
297+
page.setPageSize('A4')
298+
collection.addPage(page)
299+
self.assertTrue(collection.hasUniformPageSizes())
300+
301+
# add a second page
302+
page2 = QgsLayoutItemPage(l)
303+
page2.setPageSize(QgsLayoutSize(21.0, 29.7, QgsUnitTypes.LayoutCentimeters))
304+
collection.addPage(page2)
305+
self.assertTrue(collection.hasUniformPageSizes())
306+
307+
# add a page with other units
308+
page3 = QgsLayoutItemPage(l)
309+
page3.setPageSize('A5')
310+
collection.addPage(page3)
311+
self.assertFalse(collection.hasUniformPageSizes())
312+
285313
def testReflow(self):
286314
"""
287315
Test reflowing pages

0 commit comments

Comments
 (0)
Please sign in to comment.