Generalize previews: Add PreviewGenerator*
Este cometimento está contido em:
ascendente
95b3d1fce2
cometimento
d816603a1c
1
gui/previewgenerator.cpp
Ficheiro normal
1
gui/previewgenerator.cpp
Ficheiro normal
@ -0,0 +1 @@
|
||||
#include "previewgenerator.h"
|
17
gui/previewgenerator.h
Ficheiro normal
17
gui/previewgenerator.h
Ficheiro normal
@ -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
|
44
gui/previewgeneratormapfunctor.cpp
Ficheiro normal
44
gui/previewgeneratormapfunctor.cpp
Ficheiro normal
@ -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);
|
||||
}
|
32
gui/previewgeneratormapfunctor.h
Ficheiro normal
32
gui/previewgeneratormapfunctor.h
Ficheiro normal
@ -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
Ficheiro normal
59
gui/previewgeneratorpdf.cpp
Ficheiro normal
@ -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
Ficheiro normal
24
gui/previewgeneratorpdf.h
Ficheiro normal
@ -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
|
Carregando…
Criar uma nova questão referindo esta
Bloquear um utilizador