new future: highlight searched words in pdf preview

Bu işleme şunda yer alıyor:
Albert S. 2019-08-18 23:23:11 +02:00
ebeveyn cff481a57e
işleme 22bcc888b9
4 değiştirilmiş dosya ile 39 ekleme ve 8 silme

Dosyayı Görüntüle

@ -16,7 +16,6 @@
#include "clicklabel.h" #include "clicklabel.h"
#include "../shared/sqlitesearch.h" #include "../shared/sqlitesearch.h"
#include "../shared/qssgeneralexception.h" #include "../shared/qssgeneralexception.h"
#include "../shared/qssquery.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -164,7 +163,8 @@ void MainWindow::lineEditReturnPressed()
[&, q]() [&, q]()
{ {
SqliteSearch searcher(db); SqliteSearch searcher(db);
return searcher.search(QSSQuery::build(q)); this->currentQuery = QSSQuery::build(q);
return searcher.search(this->currentQuery);
}); });
searchWatcher.setFuture(searchFuture); searchWatcher.setFuture(searchFuture);
} }
@ -215,8 +215,22 @@ void MainWindow::makePdfPreview()
processedPdfPreviews = 0; processedPdfPreviews = 0;
QString scaleText = ui->comboScale->currentText(); QString scaleText = ui->comboScale->currentText();
scaleText.chop(1); scaleText.chop(1);
QVector<QString> wordsToHighlight;
for(const Token &token : this->currentQuery.getTokens())
{
if(token.type == FILTER_CONTENT_CONTAINS)
{
auto splitted = token.value.split(" ");
for(QString &str : splitted)
{
wordsToHighlight.append(str);
}
}
}
PdfWorker worker; PdfWorker worker;
this->pdfWorkerWatcher.setFuture(worker.generatePreviews(this->pdfSearchResults, scaleText.toInt() / 100.)); this->pdfWorkerWatcher.setFuture(
worker.generatePreviews(this->pdfSearchResults, wordsToHighlight, scaleText.toInt() / 100.));
ui->pdfProcessBar->setMaximum(this->pdfWorkerWatcher.progressMaximum()); ui->pdfProcessBar->setMaximum(this->pdfWorkerWatcher.progressMaximum());
ui->pdfProcessBar->setMinimum(this->pdfWorkerWatcher.progressMinimum()); ui->pdfProcessBar->setMinimum(this->pdfWorkerWatcher.progressMinimum());
} }

Dosyayı Görüntüle

@ -9,6 +9,7 @@
#include <QFutureWatcher> #include <QFutureWatcher>
#include <QSqlDatabase> #include <QSqlDatabase>
#include "pdfworker.h" #include "pdfworker.h"
#include "../shared/qssquery.h"
namespace Ui namespace Ui
{ {
class MainWindow; class MainWindow;
@ -41,6 +42,7 @@ class MainWindow : public QMainWindow
unsigned int processedPdfPreviews; unsigned int processedPdfPreviews;
void handleSearchResults(const QVector<SearchResult> &results); void handleSearchResults(const QVector<SearchResult> &results);
void handleSearchError(QString error); void handleSearchError(QString error);
QSSQuery currentQuery;
private slots: private slots:
void lineEditReturnPressed(); void lineEditReturnPressed();
void treeSearchItemActivated(QTreeWidgetItem *item, int i); void treeSearchItemActivated(QTreeWidgetItem *item, int i);

Dosyayı Görüntüle

@ -6,6 +6,7 @@
#include <QMutexLocker> #include <QMutexLocker>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <QtConcurrent/QtConcurrentMap> #include <QtConcurrent/QtConcurrentMap>
#include <QPainter>
#include <atomic> #include <atomic>
#include "pdfworker.h" #include "pdfworker.h"
@ -19,12 +20,13 @@ struct Renderer
QHash<QString, Poppler::Document *> documentcache; QHash<QString, Poppler::Document *> documentcache;
qsizetype maxTotalPreviewImageMemUsage; qsizetype maxTotalPreviewImageMemUsage;
std::atomic<qsizetype> currentTotalPreviewImageMemUsage{0}; std::atomic<qsizetype> currentTotalPreviewImageMemUsage{0};
QVector<QString> wordsToHighlight;
Renderer(double scaleX, double scaleY, qsizetype maxPreviewImageMemUsage) Renderer(double scaleX, double scaleY, qsizetype maxPreviewImageMemUsage, QVector<QString> wordsToHighlight)
{ {
this->scaleX = scaleX; this->scaleX = scaleX;
this->scaleY = scaleY; this->scaleY = scaleY;
this->maxTotalPreviewImageMemUsage = maxPreviewImageMemUsage; this->maxTotalPreviewImageMemUsage = maxPreviewImageMemUsage;
this->wordsToHighlight = wordsToHighlight;
} }
/*we need this one because std::atomic has none, but this is only a functor for /*we need this one because std::atomic has none, but this is only a functor for
@ -34,6 +36,7 @@ struct Renderer
this->scaleX = o.scaleX; this->scaleX = o.scaleX;
this->scaleY = o.scaleY; this->scaleY = o.scaleY;
this->maxTotalPreviewImageMemUsage = o.maxTotalPreviewImageMemUsage; this->maxTotalPreviewImageMemUsage = o.maxTotalPreviewImageMemUsage;
this->wordsToHighlight = o.wordsToHighlight;
} }
~Renderer() ~Renderer()
@ -79,6 +82,16 @@ struct Renderer
} }
Poppler::Page *pdfPage = doc->page(p); Poppler::Page *pdfPage = doc->page(p);
QImage img = pdfPage->renderToImage(scaleX, scaleY); QImage img = pdfPage->renderToImage(scaleX, scaleY);
for(QString &word : wordsToHighlight)
{
QList<QRectF> rects = pdfPage->search(word);
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; result.previewImage = img;
currentTotalPreviewImageMemUsage += img.sizeInBytes(); currentTotalPreviewImageMemUsage += img.sizeInBytes();
} }
@ -86,7 +99,8 @@ struct Renderer
} }
}; };
QFuture<PdfPreview> PdfWorker::generatePreviews(const QVector<SearchResult> paths, double scalefactor) QFuture<PdfPreview> PdfWorker::generatePreviews(const QVector<SearchResult> paths, QVector<QString> wordsToHighlight,
double scalefactor)
{ {
QVector<PdfPreview> previews; QVector<PdfPreview> previews;
@ -106,5 +120,5 @@ QFuture<PdfPreview> PdfWorker::generatePreviews(const QVector<SearchResult> path
QSettings setting; QSettings setting;
qsizetype maxPreviewImageMemUsage = setting.value("maxpreviewimagesmemory", 1024 * 1024 * 1024).toLongLong(); qsizetype maxPreviewImageMemUsage = setting.value("maxpreviewimagesmemory", 1024 * 1024 * 1024).toLongLong();
return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY, maxPreviewImageMemUsage)); return QtConcurrent::mapped(previews, Renderer(scaleX, scaleY, maxPreviewImageMemUsage, wordsToHighlight));
} }

Dosyayı Görüntüle

@ -16,7 +16,8 @@ class PdfWorker : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
QFuture<PdfPreview> generatePreviews(const QVector<SearchResult> paths, double scalefactor); QFuture<PdfPreview> generatePreviews(const QVector<SearchResult> paths, QVector<QString> wordsToHighlight,
double scalefactor);
}; };
#endif // PDFWORKER_H #endif // PDFWORKER_H