Skip to content

Commit 59b6bf6

Browse files
committed
Add a method to clear an existing layout
1 parent 8feac30 commit 59b6bf6

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

python/core/layout/qgslayout.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
4949
a new layout.
5050
%End
5151

52+
void clear();
53+
%Docstring
54+
Clears the layout.
55+
56+
Calling this method removes all items and pages from the layout.
57+
%End
58+
5259
QgsProject *project() const;
5360
%Docstring
5461
The project associated with the layout. Used to get access to layers, map themes,

src/core/layout/qgslayout.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ void QgsLayout::initializeDefaults()
8181
mUndoStack->stack()->clear();
8282
}
8383

84+
void QgsLayout::clear()
85+
{
86+
deleteAndRemoveMultiFrames();
87+
88+
//delete all non paper items
89+
const QList<QGraphicsItem *> itemList = items();
90+
for ( QGraphicsItem *item : itemList )
91+
{
92+
QgsLayoutItem *cItem = dynamic_cast<QgsLayoutItem *>( item );
93+
QgsLayoutItemPage *pItem = dynamic_cast<QgsLayoutItemPage *>( item );
94+
if ( cItem && !pItem )
95+
{
96+
removeLayoutItemPrivate( cItem );
97+
}
98+
}
99+
mItemsModel->clear();
100+
101+
mPageCollection->clear();
102+
mUndoStack->stack()->clear();
103+
}
104+
84105
QgsProject *QgsLayout::project() const
85106
{
86107
return mProject;

src/core/layout/qgslayout.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
7373
*/
7474
void initializeDefaults();
7575

76+
/**
77+
* Clears the layout.
78+
*
79+
* Calling this method removes all items and pages from the layout.
80+
*/
81+
void clear();
82+
7683
/**
7784
* The project associated with the layout. Used to get access to layers, map themes,
7885
* relations and various other bits. It is never null.

tests/src/core/testqgslayout.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestQgsLayout: public QObject
4444
void undoRedoOccurred();
4545
void itemsOnPage(); //test fetching matching items on a set page
4646
void pageIsEmpty();
47+
void clear();
4748

4849
private:
4950
QString mReport;
@@ -609,6 +610,43 @@ void TestQgsLayout::pageIsEmpty()
609610
QCOMPARE( l.pageCollection()->pageIsEmpty( 2 ), true );
610611
}
611612

613+
void TestQgsLayout::clear()
614+
{
615+
QgsProject proj;
616+
QgsLayout l( &proj );
617+
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
618+
page->setPageSize( "A4" );
619+
l.pageCollection()->addPage( page );
620+
QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l );
621+
page2->setPageSize( "A4" );
622+
l.pageCollection()->addPage( page2 );
623+
QgsLayoutItemPage *page3 = new QgsLayoutItemPage( &l );
624+
page3->setPageSize( "A4" );
625+
l.pageCollection()->addPage( page3 );
626+
627+
//add some items to the composition
628+
QgsLayoutItemShape *label1 = new QgsLayoutItemShape( &l );
629+
l.addLayoutItem( label1 );
630+
QPointer< QgsLayoutItem > item1P = label1;
631+
QgsLayoutItemShape *label2 = new QgsLayoutItemShape( &l );
632+
l.addLayoutItem( label2 );
633+
QPointer< QgsLayoutItem > item2P = label2;
634+
QgsLayoutItemShape *label3 = new QgsLayoutItemShape( &l );
635+
l.addLayoutItem( label3 );
636+
QPointer< QgsLayoutItem > item3P = label3;
637+
638+
l.clear();
639+
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
640+
QCOMPARE( l.pageCollection()->pageCount(), 0 );
641+
QVERIFY( !item1P );
642+
QVERIFY( !item2P );
643+
QVERIFY( !item3P );
644+
QList< QgsLayoutItem * > items;
645+
l.layoutItems( items );
646+
QVERIFY( items.empty() );
647+
QCOMPARE( l.undoStack()->stack()->count(), 0 );
648+
}
649+
612650

613651
QGSTEST_MAIN( TestQgsLayout )
614652
#include "testqgslayout.moc"

0 commit comments

Comments
 (0)