2018-08-12 16:45:39 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QScreen>
|
|
|
|
#include <QDebug>
|
|
|
|
#include "pdfworker.h"
|
|
|
|
|
|
|
|
PdfWorker::PdfWorker()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Poppler::Document * PdfWorker::document(QString path)
|
|
|
|
{
|
|
|
|
if(this->documentcache.contains(path))
|
|
|
|
return this->documentcache.value(path);
|
|
|
|
|
|
|
|
Poppler::Document *result = Poppler::Document::load(path);
|
2018-08-30 21:54:29 +02:00
|
|
|
if(result == nullptr)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-08-12 16:45:39 +02:00
|
|
|
result->setRenderHint(Poppler::Document::TextAntialiasing);
|
|
|
|
this->documentcache.insert(path, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
void PdfWorker::generatePreviews(QVector<SearchResult> paths, double scalefactor)
|
|
|
|
{
|
2018-08-30 21:54:29 +02:00
|
|
|
this->cancelCurrent = false;
|
|
|
|
this->generating = true;
|
2018-08-12 16:45:39 +02:00
|
|
|
for(SearchResult &sr : paths)
|
|
|
|
{
|
2018-08-31 20:08:23 +02:00
|
|
|
if(this->cancelCurrent.load())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2019-04-22 21:07:41 +02:00
|
|
|
Poppler::Document *doc = document(sr.fileData.absPath);
|
2018-08-30 21:54:29 +02:00
|
|
|
if(doc == nullptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-12 16:45:39 +02:00
|
|
|
if(doc->isLocked())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int p = (int)sr.page - 1;
|
|
|
|
if(p < 0)
|
|
|
|
p = 0;
|
|
|
|
Poppler::Page *pdfpage = doc->page(p);
|
|
|
|
QImage image = pdfpage->renderToImage(QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor, QGuiApplication::primaryScreen()->physicalDotsPerInchY() *scalefactor);
|
|
|
|
|
|
|
|
PdfPreview preview;
|
|
|
|
preview.previewImage = image;
|
2019-04-22 21:07:41 +02:00
|
|
|
preview.documentPath = sr.fileData.absPath;
|
2018-08-31 20:08:23 +02:00
|
|
|
preview.page = sr.page;
|
2018-08-12 16:45:39 +02:00
|
|
|
emit previewReady(preview);
|
|
|
|
}
|
2018-08-30 21:54:29 +02:00
|
|
|
isFreeMutex.lock();
|
|
|
|
isFree.wakeOne();
|
|
|
|
isFreeMutex.unlock();
|
|
|
|
generating = false;
|
2018-08-12 16:45:39 +02:00
|
|
|
emit previewsFinished();
|
|
|
|
}
|
2018-08-30 21:54:29 +02:00
|
|
|
|
|
|
|
void PdfWorker::cancelAndWait()
|
|
|
|
{
|
|
|
|
if(this->generating.load())
|
|
|
|
{
|
|
|
|
this->cancelCurrent = true;
|
|
|
|
|
|
|
|
isFreeMutex.lock();
|
|
|
|
isFree.wait(&isFreeMutex);
|
|
|
|
isFreeMutex.unlock();
|
|
|
|
}
|
|
|
|
}
|