Wait till pdfworker finishes before new search, cancel running

This commit is contained in:
Albert S. 2018-08-30 21:54:29 +02:00
parent 854f252dd9
commit 64b2eda9e5
5 changed files with 50 additions and 1 deletions

3
TODO
View File

@ -25,5 +25,6 @@ type:file
endswith: endswith:
startswith: startswith:
mtime: mtime:
tag:
page:
SearchWorker::setFilters() SearchWorker::setFilters()

View File

@ -119,6 +119,9 @@ void MainWindow::makePdfPreview()
{ {
if(!pdfWorkerThread.isRunning()) if(!pdfWorkerThread.isRunning())
pdfWorkerThread.start(); pdfWorkerThread.start();
pdfWorker->cancelAndWait();
QCoreApplication::processEvents(); // Process not processed images
qDeleteAll(ui->scrollAreaWidgetContents->children()); qDeleteAll(ui->scrollAreaWidgetContents->children());
ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout()); ui->scrollAreaWidgetContents->setLayout(new QHBoxLayout());

View File

@ -14,15 +14,25 @@ Poppler::Document *PdfWorker::document(QString path)
return this->documentcache.value(path); return this->documentcache.value(path);
Poppler::Document *result = Poppler::Document::load(path); Poppler::Document *result = Poppler::Document::load(path);
if(result == nullptr)
{
return nullptr;
}
result->setRenderHint(Poppler::Document::TextAntialiasing); result->setRenderHint(Poppler::Document::TextAntialiasing);
this->documentcache.insert(path, result); this->documentcache.insert(path, result);
return result; return result;
} }
void PdfWorker::generatePreviews(QVector<SearchResult> paths, double scalefactor) void PdfWorker::generatePreviews(QVector<SearchResult> paths, double scalefactor)
{ {
this->cancelCurrent = false;
this->generating = true;
for(SearchResult &sr : paths) for(SearchResult &sr : paths)
{ {
Poppler::Document *doc = document(sr.path); Poppler::Document *doc = document(sr.path);
if(doc == nullptr)
{
continue;
}
qDebug() << sr.path; qDebug() << sr.path;
if(doc->isLocked()) if(doc->isLocked())
{ {
@ -39,6 +49,27 @@ void PdfWorker::generatePreviews(QVector<SearchResult> paths, double scalefactor
preview.previewImage = image; preview.previewImage = image;
preview.documentPath = sr.path; preview.documentPath = sr.path;
emit previewReady(preview); emit previewReady(preview);
if(this->cancelCurrent.load())
{
break;
} }
}
isFreeMutex.lock();
isFree.wakeOne();
isFreeMutex.unlock();
generating = false;
emit previewsFinished(); emit previewsFinished();
} }
void PdfWorker::cancelAndWait()
{
if(this->generating.load())
{
this->cancelCurrent = true;
isFreeMutex.lock();
isFree.wait(&isFreeMutex);
isFreeMutex.unlock();
}
}

View File

@ -3,6 +3,9 @@
#include <QObject> #include <QObject>
#include <QImage> #include <QImage>
#include <QHash> #include <QHash>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <poppler-qt5.h> #include <poppler-qt5.h>
#include "pdfpreview.h" #include "pdfpreview.h"
#include "searchresult.h" #include "searchresult.h"
@ -13,9 +16,14 @@ class PdfWorker : public QObject
private: private:
QHash<QString, Poppler::Document *> documentcache; QHash<QString, Poppler::Document *> documentcache;
Poppler::Document *document(QString path); Poppler::Document *document(QString path);
std::atomic<bool> cancelCurrent{false};
std::atomic<bool> generating{false};
QMutex isFreeMutex;
QWaitCondition isFree;
public: public:
PdfWorker(); PdfWorker();
void cancelAndWait();
public slots: public slots:
void generatePreviews(QVector<SearchResult> paths, double scalefactor); void generatePreviews(QVector<SearchResult> paths, double scalefactor);
signals: signals:

View File

@ -21,6 +21,12 @@ SearchWorker::SearchWorker(const QString &dbpath)
"content_fts.content MATCH ? ORDER By file.mtime DESC, content.page ASC"); "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) void SearchWorker::searchForFile(const QString &query)
{ {
QVector<SearchResult> results; QVector<SearchResult> results;