-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
71 lines (61 loc) · 2.2 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "MainWindow.hpp"
#include "ui_MainWindow.h"
MainWindow::MainWindow(Dictionary* dictionary, QWidget* parent)
: QMainWindow(parent)
, mUi(new Ui::MainWindow)
, mDictionary(dictionary)
, mDictionaryThread(this)
, mUpdateTimer(new QTimer(this))
{
mUi->setupUi(this);
connect(mUi->inputEdit, &QLineEdit::textEdited, this, &MainWindow::initiateSearch);
connect(mUi->subsequentCheckBox, &QCheckBox::stateChanged, this, [this](int) {
this->initiateSearch(this->mUi->inputEdit->text());
} );
// Dictionary:
mDictionary->moveToThread(&mDictionaryThread);
connect(&mDictionaryThread, &QThread::finished, mDictionary, &QObject::deleteLater);
connect(mDictionary, &Dictionary::stateChanged, this, &MainWindow::searchStateChanged);
connect(this, &MainWindow::search, mDictionary, &Dictionary::search);
mDictionaryThread.start();
mUpdateTimer->start(100);
}
MainWindow::~MainWindow()
{
delete mUi;
mDictionaryThread.quit();
mDictionaryThread.wait();
}
void MainWindow::initiateSearch(const QString& searchLine)
{
if (searchLine.isEmpty()) {
mDictionary->getNewSeed(); // Cancel the last search
mDictionary->wipeResults(); // Wipe results of that search
mUi->resultEdit->clear();
return;
}
mDictionary->wipeResults(); // Wipe previous search's results
Dictionary::SearchType type;
if (mUi->subsequentCheckBox->checkState() == Qt::Checked)
type = Dictionary::SearchType::SUBSEQUENT;
else
type = Dictionary::SearchType::QUICK;
emit search(searchLine, type, mDictionary->getNewSeed());
}
void MainWindow::updateResults()
{
auto locker = mDictionary->getResultsLocker();
mUi->resultEdit->setPlainText(mDictionary->getResults());
}
void MainWindow::searchStateChanged(Dictionary::State newState)
{
if (newState == Dictionary::State::SEARCH) {
mUi->statusbar->showMessage("Searching...");
connect(mUpdateTimer, &QTimer::timeout, this, &MainWindow::updateResults);
}
else {
mUi->statusbar->showMessage("Done.");
disconnect(mUpdateTimer, &QTimer::timeout, this, &MainWindow::updateResults);
updateResults();
}
}