Generalize previews: Add PreviewGenerator*

这个提交包含在:
Albert S. 2022-01-03 22:59:44 +01:00
父节点 95b3d1fce2
当前提交 d816603a1c
共有 6 个文件被更改,包括 177 次插入0 次删除

1
gui/previewgenerator.cpp 普通文件
查看文件

@ -0,0 +1 @@
#include "previewgenerator.h"

17
gui/previewgenerator.h 普通文件
查看文件

@ -0,0 +1,17 @@
#ifndef PREVIEWGENERATOR_H
#define PREVIEWGENERATOR_H
#include <QVector>
#include <QSharedPointer>
#include "previewresult.h"
#include "renderconfig.h"
class PreviewGenerator
{
public:
virtual PreviewResult *generate(RenderConfig config, QString documentPath, unsigned int page) = 0;
virtual ~PreviewGenerator()
{
}
};
#endif // PREVIEWGENERATOR_H

查看文件

@ -0,0 +1,44 @@
#include "previewgeneratormapfunctor.h"
#include "previewgeneratorpdf.h"
PreviewGeneratorMapFunctor::PreviewGeneratorMapFunctor()
{
generator[GeneratorIndex::PDF] = new PreviewGeneratorPdf();
}
PreviewGenerator *PreviewGeneratorMapFunctor::getGenerator(QString filePath)
{
/* Dirty, but that's all we have at this point */
if(filePath.endsWith(".pdf"))
{
return generator[GeneratorIndex::PDF];
}
return nullptr;
}
PreviewGeneratorMapFunctor::~PreviewGeneratorMapFunctor()
{
for(int i = GeneratorIndex::PDF; i < GeneratorIndex::LAST_DUMMY; i++)
{
// delete generator[i];
generator[i] = nullptr;
}
}
void PreviewGeneratorMapFunctor::setRenderConfig(RenderConfig config)
{
this->renderConfig = config;
}
QSharedPointer<PreviewResult> PreviewGeneratorMapFunctor::operator()(const QSharedPointer<PreviewResult> &renderResult)
{
PreviewGenerator *previewGenerator = getGenerator(renderResult->getDocumentPath());
if(previewGenerator == nullptr)
{
return QSharedPointer<PreviewResult>();
}
auto preview =
previewGenerator->generate(this->renderConfig, renderResult->getDocumentPath(), renderResult->getPage());
return QSharedPointer<PreviewResult>(preview);
}

查看文件

@ -0,0 +1,32 @@
#ifndef PREVIEWGENERATORMAPFUNCTOR_H
#define PREVIEWGENERATORMAPFUNCTOR_H
#include "renderconfig.h"
#include "previewgenerator.h"
class PreviewGeneratorMapFunctor
{
private:
enum GeneratorIndex
{
PDF = 0,
LAST_DUMMY
};
RenderConfig renderConfig;
PreviewGenerator *generator[LAST_DUMMY];
PreviewGenerator *getGenerator(QString filePath);
public:
typedef QSharedPointer<PreviewResult> result_type;
PreviewGeneratorMapFunctor();
~PreviewGeneratorMapFunctor();
void setRenderConfig(RenderConfig config);
QSharedPointer<PreviewResult> operator()(const QSharedPointer<PreviewResult> &renderResult);
};
#endif // PREVIEWGENERATORMAPFUNCTOR_H

59
gui/previewgeneratorpdf.cpp 普通文件
查看文件

@ -0,0 +1,59 @@
#include <QMutexLocker>
#include <QPainter>
#include "previewgeneratorpdf.h"
static QMutex cacheMutex;
Poppler::Document *PreviewGeneratorPdf::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;
}
PreviewResult *PreviewGeneratorPdf::generate(RenderConfig config, QString documentPath, unsigned int page)
{
PreviewResultPdf *result = new PreviewResultPdf(documentPath, page);
Poppler::Document *doc = document(documentPath);
if(doc == nullptr)
{
return result;
}
if(doc->isLocked())
{
return result;
}
int p = (int)page - 1;
if(p < 0)
{
p = 0;
}
Poppler::Page *pdfPage = doc->page(p);
QImage img = pdfPage->renderToImage(config.scaleX, config.scaleY);
for(QString &word : config.wordsToHighlight)
{
QList<QRectF> rects = pdfPage->search(word, Poppler::Page::SearchFlag::IgnoreCase);
for(QRectF &rect : rects)
{
QPainter painter(&img);
painter.scale(config.scaleX / 72.0, config.scaleY / 72.0);
painter.fillRect(rect, QColor(255, 255, 0, 64));
}
}
result->previewImage = img;
return result;
}

24
gui/previewgeneratorpdf.h 普通文件
查看文件

@ -0,0 +1,24 @@
#ifndef PREVIEWGENERATORPDF_H
#define PREVIEWGENERATORPDF_H
#include <poppler-qt5.h>
#include "previewgenerator.h"
#include "previewresultpdf.h"
class PreviewGeneratorPdf : public PreviewGenerator
{
protected:
QHash<QString, Poppler::Document *> documentcache;
Poppler::Document *document(QString path);
public:
using PreviewGenerator::PreviewGenerator;
PreviewResult *generate(RenderConfig config, QString documentPath, unsigned int page);
~PreviewGeneratorPdf()
{
qDeleteAll(documentcache);
}
};
#endif // PREVIEWGENERATORPDF_H