Skip to content

Commit

Permalink
Port pageItemBounds method from composer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 17, 2017
1 parent d7e179c commit 71dd3b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -420,6 +420,32 @@ QRectF QgsLayout::layoutBounds( bool ignorePages, double margin ) const

}

QRectF QgsLayout::pageItemBounds( int page, bool visibleOnly ) const
{
//start with an empty rectangle
QRectF bounds;

//add all QgsLayoutItems on page
const QList<QGraphicsItem *> itemList = items();
for ( QGraphicsItem *item : itemList )
{
const QgsLayoutItem *layoutItem = dynamic_cast<const QgsLayoutItem *>( item );
if ( layoutItem && layoutItem->type() != QgsLayoutItemRegistry::LayoutPage && layoutItem->page() == page )
{
if ( visibleOnly && !layoutItem->isVisible() )
continue;

//expand bounds with current item's bounds
if ( bounds.isValid() )
bounds = bounds.united( item->sceneBoundingRect() );
else
bounds = item->sceneBoundingRect();
}
}

return bounds;
}

void QgsLayout::addLayoutItem( QgsLayoutItem *item )
{
addLayoutItemPrivate( item );
Expand Down
14 changes: 14 additions & 0 deletions src/core/layout/qgslayout.h
Expand Up @@ -427,9 +427,23 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
* \param ignorePages set to true to ignore page items
* \param margin optional marginal (in percent, e.g., 0.05 = 5% ) to add around items
* \returns layout bounds, in layout units.
*
* \see pageItemBounds()
*/
QRectF layoutBounds( bool ignorePages = false, double margin = 0.0 ) const;

/**
* Returns the bounding box of the items contained on a specified \a page.
* A page number of 0 represents the first page in the layout.
*
* Set \a visibleOnly to true to only include visible items.
*
* The returned bounds are in layout units.
*
* \see layoutBounds()
*/
QRectF pageItemBounds( int page, bool visibleOnly = false ) const;

/**
* Adds an \a item to the layout. This should be called instead of the base class addItem()
* method. Ownership of the item is transferred to the layout.
Expand Down
8 changes: 3 additions & 5 deletions tests/src/core/testqgslayout.cpp
Expand Up @@ -306,25 +306,23 @@ void TestQgsLayout::bounds()
QGSCOMPARENEAR( layoutBoundsNoPage.left(), 9.85, 0.01 );
QGSCOMPARENEAR( layoutBoundsNoPage.top(), 49.79, 0.01 );

#if 0
QRectF page1Bounds = composition->pageItemBounds( 0, true );
QRectF page1Bounds = l.pageItemBounds( 0, true );
QGSCOMPARENEAR( page1Bounds.height(), 150.36, 0.01 );
QGSCOMPARENEAR( page1Bounds.width(), 155.72, 0.01 );
QGSCOMPARENEAR( page1Bounds.left(), 54.43, 0.01 );
QGSCOMPARENEAR( page1Bounds.top(), 49.79, 0.01 );

QRectF page2Bounds = composition->pageItemBounds( 1, true );
QRectF page2Bounds = l.pageItemBounds( 1, true );
QGSCOMPARENEAR( page2Bounds.height(), 100.30, 0.01 );
QGSCOMPARENEAR( page2Bounds.width(), 50.30, 0.01 );
QGSCOMPARENEAR( page2Bounds.left(), 209.85, 0.01 );
QGSCOMPARENEAR( page2Bounds.top(), 249.85, 0.01 );

QRectF page2BoundsWithHidden = composition->pageItemBounds( 1, false );
QRectF page2BoundsWithHidden = l.pageItemBounds( 1, false );
QGSCOMPARENEAR( page2BoundsWithHidden.height(), 120.30, 0.01 );
QGSCOMPARENEAR( page2BoundsWithHidden.width(), 250.30, 0.01 );
QGSCOMPARENEAR( page2BoundsWithHidden.left(), 9.85, 0.01 );
QGSCOMPARENEAR( page2BoundsWithHidden.top(), 249.85, 0.01 );
#endif
}

void TestQgsLayout::addItem()
Expand Down

0 comments on commit 71dd3b9

Please sign in to comment.