From 964424d62fa4426a6cda19c3bc6eba3339a04502 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sun, 8 Jul 2007 21:22:37 +0000 Subject: [PATCH] stop gracefully the font extraction thread when closing a document or the properties dialog; make the thread deleting itself when finished svn path=/trunk/KDE/kdegraphics/okular/; revision=685437 --- core/document.cpp | 23 ++++++++++++++++++++--- core/document.h | 6 ++++++ core/generator_p.cpp | 11 +++++++++-- core/generator_p.h | 2 ++ ui/propertiesdialog.cpp | 5 +++++ ui/propertiesdialog.h | 1 + 6 files changed, 43 insertions(+), 5 deletions(-) diff --git a/core/document.cpp b/core/document.cpp index a884dd5eb..0bab126bb 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -120,7 +120,6 @@ class Okular::DocumentPrivate m_saveBookmarksTimer( 0 ), m_generator( 0 ), m_generatorsLoaded( false ), - m_fontThread( 0 ), m_fontsCached( false ) { } @@ -207,7 +206,7 @@ class Okular::DocumentPrivate // cache of the mimetype we support QStringList m_supportedMimeTypes; - FontExtractionThread * m_fontThread; + QPointer< FontExtractionThread > m_fontThread; bool m_fontsCached; FontInfo::List m_fontsCache; }; @@ -718,7 +717,6 @@ void DocumentPrivate::fontReadingProgress( int page ) if ( page == (int)m_parent->pages() ) { emit m_parent->fontReadingEnded(); - delete m_fontThread; m_fontThread = 0; m_fontsCached = true; } @@ -990,6 +988,14 @@ void Document::setupGui( KActionCollection *ac, QToolBox *tBox ) void Document::closeDocument() { + if ( d->m_fontThread ) + { + disconnect( d->m_fontThread, 0, this, 0 ); + d->m_fontThread->stopExtraction(); + d->m_fontThread->wait(); + d->m_fontThread = 0; + } + // close the current document and save document info if a document is still opened if ( d->m_generator && d->m_pagesVector.size() > 0 ) { @@ -1230,6 +1236,17 @@ void Document::startFontReading() d->m_fontThread->startExtraction( /*d->m_generator->hasFeature( Generator::Threaded )*/true ); } +void Document::stopFontReading() +{ + if ( !d->m_fontThread ) + return; + + disconnect( d->m_fontThread, 0, this, 0 ); + d->m_fontThread->stopExtraction(); + d->m_fontThread = 0; + d->m_fontsCache.clear(); +} + bool Document::canProvideFontInformation() const { return d->m_generator ? d->m_generator->hasFeature( Generator::FontInfo ) : false; diff --git a/core/document.h b/core/document.h index ad9aca828..82424e5de 100644 --- a/core/document.h +++ b/core/document.h @@ -146,6 +146,12 @@ class OKULAR_EXPORT Document : public QObject */ void startFontReading(); + /** + * Force the termination of the reading of the informations about the + * fonts in the document, if running. + */ + void stopFontReading(); + /** * Whether the current document can provide information about the * fonts used in it. diff --git a/core/generator_p.cpp b/core/generator_p.cpp index 353552a5f..f3e8b7b63 100644 --- a/core/generator_p.cpp +++ b/core/generator_p.cpp @@ -89,7 +89,7 @@ void TextPageGenerationThread::run() FontExtractionThread::FontExtractionThread( Generator *generator, int pages ) - : mGenerator( generator ), mNumOfPages( pages ) + : mGenerator( generator ), mNumOfPages( pages ), mGoOn( true ) { } @@ -97,17 +97,24 @@ void FontExtractionThread::startExtraction( bool async ) { if ( async ) { + connect( this, SIGNAL( finished() ), this, SLOT( deleteLater() ) ); start( QThread::InheritPriority ); } else { run(); + deleteLater(); } } +void FontExtractionThread::stopExtraction() +{ + mGoOn = false; +} + void FontExtractionThread::run() { - for ( int i = 1; i <= mNumOfPages; ++i ) + for ( int i = 1; i <= mNumOfPages && mGoOn; ++i ) { FontInfo::List list = mGenerator->fontsForPage( i ); foreach ( const FontInfo& fi, list ) diff --git a/core/generator_p.h b/core/generator_p.h index 5881d86ca..1a08819d5 100644 --- a/core/generator_p.h +++ b/core/generator_p.h @@ -112,6 +112,7 @@ class FontExtractionThread : public QThread FontExtractionThread( Generator *generator, int pages ); void startExtraction( bool async ); + void stopExtraction(); Q_SIGNALS: void gotFont( const Okular::FontInfo& ); @@ -123,6 +124,7 @@ class FontExtractionThread : public QThread private: Generator *mGenerator; int mNumOfPages; + bool mGoOn; }; } diff --git a/ui/propertiesdialog.cpp b/ui/propertiesdialog.cpp index 51eeba16c..3ff969e4f 100644 --- a/ui/propertiesdialog.cpp +++ b/ui/propertiesdialog.cpp @@ -135,6 +135,11 @@ PropertiesDialog::PropertiesDialog(QWidget *parent, Okular::Document *doc) this, SLOT( pageChanged( KPageWidgetItem *, KPageWidgetItem * ) ) ); } +PropertiesDialog::~PropertiesDialog() +{ + m_document->stopFontReading(); +} + void PropertiesDialog::pageChanged( KPageWidgetItem *current, KPageWidgetItem * ) { if ( current == m_fontPage && !m_fontScanStarted ) diff --git a/ui/propertiesdialog.h b/ui/propertiesdialog.h index 6ee02488f..e1be9bf53 100644 --- a/ui/propertiesdialog.h +++ b/ui/propertiesdialog.h @@ -31,6 +31,7 @@ class PropertiesDialog : public KPageDialog public: PropertiesDialog( QWidget *parent, Okular::Document *doc ); + virtual ~PropertiesDialog(); private slots: void pageChanged( KPageWidgetItem *, KPageWidgetItem * );