Skip to content
Permalink
Browse files

Port method to retrieve items of a set type on a page

  • Loading branch information
nyalldawson committed Dec 5, 2017
1 parent a3c9dc3 commit f3fcb68ec45fb1ab5ddee749870396e0ac5a5de4
Showing with 72 additions and 5 deletions.
  1. +24 −0 src/core/layout/qgslayoutpagecollection.h
  2. +48 −5 tests/src/core/testqgslayout.cpp
@@ -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
@@ -22,6 +22,8 @@
#include "qgslayoutitemshape.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutundostack.h"
#include "qgslayoutitemlabel.h"
#include "qgslayoutitempolyline.h"

class TestQgsLayout: public QObject
{
@@ -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 );
@@ -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 );

@@ -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 );
@@ -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()

0 comments on commit f3fcb68

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