Skip to content
Permalink
Browse files

Add method to determine largest page size

  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent 71dd3b9 commit e8a42c92b49460bac20d0568d5261cfd42f5df45
@@ -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;
@@ -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).
*
@@ -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)
@@ -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):
"""

0 comments on commit e8a42c9

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