gui: remove searchworker

This commit is contained in:
Albert S. 2019-04-27 21:24:53 +02:00
父節點 6f92b9ff5a
當前提交 2cf26a17e1
共有 5 個檔案被更改,包括 30 行新增89 行删除

查看文件

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

查看文件

@ -10,6 +10,7 @@
#include <QDateTime>
#include <QProcess>
#include <QComboBox>
#include <QtConcurrent/QtConcurrent>
#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<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);
@ -141,7 +153,13 @@ 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)

查看文件

@ -6,7 +6,8 @@
#include <QTreeWidgetItem>
#include <QFileIconProvider>
#include <QKeyEvent>
#include "searchworker.h"
#include <QFutureWatcher>
#include <QSqlDatabase>
#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<QVector<SearchResult>> searchFuture;
QFutureWatcher<QVector<SearchResult>> searchWatcher;
PdfWorker *pdfWorker;
void add(QString path, unsigned int page);
QThread searchThread;

查看文件

@ -1,34 +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);
}
}

查看文件

@ -1,44 +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