-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDictMainWindow.cpp
480 lines (431 loc) · 12.6 KB
/
DictMainWindow.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "DictMainWindow.h"
#include "ui_DictMainWindow.h"
#include "PathMgr.h"
#include "AddDictDialog.h"
#include "DeleteDictDialog.h"
#include "ConfigDialog.h"
#include "QMessageBox"
#include "AboutDialog.h"
#include "QTableWidgetItem"
#include "UserSelectDialog.h"
#include "QTimer"
#include "QDebug"
////////////////////////////////////////////////////
//
DictMainWindow::DictMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::DictMainWindow)
{
ui->setupUi(this);
///////////////////////////////////////////////////////////////
//
QIcon icon = QIcon("./rc/Wordpress.ico");
//创建托盘图标
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(icon);
QString titlec=tr("德语电子词典 -- AnsleDict");
QString textc=tr("欢迎您使用德语电子词典 -- AnsleDict");
ui->statusBar->showMessage(tr("欢迎您使用德语背单词软件德语电子词典! -- ansleliu"));
trayIcon->show();
//弹出气泡提示
trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
//添加单/双击鼠标相应
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,SLOT(trayiconActivated(QSystemTrayIcon::ActivationReason)));
//创建监听行为
minimizeAction = new QAction(tr("最小化 (&I)"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
restoreAction = new QAction(tr("还原 (&R)"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("退出 (&Q)"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
//创建右键弹出菜单
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
///////////////////////////////////////////////////////////////////////////////////////
//发音方法配置
speakConfig = new ConfigFile(PathManage::makePathStr("/AnsleDict/speakConfig.ini"));
//////////////////////////////////////////////////////////////////////
//设置软件相关路径
PathManage::createRelExeDir("/AnsleDict/MyDict/");
PathManage::createRelExeDir("/AnsleDict/MyDict/Dict/");
// PathManage::createRelExeDir("/AnsleDict/MyDict/我的生词本/");
//创建我的生词本
myBook = NULL;
speakMgr = NULL;
// QString myBookDB = PathManage::makePathStr("/AnsleDict/MyDict/我的生词本/我的生词本.db");
// myBook = new CreatWordListDB(myBookDB,"MyBook");
//创建词典列表数据库
QString myDictDB = PathManage::makePathStr("/AnsleDict/MyDict/MyDictList.db");
MyDictListDB = new MyDictList(myDictDB,"MyDictList");
/////////////////////////////////////////////////
//
myDict = NULL;
currDictPath = "";
currDictName = "";
//////////////////////////////////////////////////////////////
//音频信号与槽连接
ui->action_Speak->setEnabled(false);
ui->action_AddToNewWord->setEnabled(false);
speakMgr = NULL;
model = NULL;
ui->WordTableView->setUpdatesEnabled(true);
dictListShow();
ui->WordLineEdit->setFocus();
///////////////////////////////////////////////////////////////
//默认用户选择
defaultUser = "default";
QTimer::singleShot(200, this, SLOT(userSelect()));
}
DictMainWindow::~DictMainWindow()
{
if(model != NULL)
{
model->deleteLater();
model = NULL;
}
delete MyDictListDB;
MyDictListDB = NULL;
if(myBook != NULL)
{
delete myBook;
myBook = NULL;
}
// qDebug() << QString("删除myDict");
if(myDict != NULL)
{
delete myDict;
myDict = NULL;
}
if(speakMgr != NULL)
{
speakMgr->deleteLater();
speakMgr = NULL;
}
delete ui;
}
void DictMainWindow::dictListShow()
{
ui->DictComboBox->clear();
QStringList dictList = MyDictListDB->getDictNames();
QStringListIterator Iterator(dictList);
//向combobox中添加单元
while(Iterator.hasNext())
{
ui->DictComboBox->addItem(QIcon("./rc/Dots.ico"),Iterator.next());
}
}
void DictMainWindow::userSelect()
{
UserSelectDialog *user;
user = new UserSelectDialog(this,false);
if(user->exec() == QDialog::Accepted)
{
defaultUser = user->getDefaultUser();
QString myBookDB = PathManage::makePathStr("/userdata/" + defaultUser + "/我的生词本/我的生词本.db");
myBook = new CreatWordListDB(myBookDB,"MYBOOk");
this->setWindowTitle(QString("AnsleDict -- 默认当前用户:%1").arg(defaultUser));
user->close();
user->deleteLater();
user = NULL;
}
else
{
on_action_quit_triggered();
user->close();
user->deleteLater();
user = NULL;
}
}
void DictMainWindow::on_action_Add_triggered()
{
AddDictDialog *AddDict;
AddDict = new AddDictDialog(this);
if(AddDict->exec() == QDialog::Accepted)
{
currDictPath = AddDict->getDictPath();
currDictName = AddDict->getDictName();
if(currDictPath == "" || currDictName == "")
{
qDebug() << QString("添加词典失败!");
}
else
{
MyDictListDB->addNewDict(currDictName,currDictPath);
dictListShow();
}
}
AddDict->close();
AddDict->deleteLater();
AddDict = NULL;
ui->action_Speak->setEnabled(false);
ui->action_AddToNewWord->setEnabled(false);
}
void DictMainWindow::on_action_Delete_triggered()
{
DeleteDictDialog * deleteDict;
deleteDict = new DeleteDictDialog(MyDictListDB,this);
if(deleteDict->exec() == QDialog::Accepted)
{
dictListShow();
}
else
{
dictListShow();
}
deleteDict->close();
deleteDict->deleteLater();
deleteDict = NULL;
ui->action_Speak->setEnabled(false);
ui->action_AddToNewWord->setEnabled(false);
}
void DictMainWindow::on_action_Config_triggered()
{
ConfigDialog * config;
config = new ConfigDialog(this,speakConfig);
if(config->exec() == QDialog::Accepted)
{
}
config->close();
config->deleteLater();
config = NULL;
}
void DictMainWindow::on_action_Mini_triggered()
{
this->hide();
}
void DictMainWindow::on_action_quit_triggered()
{
if (QMessageBox::information(this, "提示信息", "您确定要退出吗?",
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
{
QApplication::closeAllWindows();
}
else
{
}
}
void DictMainWindow::on_action_NomWindow_triggered()
{
this->showNormal();
}
void DictMainWindow::on_action_Max_triggered()
{
this->showFullScreen();
}
void DictMainWindow::on_action_Qt_triggered()
{
QApplication::aboutQt();
}
void DictMainWindow::on_action_About_triggered()
{
AboutDialog *aboutD = new AboutDialog(this);
if(aboutD->exec() == QDialog::Rejected)
{
}
aboutD->close();
aboutD->deleteLater();
aboutD = NULL;
}
void DictMainWindow::on_DictComboBox_currentIndexChanged(const QString &dictname)
{
ui->action_Speak->setEnabled(false);
ui->action_AddToNewWord->setEnabled(false);
if(dictname == "")
{
if(myDict != NULL)
{
delete myDict;
myDict = NULL;
}
model = NULL;
ui->MeanTextBrowser->clear();
ui->WordLineEdit->clear();
}
else
{
ui->MeanTextBrowser->clear();
ui->WordLineEdit->clear();
QString dictPath = MyDictListDB->getDictPath(dictname);
if(myDict != NULL)
{
delete myDict;
myDict = NULL;
}
myDict = new CreatWordListDB(dictPath,"MyDict");
model = NULL;
model = myDict->wordListDBModel(this,"WordsBook");
ui->WordTableView->setModel(model);
ui->WordTableView->hideColumn(0);
ui->WordTableView->hideColumn(2);
ui->WordTableView->hideColumn(3);
ui->WordTableView->hideColumn(4);
model->select();
}
}
void DictMainWindow::on_WordLineEdit_textChanged(const QString &word)
{
ui->WordLineEdit->setFocus();
ui->MeanTextBrowser->clear();
ui->action_Speak->setEnabled(false);
ui->action_AddToNewWord->setEnabled(false);
// model->setFilter(QString("Wort like '%%1%'").arg(word));
//显示结果
// model->select();
if(ui->ExpComboBox->currentIndex() == 0)
{
model->setFilter(QString("Wort like '%1%'").arg(word));
//显示结果
model->select();
}
else if(ui->ExpComboBox->currentIndex() == 1)
{
model->setFilter(QString("Wort like '%%1%'").arg(word));
//显示结果
model->select();
}
else if(ui->ExpComboBox->currentIndex() == 2)
{
model->setFilter(QString("Wort like '%%1'").arg(word));
//显示结果
model->select();
}
}
void DictMainWindow::on_action_AddToNewWord_triggered()
{
QModelIndex currIndex = ui->WordTableView->currentIndex();
QString word = currIndex.data().toString();
if(myBook->isExist(word))
{
QMessageBox::information(this, "提示信息", "该单词在生词本中已经存在,不必重复添加");
ui->action_AddToNewWord->setEnabled(false);
}
else
{
int row = currIndex.row();
QString mean = model->index(row,2).data().toString();
int count = myBook->getRowCount();
int lektion = (count+1)/200 + 1;
myBook->addNewRecord(word,mean,"",lektion);
ui->action_AddToNewWord->setEnabled(false);
}
}
void DictMainWindow::on_WordTableView_clicked(const QModelIndex &index)
{
ui->action_Speak->setEnabled(true);
// QString word = index.data().toString();
int row = index.row();
QString mean = model->index(row,2).data().toString();
ui->MeanTextBrowser->clear();
// ui->WordLineEdit->setText(word);
ui->MeanTextBrowser->setHtml(mean);
ui->action_Speak->setEnabled(true);
ui->action_AddToNewWord->setEnabled(true);
}
void DictMainWindow::trayiconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
//单击托盘图标
{
if(this->isHidden())
{
this->showNormal();
this->raise();
break;
}
else
{
this->hide();
break;
}
}
case QSystemTrayIcon::DoubleClick:
//双击托盘图标
{
if(this->isHidden())
{
this->showNormal();
this->raise();
break;
}
else
{
this->hide();
break;
}
}
default:
break;
}
}
void DictMainWindow::on_WordTableView_customContextMenuRequested(const QPoint &pos)
{
QModelIndex currIndex = ui->WordTableView->indexAt(pos);
//这种情况是右键的位置不在范围内,即在空白位置右击
if(!currIndex.isValid())
{
}
else
{
QMenu *popMenu =new QMenu(this);//定义一个右键弹出菜单
popMenu->addAction(ui->action_AddToNewWord);
popMenu->addAction(ui->action_Speak);
popMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置
delete popMenu;
popMenu = NULL;
}
}
void DictMainWindow::on_action_Speak_triggered()
{
ui->action_Speak->setEnabled(false);
QModelIndex currIndex = ui->WordTableView->currentIndex();
QString word = currIndex.data().toString();
if(speakMgr != NULL)
{
speakMgr->deleteLater();
speakMgr = NULL;
}
speakMgr = new SpeakMgr(speakConfig,this);
connect(speakMgr,SIGNAL(SpeakFininshed()),this,SLOT(setButtonState()));
speakMgr->Speak(word,500,1);
}
void DictMainWindow::setButtonState()
{
ui->action_Speak->setEnabled(true);
if(speakMgr != NULL)
{
speakMgr->deleteLater();
speakMgr = NULL;
}
}
void DictMainWindow::on_ExpComboBox_currentIndexChanged(int index)
{
ui->WordLineEdit->setFocus();
ui->MeanTextBrowser->clear();
ui->action_Speak->setEnabled(false);
ui->action_AddToNewWord->setEnabled(false);
if(index == 0)
{
model->setFilter(QString("Wort like '%1%'").arg(ui->WordLineEdit->text()));
//显示结果
model->select();
}
else if(index == 1)
{
model->setFilter(QString("Wort like '%%1%'").arg(ui->WordLineEdit->text()));
//显示结果
model->select();
}
else if(index == 2)
{
model->setFilter(QString("Wort like '%%1'").arg(ui->WordLineEdit->text()));
//显示结果
model->select();
}
}