From 53cf73cc8d637af4bb51e8ab474316241f276465 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 | 5 ++++- gui/pdfworker.cpp | 31 +++++++++++++++++++++++++++++++ gui/pdfworker.h | 8 ++++++++ gui/searchworker.cpp | 7 +++++++ 5 files changed, 52 insertions(+), 2 deletions(-) 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 7ecaaa1..eb60358 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -121,7 +121,10 @@ void MainWindow::makePdfPreview() { if(!pdfWorkerThread.isRunning()) pdfWorkerThread.start(); - qDeleteAll(ui->scrollAreaWidgetContents->children()); + + pdfWorker->cancelAndWait(); + QCoreApplication::processEvents(); //Process not processed images + qDeleteAll(ui->scrollAreaWidgetContents->children()); ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout()); emit startPdfPreviewGeneration(this->pdfSearchResults, 0.75); diff --git a/gui/pdfworker.cpp b/gui/pdfworker.cpp index 5fcdbaa..028b59c 100644 --- a/gui/pdfworker.cpp +++ b/gui/pdfworker.cpp @@ -15,15 +15,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 a141b7c..4717558 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,8 +16,13 @@ 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 32d0e28..1231782 100644 --- a/gui/searchworker.cpp +++ b/gui/searchworker.cpp @@ -20,6 +20,13 @@ SearchWorker::SearchWorker(const QString &dbpath) queryContent->prepare("SELECT file.path, content.page, file.mtime FROM file INNER JOIN content ON file.id = content.fileid INNER JOIN content_fts ON content.id = content_fts.ROWID WHERE 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;