Skip to content

Commit

Permalink
Add method to determine largest page size
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 17, 2017
1 parent 71dd3b9 commit e8a42c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -71,13 +71,30 @@ void QgsLayoutPageCollection::reflow()
double QgsLayoutPageCollection::maximumPageWidth() const
{
double maxWidth = 0;
Q_FOREACH ( QgsLayoutItemPage *page, mPages )
for ( QgsLayoutItemPage *page : mPages )
{
maxWidth = std::max( maxWidth, mLayout->convertToLayoutUnits( page->pageSize() ).width() );
}
return maxWidth;
}

QSizeF QgsLayoutPageCollection::maximumPageSize() const
{
double maxArea = 0;
QSizeF maxSize;
for ( QgsLayoutItemPage *page : mPages )
{
QSizeF pageSize = mLayout->convertToLayoutUnits( page->pageSize() );
double area = pageSize.width() * pageSize.height();
if ( area > maxArea )
{
maxArea = area;
maxSize = pageSize;
}
}
return maxSize;
}

int QgsLayoutPageCollection::pageNumberForPoint( QPointF point ) const
{
int pageNumber = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -229,9 +229,19 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
/**
* Returns the maximum width of pages in the collection. The returned value is
* in layout units.
*
* \see maximumPageSize()
*/
double maximumPageWidth() const;

/**
* Returns the maximum size of any page in the collection, by area. The returned value
* is in layout units.
*
* \see maximumPageWidth()
*/
QSizeF maximumPageSize() const;

/**
* Returns the page number corresponding to a \a point in the layout (in layout units).
*
Expand Down
10 changes: 8 additions & 2 deletions tests/src/python/test_qgslayoutpagecollection.py
Expand Up @@ -250,9 +250,9 @@ def testExtendByNewPage(self):
self.assertEqual(collection.pageCount(), 3)
self.assertEqual(new_page2.sizeWithUnits(), new_page.sizeWithUnits())

def testMaxPageWidth(self):
def testMaxPageWidthAndSize(self):
"""
Test calculating maximum page width
Test calculating maximum page width and size
"""
p = QgsProject()
l = QgsLayout(p)
Expand All @@ -263,18 +263,24 @@ def testMaxPageWidth(self):
page.setPageSize('A4')
collection.addPage(page)
self.assertEqual(collection.maximumPageWidth(), 210.0)
self.assertEqual(collection.maximumPageSize().width(), 210.0)
self.assertEqual(collection.maximumPageSize().height(), 297.0)

# add a second page
page2 = QgsLayoutItemPage(l)
page2.setPageSize('A3')
collection.addPage(page2)
self.assertEqual(collection.maximumPageWidth(), 297.0)
self.assertEqual(collection.maximumPageSize().width(), 297.0)
self.assertEqual(collection.maximumPageSize().height(), 420.0)

# add a page with other units
page3 = QgsLayoutItemPage(l)
page3.setPageSize(QgsLayoutSize(100, 100, QgsUnitTypes.LayoutMeters))
collection.addPage(page3)
self.assertEqual(collection.maximumPageWidth(), 100000.0)
self.assertEqual(collection.maximumPageSize().width(), 100000.0)
self.assertEqual(collection.maximumPageSize().height(), 100000.0)

def testReflow(self):
"""
Expand Down

0 comments on commit e8a42c9

Please sign in to comment.