Skip to content

Commit

Permalink
Add method to clear page collections for layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent 6a78d48 commit 8feac30
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/core/layout/qgslayoutpagecollection.sip
Expand Up @@ -144,6 +144,8 @@ class QgsLayoutPageCollection : QObject, QgsLayoutSerializableObject
the first page in the collection.

Calling deletePage() automatically triggers a reflow() of pages.

.. seealso:: clear()
%End

void deletePage( QgsLayoutItemPage *page );
Expand All @@ -152,6 +154,14 @@ class QgsLayoutPageCollection : QObject, QgsLayoutSerializableObject
from the collection's layout().

Calling deletePage() automatically triggers a reflow() of pages.

.. seealso:: clear()
%End

void clear();
%Docstring
Removes all pages from the collection.
.. seealso:: deletePage()
%End

QgsLayoutItemPage *takePage( QgsLayoutItemPage *page ) /TransferBack/;
Expand Down
20 changes: 20 additions & 0 deletions src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -454,6 +454,26 @@ void QgsLayoutPageCollection::deletePage( QgsLayoutItemPage *page )
}
}

void QgsLayoutPageCollection::clear()
{
if ( !mBlockUndoCommands )
{
mLayout->undoStack()->beginMacro( tr( "Remove Pages" ) );
mLayout->undoStack()->beginCommand( this, tr( "Remove Pages" ) );
}
for ( int i = mPages.count() - 1; i >= 0; --i )
{
emit pageAboutToBeRemoved( i );
mPages.takeAt( i )->deleteLater();
}
reflow();
if ( !mBlockUndoCommands )
{
mLayout->undoStack()->endCommand();
mLayout->undoStack()->endMacro();
}
}

QgsLayoutItemPage *QgsLayoutPageCollection::takePage( QgsLayoutItemPage *page )
{
mPages.removeAll( page );
Expand Down
10 changes: 10 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -153,6 +153,8 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
* the first page in the collection.
*
* Calling deletePage() automatically triggers a reflow() of pages.
*
* \see clear()
*/
void deletePage( int pageNumber );

Expand All @@ -161,9 +163,17 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
* from the collection's layout().
*
* Calling deletePage() automatically triggers a reflow() of pages.
*
* \see clear()
*/
void deletePage( QgsLayoutItemPage *page );

/**
* Removes all pages from the collection.
* \see deletePage()
*/
void clear();

/**
* Takes a \a page from the collection, returning ownership of the page to the caller.
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/src/python/test_qgslayoutpagecollection.py
Expand Up @@ -187,6 +187,36 @@ def testDeletePages(self):
self.assertEqual(len(page_about_to_be_removed_spy), 2)
self.assertEqual(page_about_to_be_removed_spy[-1][0], 0)

def testClear(self):
"""
Test clearing the collection
"""
p = QgsProject()
l = QgsLayout(p)
collection = l.pageCollection()

collection.clear()

# add a page
page = QgsLayoutItemPage(l)
page.setPageSize('A4')
collection.addPage(page)
# add a second page
page2 = QgsLayoutItemPage(l)
page2.setPageSize('A5')
collection.addPage(page2)

page_about_to_be_removed_spy = QSignalSpy(collection.pageAboutToBeRemoved)

# clear
collection.clear()
self.assertEqual(collection.pageCount(), 0)
self.assertEqual(len(page_about_to_be_removed_spy), 2)

QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
self.assertTrue(sip.isdeleted(page))
self.assertTrue(sip.isdeleted(page2))

def testExtendByNewPage(self):
"""
Test extend by adding new page
Expand Down

0 comments on commit 8feac30

Please sign in to comment.