diff options
author | Tobias Koenig <tobias.koenig@kdab.com> | 2016-08-06 20:30:22 +0200 |
---|---|---|
committer | Tobias Koenig <tobias.koenig@kdab.com> | 2016-08-08 07:10:23 +0000 |
commit | 322ab60499477ce0860f3a9b697e9b696da9570e (patch) | |
tree | 909624573cff135bcc6f7425b5a2dd9258cc2446 | |
parent | cab3f0279696c5213a33f7875a40dc6d68709d82 (diff) |
Add close() method to QPdfDocument
The close() method will close an open document and emit
the aboutToBeClosed() signal, so that other component, which
keep a pointer to QPdfDocument, can react to it.
Change-Id: I93200eb0b4bf96479fc114b43c9f6f2af4d15ffa
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r-- | src/pdf/qpdfdocument.cpp | 30 | ||||
-rw-r--r-- | src/pdf/qpdfdocument.h | 3 | ||||
-rw-r--r-- | tests/auto/qpdfdocument/tst_qpdfdocument.cpp | 68 |
3 files changed, 99 insertions, 2 deletions
diff --git a/src/pdf/qpdfdocument.cpp b/src/pdf/qpdfdocument.cpp index 4d6a2e0..6802089 100644 --- a/src/pdf/qpdfdocument.cpp +++ b/src/pdf/qpdfdocument.cpp @@ -59,9 +59,9 @@ QPdfDocumentPrivate::QPdfDocumentPrivate() QPdfDocumentPrivate::~QPdfDocumentPrivate() { - const QMutexLocker lock(pdfMutex()); + q->close(); - clear(); + const QMutexLocker lock(pdfMutex()); if (!--libraryRefCount) FPDF_DestroyLibrary(); @@ -249,6 +249,9 @@ QPdfDocument::QPdfDocument(QObject *parent) d->q = this; } +/*! + Destroys the document. +*/ QPdfDocument::~QPdfDocument() { } @@ -298,6 +301,29 @@ QPdfDocument::Error QPdfDocument::error() const return d->lastError; } +/*! + \fn void QPdfDocument::aboutToBeClosed() + + This signal is emitted whenever the document is closed. + + \sa close() +*/ + +/*! + Closes the document. +*/ +void QPdfDocument::close() +{ + if (!d->doc) + return; + + emit aboutToBeClosed(); + + const QMutexLocker lock(pdfMutex()); + + d->clear(); +} + int QPdfDocument::pageCount() const { if (!d->doc) diff --git a/src/pdf/qpdfdocument.h b/src/pdf/qpdfdocument.h index 6a46c50..bb3602d 100644 --- a/src/pdf/qpdfdocument.h +++ b/src/pdf/qpdfdocument.h @@ -61,6 +61,8 @@ public: Error error() const; + void close(); + int pageCount() const; QSizeF pageSize(int page) const; @@ -71,6 +73,7 @@ Q_SIGNALS: void passwordRequired(); void documentLoadStarted(); void documentLoadFinished(); + void aboutToBeClosed(); void pageCountChanged(); private: diff --git a/tests/auto/qpdfdocument/tst_qpdfdocument.cpp b/tests/auto/qpdfdocument/tst_qpdfdocument.cpp index 336c6e8..ef00bdb 100644 --- a/tests/auto/qpdfdocument/tst_qpdfdocument.cpp +++ b/tests/auto/qpdfdocument/tst_qpdfdocument.cpp @@ -19,6 +19,9 @@ private slots: void loadFromIODevice(); void loadAsync(); void password(); + void close(); + void loadAfterClose(); + void closeOnDestroy(); }; struct TemporaryPdf: public QTemporaryFile @@ -107,6 +110,71 @@ void tst_QPdfDocument::password() QCOMPARE(doc.pageCount(), 1); } +void tst_QPdfDocument::close() +{ + TemporaryPdf tempPdf; + QPdfDocument doc; + + QSignalSpy aboutToBeClosedSpy(&doc, SIGNAL(aboutToBeClosed())); + + doc.load(&tempPdf); + QCOMPARE(aboutToBeClosedSpy.count(), 0); + doc.close(); + QCOMPARE(aboutToBeClosedSpy.count(), 1); + QCOMPARE(doc.pageCount(), 0); +} + +void tst_QPdfDocument::loadAfterClose() +{ + TemporaryPdf tempPdf; + QPdfDocument doc; + + QSignalSpy aboutToBeClosedSpy(&doc, SIGNAL(aboutToBeClosed())); + + doc.load(&tempPdf); + QCOMPARE(aboutToBeClosedSpy.count(), 0); + doc.close(); + QCOMPARE(aboutToBeClosedSpy.count(), 1); + + QSignalSpy startedSpy(&doc, SIGNAL(documentLoadStarted())); + QSignalSpy finishedSpy(&doc, SIGNAL(documentLoadFinished())); + doc.load(&tempPdf); + QCOMPARE(startedSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 1); + QCOMPARE(doc.error(), QPdfDocument::NoError); + QCOMPARE(doc.pageCount(), 2); +} + +void tst_QPdfDocument::closeOnDestroy() +{ + TemporaryPdf tempPdf; + + // deleting an open document should automatically close it + { + QPdfDocument *doc = new QPdfDocument; + + QSignalSpy aboutToBeClosedSpy(doc, SIGNAL(aboutToBeClosed())); + doc->load(&tempPdf); + + delete doc; + + QCOMPARE(aboutToBeClosedSpy.count(), 1); + } + + // deleting a closed document should not emit any signal + { + QPdfDocument *doc = new QPdfDocument; + doc->load(&tempPdf); + doc->close(); + + QSignalSpy aboutToBeClosedSpy(doc, SIGNAL(aboutToBeClosed())); + + delete doc; + + QCOMPARE(aboutToBeClosedSpy.count(), 0); + } +} + QTEST_MAIN(tst_QPdfDocument) #include "tst_qpdfdocument.moc" |