Skip to content
Permalink
Browse files

Add signal when page is about to be deleted

  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 04e6529 commit 94362fe2187e1f93240a8b75993924383059a8d5
@@ -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

};

/************************************************************************
@@ -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();
@@ -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();
@@ -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;
@@ -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

@@ -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):
"""

0 comments on commit 94362fe

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