Generalize previews: Retire PdfWorker, Add PreviewWorker
This commit is contained in:
parent
d816603a1c
commit
0cbd0dd9eb
@ -1,107 +0,0 @@
|
|||||||
|
|
||||||
#include <QApplication>
|
|
||||||
#include <QScreen>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QScopedPointer>
|
|
||||||
#include <QMutexLocker>
|
|
||||||
#include <QtConcurrent/QtConcurrent>
|
|
||||||
#include <QtConcurrent/QtConcurrentMap>
|
|
||||||
#include <QPainter>
|
|
||||||
#include <atomic>
|
|
||||||
#include "pdfworker.h"
|
|
||||||
|
|
||||||
static QMutex cacheMutex;
|
|
||||||
struct Renderer
|
|
||||||
{
|
|
||||||
|
|
||||||
typedef PdfPreview result_type;
|
|
||||||
double scaleX;
|
|
||||||
double scaleY;
|
|
||||||
QHash<QString, Poppler::Document *> documentcache;
|
|
||||||
QVector<QString> wordsToHighlight;
|
|
||||||
Renderer(double scaleX, double scaleY, QVector<QString> wordsToHighlight)
|
|
||||||
{
|
|
||||||
this->scaleX = scaleX;
|
|
||||||
this->scaleY = scaleY;
|
|
||||||
this->wordsToHighlight = wordsToHighlight;
|
|
||||||
}
|
|
||||||
|
|
||||||
~Renderer()
|
|
||||||
{
|
|
||||||
qDeleteAll(documentcache);
|
|
||||||
}
|
|
||||||
|
|
||||||
Poppler::Document *document(QString path)
|
|
||||||
{
|
|
||||||
if(documentcache.contains(path))
|
|
||||||
{
|
|
||||||
return documentcache.value(path);
|
|
||||||
}
|
|
||||||
Poppler::Document *result = Poppler::Document::load(path);
|
|
||||||
if(result == nullptr)
|
|
||||||
{
|
|
||||||
// TODO: some kind of user feedback would be nice
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
result->setRenderHint(Poppler::Document::TextAntialiasing);
|
|
||||||
QMutexLocker locker(&cacheMutex);
|
|
||||||
documentcache.insert(path, result);
|
|
||||||
locker.unlock();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
PdfPreview operator()(const PdfPreview &preview)
|
|
||||||
{
|
|
||||||
PdfPreview result = preview;
|
|
||||||
Poppler::Document *doc = document(preview.documentPath);
|
|
||||||
if(doc == nullptr)
|
|
||||||
{
|
|
||||||
return preview;
|
|
||||||
}
|
|
||||||
if(doc->isLocked())
|
|
||||||
{
|
|
||||||
return preview;
|
|
||||||
}
|
|
||||||
int p = (int)preview.page - 1;
|
|
||||||
if(p < 0)
|
|
||||||
{
|
|
||||||
p = 0;
|
|
||||||
}
|
|
||||||
Poppler::Page *pdfPage = doc->page(p);
|
|
||||||
QImage img = pdfPage->renderToImage(scaleX, scaleY);
|
|
||||||
for(QString &word : wordsToHighlight)
|
|
||||||
{
|
|
||||||
QList<QRectF> rects = pdfPage->search(word, Poppler::Page::SearchFlag::IgnoreCase);
|
|
||||||
for(QRectF &rect : rects)
|
|
||||||
{
|
|
||||||
QPainter painter(&img);
|
|
||||||
painter.scale(scaleX / 72.0, scaleY / 72.0);
|
|
||||||
painter.fillRect(rect, QColor(255, 255, 0, 64));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.previewImage = img;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
QFuture<PdfPreview> PdfWorker::generatePreviews(const QVector<SearchResult> paths, QVector<QString> wordsToHighlight,
|
|
||||||
double scalefactor)
|
|
||||||
{
|
|
||||||
QVector<PdfPreview> previews;
|
|
||||||
|
|
||||||
for(const SearchResult &sr : paths)
|
|
||||||
{
|
|
||||||
for(int page : sr.pages)
|
|
||||||
{
|
|
||||||
PdfPreview p;
|
|
||||||
p.documentPath = sr.fileData.absPath;
|
|
||||||
p.page = page;
|
|
||||||
previews.append(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double scaleX = QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor;
|
|
||||||
double scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor;
|
|
||||||
|
|
||||||
return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY, wordsToHighlight));
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
#ifndef PDFWORKER_H
|
|
||||||
#define PDFWORKER_H
|
|
||||||
#include <QObject>
|
|
||||||
#include <QImage>
|
|
||||||
#include <QHash>
|
|
||||||
#include <QThread>
|
|
||||||
#include <QMutex>
|
|
||||||
#include <QWaitCondition>
|
|
||||||
#include <QMutex>
|
|
||||||
#include <QFuture>
|
|
||||||
#include <poppler-qt5.h>
|
|
||||||
#include "pdfpreview.h"
|
|
||||||
#include "searchresult.h"
|
|
||||||
|
|
||||||
class PdfWorker : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
public:
|
|
||||||
QFuture<PdfPreview> generatePreviews(const QVector<SearchResult> paths, QVector<QString> wordsToHighlight,
|
|
||||||
double scalefactor);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // PDFWORKER_H
|
|
39
gui/previewworker.cpp
Normal file
39
gui/previewworker.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <QApplication>
|
||||||
|
#include <QScreen>
|
||||||
|
#include <QScopedPointer>
|
||||||
|
#include <QMutexLocker>
|
||||||
|
#include <QtConcurrent/QtConcurrent>
|
||||||
|
#include <QtConcurrent/QtConcurrentMap>
|
||||||
|
#include <atomic>
|
||||||
|
#include "previewworker.h"
|
||||||
|
|
||||||
|
PreviewWorker::PreviewWorker()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QFuture<QSharedPointer<PreviewResult>> PreviewWorker::generatePreviews(const QVector<SearchResult> paths,
|
||||||
|
QVector<QString> wordsToHighlight,
|
||||||
|
double scalefactor)
|
||||||
|
{
|
||||||
|
QVector<QSharedPointer<PreviewResult>> previews;
|
||||||
|
|
||||||
|
for(const SearchResult &sr : paths)
|
||||||
|
{
|
||||||
|
for(unsigned int page : sr.pages)
|
||||||
|
{
|
||||||
|
QSharedPointer<PreviewResult> ptr =
|
||||||
|
QSharedPointer<PreviewResult>(new PreviewResult{sr.fileData.absPath, page});
|
||||||
|
previews.append(ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderConfig renderConfig;
|
||||||
|
renderConfig.scaleX = QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor;
|
||||||
|
renderConfig.scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor;
|
||||||
|
renderConfig.wordsToHighlight = wordsToHighlight;
|
||||||
|
|
||||||
|
auto mapFunctor = new PreviewGeneratorMapFunctor();
|
||||||
|
mapFunctor->setRenderConfig(renderConfig);
|
||||||
|
|
||||||
|
return QtConcurrent::mapped(previews, *mapFunctor);
|
||||||
|
}
|
29
gui/previewworker.h
Normal file
29
gui/previewworker.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#ifndef PREVIEWWORKER_H
|
||||||
|
#define PREVIEWWORKER_H
|
||||||
|
#include <QObject>
|
||||||
|
#include <QImage>
|
||||||
|
#include <QHash>
|
||||||
|
#include <QThread>
|
||||||
|
#include <QMutex>
|
||||||
|
#include <QWaitCondition>
|
||||||
|
#include <QMutex>
|
||||||
|
#include <QFuture>
|
||||||
|
#include "previewresultpdf.h"
|
||||||
|
#include "searchresult.h"
|
||||||
|
#include "previewgenerator.h"
|
||||||
|
#include "previewworker.h"
|
||||||
|
#include "previewgeneratorpdf.h"
|
||||||
|
#include "previewgeneratormapfunctor.h"
|
||||||
|
|
||||||
|
class PreviewWorker : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
PreviewWorker();
|
||||||
|
QSharedPointer<PreviewGenerator> createGenerator(QString path);
|
||||||
|
|
||||||
|
QFuture<QSharedPointer<PreviewResult>> generatePreviews(const QVector<SearchResult> paths,
|
||||||
|
QVector<QString> wordsToHighlight, double scalefactor);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PREVIEWWORKER_H
|
Loading…
Reference in New Issue
Block a user