gui: remove searchworker

This commit is contained in:
Albert S. 2019-04-27 21:24:53 +02:00
parent 233c30b66b
commit 021722b090
5 changed files with 27 additions and 93 deletions

View File

@ -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

View File

@ -10,6 +10,7 @@
#include <QDateTime>
#include <QProcess>
#include <QComboBox>
#include <QtConcurrent/QtConcurrent>
#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<SearchResult>::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<QVector<SearchResult>> searchFuture = QtConcurrent::run([&, q]() {
SqliteSearch searcher(db);
return searcher.search(q);
});
searchWatcher.setFuture(searchFuture);
}
void MainWindow::handleSearchResults(const QVector<SearchResult> &results)

View File

@ -6,7 +6,8 @@
#include <QTreeWidgetItem>
#include <QFileIconProvider>
#include <QKeyEvent>
#include "searchworker.h"
#include <QFutureWatcher>
#include <QSqlDatabase>
#include "pdfworker.h"
namespace Ui {
class MainWindow;
@ -26,7 +27,9 @@ private:
Ui::MainWindow *ui;
QFileIconProvider iconProvider;
bool pdfDirty;
SearchWorker *searchWorker;
QSqlDatabase db;
QFuture<QVector<SearchResult>> searchFuture;
QFutureWatcher<QVector<SearchResult>> searchWatcher;
PdfWorker *pdfWorker;
void add(QString path, unsigned int page);
QThread searchThread;

View File

@ -1,37 +0,0 @@
#include <QRegularExpression>
#include <QDebug>
#include <QSqlError>
#include <QStack>
#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);
}
}

View File

@ -1,45 +0,0 @@
#ifndef SEARCHWORKER_H
#define SEARCHWORKER_H
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QVector>
#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<Command> tokenize(QString expression);
QString createSql(const Command &cmd);
QString makeSql(const QVector<Command> &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<SearchResult> &results);
void searchCancelled();
void searchError(QString e);
};
#endif // SEARCHWORKER_H