Skip to content
Permalink
Browse files

Add method to detect whether layout has uniform page sizes

  • Loading branch information
nyalldawson committed Dec 11, 2017
1 parent 069a0ba commit 5cf36cd819fc1ed7ced23f41a882b48b28e01485
@@ -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).
@@ -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;
@@ -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).
*
@@ -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

0 comments on commit 5cf36cd

Please sign in to comment.
You can’t perform that action at this time.