Skip to content

Commit f3fcb68

Browse files
committed
Port method to retrieve items of a set type on a page
1 parent a3c9dc3 commit f3fcb68

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

src/core/layout/qgslayoutpagecollection.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
106106
*/
107107
QList< QgsLayoutItem *> itemsOnPage( int page ) const;
108108

109+
/**
110+
* Returns layout items of a specific type on a specified \a page.
111+
* \note not available in Python bindings.
112+
*/
113+
template<class T> void itemsOnPage( QList<T *> &itemList, int page ) const SIP_SKIP
114+
{
115+
itemList.clear();
116+
const QList<QGraphicsItem *> graphicsItemList = mLayout->items();
117+
for ( QGraphicsItem *graphicsItem : graphicsItemList )
118+
{
119+
T *item = dynamic_cast<T *>( graphicsItem );
120+
if ( item && item->page() == page )
121+
{
122+
itemList.push_back( item );
123+
}
124+
}
125+
}
126+
127+
/**
128+
* Returns whether the specified \a page number should be included in exports of the layouts.
129+
* \see pageIsEmpty()
130+
*/
131+
bool shouldExportPage( int page ) const;
132+
109133
/**
110134
* Adds a \a page to the collection. Ownership of the \a page is transferred
111135
* to the collection, and the page will automatically be added to the collection's

tests/src/core/testqgslayout.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "qgslayoutitemshape.h"
2323
#include "qgslayoutpagecollection.h"
2424
#include "qgslayoutundostack.h"
25+
#include "qgslayoutitemlabel.h"
26+
#include "qgslayoutitempolyline.h"
2527

2628
class TestQgsLayout: public QObject
2729
{
@@ -521,13 +523,13 @@ void TestQgsLayout::itemsOnPage()
521523
page3->setPageSize( "A4" );
522524
l.pageCollection()->addPage( page3 );
523525

524-
QgsLayoutItemShape *label1 = new QgsLayoutItemShape( &l );
526+
QgsLayoutItemLabel *label1 = new QgsLayoutItemLabel( &l );
525527
l.addLayoutItem( label1 );
526528
label1->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 0 );
527-
QgsLayoutItemShape *label2 = new QgsLayoutItemShape( &l );
529+
QgsLayoutItemLabel *label2 = new QgsLayoutItemLabel( &l );
528530
l.addLayoutItem( label2 );
529531
label2->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 0 );
530-
QgsLayoutItemShape *label3 = new QgsLayoutItemShape( &l );
532+
QgsLayoutItemLabel *label3 = new QgsLayoutItemLabel( &l );
531533
l.addLayoutItem( label3 );
532534
label3->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 1 );
533535
QgsLayoutItemShape *shape1 = new QgsLayoutItemShape( &l );
@@ -536,10 +538,10 @@ void TestQgsLayout::itemsOnPage()
536538
QgsLayoutItemShape *shape2 = new QgsLayoutItemShape( &l );
537539
l.addLayoutItem( shape2 );
538540
shape2->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 1 );
539-
QgsLayoutItemShape *arrow1 = new QgsLayoutItemShape( &l );
541+
QgsLayoutItemPolyline *arrow1 = new QgsLayoutItemPolyline( &l );
540542
l.addLayoutItem( arrow1 );
541543
arrow1->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 2 );
542-
QgsLayoutItemShape *arrow2 = new QgsLayoutItemShape( &l );
544+
QgsLayoutItemPolyline *arrow2 = new QgsLayoutItemPolyline( &l );
543545
l.addLayoutItem( arrow2 );
544546
arrow2->attemptMove( QgsLayoutPoint( 10, 10 ), true, false, 2 );
545547

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

559+
//check fetching specific item types
560+
QList<QgsLayoutItemLabel *> labels;
561+
l.pageCollection()->itemsOnPage( labels, 0 );
562+
//should be 2 labels on page 1
563+
QCOMPARE( labels.length(), 2 );
564+
l.pageCollection()->itemsOnPage( labels, 1 );
565+
//should be 1 label on page 2
566+
QCOMPARE( labels.length(), 1 );
567+
l.pageCollection()->itemsOnPage( labels, 2 );
568+
//should be no label on page 3
569+
QCOMPARE( labels.length(), 0 );
570+
571+
QList<QgsLayoutItemShape *> shapes;
572+
l.pageCollection()->itemsOnPage( shapes, 0 );
573+
//should be 1 shapes on page 1
574+
QCOMPARE( shapes.length(), 1 );
575+
l.pageCollection()->itemsOnPage( shapes, 1 );
576+
//should be 1 shapes on page 2
577+
QCOMPARE( shapes.length(), 1 );
578+
l.pageCollection()->itemsOnPage( shapes, 2 );
579+
//should be no shapes on page 3
580+
QCOMPARE( shapes.length(), 0 );
581+
582+
QList<QgsLayoutItemPolyline *> arrows;
583+
l.pageCollection()->itemsOnPage( arrows, 0 );
584+
//should be no arrows on page 1
585+
QCOMPARE( arrows.length(), 0 );
586+
l.pageCollection()->itemsOnPage( arrows, 1 );
587+
//should be no arrows on page 2
588+
QCOMPARE( arrows.length(), 0 );
589+
l.pageCollection()->itemsOnPage( arrows, 2 );
590+
//should be 2 arrows on page 3
591+
QCOMPARE( arrows.length(), 2 );
592+
557593
l.removeLayoutItem( label1 );
558594
l.removeLayoutItem( label2 );
559595
l.removeLayoutItem( label3 );
@@ -569,6 +605,13 @@ void TestQgsLayout::itemsOnPage()
569605
QCOMPARE( items.length(), 1 );
570606
items = l.pageCollection()->itemsOnPage( 2 );
571607
QCOMPARE( items.length(), 1 );
608+
609+
l.pageCollection()->itemsOnPage( labels, 0 );
610+
QCOMPARE( labels.length(), 0 );
611+
l.pageCollection()->itemsOnPage( labels, 1 );
612+
QCOMPARE( labels.length(), 0 );
613+
l.pageCollection()->itemsOnPage( labels, 2 );
614+
QCOMPARE( labels.length(), 0 );
572615
}
573616

574617
void TestQgsLayout::pageIsEmpty()

0 commit comments

Comments
 (0)