From 8c027566e3c6e745a50cde44189c1c83ee131f35 Mon Sep 17 00:00:00 2001 From: Albert S Date: Fri, 26 Apr 2019 15:31:42 +0200 Subject: [PATCH] search: Avoid redundant results by placing pages into vector instead of returning searchresult for each page --- TODO | 1 - gui/mainwindow.cpp | 23 +++++++++-------------- gui/pdfworker.cpp | 27 ++++++++++++++++----------- shared/searchresult.h | 2 +- shared/sqlitesearch.cpp | 19 ++++++++++++++----- 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/TODO b/TODO index 5cbe48a..e9e4a0e 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ -Remove redundant searchresult info (redundant filedata (paths etc:)) pdfworker: use qtconcurrent Consider injecting Logger (default stdout/stderr) to classes instead of using Logger::info()/Logger::error():: sync/share GUI and CLI data-structures. Better to share codebase in general diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 7820df5..1c970a1 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -106,12 +106,12 @@ void MainWindow::pdfPreviewReceived(PdfPreview preview) label->setPixmap(QPixmap::fromImage(preview.previewImage)); ui->scrollAreaWidgetContents->layout()->addWidget(label); ui->pdfProcessBar->setValue(++processedPdfPreviews); + connect(label, &ClickLabel::clicked, [=]() { QSettings settings; QString command = settings.value("pdfviewer").toString(); - qDebug() << command; if(command != "" && command.contains("%p") && command.contains("%f")) { command = command.replace("%f", preview.documentPath); @@ -149,21 +149,17 @@ void MainWindow::handleSearchResults(const QVector &results) this->pdfSearchResults.clear(); ui->treeResultsList->clear(); ui->lblSearchResults->setText("Results: " + QString::number(results.size())); - QString lastpath = ""; for(const SearchResult &result : results) { - if(lastpath != result.fileData.absPath) - { - QFileInfo pathInfo(result.fileData.absPath); - QString fileName = pathInfo.fileName(); - QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeResultsList); + QFileInfo pathInfo(result.fileData.absPath); + QString fileName = pathInfo.fileName(); + QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeResultsList); - QDateTime dt = QDateTime::fromSecsSinceEpoch(result.fileData.mtime); - item->setIcon(0, iconProvider.icon(pathInfo)); - item->setText(0, fileName); - item->setText(1, result.fileData.absPath); - item->setText(2, dt.toString(Qt::ISODate)); - } + QDateTime dt = QDateTime::fromSecsSinceEpoch(result.fileData.mtime); + item->setIcon(0, iconProvider.icon(pathInfo)); + 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) @@ -173,7 +169,6 @@ void MainWindow::handleSearchResults(const QVector &results) this->pdfSearchResults.append(result); } } - lastpath = result.fileData.absPath; } ui->treeResultsList->resizeColumnToContents(0); ui->treeResultsList->resizeColumnToContents(1); diff --git a/gui/pdfworker.cpp b/gui/pdfworker.cpp index ec86d51..ea8ecba 100644 --- a/gui/pdfworker.cpp +++ b/gui/pdfworker.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "pdfworker.h" PdfWorker::PdfWorker() @@ -41,18 +42,22 @@ void PdfWorker::generatePreviews(QVector paths, double scalefactor { continue; } - int p = (int)sr.page - 1; - if(p < 0) - p = 0; - Poppler::Page *pdfpage = doc->page(p); - QImage image = pdfpage->renderToImage(QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor, - QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor); + for(unsigned int page : sr.pages) + { + int p = (int)page - 1; + if(p < 0) + p = 0; + Poppler::Page *pdfPage = doc->page(p); + QImage image = + pdfPage->renderToImage(QGuiApplication::primaryScreen()->physicalDotsPerInchX() * scalefactor, + QGuiApplication::primaryScreen()->physicalDotsPerInchY() * scalefactor); - PdfPreview preview; - preview.previewImage = image; - preview.documentPath = sr.fileData.absPath; - preview.page = sr.page; - emit previewReady(preview); + PdfPreview preview; + preview.previewImage = image; + preview.documentPath = sr.fileData.absPath; + preview.page = page; + emit previewReady(preview); + } } isFreeMutex.lock(); isFree.wakeOne(); diff --git a/shared/searchresult.h b/shared/searchresult.h index eb27d90..3a40975 100644 --- a/shared/searchresult.h +++ b/shared/searchresult.h @@ -6,7 +6,7 @@ class SearchResult { public: FileData fileData; - unsigned int page; + QVector pages; }; #endif // SEARCHRESULT_H diff --git a/shared/sqlitesearch.cpp b/shared/sqlitesearch.cpp index 09289aa..d30c65e 100644 --- a/shared/sqlitesearch.cpp +++ b/shared/sqlitesearch.cpp @@ -223,13 +223,14 @@ QSqlQuery SqliteSearch::makeSqlQuery(const QVector &tokens) QString prepSql; if(isContentSearch) { - prepSql = "SELECT file.path AS path, content.page AS page, file.mtime AS mtime, file.size AS size, " - "file.filetype AS filetype FROM file INNER JOIN content ON file.id = content.fileid WHERE 1=1 AND " + - whereSql + " " + sortSql; + prepSql = + "SELECT file.path AS path, group_concat(content.page) AS pages, file.mtime AS mtime, file.size AS size, " + "file.filetype AS filetype FROM file INNER JOIN content ON file.id = content.fileid WHERE 1=1 AND " + + whereSql + " " + sortSql + " GROUP BY file.path"; } else { - prepSql = "SELECT file.path AS path, 0 as page, file.mtime AS mtime, file.size AS size, file.filetype AS " + prepSql = "SELECT file.path AS path, '0' as pages, file.mtime AS mtime, file.size AS size, file.filetype AS " "filetype FROM file WHERE 1=1 AND " + whereSql + " " + sortSql; } @@ -266,7 +267,15 @@ QVector SqliteSearch::search(const QString &query) result.fileData.mtime = dbQuery.value("mtime").toUInt(); result.fileData.size = dbQuery.value("size").toUInt(); result.fileData.filetype = dbQuery.value("filetype").toChar(); - result.page = dbQuery.value("page").toUInt(); + QString pages = dbQuery.value("pages").toString(); + QStringList pagesList = pages.split(","); + for(QString &page : pagesList) + { + if(page != "") + { + result.pages.append(page.toUInt()); + } + } results.append(result); } return results;