Skip to content

Commit e8a42c9

Browse files
committed
Add method to determine largest page size
1 parent 71dd3b9 commit e8a42c9

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/core/layout/qgslayoutpagecollection.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,30 @@ void QgsLayoutPageCollection::reflow()
7171
double QgsLayoutPageCollection::maximumPageWidth() const
7272
{
7373
double maxWidth = 0;
74-
Q_FOREACH ( QgsLayoutItemPage *page, mPages )
74+
for ( QgsLayoutItemPage *page : mPages )
7575
{
7676
maxWidth = std::max( maxWidth, mLayout->convertToLayoutUnits( page->pageSize() ).width() );
7777
}
7878
return maxWidth;
7979
}
8080

81+
QSizeF QgsLayoutPageCollection::maximumPageSize() const
82+
{
83+
double maxArea = 0;
84+
QSizeF maxSize;
85+
for ( QgsLayoutItemPage *page : mPages )
86+
{
87+
QSizeF pageSize = mLayout->convertToLayoutUnits( page->pageSize() );
88+
double area = pageSize.width() * pageSize.height();
89+
if ( area > maxArea )
90+
{
91+
maxArea = area;
92+
maxSize = pageSize;
93+
}
94+
}
95+
return maxSize;
96+
}
97+
8198
int QgsLayoutPageCollection::pageNumberForPoint( QPointF point ) const
8299
{
83100
int pageNumber = 0;

src/core/layout/qgslayoutpagecollection.h

+10
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,19 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
229229
/**
230230
* Returns the maximum width of pages in the collection. The returned value is
231231
* in layout units.
232+
*
233+
* \see maximumPageSize()
232234
*/
233235
double maximumPageWidth() const;
234236

237+
/**
238+
* Returns the maximum size of any page in the collection, by area. The returned value
239+
* is in layout units.
240+
*
241+
* \see maximumPageWidth()
242+
*/
243+
QSizeF maximumPageSize() const;
244+
235245
/**
236246
* Returns the page number corresponding to a \a point in the layout (in layout units).
237247
*

tests/src/python/test_qgslayoutpagecollection.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ def testExtendByNewPage(self):
250250
self.assertEqual(collection.pageCount(), 3)
251251
self.assertEqual(new_page2.sizeWithUnits(), new_page.sizeWithUnits())
252252

253-
def testMaxPageWidth(self):
253+
def testMaxPageWidthAndSize(self):
254254
"""
255-
Test calculating maximum page width
255+
Test calculating maximum page width and size
256256
"""
257257
p = QgsProject()
258258
l = QgsLayout(p)
@@ -263,18 +263,24 @@ def testMaxPageWidth(self):
263263
page.setPageSize('A4')
264264
collection.addPage(page)
265265
self.assertEqual(collection.maximumPageWidth(), 210.0)
266+
self.assertEqual(collection.maximumPageSize().width(), 210.0)
267+
self.assertEqual(collection.maximumPageSize().height(), 297.0)
266268

267269
# add a second page
268270
page2 = QgsLayoutItemPage(l)
269271
page2.setPageSize('A3')
270272
collection.addPage(page2)
271273
self.assertEqual(collection.maximumPageWidth(), 297.0)
274+
self.assertEqual(collection.maximumPageSize().width(), 297.0)
275+
self.assertEqual(collection.maximumPageSize().height(), 420.0)
272276

273277
# add a page with other units
274278
page3 = QgsLayoutItemPage(l)
275279
page3.setPageSize(QgsLayoutSize(100, 100, QgsUnitTypes.LayoutMeters))
276280
collection.addPage(page3)
277281
self.assertEqual(collection.maximumPageWidth(), 100000.0)
282+
self.assertEqual(collection.maximumPageSize().width(), 100000.0)
283+
self.assertEqual(collection.maximumPageSize().height(), 100000.0)
278284

279285
def testReflow(self):
280286
"""

0 commit comments

Comments
 (0)