Add a method to clear an existing layout

This commit is contained in:
Nyall Dawson 2017-12-04 14:29:58 +10:00
parent 8feac30f07
commit 59b6bf62ab
4 changed files with 73 additions and 0 deletions

View File

@ -49,6 +49,13 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
a new layout. a new layout.
%End %End
void clear();
%Docstring
Clears the layout.
Calling this method removes all items and pages from the layout.
%End
QgsProject *project() const; QgsProject *project() const;
%Docstring %Docstring
The project associated with the layout. Used to get access to layers, map themes, The project associated with the layout. Used to get access to layers, map themes,

View File

@ -81,6 +81,27 @@ void QgsLayout::initializeDefaults()
mUndoStack->stack()->clear(); 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 QgsProject *QgsLayout::project() const
{ {
return mProject; return mProject;

View File

@ -73,6 +73,13 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/ */
void initializeDefaults(); 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, * The project associated with the layout. Used to get access to layers, map themes,
* relations and various other bits. It is never null. * relations and various other bits. It is never null.

View File

@ -44,6 +44,7 @@ class TestQgsLayout: public QObject
void undoRedoOccurred(); void undoRedoOccurred();
void itemsOnPage(); //test fetching matching items on a set page void itemsOnPage(); //test fetching matching items on a set page
void pageIsEmpty(); void pageIsEmpty();
void clear();
private: private:
QString mReport; QString mReport;
@ -609,6 +610,43 @@ void TestQgsLayout::pageIsEmpty()
QCOMPARE( l.pageCollection()->pageIsEmpty( 2 ), true ); 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 ) QGSTEST_MAIN( TestQgsLayout )
#include "testqgslayout.moc" #include "testqgslayout.moc"