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 1c970a1..38c8610 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" @@ -18,9 +19,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi { 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(); @@ -37,10 +44,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi 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); @@ -141,7 +153,13 @@ 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 c7409f4..9cb256f 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 { @@ -28,7 +29,9 @@ class MainWindow : public QMainWindow 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 e93acdc..0000000 --- a/gui/searchworker.cpp +++ /dev/null @@ -1,34 +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 2a80258..0000000 --- a/gui/searchworker.h +++ /dev/null @@ -1,44 +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