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
förälder 8d6ee157a7
incheckning a4f2612071
3 ändrade filer med 71 tillägg och 48 borttagningar

Visa fil

@ -114,30 +114,33 @@ void MainWindow::tabChanged()
void MainWindow::pdfPreviewReceived(PdfPreview preview)
{
ClickLabel *label = new ClickLabel();
label->setPixmap(QPixmap::fromImage(preview.previewImage));
label->setToolTip(preview.documentPath);
ui->scrollAreaWidgetContents->layout()->addWidget(label);
connect(label, &ClickLabel::leftClick, [=]() {
QSettings settings;
QString command = settings.value("pdfviewer").toString();
if(command != "" && command.contains("%p") && command.contains("%f"))
{
command = command.replace("%f", preview.documentPath);
command = command.replace("%p", QString::number(preview.page));
QStringList splitted = command.split(" ");
if(splitted.size() > 1)
if(preview.hasPreviewImage())
{
ClickLabel *label = new ClickLabel();
label->setPixmap(QPixmap::fromImage(preview.previewImage));
label->setToolTip(preview.documentPath);
ui->scrollAreaWidgetContents->layout()->addWidget(label);
connect(label, &ClickLabel::leftClick, [=]() {
QSettings settings;
QString command = settings.value("pdfviewer").toString();
if(command != "" && command.contains("%p") && command.contains("%f"))
{
QString cmd = splitted[0];
QStringList args = splitted.mid(1);
QProcess::startDetached(cmd, args);
command = command.replace("%f", preview.documentPath);
command = command.replace("%p", QString::number(preview.page));
QStringList splitted = command.split(" ");
if(splitted.size() > 1)
{
QString cmd = splitted[0];
QStringList args = splitted.mid(1);
QProcess::startDetached(cmd, args);
}
}
}
else
{
QDesktopServices::openUrl(QUrl::fromLocalFile(preview.documentPath));
}
});
else
{
QDesktopServices::openUrl(QUrl::fromLocalFile(preview.documentPath));
}
});
}
}
void MainWindow::lineEditReturnPressed()
@ -174,14 +177,9 @@ void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
item->setText(0, fileName);
item->setText(1, result.fileData.absPath);
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);

Visa fil

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

Visa fil

@ -6,6 +6,7 @@
#include <QMutexLocker>
#include <QtConcurrent/QtConcurrent>
#include <QtConcurrent/QtConcurrentMap>
#include <atomic>
#include "pdfworker.h"
@ -17,13 +18,25 @@ struct Renderer
double scaleX;
double scaleY;
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->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()
{
qDeleteAll(documentcache);
@ -38,7 +51,7 @@ struct Renderer
Poppler::Document *result = Poppler::Document::load(path);
if(result == nullptr)
{
//TODO: some kind of user feedback would be nicec
//TODO: some kind of user feedback would be nice
return nullptr;
}
result->setRenderHint(Poppler::Document::TextAntialiasing);
@ -51,23 +64,28 @@ struct Renderer
PdfPreview operator()(const PdfPreview &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);
PdfPreview result = preview;
result.previewImage = pdfPage->renderToImage(scaleX, scaleY);
if(currentTotalPreviewImageMemUsage.load() < maxTotalPreviewImageMemUsage)
{
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);
result.previewImage = img;
currentTotalPreviewImageMemUsage += img.sizeInBytes();
}
return result;
}
};
@ -94,7 +112,9 @@ QFuture<PdfPreview> PdfWorker::generatePreviews(const QVector<SearchResult> path
double scaleX = QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor;
double scaleY = QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor;
QSettings setting;
qsizetype maxPreviewImageMemUsage = setting.value("maxpreviewimagesmemory", 1024 * 1024 * 1024).toLongLong();
return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY));
return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY, maxPreviewImageMemUsage));
}