Skip to content

Commit

Permalink
Add method to detect whether layout has uniform page sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 17, 2017
1 parent 069a0ba commit 5cf36cd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/layout/qgslayoutpagecollection.sip
Expand Up @@ -198,6 +198,15 @@ Returns the maximum width of pages in the collection. The returned value is
in layout units.
%End

bool hasUniformPageSizes() const;
%Docstring
Returns true if the layout has uniform page sizes, e.g. all pages are the same size.

This method does not consider differing units as non-uniform sizes, only the actual
physical size of the pages.
:rtype: bool
%End

int pageNumberForPoint( QPointF point ) const;
%Docstring
Returns the page number corresponding to a ``point`` in the layout (in layout units).
Expand Down
18 changes: 18 additions & 0 deletions src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -95,6 +95,24 @@ QSizeF QgsLayoutPageCollection::maximumPageSize() const
return maxSize;
}

bool QgsLayoutPageCollection::hasUniformPageSizes() const
{
QSizeF size;
for ( QgsLayoutItemPage *page : mPages )
{
QSizeF pageSize = mLayout->convertToLayoutUnits( page->pageSize() );
if ( !size.isValid() )
size = pageSize;
else
{
if ( !qgsDoubleNear( pageSize.width(), size.width(), 0.01 )
|| !qgsDoubleNear( pageSize.height(), size.height(), 0.01 ) )
return false;
}
}
return true;
}

int QgsLayoutPageCollection::pageNumberForPoint( QPointF point ) const
{
int pageNumber = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -242,6 +242,14 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
*/
QSizeF maximumPageSize() const;

/**
* Returns true if the layout has uniform page sizes, e.g. all pages are the same size.
*
* This method does not consider differing units as non-uniform sizes, only the actual
* physical size of the pages.
*/
bool hasUniformPageSizes() const;

/**
* Returns the page number corresponding to a \a point in the layout (in layout units).
*
Expand Down
28 changes: 28 additions & 0 deletions tests/src/python/test_qgslayoutpagecollection.py
Expand Up @@ -282,6 +282,34 @@ def testMaxPageWidthAndSize(self):
self.assertEqual(collection.maximumPageSize().width(), 100000.0)
self.assertEqual(collection.maximumPageSize().height(), 100000.0)

def testUniformPageSizes(self):
"""
Test detection of uniform page sizes
"""
p = QgsProject()
l = QgsLayout(p)
collection = l.pageCollection()

self.assertTrue(collection.hasUniformPageSizes())

# add a page
page = QgsLayoutItemPage(l)
page.setPageSize('A4')
collection.addPage(page)
self.assertTrue(collection.hasUniformPageSizes())

# add a second page
page2 = QgsLayoutItemPage(l)
page2.setPageSize(QgsLayoutSize(21.0, 29.7, QgsUnitTypes.LayoutCentimeters))
collection.addPage(page2)
self.assertTrue(collection.hasUniformPageSizes())

# add a page with other units
page3 = QgsLayoutItemPage(l)
page3.setPageSize('A5')
collection.addPage(page3)
self.assertFalse(collection.hasUniformPageSizes())

def testReflow(self):
"""
Test reflowing pages
Expand Down

0 comments on commit 5cf36cd

Please sign in to comment.