-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetsearch.cpp
325 lines (276 loc) · 10.1 KB
/
widgetsearch.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "widgetsearch.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <filesystem>
#include "App.h"
#include "Dictionary.h"
#include "ui_widgetsearch.h"
#include "widgetdefinition.h"
WidgetSearch::WidgetSearch(QWidget* parent)
: QWidget(parent), ui(new Ui::WidgetSearch) {
ui->setupUi(this);
App::get();
for (auto& dictName : App::get().getListDictionaries()) {
ui->comboBoxDictionaryType->addItem(dictName);
}
currentDictName = ui->comboBoxDictionaryType->currentText();
QFile fin("data/currentDictionary.txt");
if (!fin.open(QFile::ReadOnly | QFile::Text)) return;
QTextStream in(&fin);
QString dictName = in.readLine();
fin.close();
qDebug() << dictName << '\n';
ui->comboBoxDictionaryType->setCurrentText(dictName);
ui->tabWidgetDefinition->clear();
ui->radioButtonSearchKeyword->setChecked(true);
ui->pushButtonAddWord->setEnabled(false);
loadHistory();
}
WidgetSearch::~WidgetSearch() {
delete ui;
}
// Double click to word in history
void WidgetSearch::on_listWidgetHistory_itemDoubleClicked(
QListWidgetItem* item) {
QString word = item->text();
for (int tabIndex = 0; tabIndex < ui->tabWidgetDefinition->count();
++tabIndex) {
if (ui->tabWidgetDefinition->tabText(tabIndex) == word) {
ui->tabWidgetDefinition->setCurrentIndex(tabIndex);
return;
}
}
auto newTab = new WidgetDefinition(this);
ui->tabWidgetDefinition->addTab(newTab, word);
ui->tabWidgetDefinition->setCurrentIndex(ui->tabWidgetDefinition->count() -
1);
// set title of the definition
newTab->setWord(word);
// set definition of the definition
QString definition = App::get().getDefinition(word);
newTab->setDefinition(definition);
// set favorite
newTab->setFavoriteState(word);
}
void WidgetSearch::removeCurrentTabDefinition() {
int index = ui->tabWidgetDefinition->currentIndex();
ui->tabWidgetDefinition->removeTab(index);
}
void WidgetSearch::reloadFavoriteStates() {
QList<QString> words;
for (int tabIndex = 0; tabIndex < ui->tabWidgetDefinition->count();
++tabIndex) {
words.push_back(ui->tabWidgetDefinition->tabText(tabIndex));
}
ui->tabWidgetDefinition->clear();
for (auto& word : words) {
auto newTab = new WidgetDefinition(this);
ui->tabWidgetDefinition->addTab(newTab, word);
ui->tabWidgetDefinition->setCurrentIndex(
ui->tabWidgetDefinition->count() - 1);
// set title of the definition
newTab->setWord(word);
// set definition of the definition
QString definition = App::get().getDefinition(word);
newTab->setDefinition(definition);
// set favorite
newTab->setFavoriteState(word);
}
}
void WidgetSearch::clearWidgetHistory() {
ui->listWidgetHistory->clear();
}
void WidgetSearch::clearLineEditSearch() {
ui->lineEditSearch->clear();
}
void WidgetSearch::on_tabWidgetDefinition_tabCloseRequested(int index) {
ui->tabWidgetDefinition->removeTab(index);
}
void WidgetSearch::on_pushButtonRandomWord_clicked() {
QString word = App::get().getRandomWord();
for (int tabIndex = 0; tabIndex < ui->tabWidgetDefinition->count();
++tabIndex) {
if (ui->tabWidgetDefinition->tabText(tabIndex) == word) {
ui->tabWidgetDefinition->setCurrentIndex(tabIndex);
return;
}
}
auto newTab = new WidgetDefinition(this);
ui->tabWidgetDefinition->addTab(newTab, word);
ui->tabWidgetDefinition->setCurrentIndex(ui->tabWidgetDefinition->count() -
1);
// set title of the definition
newTab->setWord(word);
// set definition of the definition
QString definition = App::get().getDefinition(word);
newTab->setDefinition(definition);
// set favorite
newTab->setFavoriteState(word);
loadHistory();
}
// Change text in combo box dictionary type
void WidgetSearch::on_comboBoxDictionaryType_currentTextChanged(
const QString& text) {
QString dictName = ui->comboBoxDictionaryType->currentText();
App::get().changeDictionary(dictName);
currentDictName = dictName;
ui->lineEditSearch->clear();
ui->tabWidgetDefinition->clear();
loadHistory();
}
// Change text in searching bar -> change text in history widget
void WidgetSearch::on_lineEditSearch_textChanged(const QString& prefix) {
if (prefix == "") {
ui->pushButtonAddWord->setEnabled(false);
loadHistory();
return;
}
if (!ui->radioButtonSearchKeyword->isChecked()) {
return;
}
auto result = App::get().getListWordsWithPrefix(prefix);
if (result.size() == 0) {
// Add new word enabled
ui->pushButtonAddWord->setEnabled(true);
} else
ui->pushButtonAddWord->setEnabled(false);
ui->listWidgetHistory->clear();
for (auto& word : result) {
new QListWidgetItem(word, ui->listWidgetHistory);
}
}
// Change text in history widget
void WidgetSearch::loadHistory() {
ui->listWidgetHistory->clear();
for (auto& word : App::get().getHistory()) {
new QListWidgetItem(word, ui->listWidgetHistory);
}
for (int listIndex = 0; listIndex < ui->listWidgetHistory->count();
++listIndex) {
auto item = ui->listWidgetHistory->item(listIndex);
item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
}
}
// Press reset button
void WidgetSearch::on_pushButtonReset_clicked() {
QMessageBox::StandardButton confirm = QMessageBox::question(
this, "Title", "Do you want to reset the current dictionary ?",
QMessageBox::Yes | QMessageBox::No);
if (confirm == QMessageBox::Yes) {
ui->listWidgetHistory->clear();
ui->tabWidgetDefinition->clear();
loadHistory();
ui->lineEditSearch->clear();
App::get().resetDictionary();
}
}
void WidgetSearch::openDialogAddNewWord() {
QString newWord = ui->lineEditSearch->text();
DialogAddNewWord dialogAddNewWord;
dialogAddNewWord.setDataAddNewWord(newWord);
dialogAddNewWord.exec();
}
// Press add new word button
void WidgetSearch::on_pushButtonAddWord_clicked() {
openDialogAddNewWord();
}
void WidgetSearch::on_lineEditSearch_returnPressed() {
if (ui->radioButtonSearchDictionary->isChecked()) {
auto result =
App::get().getListWordsHaveDefinition(ui->lineEditSearch->text());
ui->listWidgetHistory->clear();
for (auto& word : result) {
new QListWidgetItem(word, ui->listWidgetHistory);
}
}
}
const QString& WidgetSearch::getCurrentDictName() const {
return currentDictName;
}
namespace fs = std::filesystem;
void WidgetSearch::on_pushButtonAddDictionary_clicked() {
const int NUM_PARTS = 1000;
QString path = QFileDialog::getOpenFileName(this, "Open file", "C://",
tr("CSV files (*.csv)"));
if (path == "") return;
int index = path.lastIndexOf('/');
QString dictName = path.mid(index + 1, path.length() - index - 5);
if (App::get().getListDictionaries().contains(dictName)) {
QMessageBox::critical(this, "Add Dictionary",
"This dictionary already exists!");
return;
}
QString defisPath = "data/dicts/" + dictName + "/defis/";
QString wordsPath = "data/dicts/" + dictName + "/words/";
fs::create_directories(defisPath.toStdString());
fs::create_directories(wordsPath.toStdString());
int len = 0;
int i = 0;
QFile fin;
fin.setFileName(path);
if (!fin.open(QFile::ReadOnly | QFile::Text)) return;
QString line;
QTextStream in;
in.setDevice(&fin);
while (!in.atEnd()) {
line = in.readLine();
len++;
QString word;
QFile fout;
fout.setFileName(defisPath + QString::number(i) + ".csv");
if (!fout.open(QFile::WriteOnly | QFile::Text | QFile::Append)) return;
QTextStream out;
out.setDevice(&fout);
out << line << "\n";
fout.close();
word = CSV::readWordInLine(line);
fout.setFileName(wordsPath + "index.csv");
if (!fout.open(QFile::WriteOnly | QFile::Text | QFile::Append)) return;
out << CSV::writeLine(word, QString::number(i)) << "\n";
fout.close();
i = (i + 1) % NUM_PARTS;
}
fin.close();
QFile fout;
QTextStream out;
fout.setFileName(defisPath + "info.txt");
if (!fout.open(QFile::WriteOnly | QFile::Text)) return;
out.setDevice(&fout);
out << len / NUM_PARTS + 1 << '\n' << NUM_PARTS;
fout.close();
fout.setFileName(wordsPath + "info.txt");
if (!fout.open(QFile::WriteOnly | QFile::Text)) return;
out << len;
fout.close();
fs::copy("data/dicts/" + dictName.toStdString(),
"data/dicts-origin/" + dictName.toStdString(),
fs::copy_options::recursive);
ui->comboBoxDictionaryType->addItem(dictName);
App::get().addDictionary(dictName);
ui->comboBoxDictionaryType->setCurrentText(dictName);
return;
}
void WidgetSearch::on_pushButtonRemoveDictionary_clicked()
{
QMessageBox::StandardButton confirm = QMessageBox::question(
this, "Title", "Do you want to remove the current dictionary?",
QMessageBox::Yes | QMessageBox::No);
if (confirm == QMessageBox::Yes) {
if (App::get().getListDictionaries().size() == 1) {
QMessageBox::critical(this, "Remove Dictionary",
"Can't remove the only dictionary!");
return;
}
int index = ui->comboBoxDictionaryType->currentIndex();
QString dictName = ui->comboBoxDictionaryType->currentText();
QString newFirstDict = App::get().getListDictionaries().front();
if (dictName == newFirstDict) {
newFirstDict = App::get().getListDictionaries()[1];
}
App::get().changeDictionary(newFirstDict);
App::get().removeDictionary(dictName);
ui->comboBoxDictionaryType->setCurrentText(newFirstDict);
ui->comboBoxDictionaryType->removeItem(index);
}
}