2018-08-12 16:45:39 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QScreen>
|
|
|
|
#include <QDebug>
|
2019-04-26 15:31:42 +02:00
|
|
|
#include <QScopedPointer>
|
2019-04-29 20:50:52 +02:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QtConcurrent/QtConcurrent>
|
|
|
|
#include <QtConcurrent/QtConcurrentMap>
|
2018-08-12 16:45:39 +02:00
|
|
|
#include "pdfworker.h"
|
|
|
|
|
|
|
|
|
2019-04-29 20:50:52 +02:00
|
|
|
static QMutex cacheMutex;
|
|
|
|
struct Renderer
|
2018-08-12 16:45:39 +02:00
|
|
|
{
|
|
|
|
|
2019-04-29 20:50:52 +02:00
|
|
|
typedef PdfPreview result_type;
|
|
|
|
double scaleX;
|
|
|
|
double scaleY;
|
|
|
|
QHash<QString, Poppler::Document *> documentcache;
|
|
|
|
Renderer(double scaleX, double scaleY)
|
2018-08-30 21:54:29 +02:00
|
|
|
{
|
2019-04-29 20:50:52 +02:00
|
|
|
this->scaleX = scaleX;
|
|
|
|
this->scaleY = scaleY;
|
2019-04-29 22:44:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~Renderer()
|
|
|
|
{
|
|
|
|
qDeleteAll(documentcache);
|
2018-08-30 21:54:29 +02:00
|
|
|
}
|
2019-04-29 22:44:39 +02:00
|
|
|
|
|
|
|
|
2019-04-29 20:50:52 +02:00
|
|
|
Poppler::Document *document(QString path)
|
2018-08-12 16:45:39 +02:00
|
|
|
{
|
2019-04-29 20:50:52 +02:00
|
|
|
if(documentcache.contains(path))
|
|
|
|
return documentcache.value(path);
|
|
|
|
|
|
|
|
Poppler::Document *result = Poppler::Document::load(path);
|
|
|
|
if(result == nullptr)
|
2018-08-31 20:08:23 +02:00
|
|
|
{
|
2019-04-29 22:44:39 +02:00
|
|
|
//TODO: some kind of user feedback would be nicec
|
2019-04-29 20:50:52 +02:00
|
|
|
return nullptr;
|
2018-08-31 20:08:23 +02:00
|
|
|
}
|
2019-04-29 20:50:52 +02:00
|
|
|
result->setRenderHint(Poppler::Document::TextAntialiasing);
|
|
|
|
QMutexLocker locker(&cacheMutex);
|
|
|
|
documentcache.insert(path, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PdfPreview operator()(const PdfPreview &preview)
|
|
|
|
{
|
|
|
|
Poppler::Document *doc = document(preview.documentPath);
|
2018-08-30 21:54:29 +02:00
|
|
|
if(doc == nullptr)
|
|
|
|
{
|
2019-04-29 20:50:52 +02:00
|
|
|
return preview;
|
2018-08-30 21:54:29 +02:00
|
|
|
}
|
2018-08-12 16:45:39 +02:00
|
|
|
if(doc->isLocked())
|
|
|
|
{
|
2019-04-29 20:50:52 +02:00
|
|
|
return preview;
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
2019-04-29 20:50:52 +02:00
|
|
|
int p = (int)preview.page - 1;
|
|
|
|
if(p < 0)
|
2019-04-26 15:31:42 +02:00
|
|
|
{
|
2019-04-29 20:50:52 +02:00
|
|
|
p = 0;
|
2019-04-26 15:31:42 +02:00
|
|
|
}
|
2019-04-29 20:50:52 +02:00
|
|
|
Poppler::Page *pdfPage = doc->page(p);
|
|
|
|
PdfPreview result = preview;
|
|
|
|
result.previewImage = pdfPage->renderToImage(scaleX, scaleY);
|
|
|
|
return result;
|
2018-08-12 16:45:39 +02:00
|
|
|
}
|
2019-04-29 20:50:52 +02:00
|
|
|
};
|
2018-08-30 21:54:29 +02:00
|
|
|
|
2019-04-29 20:50:52 +02:00
|
|
|
|
|
|
|
|
2019-04-29 23:01:11 +02:00
|
|
|
QFuture<PdfPreview> PdfWorker::generatePreviews(const QVector<SearchResult> paths, double scalefactor)
|
2018-08-30 21:54:29 +02:00
|
|
|
{
|
2019-04-29 20:50:52 +02:00
|
|
|
QVector<PdfPreview> previews;
|
2018-08-30 21:54:29 +02:00
|
|
|
|
2019-04-29 23:01:11 +02:00
|
|
|
for(const SearchResult &sr : paths)
|
2019-04-29 20:50:52 +02:00
|
|
|
{
|
|
|
|
for(int page : sr.pages)
|
|
|
|
{
|
|
|
|
PdfPreview p;
|
|
|
|
p.documentPath = sr.fileData.absPath;
|
|
|
|
p.page = page;
|
|
|
|
previews.append(p);
|
|
|
|
}
|
2018-08-30 21:54:29 +02:00
|
|
|
}
|
2019-04-29 20:50:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double scaleX = QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor;
|
|
|
|
double scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor;
|
|
|
|
|
|
|
|
|
|
|
|
return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY));
|
2018-08-30 21:54:29 +02:00
|
|
|
}
|
2019-04-29 20:50:52 +02:00
|
|
|
|