Skip to content

Commit 71dd3b9

Browse files
committed
Port pageItemBounds method from composer
1 parent d7e179c commit 71dd3b9

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/core/layout/qgslayout.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,32 @@ QRectF QgsLayout::layoutBounds( bool ignorePages, double margin ) const
420420

421421
}
422422

423+
QRectF QgsLayout::pageItemBounds( int page, bool visibleOnly ) const
424+
{
425+
//start with an empty rectangle
426+
QRectF bounds;
427+
428+
//add all QgsLayoutItems on page
429+
const QList<QGraphicsItem *> itemList = items();
430+
for ( QGraphicsItem *item : itemList )
431+
{
432+
const QgsLayoutItem *layoutItem = dynamic_cast<const QgsLayoutItem *>( item );
433+
if ( layoutItem && layoutItem->type() != QgsLayoutItemRegistry::LayoutPage && layoutItem->page() == page )
434+
{
435+
if ( visibleOnly && !layoutItem->isVisible() )
436+
continue;
437+
438+
//expand bounds with current item's bounds
439+
if ( bounds.isValid() )
440+
bounds = bounds.united( item->sceneBoundingRect() );
441+
else
442+
bounds = item->sceneBoundingRect();
443+
}
444+
}
445+
446+
return bounds;
447+
}
448+
423449
void QgsLayout::addLayoutItem( QgsLayoutItem *item )
424450
{
425451
addLayoutItemPrivate( item );

src/core/layout/qgslayout.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,23 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
427427
* \param ignorePages set to true to ignore page items
428428
* \param margin optional marginal (in percent, e.g., 0.05 = 5% ) to add around items
429429
* \returns layout bounds, in layout units.
430+
*
431+
* \see pageItemBounds()
430432
*/
431433
QRectF layoutBounds( bool ignorePages = false, double margin = 0.0 ) const;
432434

435+
/**
436+
* Returns the bounding box of the items contained on a specified \a page.
437+
* A page number of 0 represents the first page in the layout.
438+
*
439+
* Set \a visibleOnly to true to only include visible items.
440+
*
441+
* The returned bounds are in layout units.
442+
*
443+
* \see layoutBounds()
444+
*/
445+
QRectF pageItemBounds( int page, bool visibleOnly = false ) const;
446+
433447
/**
434448
* Adds an \a item to the layout. This should be called instead of the base class addItem()
435449
* method. Ownership of the item is transferred to the layout.

tests/src/core/testqgslayout.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,25 +306,23 @@ void TestQgsLayout::bounds()
306306
QGSCOMPARENEAR( layoutBoundsNoPage.left(), 9.85, 0.01 );
307307
QGSCOMPARENEAR( layoutBoundsNoPage.top(), 49.79, 0.01 );
308308

309-
#if 0
310-
QRectF page1Bounds = composition->pageItemBounds( 0, true );
309+
QRectF page1Bounds = l.pageItemBounds( 0, true );
311310
QGSCOMPARENEAR( page1Bounds.height(), 150.36, 0.01 );
312311
QGSCOMPARENEAR( page1Bounds.width(), 155.72, 0.01 );
313312
QGSCOMPARENEAR( page1Bounds.left(), 54.43, 0.01 );
314313
QGSCOMPARENEAR( page1Bounds.top(), 49.79, 0.01 );
315314

316-
QRectF page2Bounds = composition->pageItemBounds( 1, true );
315+
QRectF page2Bounds = l.pageItemBounds( 1, true );
317316
QGSCOMPARENEAR( page2Bounds.height(), 100.30, 0.01 );
318317
QGSCOMPARENEAR( page2Bounds.width(), 50.30, 0.01 );
319318
QGSCOMPARENEAR( page2Bounds.left(), 209.85, 0.01 );
320319
QGSCOMPARENEAR( page2Bounds.top(), 249.85, 0.01 );
321320

322-
QRectF page2BoundsWithHidden = composition->pageItemBounds( 1, false );
321+
QRectF page2BoundsWithHidden = l.pageItemBounds( 1, false );
323322
QGSCOMPARENEAR( page2BoundsWithHidden.height(), 120.30, 0.01 );
324323
QGSCOMPARENEAR( page2BoundsWithHidden.width(), 250.30, 0.01 );
325324
QGSCOMPARENEAR( page2BoundsWithHidden.left(), 9.85, 0.01 );
326325
QGSCOMPARENEAR( page2BoundsWithHidden.top(), 249.85, 0.01 );
327-
#endif
328326
}
329327

330328
void TestQgsLayout::addItem()

0 commit comments

Comments
 (0)