Skip to content
Permalink
Browse files

Add method to clear page collections for layouts

  • Loading branch information
nyalldawson committed Dec 4, 2017
1 parent 6a78d48 commit 8feac30f07b2534ef8955ab2baa0a9c329bab075
@@ -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 );
@@ -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/;
@@ -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 );
@@ -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 );

@@ -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.
*/
@@ -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

0 comments on commit 8feac30

Please sign in to comment.
You can’t perform that action at this time.