-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathayikoption.cpp
334 lines (298 loc) · 10.8 KB
/
ayikoption.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
#include "ayikoption.h"
#include <QFile>
#include <QtWidgets/QSplitter>
#include <QtWidgets/QDialog>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QProgressDialog>
#include <QtWidgets/QLabel>
AyikOption* AyikOption::instance;
AyikOption::AyikOption()
{
init();
}
void AyikOption::init()
{
}
void AyikOption::postInit()
{
tableHeaders<<tr("Tag")<<tr("Name")<<tr("Rating")<<tr("Meaning");
ratingLevels<<"1"<<"2"<<"3"<<"4"<<"5";
freqLevels<<"1"<<"5"<<"10"<<"20";
tagAllWords=tr("All");
txt_meaning_changed=false;
tabs = new QTabWidget();
QWidget* central = new QWidget();
QWidget* wordswidget = new QWidget();
QWidget* helpwidget = new QWidget();
QVBoxLayout *layout = new QVBoxLayout(central);
//-- frequency
group_freq = new QGroupBox(tr("Frequency"));
cmb_freq = new QComboBox();
cmb_freq->setEditable(true);
cmb_freq->addItems(freqLevels);
QLabel *lbl_freq = new QLabel(tr("Frequency(mins)"));
QHBoxLayout *freqlayout = new QHBoxLayout(group_freq);
freqlayout->addWidget(lbl_freq);
freqlayout->addWidget(cmb_freq);
freqlayout->addStretch();
//-- tag
group_tag = new QGroupBox(tr("Tag"));
//cmb_tag = new QComboBox();
//cmb_freq->setEditable(true);
tagslist = new QListWidget();
QLabel *lbl_tag = new QLabel(tr("Tag"));
btn_tag_select = new QPushButton(tr("Choose"));
lineedit_tag = new QLineEdit();
lineedit_tag->setText(tagAllWords);
lineedit_tag->setReadOnly(true);
QVBoxLayout *tag2layout = new QVBoxLayout(group_tag);
QHBoxLayout *taglayout = new QHBoxLayout();
taglayout->addWidget(lbl_tag);
//taglayout->addWidget(cmb_tag);
taglayout->addWidget(lineedit_tag);
taglayout->addWidget(btn_tag_select);
taglayout->addStretch();
//todo
tagslist->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed));
tagslist->setFixedHeight(60);
tag2layout->addLayout(taglayout);
tag2layout->addWidget(tagslist);
//tag2layout->addStretch();
//-- source history
group_src_hist = new QGroupBox(tr("Word Level"));
//-- history
rating = new AyikRating(false,true,1,5);
QLabel *lbl_rating = new QLabel(tr("Level"));
cmb_rating_oper = new QComboBox();
QStringList items_rating;
items_rating << ">=" << "=" << "<=" ;
cmb_rating_oper->addItems(items_rating);
QHBoxLayout *groupsrchistlayout = new QHBoxLayout(group_src_hist);
groupsrchistlayout->addWidget(lbl_rating);
groupsrchistlayout->addWidget(cmb_rating_oper);
groupsrchistlayout->addWidget(rating);
groupsrchistlayout->addStretch();
layout->addWidget(group_freq);
layout->addWidget(group_tag);
layout->addWidget(group_src_hist);
layout->addStretch();
//-- words
QVBoxLayout *wwlayout = new QVBoxLayout(wordswidget);
btn_add_file = new QPushButton(tr("Add..."));
btn_delete_word = new QPushButton(tr("Delete"));
btn_delete_all = new QPushButton(tr("Delete All"));
btn_update_meaning = new QPushButton(QIcon(":/images/ok.png"),tr("Update"));
lineedit_word_filter = new QLineEdit();
db = AyikDB::getInstance()->db;
tableview = new TableView(db);
tableview->model->setTable("words");
tableview->model->select();
tableview->model->setHeaderData(0, Qt::Horizontal, QObject::tr("Id"));
tableview->model->setHeaderData(1, Qt::Horizontal, QObject::tr("Tag"));
tableview->model->setHeaderData(2, Qt::Horizontal, QObject::tr("Name"));
tableview->model->setHeaderData(3, Qt::Horizontal, QObject::tr("Meaning"));
tableview->model->setHeaderData(4, Qt::Horizontal, QObject::tr("Rating"));
//tableview->model->setHeaders(tableHeaders);
tableview->setColumnHidden(0,true);
tableview->setColumnHidden(3,true);
tableview->setColumnHidden(4,true);
tableview->setModel(tableview->model);
txt_meaning = new QTextEdit();
QHBoxLayout *wwbtnlayout = new QHBoxLayout();
wwbtnlayout->addWidget(btn_add_file);
wwbtnlayout->addWidget(btn_delete_word);
wwbtnlayout->addWidget(btn_delete_all);
wwbtnlayout->addStretch();
QHBoxLayout *wwfilterlayout = new QHBoxLayout();
wwfilterlayout->addWidget(new QLabel(tr("Filter:")));
wwfilterlayout->addWidget(lineedit_word_filter);
wwbtnlayout->addStretch();
wwlayout->addLayout(wwbtnlayout);
wwlayout->addLayout(wwfilterlayout);
QSplitter *word_meaning_splitter = new QSplitter(Qt::Vertical,this);
word_meaning_splitter->addWidget(tableview);
word_meaning_splitter->addWidget(txt_meaning);
word_meaning_splitter->addWidget(btn_update_meaning);
wwlayout->addWidget(word_meaning_splitter);
//help
QVBoxLayout *helplayout = new QVBoxLayout();
QTextEdit *txt_help = new QTextEdit();
txt_help->setReadOnly(true);
QFile file(":/images/help.html");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString text = in.readAll();
txt_help->setHtml(text);
}
helplayout->addWidget(txt_help);
helpwidget->setLayout(helplayout);
tabs->addTab(central,tr("General"));
tabs->addTab(wordswidget,tr("Words"));
tabs->addTab(helpwidget,tr("Help"));
setCentralWidget(tabs);
tagslist->setVisible(false);//after initing
createActions();
}
AyikOption* AyikOption::getInstance()
{
if(instance==NULL) instance = new AyikOption();
return instance;
}
int AyikOption::getMessageFrequency() {
QString siklik = cmb_freq->currentText();
return siklik.toInt();
}
QString AyikOption::getTag()
{
QString tag="";
/*if(cmb_tag->currentIndex()>0) {
tag = cmb_tag->currentText();
}*/
tag = lineedit_tag->text();
if(tag == tagAllWords) tag="";
return tag;
}
QString AyikOption::getRating()
{
return rating->getRating();
}
QString AyikOption::getRatingOperator()
{
QString ratingoper = cmb_rating_oper->currentText();
return ratingoper;
}
void AyikOption::createActions()
{
connect(btn_add_file,SIGNAL(clicked()),this,SLOT(slot_add_file()));
connect(btn_delete_word,SIGNAL(clicked()),this,SLOT(slot_delete_word()));
connect(btn_delete_all,SIGNAL(clicked()),this,SLOT(slot_delete_all()));
connect(btn_update_meaning,SIGNAL(clicked()),this,SLOT(slot_update_meaning()));
//connect(cmb_tag,SIGNAL(clicked()),this,SLOT(slot_add_file()));//todo
connect(tableview->selectionModel(),SIGNAL(currentRowChanged(const QModelIndex&,const QModelIndex&)),this,SLOT(slot_word_selected(const QModelIndex&,const QModelIndex&)));
connect(txt_meaning,SIGNAL(textChanged()),this,SLOT(slot_txt_meaning_changed()));
connect(lineedit_word_filter,SIGNAL(textChanged(const QString&)),this,SLOT(slot_filterTextChanged(const QString&)));
connect(btn_tag_select,SIGNAL(clicked()),this,SLOT(slot_chooseTag()));
connect(tagslist,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(slot_tagChosen(QListWidgetItem*)));
}
void AyikOption::closeEvent(QCloseEvent *event)
{
//todo: save & load
event->ignore();
// loadCheckedFileList();
hide();
}
void AyikOption::slot_add_file()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Open Words File"), "", tr("Text Files (*.txt)"));
if(fileName!="") {
bool tagOk;
QString tag = QInputDialog::getText(this, tr("Tag"),tr("Tag:"), QLineEdit::Normal, "", &tagOk);
if (tagOk && !tag.isEmpty()) {
bool ratingOk;
QString rating = QInputDialog::getItem(this, tr("Rating"), tr("Rating:"), ratingLevels, 2, false, &ratingOk);
if (ratingOk && !rating.isEmpty()) {
//qDebug()<<fileName<<tag<<rating;
//QMessageBox::information(0,"info",fileName+" "+tag+" "+rating);
AyikDB::getInstance()->addWords(fileName,tag,rating);
tableview->model->select();//reload list
}
}
}
}
void AyikOption::slot_delete_word()
{
QString err;
QModelIndexList selection = tableview->selectionModel()->selectedRows(1);
QProgressDialog pd(tr("Deleting.."), tr("Cancel"), 0, selection.size());
pd.setWindowModality(Qt::WindowModal);
int steps=0;
QList<AyikWord> wordlist;
for(int i=selection.size() - 1;i >=0 ;i--) {
if (pd.wasCanceled()) break;
QModelIndex index = selection.at(i);
QString name = tableview->model->data(index,Qt::EditRole).toString();
AyikWord w;
w.setName(name);
wordlist.append(w);
if(wordlist.size()>50) {
AyikDB::getInstance()->deleteWords(wordlist,err);
steps+=wordlist.size();
wordlist.clear();
pd.setValue(steps);
}
tableview->model->removeRow(index.row());
}
if(wordlist.size()>0) {
AyikDB::getInstance()->deleteWords(wordlist,err);
steps+=wordlist.size();
wordlist.clear();
pd.setValue(steps);
}
currword.setName("");
txt_meaning->clear();
tableview->model->select();
}
void AyikOption::slot_delete_all()
{
QString err;
AyikDB::getInstance()->deleteWordsLike(lineedit_word_filter->text(), err);
currword.setName("");
txt_meaning->clear();
tableview->model->select();
}
void AyikOption::slot_update_meaning()
{
currword.setMeaning(txt_meaning->toPlainText().replace("\n","<br>"));
AyikDB::getInstance()->updateWord(currword);
tableview->model->select();
}
void AyikOption::slot_word_selected(const QModelIndex ¤t,const QModelIndex &previous)
{
if(current.isValid()) {
QModelIndex nameIndex = tableview->model->index(current.row(), 2);
QModelIndex meaningIndex = tableview->model->index(current.row(), 3);
QString currwordname = tableview->model->data(nameIndex).toString();
QString currwordmeaning = tableview->model->data(meaningIndex).toString();
QString currerr;
AyikDB::getInstance()->getWord(currword,currwordname,currerr);
currwordmeaning = currword.getMeaning();
txt_meaning->setHtml(currwordmeaning);
} else {
currword.setName("");
txt_meaning->clear();
}
}
void AyikOption::slot_txt_meaning_changed()
{
txt_meaning_changed=true;
}
void AyikOption::slot_filterTextChanged(const QString& text)
{
tableview->model->setFilter("tag like '%"+text+"%' or name like '%"+text+"%'");
tableview->model->select();
}
void AyikOption::showHelp()
{
tabs->setCurrentIndex(2);
setVisible(true);
}
void AyikOption::slot_chooseTag()
{
reloadTagList();
tagslist->setVisible(true);
}
void AyikOption::reloadTagList()
{
QStringList items_tag;
QString err;
AyikDB::getInstance()->getTagList(items_tag,err);//todo: err check
items_tag.push_front(tagAllWords);
tagslist->clear();
tagslist->addItems(items_tag);
}
void AyikOption::slot_tagChosen(QListWidgetItem * item)
{
lineedit_tag->setText(item->text());
tagslist->setVisible(false);
}