From 021722b090414ab1c333699a869803600027f602 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 27 Apr 2019 21:24:53 +0200 Subject: [PATCH] gui: remove searchworker --- gui/gui.pro | 2 -- gui/mainwindow.cpp | 29 +++++++++++++++++++++------- gui/mainwindow.h | 7 +++++-- gui/searchworker.cpp | 37 ------------------------------------ gui/searchworker.h | 45 -------------------------------------------- 5 files changed, 27 insertions(+), 93 deletions(-) delete mode 100644 gui/searchworker.cpp delete mode 100644 gui/searchworker.h diff --git a/gui/gui.pro b/gui/gui.pro index 48cbcaa..1fcfd40 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -25,14 +25,12 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow.cpp \ - searchworker.cpp \ pdfworker.cpp \ pdfpreview.cpp \ clicklabel.cpp HEADERS += \ mainwindow.h \ - searchworker.h \ pdfworker.h \ pdfpreview.h \ clicklabel.h diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 3a851b2..ed8f35a 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "mainwindow.h" #include "ui_mainwindow.h" #include "clicklabel.h" @@ -20,9 +21,15 @@ MainWindow::MainWindow(QWidget *parent) : { ui->setupUi(this); - searchWorker = new SearchWorker(QSettings().value("dbpath").toString()); + db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(QSettings().value("dbpath").toString()); + if(!db.open()) + { + qDebug() << "failed to open database"; + throw std::runtime_error("Failed to open database"); + } + pdfWorker = new PdfWorker(); - searchWorker->moveToThread(&searchThread); pdfWorker->moveToThread(&pdfWorkerThread); connectSignals(); searchThread.start(); @@ -39,10 +46,13 @@ MainWindow::MainWindow(QWidget *parent) : void MainWindow::connectSignals() { connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed); - connect(this, &MainWindow::beginSearch, searchWorker, &SearchWorker::search); - connect(searchWorker, &SearchWorker::searchResultsReady, this, &MainWindow::handleSearchResults); - connect(searchWorker, &SearchWorker::searchCancelled, this, &MainWindow::handleCancelledSearch); - connect(searchWorker, &SearchWorker::searchError, this, &MainWindow::handleSearchError); + // connect(this, &MainWindow::beginSearch, searchWorker, &SearchWorker::search); + connect(&searchWatcher, &QFutureWatcher::finished, this, [&]{ + auto results = searchWatcher.future().result(); + handleSearchResults(results); + }); + // connect(searchWorker, &SearchWorker::searchCancelled, this, &MainWindow::handleCancelledSearch); + // connect(searchWorker, &SearchWorker::searchError, this, &MainWindow::handleSearchError); connect(ui->treeResultsList, &QTreeWidget::itemActivated, this, &MainWindow::treeSearchItemActivated); connect(ui->treeResultsList, &QTreeWidget::customContextMenuRequested, this, &MainWindow::showSearchResultsContextMenu); connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::tabChanged); @@ -138,7 +148,12 @@ void MainWindow::lineEditReturnPressed() } //TODO: validate q; ui->lblSearchResults->setText("Searching..."); - emit beginSearch(q); + QFuture> searchFuture = QtConcurrent::run([&, q]() { + SqliteSearch searcher(db); + return searcher.search(q); + }); + searchWatcher.setFuture(searchFuture); + } void MainWindow::handleSearchResults(const QVector &results) diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 6ac5b4e..281408b 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -6,7 +6,8 @@ #include #include #include -#include "searchworker.h" +#include +#include #include "pdfworker.h" namespace Ui { class MainWindow; @@ -26,7 +27,9 @@ private: Ui::MainWindow *ui; QFileIconProvider iconProvider; bool pdfDirty; - SearchWorker *searchWorker; + QSqlDatabase db; + QFuture> searchFuture; + QFutureWatcher> searchWatcher; PdfWorker *pdfWorker; void add(QString path, unsigned int page); QThread searchThread; diff --git a/gui/searchworker.cpp b/gui/searchworker.cpp deleted file mode 100644 index 38d8a88..0000000 --- a/gui/searchworker.cpp +++ /dev/null @@ -1,37 +0,0 @@ - -#include -#include -#include -#include -#include "searchworker.h" -#include "../shared/sqlitesearch.h" -#include "qssgeneralexception.h" -SearchWorker::SearchWorker() -{ - -} -SearchWorker::SearchWorker(const QString &dbpath) -{ - db = QSqlDatabase::addDatabase("QSQLITE"); - db.setDatabaseName(dbpath); - if(!db.open()) - { - qDebug() << "failed to open database"; - } -} - -void SearchWorker::search(const QString &query) -{ - SqliteSearch searcher(db); - try - { - auto result = searcher.search(query); - emit searchResultsReady(searcher.search(query)); - } - catch(QSSGeneralException &e) - { - emit searchError(e.message); - } - -} - diff --git a/gui/searchworker.h b/gui/searchworker.h deleted file mode 100644 index 59a54a5..0000000 --- a/gui/searchworker.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef SEARCHWORKER_H -#define SEARCHWORKER_H -#include -#include -#include -#include -#include -#include "../shared/searchresult.h" - -class SearchWorker : public QObject -{ - class Command - { - public: - QString key; - QString value; - - Command(QString key="", QString value="") - { - this->key = key; - this->value = value; - } - - }; - - - Q_OBJECT -private: - QVector tokenize(QString expression); - QString createSql(const Command &cmd); - QString makeSql(const QVector &tokens); - QSqlDatabase db; -public: - SearchWorker(); - SearchWorker(const QString &dbpath); - bool checkParanthesis(QString expression); -public slots: - void search(const QString &query); -signals: - void searchResultsReady(const QVector &results); - void searchCancelled(); - void searchError(QString e); -}; - -#endif // SEARCHWORKER_H