From 64b2eda9e509a95fbf1b0802df013d6e01b8483f Mon Sep 17 00:00:00 2001 From: Albert S Date: Thu, 30 Aug 2018 21:54:29 +0200 Subject: [PATCH] Wait till pdfworker finishes before new search, cancel running --- TODO | 3 ++- gui/mainwindow.cpp | 3 +++ gui/pdfworker.cpp | 31 +++++++++++++++++++++++++++++++ gui/pdfworker.h | 8 ++++++++ gui/searchworker.cpp | 6 ++++++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index 2a6802f..8a0ae23 100644 --- a/TODO +++ b/TODO @@ -25,5 +25,6 @@ type:file endswith: startswith: mtime: - +tag: +page: SearchWorker::setFilters() diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 5b823fd..6b8ee81 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -119,6 +119,9 @@ void MainWindow::makePdfPreview() { if(!pdfWorkerThread.isRunning()) pdfWorkerThread.start(); + + pdfWorker->cancelAndWait(); + QCoreApplication::processEvents(); // Process not processed images qDeleteAll(ui->scrollAreaWidgetContents->children()); ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout()); diff --git a/gui/pdfworker.cpp b/gui/pdfworker.cpp index da8ccd4..6fc5d26 100644 --- a/gui/pdfworker.cpp +++ b/gui/pdfworker.cpp @@ -14,15 +14,25 @@ Poppler::Document *PdfWorker::document(QString path) return this->documentcache.value(path); Poppler::Document *result = Poppler::Document::load(path); + if(result == nullptr) + { + return nullptr; + } result->setRenderHint(Poppler::Document::TextAntialiasing); this->documentcache.insert(path, result); return result; } void PdfWorker::generatePreviews(QVector paths, double scalefactor) { + this->cancelCurrent = false; + this->generating = true; for(SearchResult &sr : paths) { Poppler::Document *doc = document(sr.path); + if(doc == nullptr) + { + continue; + } qDebug() << sr.path; if(doc->isLocked()) { @@ -39,6 +49,27 @@ void PdfWorker::generatePreviews(QVector paths, double scalefactor preview.previewImage = image; preview.documentPath = sr.path; emit previewReady(preview); + if(this->cancelCurrent.load()) + { + + break; + } } + isFreeMutex.lock(); + isFree.wakeOne(); + isFreeMutex.unlock(); + generating = false; emit previewsFinished(); } + +void PdfWorker::cancelAndWait() +{ + if(this->generating.load()) + { + this->cancelCurrent = true; + + isFreeMutex.lock(); + isFree.wait(&isFreeMutex); + isFreeMutex.unlock(); + } +} diff --git a/gui/pdfworker.h b/gui/pdfworker.h index 7b243b2..33f4672 100644 --- a/gui/pdfworker.h +++ b/gui/pdfworker.h @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include #include #include "pdfpreview.h" #include "searchresult.h" @@ -13,9 +16,14 @@ class PdfWorker : public QObject private: QHash documentcache; Poppler::Document *document(QString path); + std::atomic cancelCurrent{false}; + std::atomic generating{false}; + QMutex isFreeMutex; + QWaitCondition isFree; public: PdfWorker(); + void cancelAndWait(); public slots: void generatePreviews(QVector paths, double scalefactor); signals: diff --git a/gui/searchworker.cpp b/gui/searchworker.cpp index db44057..bace4aa 100644 --- a/gui/searchworker.cpp +++ b/gui/searchworker.cpp @@ -21,6 +21,12 @@ SearchWorker::SearchWorker(const QString &dbpath) "content_fts.content MATCH ? ORDER By file.mtime DESC, content.page ASC"); } +QString normalize(QString str) +{ + str = str.replace(" ", " AND "); + str = str.replace("|", " OR "); + return str; +} void SearchWorker::searchForFile(const QString &query) { QVector results;