Skip to content

Commit

Permalink
Add a method to clear an existing layout
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent 8feac30 commit 59b6bf6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/layout/qgslayout.sip
Expand Up @@ -49,6 +49,13 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
a new layout.
%End

void clear();
%Docstring
Clears the layout.

Calling this method removes all items and pages from the layout.
%End

QgsProject *project() const;
%Docstring
The project associated with the layout. Used to get access to layers, map themes,
Expand Down
21 changes: 21 additions & 0 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -81,6 +81,27 @@ void QgsLayout::initializeDefaults()
mUndoStack->stack()->clear();
}

void QgsLayout::clear()
{
deleteAndRemoveMultiFrames();

//delete all non paper items
const QList<QGraphicsItem *> itemList = items();
for ( QGraphicsItem *item : itemList )
{
QgsLayoutItem *cItem = dynamic_cast<QgsLayoutItem *>( item );
QgsLayoutItemPage *pItem = dynamic_cast<QgsLayoutItemPage *>( item );
if ( cItem && !pItem )
{
removeLayoutItemPrivate( cItem );
}
}
mItemsModel->clear();

mPageCollection->clear();
mUndoStack->stack()->clear();
}

QgsProject *QgsLayout::project() const
{
return mProject;
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayout.h
Expand Up @@ -73,6 +73,13 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/
void initializeDefaults();

/**
* Clears the layout.
*
* Calling this method removes all items and pages from the layout.
*/
void clear();

/**
* The project associated with the layout. Used to get access to layers, map themes,
* relations and various other bits. It is never null.
Expand Down
38 changes: 38 additions & 0 deletions tests/src/core/testqgslayout.cpp
Expand Up @@ -44,6 +44,7 @@ class TestQgsLayout: public QObject
void undoRedoOccurred();
void itemsOnPage(); //test fetching matching items on a set page
void pageIsEmpty();
void clear();

private:
QString mReport;
Expand Down Expand Up @@ -609,6 +610,43 @@ void TestQgsLayout::pageIsEmpty()
QCOMPARE( l.pageCollection()->pageIsEmpty( 2 ), true );
}

void TestQgsLayout::clear()
{
QgsProject proj;
QgsLayout l( &proj );
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
page->setPageSize( "A4" );
l.pageCollection()->addPage( page );
QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l );
page2->setPageSize( "A4" );
l.pageCollection()->addPage( page2 );
QgsLayoutItemPage *page3 = new QgsLayoutItemPage( &l );
page3->setPageSize( "A4" );
l.pageCollection()->addPage( page3 );

//add some items to the composition
QgsLayoutItemShape *label1 = new QgsLayoutItemShape( &l );
l.addLayoutItem( label1 );
QPointer< QgsLayoutItem > item1P = label1;
QgsLayoutItemShape *label2 = new QgsLayoutItemShape( &l );
l.addLayoutItem( label2 );
QPointer< QgsLayoutItem > item2P = label2;
QgsLayoutItemShape *label3 = new QgsLayoutItemShape( &l );
l.addLayoutItem( label3 );
QPointer< QgsLayoutItem > item3P = label3;

l.clear();
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QCOMPARE( l.pageCollection()->pageCount(), 0 );
QVERIFY( !item1P );
QVERIFY( !item2P );
QVERIFY( !item3P );
QList< QgsLayoutItem * > items;
l.layoutItems( items );
QVERIFY( items.empty() );
QCOMPARE( l.undoStack()->stack()->count(), 0 );
}


QGSTEST_MAIN( TestQgsLayout )
#include "testqgslayout.moc"

0 comments on commit 59b6bf6

Please sign in to comment.