gui: stop generating preview images after a memory limit has been reached.

This commit is contained in:
Albert S. 2019-04-30 14:13:10 +02:00
parent 5deb64cd28
commit 330a9e5870
3 changed files with 73 additions and 49 deletions

View File

@ -115,6 +115,8 @@ void MainWindow::tabChanged()
} }
void MainWindow::pdfPreviewReceived(PdfPreview preview) void MainWindow::pdfPreviewReceived(PdfPreview preview)
{
if(preview.hasPreviewImage())
{ {
ClickLabel *label = new ClickLabel(); ClickLabel *label = new ClickLabel();
label->setPixmap(QPixmap::fromImage(preview.previewImage)); label->setPixmap(QPixmap::fromImage(preview.previewImage));
@ -143,6 +145,7 @@ void MainWindow::pdfPreviewReceived(PdfPreview preview)
} }
}); });
} }
}
void MainWindow::lineEditReturnPressed() void MainWindow::lineEditReturnPressed()
{ {
@ -179,16 +182,11 @@ void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
item->setText(0, fileName); item->setText(0, fileName);
item->setText(1, result.fileData.absPath); item->setText(1, result.fileData.absPath);
item->setText(2, dt.toString(Qt::ISODate)); item->setText(2, dt.toString(Qt::ISODate));
// TODO: this must be user defined or done more intelligently
if(this->pdfSearchResults.size() < 300)
{
if(result.fileData.absPath.endsWith(".pdf")) if(result.fileData.absPath.endsWith(".pdf"))
{ {
this->pdfSearchResults.append(result); this->pdfSearchResults.append(result);
} }
} }
}
ui->treeResultsList->resizeColumnToContents(0); ui->treeResultsList->resizeColumnToContents(0);
ui->treeResultsList->resizeColumnToContents(1); ui->treeResultsList->resizeColumnToContents(1);
pdfDirty = !this->pdfSearchResults.empty(); pdfDirty = !this->pdfSearchResults.empty();

View File

@ -9,6 +9,11 @@ class PdfPreview
QImage previewImage; QImage previewImage;
QString documentPath; QString documentPath;
unsigned int page; unsigned int page;
bool hasPreviewImage()
{
return !previewImage.isNull();
}
}; };
#endif // PDFPREVIEW_H #endif // PDFPREVIEW_H

View File

@ -6,6 +6,7 @@
#include <QMutexLocker> #include <QMutexLocker>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <QtConcurrent/QtConcurrentMap> #include <QtConcurrent/QtConcurrentMap>
#include <atomic>
#include "pdfworker.h" #include "pdfworker.h"
static QMutex cacheMutex; static QMutex cacheMutex;
@ -16,10 +17,22 @@ struct Renderer
double scaleX; double scaleX;
double scaleY; double scaleY;
QHash<QString, Poppler::Document *> documentcache; QHash<QString, Poppler::Document *> documentcache;
Renderer(double scaleX, double scaleY) qsizetype maxTotalPreviewImageMemUsage;
std::atomic<qsizetype> currentTotalPreviewImageMemUsage{0};
Renderer(double scaleX, double scaleY, qsizetype maxPreviewImageMemUsage)
{ {
this->scaleX = scaleX; this->scaleX = scaleX;
this->scaleY = scaleY; this->scaleY = scaleY;
this->maxTotalPreviewImageMemUsage = maxPreviewImageMemUsage;
}
/*we need this one because std::atomic has none, but this is only a functor for
concurrentmap, thus, it's ok for it to be half-broken*/
Renderer(const Renderer &o)
{
this->scaleX = o.scaleX;
this->scaleY = o.scaleY;
} }
~Renderer() ~Renderer()
@ -35,7 +48,7 @@ struct Renderer
Poppler::Document *result = Poppler::Document::load(path); Poppler::Document *result = Poppler::Document::load(path);
if(result == nullptr) if(result == nullptr)
{ {
// TODO: some kind of user feedback would be nicec // TODO: some kind of user feedback would be nice
return nullptr; return nullptr;
} }
result->setRenderHint(Poppler::Document::TextAntialiasing); result->setRenderHint(Poppler::Document::TextAntialiasing);
@ -45,6 +58,9 @@ struct Renderer
} }
PdfPreview operator()(const PdfPreview &preview) PdfPreview operator()(const PdfPreview &preview)
{
PdfPreview result = preview;
if(currentTotalPreviewImageMemUsage.load() < maxTotalPreviewImageMemUsage)
{ {
Poppler::Document *doc = document(preview.documentPath); Poppler::Document *doc = document(preview.documentPath);
if(doc == nullptr) if(doc == nullptr)
@ -61,8 +77,10 @@ struct Renderer
p = 0; p = 0;
} }
Poppler::Page *pdfPage = doc->page(p); Poppler::Page *pdfPage = doc->page(p);
PdfPreview result = preview; QImage img = pdfPage->renderToImage(scaleX, scaleY);
result.previewImage = pdfPage->renderToImage(scaleX, scaleY); result.previewImage = img;
currentTotalPreviewImageMemUsage += img.sizeInBytes();
}
return result; return result;
} }
}; };
@ -85,5 +103,8 @@ QFuture<PdfPreview> PdfWorker::generatePreviews(const QVector<SearchResult> path
double scaleX = QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor; double scaleX = QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor;
double scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor; double scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor;
return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY)); QSettings setting;
qsizetype maxPreviewImageMemUsage = setting.value("maxpreviewimagesmemory", 1024 * 1024 * 1024).toLongLong();
return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY, maxPreviewImageMemUsage));
} }