-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbookeditdialog.cpp
213 lines (183 loc) · 5.72 KB
/
bookeditdialog.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
#include "bookeditdialog.h"
#include "ui_bookeditdialog.h"
#include "addnewrecorddialog.h"
#include <QMessageBox>
#include <QSqlError>
#include <QIcon>
#include <QSqlQuery>
#include <QTime>
#include <QTimer>
#include "bcmgr.h"
BookEditDialog::BookEditDialog(QWidget *parent, const QString &BookPath) :
QDialog(parent),
ui(new Ui::BookEditDialog)
{
ui->setupUi(this);
QDateTime currtime = QDateTime::currentDateTime();//获取系统现在的时间
QString str = currtime.toString("yyyy-MM-dd dddd hh:mm:ss "); //设置显示格式
ui->label->setText(str);//在标签上显示时间
//定时器,每1000ms执行一次槽函数displayTime()
QTimer *timer = new QTimer(this);
timer->start(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(displayTime()));
bookpath = BookPath;
tableName = "WordsBook";
bookEdit = new BookEdit(bookpath,"BOOKEDIT");
model = bookEdit->wordListDBModel(this,tableName);
ui->tableView->setModel(model);
asIdSelect = "FALSE";//按id查询关
asWortSelect = "FALSE";//按wort查询关
descendingOrder = "FALSE";//按降序关
ui->goTolineEdit->setDisabled(true);
}
BookEditDialog::~BookEditDialog()
{
delete bookEdit;
delete ui;
}
void BookEditDialog::displayTime()
{
QDateTime currtime = QDateTime::currentDateTime();//获取系统现在的时间
QString str = currtime.toString("yyyy-MM-dd dddd hh:mm:ss "); //设置显示格式
ui->label->setText(str);//在标签上显示时间
}
void BookEditDialog::on_newRecordButton_clicked()
{
int rowNum = bookEdit->getRowCount();
qDebug() << rowNum;
AddNewRecordDialog *newRecord;
newRecord = new AddNewRecordDialog(this);
if(newRecord->exec() == QDialog::Accepted)
{
/*
model->insertRow(rowNum); //添加一行
model->setData(model->index(rowNum,0),rowNum+1);//主键id自增
model->setData(model->index(rowNum,1),newRecord->getWord());
model->setData(model->index(rowNum,2),newRecord->getMean());
model->setData(model->index(rowNum,3),newRecord->getOther());
model->setData(model->index(rowNum,4),newRecord->getLektion());
model->submitAll(); //直接提交
*/
bookEdit->addNewRecord(newRecord->getWord(),
newRecord->getMean(),
newRecord->getOther(),
newRecord->getLektion());
model->select();
}
///////////////////////////////////////////////////////////////////
//窗口后续处理
newRecord->close();
newRecord->deleteLater();
newRecord = NULL;
////////////////////////////
//更新词库词汇量
updateWordCount();
}
void BookEditDialog::on_deleteRecordButton_clicked()
{
int rowNum = bookEdit->getRowCount();
qDebug() << rowNum;
//删除该行
// model->removeRow(rowNum-1);
int ok = QMessageBox::warning(this,tr("删除最后一行"),
tr("选择所要删除的行后再进行这个操作!"
"\n "
"你确定删除第 %1 行吗?").arg(rowNum),
QMessageBox::Yes,QMessageBox::No);
if(ok == QMessageBox::No)
{
model->select(); //如果不删除,则撤销
}
else
{
bookEdit->removeRecord(rowNum); //否则提交,在数据库中删除该行
model->select();
////////////////////////////
//更新词库词汇量
updateWordCount();
}
}
void BookEditDialog::on_showAllButton_clicked()
{
descendingOrder = "FALSE";
model->setTable(tableName);//重新关联表
model->select(); //显示整个表的内容
ui->newRecordButton->setDisabled(false);
ui->deleteRecordButton->setEnabled(true);
}
void BookEditDialog::on_ascendingOrderButton_clicked()
{
descendingOrder = "FALSE";
model->setSort(0, Qt::AscendingOrder); //按Wid属性即第0列升序排列
model->select();
}
void BookEditDialog::on_descendingOrderButton_clicked()
{
descendingOrder = "TRUE";
model->setSort(0, Qt::DescendingOrder); //按Wid属性即第0列降序排列
model->select();
}
void BookEditDialog::on_idSelectradioButton_clicked()
{
asWortSelect = "FALSE";//按wort查询关
asIdSelect = "TRUE";//按id查询开
ui->goTolineEdit->setDisabled(false);
deleteMethode == "FALSE";
ui->deleteRecordButton->setDisabled(true);
ui->newRecordButton->setDisabled(true);
}
void BookEditDialog::on_wortSelectradioButton_clicked()
{
asIdSelect = "FALSE";//按id查询关
asWortSelect = "TRUE";//按wort查询开
ui->goTolineEdit->setDisabled(false);
deleteMethode == "FALSE";
ui->deleteRecordButton->setDisabled(true);
ui->newRecordButton->setDisabled(true);
}
void BookEditDialog::updateWordCount()
{
//////////////////////////
//获取词库量
int rowCount = bookEdit->getRowCount();
bcMgr->updateTotalNumb(bookpath,rowCount);
}
void BookEditDialog::on_tableView_doubleClicked(const QModelIndex &index)
{
EditorDialog *dialog;
dialog = new EditorDialog(this);
QString text;
text = index.data().toString();
dialog->setText(text);
dialog->exec();
text = dialog->getText();
if(dialog->applyChange == "TRUE")
{
model->setData(index,text);
model->submitAll();
}
else
{
model->submitAll();
}
dialog->close();
delete dialog;
dialog = NULL;
}
void BookEditDialog::on_goTolineEdit_textChanged(const QString &keyWort)
{
ui->newRecordButton->setDisabled(true);
//根据姓名进行筛选
if(asWortSelect == "TRUE" && asIdSelect == "FALSE")
{
model->setFilter(QString("Wort like '%%1%'").arg(keyWort));
//显示结果
model->select();
}
else if(asWortSelect == "FALSE" && asIdSelect == "TRUE")
{
model->setFilter(QString("Wid = '%1'").arg(keyWort.toInt()));
//显示结果
model->select();
}
}