-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.cpp
350 lines (295 loc) · 12.6 KB
/
search.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "search.h"
#include "ui_search.h"
#include "utils.h"
#include "dicttabscontainer.h"
#include <QLabel>
#include <QCheckBox>
#include <QLineEdit>
#include <QProgressBar>
#include <QTreeWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QFormLayout>
#include <QComboBox>
#include <QSpinBox>
#include <QDebug>
Search::Search(const QSet<DictGlobalAttributes> * attrs, QWidget *parent) :
openedDicts(attrs),
QDialog(parent),
// dictsToSearch(&(ui->verticalLayout_2)),
ui(new Ui::search)
{
ui->setupUi(this);
layout = new QGridLayout(ui->whereToSearchBox);
QSet<int> set;
set.insert(1);
set.insert(2);
set.insert(3);
int counter = 0;
for (QSet<DictGlobalAttributes>::const_iterator i = attrs->begin(); i != attrs->end(); ++i, counter++) {
QCheckBox * checkbox = new QCheckBox(tr((*i).getDictname().toStdString().c_str()));
checkbox->setProperty("row", counter);
checkbox->setProperty("connection name", (*i).getFilename());
checkbox->setProperty("dictionary name", (*i).getDictname());
checkbox->setChecked(true);
layout->addWidget(checkbox, counter, 0);
// layout->addWidget(new QLabel(tr(QVariant(counter).toString().toStdString().c_str())), counter, 1);
QLineEdit * line = new QLineEdit();
line->setToolTip("В этом поле вы можете задать локальные переменные поиска для каждого из словарей. \nО переменных подробнее написано в всплывающей подсказке в поле 'глобальные переменные поиска'");
layout->addWidget(line, counter, 1);
}
ui->whereToSearchBox->setLayout(layout);
connect(ui->findButton, SIGNAL(clicked()), this, SLOT(onSearchClick()));
}
bool Search::checkVars(QString line) {
QStringList varsList = line.split(" ");
QRegExp rx("(.=(.+(,)?)+)*");
foreach (const QString &item, varsList) {
if (!rx.exactMatch(item)) return false;
}
return true;
}
bool Search::checkQuery(QString line) {
if (line.contains("delete", Qt::CaseInsensitive) || line.contains("update", Qt::CaseInsensitive) || line.contains("insert", Qt::CaseInsensitive)) {
errorMsg("Пожалуйста, не пытайтесь сломать программу (я знаю, это довольно легко, но в то же время абсолютно бесполезно).");
return false;
}
return true;
}
QMap<QString, QList<QString> > Search::parseVars(QString line) {
QMap<QString, QList<QString> > vars;
return vars;
}
QStringList constructDictionary(const QStringList& initialLine, QString vars, bool isglobal) {
QStringList dictionary;
QStringList plainDictionary; //this list contains lines that don't need to have replaces
if (vars.trimmed().isEmpty()) {
plainDictionary = initialLine;
for (int i = 0; i < plainDictionary.size(); i++) {
plainDictionary[i].append("%");
plainDictionary[i].prepend("%");
if (isglobal) {
plainDictionary[i].replace(QRegularExpression("\\$."), "_");
qDebug() << "Replaced unused variable, that's what we get: " << plainDictionary[i];
}
}
return plainDictionary;
}
foreach (const QString &line, initialLine) {
if (!line.contains("$")) {
plainDictionary.append(line);
} else {
dictionary.append(line);
}
}
foreach(const QString& item, dictionary) {
qDebug() << "For replace: " << item;
}
foreach(const QString& item, plainDictionary) {
qDebug() << "Don't need changes: " << item;
}
foreach(const QString& item, dictionary) {
qDebug() << "Replacing: " << item;
}
QStringList varsSeparate = vars.split(" ");
foreach(const QString& item, varsSeparate) {
qDebug() << "Vars separate: " << item;
}
foreach (const QString& line, dictionary) {
foreach (const QString& item, varsSeparate) {
QString varname = item[0];
varname.prepend("$");
if (!line.contains(varname)) {
qDebug() << line << " doesn't contain" << varname;
continue;
} else {
QString onlyValues = item.mid(2);
QStringList valuesList = onlyValues.split(",");
long int sizeBefore = dictionary.size();
foreach (const QString& value, valuesList) {
if (value.trimmed().isEmpty()) {
continue;
}
qDebug() << "replacing " << varname << " with " << value;
foreach (const QString& word, dictionary) {
QString tempWord = word;
qDebug() << tempWord;
tempWord.replace(varname, value);
if (tempWord != word) {
dictionary.append(tempWord);
qDebug() << tempWord << " " << tempWord.replace(varname, value);
}
}
}
for (int i = 0; i < sizeBefore; i++) {
dictionary.removeFirst();
}
}
}
}
for (int i = 0; i < plainDictionary.size(); i++) {
plainDictionary[i].append("%");
plainDictionary[i].prepend("%");
if (isglobal) {
plainDictionary[i].replace(QRegularExpression("\\$."), "_");
}
}
for (int i = 0; i < dictionary.size(); i++) {
dictionary[i].append("%");
dictionary[i].prepend("%");
if (isglobal) {
dictionary[i].replace(QRegularExpression("\\$."), "_");
}
}
dictionary.append(plainDictionary);
return dictionary;
}
QString Search::constructQuery(int dict_size) {
QString query("SELECT * FROM 'dictionary' WHERE ");
// QString query("SELECT id, word, transcription, translation FROM 'dictionary' WHERE ");
if (ui->etimCheck->isChecked()) {
query.append("(etimology_tag NOT NULL) AND ");
}
if (ui->paradigmCheck->isChecked()) {
query.append("id = (SELECT regular_form FROM 'dictionary') AND ");
}
if (ui->praatCheck->isChecked()) {
query.append("id = (SELECT wordid FROM 'dict_blobs_description' WHERE type=2) AND ");
}
if (ui->audioCheck->isChecked()) {
query.append("id = (SELECT wordid FROM 'dict_blobs_description' WHERE type=1) AND ");
}
if (ui->whereToSearchCombo->currentIndex() == 0) {
query.append("regular_form IS NULL AND (");
query.append("(transcription LIKE ?)");
for (int i = 1; i < dict_size; i++) {
query.append(" OR (transcription LIKE ?)");
}
query.append(")");
} else if (ui->whereToSearchCombo->currentIndex() == 1) {
query.append("(");
query.append("(translation LIKE ?)");
for (int i = 1; i < dict_size; i++) {
query.append(" OR (translation LIKE ?)");
}
query.append(")");
} else if (ui->whereToSearchCombo->currentIndex() == 2) {
query.append("regular_form NOT NULL AND (");
query.append("(transcription LIKE ?)");
for (int i = 1; i < dict_size; i++) {
query.append(" OR (transcription LIKE ?)");
}
query.append(")");
} else if (ui->whereToSearchCombo->currentIndex() == 3) {
query.append("(");
query.append("(word LIKE ?)");
for (int i = 1; i < dict_size; i++) {
query.append(" OR (word LIKE ?)");
}
query.append(")");
}
return query;
}
bool Search::onSearchClick() {
if (!this->checkVars(ui->globalVarsLine->text())) {
errorMsg("Вы неправильно заполнили поле глобальных переменных. Наведите мышь на поле ввода глобальных переменных на пару секунд, и выскочит подсказка, в которой написано, как правильно задавать переменные.");
}
QList<QCheckBox *> ch = ui->whereToSearchBox->findChildren<QCheckBox *>();
qDebug() << "List size:" << ch.size();
for (QList<QCheckBox *>::iterator i = ch.begin(); i != ch.end(); ++i) {
bool checked = ( (*i)->isChecked());
if (checked) {
qDebug() << (*i)->property("row");
qDebug() << (*i)->property("connection name");
QLineEdit * varline = (QLineEdit *) layout->itemAtPosition((*i)->property("row").toInt(), 1)->widget();
if (varline != NULL) {
qDebug() << "Checking var line";
if (!this->checkVars(varline->text())) {
QString msg = "Вы неправильно заполнили поле переменных. Наведите мышь на поле ввода глобальных переменных на пару секунд, и выскочит подсказка, в которой написано, как правильно задавать переменные. \nОшибка в строке: ";
msg.append(varline->text());
errorMsg(msg);
}
}
}
}
QTreeWidget * resTree = new QTreeWidget(this);
QStringList labels;
resTree->setColumnCount(3);
resTree->setSortingEnabled(true);
labels.append("Регулярная форма");
labels.append("Указатель на регулярную форму");
labels.append("Транскрипция");
labels.append("Перевод");
labels.append("ID");
resTree->setHeaderLabels(labels);
resTree->hideColumn(1);
resTree->hideColumn(4);
resTree->setColumnWidth(0,350);
for (QList<QCheckBox *>::iterator i = ch.begin(); i != ch.end(); ++i) {
bool checked = ( (*i)->isChecked());
if (checked) {
qDebug() << (*i)->property("row");
qDebug() << (*i)->property("connection name");
QTreeWidgetItem * resultsForDict = new QTreeWidgetItem((*i)->property("dictionary name").toStringList());
resTree->addTopLevelItem(resultsForDict);
QLineEdit * varline = (QLineEdit *) layout->itemAtPosition((*i)->property("row").toInt(), 1)->widget();
if (varline != NULL) {
QStringList dict;
dict.push_back(ui->searchLine->text());
dict = constructDictionary(dict, varline->text(), false);
foreach(const QString& item, dict) {
qDebug() << "Replaced by local: " << item;
}
dict = constructDictionary(dict, ui->globalVarsLine->text(), true);
foreach(const QString& item, dict) {
qDebug() << "Replaced by global: " << item;
}
qDebug() << "Dict size" << dict.size();
qDebug() << constructQuery(dict.size());
QString queryText = constructQuery(dict.size());
QSqlQuery query(QSqlDatabase::database((*i)->property("connection name").toString()));
query.prepare(queryText);
for (int i = 0; i < dict.size(); i++) {
query.bindValue(i, QVariant(dict[i]));
}
if (query.exec()) {
while (query.next()) {
QStringList item;
item.append(query.value("word").toString());
item.append(query.value("regular_form").toString());
item.append(query.value("transcription").toString());
item.append(query.value("translation").toString());
item.append(query.value("id").toString());
QTreeWidgetItem * entry = new QTreeWidgetItem(item);
resultsForDict->addChild(entry);
}
}
// qDebug() << this->parent()->parent()->metaObject()->className();
}
}
}
int tabid = (static_cast<QTabWidget *>(this->parentWidget()->parentWidget()))->addTab(resTree, ui->searchLine->text());
qDebug() << this->parentWidget()->parentWidget()->parentWidget()->parentWidget()->findChild<DictTabsContainer *>();
connect(this, SIGNAL(showDictAndWord(QString,QVariant)), this->parentWidget()->parentWidget()->parentWidget()->parentWidget()->findChild<DictTabsContainer *>(), SLOT(goToDictAndWord(QString,QVariant)));
connect(resTree, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(doubleClickTree(QModelIndex)));
(static_cast<QTabWidget *>(this->parentWidget()->parentWidget()))->setCurrentIndex(tabid);
return true;
}
bool Search::doubleClickTree(QModelIndex idx) {
QVariant id = idx.sibling(idx.row(), 4).data();
if (!id.isValid()) {
return false;
}
//that means that we were looking for paradigm
if (!idx.sibling(idx.row(), 1).data().toString().isEmpty()) {
id = idx.sibling(idx.row(), 1).data();
}
QString dictName = idx.parent().data().toString();
qDebug() << "Clicked " << dictName << id;
emit showDictAndWord(dictName, id);
return true;
}
Search::~Search()
{
delete ui;
}