Skip to content

Commit

Permalink
Add signal when page is about to be deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 04e6529 commit 94362fe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/layout/qgslayoutpagecollection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ class QgsLayoutPageCollection : QObject
Emitted when pages are added or removed from the collection.
%End

void pageAboutToBeRemoved( int pageNumber );
%Docstring
Emitted just before a page is removed from the collection.

Page numbers in collections begin at 0 - so a page number of 0 indicates the
first page.
%End

};

/************************************************************************
Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutpagecollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ void QgsLayoutPageCollection::deletePage( int pageNumber )
if ( pageNumber < 0 || pageNumber >= mPages.count() )
return;

emit pageAboutToBeRemoved( pageNumber );
QgsLayoutItemPage *page = mPages.takeAt( pageNumber );
mLayout->removeItem( page );
page->deleteLater();
Expand All @@ -232,6 +233,7 @@ void QgsLayoutPageCollection::deletePage( QgsLayoutItemPage *page )
if ( !mPages.contains( page ) )
return;

emit pageAboutToBeRemoved( mPages.indexOf( page ) );
mPages.removeAll( page );
page->deleteLater();
reflow();
Expand Down
8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
*/
void changed();

/**
* Emitted just before a page is removed from the collection.
*
* Page numbers in collections begin at 0 - so a page number of 0 indicates the
* first page.
*/
void pageAboutToBeRemoved( int pageNumber );

private:

QgsLayout *mLayout = nullptr;
Expand Down
9 changes: 9 additions & 0 deletions tests/src/python/test_qgslayoutpagecollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
QgsSimpleFillSymbolLayer,
QgsFillSymbol)
from qgis.PyQt.QtCore import Qt, QCoreApplication, QEvent, QPointF, QRectF
from qgis.PyQt.QtTest import QSignalSpy

from qgis.testing import start_app, unittest

Expand Down Expand Up @@ -152,28 +153,36 @@ def testDeletePages(self):
page2.setPageSize('A5')
collection.addPage(page2)

page_about_to_be_removed_spy = QSignalSpy(collection.pageAboutToBeRemoved)

# delete page
collection.deletePage(None)
self.assertEqual(collection.pageCount(), 2)
self.assertEqual(len(page_about_to_be_removed_spy), 0)

page3 = QgsLayoutItemPage(l)
# try deleting a page not in collection
collection.deletePage(page3)
QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
self.assertFalse(sip.isdeleted(page3))
self.assertEqual(collection.pageCount(), 2)
self.assertEqual(len(page_about_to_be_removed_spy), 0)

collection.deletePage(page)
self.assertEqual(collection.pageCount(), 1)
self.assertFalse(page in collection.pages())
QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
self.assertTrue(sip.isdeleted(page))
self.assertEqual(len(page_about_to_be_removed_spy), 1)
self.assertEqual(page_about_to_be_removed_spy[-1][0], 0)

collection.deletePage(page2)
self.assertEqual(collection.pageCount(), 0)
self.assertFalse(collection.pages())
QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
self.assertTrue(sip.isdeleted(page2))
self.assertEqual(len(page_about_to_be_removed_spy), 2)
self.assertEqual(page_about_to_be_removed_spy[-1][0], 0)

def testMaxPageWidth(self):
"""
Expand Down

0 comments on commit 94362fe

Please sign in to comment.