Skip to content
Permalink
Browse files

Port pageItemBounds method from composer

  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent d7e179c commit 71dd3b933a0a5fab96c026c120e78eba9e2f2574
Showing with 43 additions and 5 deletions.
  1. +26 −0 src/core/layout/qgslayout.cpp
  2. +14 −0 src/core/layout/qgslayout.h
  3. +3 −5 tests/src/core/testqgslayout.cpp
@@ -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 );
@@ -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.
@@ -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()

0 comments on commit 71dd3b9

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