Skip to content
Permalink
Browse files

Add a method to clear an existing layout

  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent 8feac30 commit 59b6bf62abfc32ab627e83ba5edd79fc61694c73
@@ -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,
@@ -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;
@@ -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.
@@ -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;
@@ -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.
You can’t perform that action at this time.