-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
269 lines (248 loc) · 9.63 KB
/
Copy pathmainwindow.cpp
File metadata and controls
269 lines (248 loc) · 9.63 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->listView_sentence->installEventFilter(this);
connect(this, SIGNAL(addToQuiz()), this, SLOT(addToQuizAct()));
ui->pushButton_addtoquiz->setEnabled(false);
connect(this, SIGNAL(quizToDb(QString,QString,QString)),
this, SLOT(quizToDbAct(QString,QString,QString)));
connect(this, SIGNAL(exportWordsPaper()), this, SLOT(exportWordsPaperAct()));
connect(this, SIGNAL(beginExport(QString)), this, SLOT(exportQuizFiles(QString)));
connect(this, SIGNAL(exportthem(QString,QString)), this, SLOT(exportthemact(QString,QString)));
dbStorePath = QDir::homePath()+ QDir::separator() + "h" + QDir::separator() + "EnQuizData";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked() {
QString searchWord = ui->lineEdit->text();
QString searchUrl = "http://cn.bing.com/dict/search?q="+searchWord+"&go=提交&qs=n&form=CM&pq="+searchWord+"&sc=6-3&sp=-1&sk=";
webview = new QWebView(this);
connect(webview, SIGNAL(loadFinished(bool)),
this, SLOT(viewLoadFinished(bool)));
webview->load(QUrl(searchUrl));
}
void MainWindow::viewLoadFinished(bool loadok) {
if (loadok) {
QWebFrame *frame = webview->page()->mainFrame();
QWebElement document = frame->documentElement();
QWebElement headWordEle = document.findFirst("div#headword");
QWebElement prUSe = document.findFirst("div.hd_prUS");
QWebElement prBRe = document.findFirst("div.hd_pr");
QWebElementCollection translations = document.findAll("div.qdef ul li");
QWebElementCollection sentencesEn = document.findAll("div#sentenceSeg div.se_li div.sen_en");
QWebElementCollection sentencesCn = document.findAll("div#sentenceSeg div.se_li div.sen_cn");
QString headWord = headWordEle.toPlainText().trimmed();
QString prUS = prUSe.toPlainText().trimmed();
QString prBR = prBRe.toPlainText().trimmed();
QString underline = "";
QStandardItemModel *sentenceModel = new QStandardItemModel(sentencesEn.count(), 1, this);
QString trans = "";
QRegExp headwordrx(headWord, Qt::CaseInsensitive);
for (int c = 0; c < headWord.count(); ++c) {
underline += "__";
}
for (int i = 0; i < sentencesEn.count(); ++i) {
QString sentence = sentencesEn[i].toPlainText().trimmed().replace(
headwordrx, underline)
+ "\n" + sentencesCn[i].toPlainText().trimmed();
QStandardItem *sentenceItem = new QStandardItem(sentence);
sentenceModel->setItem(i, 0, sentenceItem);
}
foreach(QWebElement tranone, translations) {
trans += tranone.toPlainText().trimmed() + " ";
}
ui->label_headword->setText(headWord);
ui->label_usap->setText(prUS);
ui->label_brp->setText(prBR);
ui->label_trans->setText(trans.trimmed());
ui->listView_sentence->setModel(sentenceModel);
webview->deleteLater();
ui->listView_sentence->setFocus();
ui->listView_sentence->setCurrentIndex(sentenceModel->index(0,0));
ui->pushButton_addtoquiz->setEnabled(true);
} else {
QMessageBox::information(this, "Error Message", "Network error. Can't load the web page.");
}
}
void MainWindow::on_lineEdit_returnPressed() {
this->on_pushButton_clicked();
}
void MainWindow::on_actionVersion_triggered() {
QMessageBox::information(this, "Version 1.0.0",
"EnQuiz Version 1.0.0.\nCopyright 2015 Qihao. Published Under LGPL License.\nMust online and has SQLite in your computer.");
}
void MainWindow::addToQuizAct() {
QModelIndex index = ui->listView_sentence->selectionModel()->currentIndex();
QString quiz = ui->listView_sentence->model()->data(index).toString();
QString answer = ui->label_headword->text() + " " + ui->label_usap->text() + " "
+ ui->label_brp->text() + " " + ui->label_trans->text();
QString word = ui->label_headword->text();
if (QMessageBox::Yes == QMessageBox::question(this, "Notice", "Add this sentence into quiz, are you sure?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes)) {
emit quizToDb(word, quiz, answer);
}
}
void MainWindow::exportQuizFiles(QString filedir) {
QString tf = filedir + "/quiz.txt";
QString af = filedir + "/answer.txt";
QFile testfile(tf);
QFile answerfile(af);
if (testfile.exists() || answerfile.exists()) {
if (QMessageBox::Yes == QMessageBox::question(this, "Notice", "File already exists, overwrite them?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes)) {
testfile.close();
answerfile.close();
emit exportthem(tf, af);
}
} else {
testfile.close();
answerfile.close();
emit exportthem(tf, af);
}
}
bool MainWindow::eventFilter(QObject *object, QEvent *event) {
if (object == (QObject *)ui->listView_sentence && event->type() == QEvent::KeyPress) {
QKeyEvent *key = dynamic_cast<QKeyEvent *>(event);
if (key->key() == Qt::Key_Enter || key->key() == Qt::Key_Return) {
emit addToQuiz();
return true;
}
}
return QMainWindow::eventFilter(object, event);
}
void MainWindow::on_pushButton_addtoquiz_clicked() {
emit addToQuiz();
}
void MainWindow::quizToDbAct(QString word, QString quiz, QString answer) {
QDir dbdir(dbStorePath);
if (!dbdir.exists()) {
dbdir.mkdir(dbStorePath);
}
QSqlDatabase db;
if (QSqlDatabase::contains("sqconn")) {
db = QSqlDatabase::database("sqconn");
} else {
db = QSqlDatabase::addDatabase("QSQLITE", "sqconn");
}
db.setHostName("localhost");
db.setDatabaseName(dbdir.path()+ QDir::separator() +"enquiz.db");
if (! db.open()) {
QMessageBox::information(this, "DB Error", "Sqlite Database Open Error");
} else {
QSqlQuery query(db);
if (query.exec("create table if not exists enquiz(id INTEGER, word TEXT, quiz TEXT, answer TEXT, PRIMARY KEY(id ASC))")) {
query.prepare("insert into enquiz (word, quiz, answer) values (:word, :quiz, :answer)");
query.bindValue(":word", word);
query.bindValue(":quiz", quiz);
query.bindValue(":answer", answer);
if (query.exec()) {
QMessageBox::information(this, "Notice", "Add quiz successful ^_^ ");
ui->lineEdit->setText("");
ui->lineEdit->setFocus();
} else {
QMessageBox::information(this, "Notice", "Add Quiz Failed. -_-u " + query.lastError().driverText());
}
} else {
QMessageBox::information(this, "Notice", "Create DB Table Failed. -_-u " + query.lastError().driverText());
}
db.close();
}
}
void MainWindow::on_pushButton_exporttofile_clicked() {
unsigned int ws = 0;
QString wordsum = "";
QDir dbdir(dbStorePath);
QSqlDatabase db;
if (QSqlDatabase::contains("sqconn")) {
db = QSqlDatabase::database("sqconn");
} else {
db = QSqlDatabase::addDatabase("QSQLITE", "sqconn");
}
db.setHostName("localhost");
db.setDatabaseName(dbdir.path()+ QDir::separator() +"enquiz.db");
if (! db.open()) {
QMessageBox::information(this, "DB Error", "Sqlite Database Open Error");
} else {
QSqlQuery query(db);
query.exec("select count(*) as totalwords from enquiz");
while (query.next()) {
ws = query.value(0).toInt();
wordsum = query.value(0).toString();
}
db.close();
}
if (ws == 0) {
QMessageBox::information(this, "No Words", "No word can be exported. Please add some first.");
} else {
if (QMessageBox::Yes == QMessageBox::question(this, "Notice", "Export "+wordsum+" words test paper file, are you sure?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes)) {
emit exportWordsPaper();
}
}
}
void MainWindow::exportWordsPaperAct() {
QFileDialog saveDialog;
saveDialog.setFileMode(QFileDialog::Directory);
saveDialog.setOption(QFileDialog::ShowDirsOnly);
QString filedir = saveDialog.getExistingDirectory(
this,
tr("Choose A Directory to Generate My English Quiz Paper File"));
if (!filedir.isNull()) {
emit beginExport(filedir);
}
}
void MainWindow::exportthemact(QString testfilepath, QString answerfilepath) {
QFile testfile(testfilepath);
QFile answerfile(answerfilepath);
QString quizstr;
QString answerstr;
QSqlDatabase db;
if (QSqlDatabase::contains("sqconn")) {
db = QSqlDatabase::database("sqconn");
} else {
db = QSqlDatabase::addDatabase("QSQLITE", "sqconn");
}
db.setHostName("localhost");
QDir dbdir(dbStorePath);
db.setDatabaseName(dbdir.path()+ QDir::separator() +"enquiz.db");
if (! db.open()) {
QMessageBox::information(this, "DB Error", "Sqlite Database Open Error");
} else {
QSqlQuery query(db);
query.prepare("select id, word, quiz, answer from enquiz");
if (query.exec()) {
while (query.next()) {
//quizstr += QString("%1. %2\n").arg(query.value("id").toString()).arg(query.value("quiz").toString());
//answerstr += QString("%1. %2\n").arg(query.value("id").toString()).arg(query.value("answer").toString());
/*
qDebug()<<"-----------------------------------------";
qDebug()<<query.value(0).toString();
qDebug()<<query.value(1).toString();
qDebug()<<query.value(2).toString();
qDebug()<<query.value(3).toString();
*/
quizstr += QString("%1. %2\n\n").arg(query.value(0).toString()).arg(query.value(2).toString());
//qDebug() << quizstr;
answerstr += QString("%1. %2\n\n").arg(query.value(0).toString()).arg(query.value(3).toString());
//qDebug() <<answerstr;
}
if ( testfile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text) ) {
QTextStream stream( &testfile );
stream << quizstr << endl;
}
if ( answerfile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text) ) {
QTextStream stream( &answerfile );
stream << answerstr << endl;
}
QMessageBox::information(this, "Notice", "Export Quiz Paper File Successful. ^_^");
} else {
QMessageBox::information(this, "Notice", "Export file Failed. -_-u " + query.lastError().driverText());
}
db.close();
}
testfile.close();
answerfile.close();
}