Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Port method to retrieve items of a set type on a page
  • Loading branch information
nyalldawson committed Dec 17, 2017
1 parent a3c9dc3 commit f3fcb68
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -106,6 +106,30 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
*/
QList< QgsLayoutItem *> itemsOnPage( int page ) const;

/**
* Returns layout items of a specific type on a specified \a page.
* \note not available in Python bindings.
*/
template<class T> void itemsOnPage( QList<T *> &itemList, int page ) const SIP_SKIP
{
itemList.clear();
const QList<QGraphicsItem *> graphicsItemList = mLayout->items();
for ( QGraphicsItem *graphicsItem : graphicsItemList )
{
T *item = dynamic_cast<T *>( graphicsItem );
if ( item && item->page() == page )
{
itemList.push_back( item );
}
}
}

/**
* Returns whether the specified \a page number should be included in exports of the layouts.
* \see pageIsEmpty()
*/
bool shouldExportPage( int page ) const;

/**
* Adds a \a page to the collection. Ownership of the \a page is transferred
* to the collection, and the page will automatically be added to the collection's
Expand Down
53 changes: 48 additions & 5 deletions tests/src/core/testqgslayout.cpp
Expand Up @@ -22,6 +22,8 @@
#include "qgslayoutitemshape.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutundostack.h"
#include "qgslayoutitemlabel.h"
#include "qgslayoutitempolyline.h"

class TestQgsLayout: public QObject
{
Expand Down Expand Up @@ -521,13 +523,13 @@ void TestQgsLayout::itemsOnPage()
page3->setPageSize( "A4" );
l.pageCollection()->addPage( page3 );

QgsLayoutItemShape *label1 = new QgsLayoutItemShape( &l );
QgsLayoutItemLabel *label1 = new QgsLayoutItemLabel( &l );
l.addLayoutItem( label1 );
label1->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 0 );
QgsLayoutItemShape *label2 = new QgsLayoutItemShape( &l );
QgsLayoutItemLabel *label2 = new QgsLayoutItemLabel( &l );
l.addLayoutItem( label2 );
label2->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 0 );
QgsLayoutItemShape *label3 = new QgsLayoutItemShape( &l );
QgsLayoutItemLabel *label3 = new QgsLayoutItemLabel( &l );
l.addLayoutItem( label3 );
label3->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 1 );
QgsLayoutItemShape *shape1 = new QgsLayoutItemShape( &l );
Expand All @@ -536,10 +538,10 @@ void TestQgsLayout::itemsOnPage()
QgsLayoutItemShape *shape2 = new QgsLayoutItemShape( &l );
l.addLayoutItem( shape2 );
shape2->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 1 );
QgsLayoutItemShape *arrow1 = new QgsLayoutItemShape( &l );
QgsLayoutItemPolyline *arrow1 = new QgsLayoutItemPolyline( &l );
l.addLayoutItem( arrow1 );
arrow1->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 2 );
QgsLayoutItemShape *arrow2 = new QgsLayoutItemShape( &l );
QgsLayoutItemPolyline *arrow2 = new QgsLayoutItemPolyline( &l );
l.addLayoutItem( arrow2 );
arrow2->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 2 );

Expand All @@ -554,6 +556,40 @@ void TestQgsLayout::itemsOnPage()
//should be 3 items on page 3
QCOMPARE( items.length(), 3 );

//check fetching specific item types
QList<QgsLayoutItemLabel *> labels;
l.pageCollection()->itemsOnPage( labels, 0 );
//should be 2 labels on page 1
QCOMPARE( labels.length(), 2 );
l.pageCollection()->itemsOnPage( labels, 1 );
//should be 1 label on page 2
QCOMPARE( labels.length(), 1 );
l.pageCollection()->itemsOnPage( labels, 2 );
//should be no label on page 3
QCOMPARE( labels.length(), 0 );

QList<QgsLayoutItemShape *> shapes;
l.pageCollection()->itemsOnPage( shapes, 0 );
//should be 1 shapes on page 1
QCOMPARE( shapes.length(), 1 );
l.pageCollection()->itemsOnPage( shapes, 1 );
//should be 1 shapes on page 2
QCOMPARE( shapes.length(), 1 );
l.pageCollection()->itemsOnPage( shapes, 2 );
//should be no shapes on page 3
QCOMPARE( shapes.length(), 0 );

QList<QgsLayoutItemPolyline *> arrows;
l.pageCollection()->itemsOnPage( arrows, 0 );
//should be no arrows on page 1
QCOMPARE( arrows.length(), 0 );
l.pageCollection()->itemsOnPage( arrows, 1 );
//should be no arrows on page 2
QCOMPARE( arrows.length(), 0 );
l.pageCollection()->itemsOnPage( arrows, 2 );
//should be 2 arrows on page 3
QCOMPARE( arrows.length(), 2 );

l.removeLayoutItem( label1 );
l.removeLayoutItem( label2 );
l.removeLayoutItem( label3 );
Expand All @@ -569,6 +605,13 @@ void TestQgsLayout::itemsOnPage()
QCOMPARE( items.length(), 1 );
items = l.pageCollection()->itemsOnPage( 2 );
QCOMPARE( items.length(), 1 );

l.pageCollection()->itemsOnPage( labels, 0 );
QCOMPARE( labels.length(), 0 );
l.pageCollection()->itemsOnPage( labels, 1 );
QCOMPARE( labels.length(), 0 );
l.pageCollection()->itemsOnPage( labels, 2 );
QCOMPARE( labels.length(), 0 );
}

void TestQgsLayout::pageIsEmpty()
Expand Down

0 comments on commit f3fcb68

Please sign in to comment.