From fd4a0ff9aeaff4d9c20e3728db0d36993afa06f1 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 18 Aug 2019 23:23:11 +0200 Subject: [PATCH] new future: highlight searched words in pdf preview --- gui/mainwindow.cpp | 19 ++++++++++++++++--- gui/mainwindow.h | 2 ++ gui/pdfworker.cpp | 21 +++++++++++++++++---- gui/pdfworker.h | 2 +- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 7d2a44a..c938f8f 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -16,7 +16,6 @@ #include "clicklabel.h" #include "../shared/sqlitesearch.h" #include "../shared/qssgeneralexception.h" -#include "../shared/qssquery.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) @@ -158,7 +157,8 @@ void MainWindow::lineEditReturnPressed() searchWatcher.waitForFinished(); QFuture> searchFuture = QtConcurrent::run([&, q]() { SqliteSearch searcher(db); - return searcher.search(QSSQuery::build(q)); + this->currentQuery = QSSQuery::build(q); + return searcher.search(this->currentQuery); }); searchWatcher.setFuture(searchFuture); @@ -210,8 +210,21 @@ void MainWindow::makePdfPreview() processedPdfPreviews = 0; QString scaleText = ui->comboScale->currentText(); scaleText.chop(1); + + QVector 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; - 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->setMinimum(this->pdfWorkerWatcher.progressMinimum()); diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 3b8f792..dbb7ae2 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -9,6 +9,7 @@ #include #include #include "pdfworker.h" +#include "../shared/qssquery.h" namespace Ui { class MainWindow; } @@ -39,6 +40,7 @@ private: unsigned int processedPdfPreviews; void handleSearchResults(const QVector &results); void handleSearchError(QString error); + QSSQuery currentQuery; private slots: void lineEditReturnPressed(); void treeSearchItemActivated(QTreeWidgetItem *item, int i); diff --git a/gui/pdfworker.cpp b/gui/pdfworker.cpp index 4364815..b8e7636 100644 --- a/gui/pdfworker.cpp +++ b/gui/pdfworker.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "pdfworker.h" @@ -20,12 +21,13 @@ struct Renderer QHash documentcache; qsizetype maxTotalPreviewImageMemUsage; std::atomic currentTotalPreviewImageMemUsage { 0 }; - - Renderer(double scaleX, double scaleY, qsizetype maxPreviewImageMemUsage) + QVector wordsToHighlight; + Renderer(double scaleX, double scaleY, qsizetype maxPreviewImageMemUsage, QVector wordsToHighlight) { this->scaleX = scaleX; this->scaleY = scaleY; this->maxTotalPreviewImageMemUsage = maxPreviewImageMemUsage; + this->wordsToHighlight = wordsToHighlight; } /*we need this one because std::atomic has none, but this is only a functor for @@ -35,6 +37,7 @@ struct Renderer this->scaleX = o.scaleX; this->scaleY = o.scaleY; this->maxTotalPreviewImageMemUsage = o.maxTotalPreviewImageMemUsage; + this->wordsToHighlight = o.wordsToHighlight; } ~Renderer() @@ -83,6 +86,16 @@ struct Renderer } Poppler::Page *pdfPage = doc->page(p); QImage img = pdfPage->renderToImage(scaleX, scaleY); + for(QString & word : wordsToHighlight) + { + QList 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; currentTotalPreviewImageMemUsage += img.sizeInBytes(); } @@ -92,7 +105,7 @@ struct Renderer -QFuture PdfWorker::generatePreviews(const QVector paths, double scalefactor) +QFuture PdfWorker::generatePreviews(const QVector paths, QVector wordsToHighlight, double scalefactor) { QVector previews; @@ -114,6 +127,6 @@ QFuture PdfWorker::generatePreviews(const QVector path QSettings setting; 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)); } diff --git a/gui/pdfworker.h b/gui/pdfworker.h index 0cdb620..0f0bef5 100644 --- a/gui/pdfworker.h +++ b/gui/pdfworker.h @@ -17,7 +17,7 @@ class PdfWorker : public QObject { Q_OBJECT public: - QFuture generatePreviews(const QVector paths, double scalefactor); + QFuture generatePreviews(const QVector paths, QVector wordsToHighlight, double scalefactor); }; #endif // PDFWORKER_H