looqs/gui/pdfworker.cpp
Albert S 03356b3190 open specififed pdfviewer jumping to the page of the preveiw image
(cherry picked from commit 0bd429be1c9e16b96dd5e503265856107e55adaf)
2018-08-31 20:53:02 +02:00

76 sor
1.5 KiB
C++

#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);
if(result == nullptr)
{
return nullptr;
}
result->setRenderHint(Poppler::Document::TextAntialiasing);
this->documentcache.insert(path, result);
return result;
}
void PdfWorker::generatePreviews(QVector<SearchResult> paths, double scalefactor)
{
this->cancelCurrent = false;
this->generating = true;
for(SearchResult &sr : paths)
{
if(this->cancelCurrent.load())
{
break;
}
Poppler::Document *doc = document(sr.path);
if(doc == nullptr)
{
continue;
}
qDebug() << sr.path;
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;
preview.documentPath = sr.path;
preview.page = sr.page;
emit previewReady(preview);
}
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();
}
}