gui: remove searchworker
This commit is contained in:
parent
6f92b9ff5a
commit
2cf26a17e1
@ -25,14 +25,12 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
|||||||
SOURCES += \
|
SOURCES += \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
mainwindow.cpp \
|
mainwindow.cpp \
|
||||||
searchworker.cpp \
|
|
||||||
pdfworker.cpp \
|
pdfworker.cpp \
|
||||||
pdfpreview.cpp \
|
pdfpreview.cpp \
|
||||||
clicklabel.cpp
|
clicklabel.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
mainwindow.h \
|
mainwindow.h \
|
||||||
searchworker.h \
|
|
||||||
pdfworker.h \
|
pdfworker.h \
|
||||||
pdfpreview.h \
|
pdfpreview.h \
|
||||||
clicklabel.h
|
clicklabel.h
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
|
#include <QtConcurrent/QtConcurrent>
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
#include "clicklabel.h"
|
#include "clicklabel.h"
|
||||||
@ -18,9 +19,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
|||||||
{
|
{
|
||||||
ui->setupUi(this);
|
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();
|
pdfWorker = new PdfWorker();
|
||||||
searchWorker->moveToThread(&searchThread);
|
|
||||||
pdfWorker->moveToThread(&pdfWorkerThread);
|
pdfWorker->moveToThread(&pdfWorkerThread);
|
||||||
connectSignals();
|
connectSignals();
|
||||||
searchThread.start();
|
searchThread.start();
|
||||||
@ -37,10 +44,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
|||||||
void MainWindow::connectSignals()
|
void MainWindow::connectSignals()
|
||||||
{
|
{
|
||||||
connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed);
|
connect(ui->txtSearch, &QLineEdit::returnPressed, this, &MainWindow::lineEditReturnPressed);
|
||||||
connect(this, &MainWindow::beginSearch, searchWorker, &SearchWorker::search);
|
// connect(this, &MainWindow::beginSearch, searchWorker, &SearchWorker::search);
|
||||||
connect(searchWorker, &SearchWorker::searchResultsReady, this, &MainWindow::handleSearchResults);
|
connect(&searchWatcher, &QFutureWatcher<SearchResult>::finished, this,
|
||||||
connect(searchWorker, &SearchWorker::searchCancelled, this, &MainWindow::handleCancelledSearch);
|
[&]
|
||||||
connect(searchWorker, &SearchWorker::searchError, this, &MainWindow::handleSearchError);
|
{
|
||||||
|
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::itemActivated, this, &MainWindow::treeSearchItemActivated);
|
||||||
connect(ui->treeResultsList, &QTreeWidget::customContextMenuRequested, this,
|
connect(ui->treeResultsList, &QTreeWidget::customContextMenuRequested, this,
|
||||||
&MainWindow::showSearchResultsContextMenu);
|
&MainWindow::showSearchResultsContextMenu);
|
||||||
@ -141,7 +153,13 @@ void MainWindow::lineEditReturnPressed()
|
|||||||
}
|
}
|
||||||
// TODO: validate q;
|
// TODO: validate q;
|
||||||
ui->lblSearchResults->setText("Searching...");
|
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)
|
void MainWindow::handleSearchResults(const QVector<SearchResult> &results)
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
#include <QTreeWidgetItem>
|
#include <QTreeWidgetItem>
|
||||||
#include <QFileIconProvider>
|
#include <QFileIconProvider>
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include "searchworker.h"
|
#include <QFutureWatcher>
|
||||||
|
#include <QSqlDatabase>
|
||||||
#include "pdfworker.h"
|
#include "pdfworker.h"
|
||||||
namespace Ui
|
namespace Ui
|
||||||
{
|
{
|
||||||
@ -28,7 +29,9 @@ class MainWindow : public QMainWindow
|
|||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
QFileIconProvider iconProvider;
|
QFileIconProvider iconProvider;
|
||||||
bool pdfDirty;
|
bool pdfDirty;
|
||||||
SearchWorker *searchWorker;
|
QSqlDatabase db;
|
||||||
|
QFuture<QVector<SearchResult>> searchFuture;
|
||||||
|
QFutureWatcher<QVector<SearchResult>> searchWatcher;
|
||||||
PdfWorker *pdfWorker;
|
PdfWorker *pdfWorker;
|
||||||
void add(QString path, unsigned int page);
|
void add(QString path, unsigned int page);
|
||||||
QThread searchThread;
|
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
|
|
Loading…
Reference in New Issue
Block a user