Skip to content

Commit 8feac30

Browse files
committed
Add method to clear page collections for layouts
1 parent 6a78d48 commit 8feac30

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

python/core/layout/qgslayoutpagecollection.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class QgsLayoutPageCollection : QObject, QgsLayoutSerializableObject
144144
the first page in the collection.
145145

146146
Calling deletePage() automatically triggers a reflow() of pages.
147+
148+
.. seealso:: clear()
147149
%End
148150

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

154156
Calling deletePage() automatically triggers a reflow() of pages.
157+
158+
.. seealso:: clear()
159+
%End
160+
161+
void clear();
162+
%Docstring
163+
Removes all pages from the collection.
164+
.. seealso:: deletePage()
155165
%End
156166

157167
QgsLayoutItemPage *takePage( QgsLayoutItemPage *page ) /TransferBack/;

src/core/layout/qgslayoutpagecollection.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@ void QgsLayoutPageCollection::deletePage( QgsLayoutItemPage *page )
454454
}
455455
}
456456

457+
void QgsLayoutPageCollection::clear()
458+
{
459+
if ( !mBlockUndoCommands )
460+
{
461+
mLayout->undoStack()->beginMacro( tr( "Remove Pages" ) );
462+
mLayout->undoStack()->beginCommand( this, tr( "Remove Pages" ) );
463+
}
464+
for ( int i = mPages.count() - 1; i >= 0; --i )
465+
{
466+
emit pageAboutToBeRemoved( i );
467+
mPages.takeAt( i )->deleteLater();
468+
}
469+
reflow();
470+
if ( !mBlockUndoCommands )
471+
{
472+
mLayout->undoStack()->endCommand();
473+
mLayout->undoStack()->endMacro();
474+
}
475+
}
476+
457477
QgsLayoutItemPage *QgsLayoutPageCollection::takePage( QgsLayoutItemPage *page )
458478
{
459479
mPages.removeAll( page );

src/core/layout/qgslayoutpagecollection.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
153153
* the first page in the collection.
154154
*
155155
* Calling deletePage() automatically triggers a reflow() of pages.
156+
*
157+
* \see clear()
156158
*/
157159
void deletePage( int pageNumber );
158160

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

171+
/**
172+
* Removes all pages from the collection.
173+
* \see deletePage()
174+
*/
175+
void clear();
176+
167177
/**
168178
* Takes a \a page from the collection, returning ownership of the page to the caller.
169179
*/

tests/src/python/test_qgslayoutpagecollection.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,36 @@ def testDeletePages(self):
187187
self.assertEqual(len(page_about_to_be_removed_spy), 2)
188188
self.assertEqual(page_about_to_be_removed_spy[-1][0], 0)
189189

190+
def testClear(self):
191+
"""
192+
Test clearing the collection
193+
"""
194+
p = QgsProject()
195+
l = QgsLayout(p)
196+
collection = l.pageCollection()
197+
198+
collection.clear()
199+
200+
# add a page
201+
page = QgsLayoutItemPage(l)
202+
page.setPageSize('A4')
203+
collection.addPage(page)
204+
# add a second page
205+
page2 = QgsLayoutItemPage(l)
206+
page2.setPageSize('A5')
207+
collection.addPage(page2)
208+
209+
page_about_to_be_removed_spy = QSignalSpy(collection.pageAboutToBeRemoved)
210+
211+
# clear
212+
collection.clear()
213+
self.assertEqual(collection.pageCount(), 0)
214+
self.assertEqual(len(page_about_to_be_removed_spy), 2)
215+
216+
QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
217+
self.assertTrue(sip.isdeleted(page))
218+
self.assertTrue(sip.isdeleted(page2))
219+
190220
def testExtendByNewPage(self):
191221
"""
192222
Test extend by adding new page

0 commit comments

Comments
 (0)