-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowtablewidget.cpp
More file actions
255 lines (214 loc) · 6.83 KB
/
showtablewidget.cpp
File metadata and controls
255 lines (214 loc) · 6.83 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
#include <QtGui>
#include <QMenu>
#include <QtSql>
#include "showtablewidget.h"
#include <QSqlTableModel>
#include <QSqlRecord>
struct ShowTableObjects{
QMap<QPair<QString,QString>,ShowTableWidget*> widgets;
};
ShowTableObjects* ShowTableWidget::objs=0;
ShowTableWidget* ShowTableWidget::createObject(const QString &table_name,
const QString &constr,
bool &isExist)
{
QPair<QString,QString> pair;
pair.first = table_name;
pair.second = constr;
if(objs->widgets.contains(pair))
{
isExist = true;
return objs->widgets[pair];
}else{
isExist = false;
ShowTableWidget *widget = new ShowTableWidget;
widget->init(table_name,constr);
objs->widgets.insert(pair,widget);
return widget;
}
}
void ShowTableWidget::deleteObject(const QString &table_name, const QString &constr)
{
QPair<QString,QString> pair;
pair.first = table_name;
pair.second = constr;
objs->widgets.remove(pair);
}
void ShowTableWidget::initObjContainer()
{
objs = new ShowTableObjects;
}
void ShowTableWidget::delObjContainer()
{
objs->widgets.clear();
delete objs;
}
QList<QWidget*>* ShowTableWidget::dbTables(const QString &constr)
{
QList<QPair<QString,QString> > keys = objs->widgets.keys();
QPair<QString,QString> pair;
QStringList tables;
foreach(pair,keys){
//keys.at(i);
if(pair.second==constr){
tables.append(pair.first);
}
}
QList<QWidget*> *ret = new QList<QWidget*>();
foreach(QString table,tables){
pair.first = table;
pair.second = constr;
ShowTableWidget *widget = objs->widgets.find(pair).value();
ret->append(widget);
}
return ret;
//QMap<QPair<QString,QString>,ShowTableWidget*>;
}
QList<ShowTableWidget*> ShowTableWidget::allTables()
{
return objs->widgets.values();
}
ShowTableWidget::ShowTableWidget()
{
}
void ShowTableWidget::init(const QString &table_name,const QString &constr)
{
isDirty = false;
table = table_name;
con = constr;
dbname = QSqlDatabase::database(con).databaseName();
init_model();
init_table();
init_menu();
init_toolbar();
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(toolbar);
vbox->addWidget(view,1);
vbox->setMargin(0);
vbox->setSpacing(0);
this->setLayout(vbox);
setWindowTitle(QString("数据表%1.%2").arg(dbname).arg(table));
}
void ShowTableWidget::init_table(){
view = new QTableView(this);
view->setModel(model);
view->resizeRowsToContents();
view->setContextMenuPolicy(Qt::CustomContextMenu);
connect(view,SIGNAL(customContextMenuRequested(QPoint)),
this,SLOT(showContextMenu(QPoint)));
}
void ShowTableWidget::init_model(){
QSqlDatabase db = QSqlDatabase::database(con);
model = new QSqlTableModel(this,db);
model->setTable(table);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
QSqlRecord record = db.record(table);
for(int i=0;i<record.count();++i)
{
model->setHeaderData(i,Qt::Horizontal,record.fieldName(i));
}
}
void ShowTableWidget::init_menu(){
contextMenu = new QMenu(view);
appendAct = contextMenu->addAction(QIcon(":/images/new.png"),
QString("追加行"),this,SLOT(appendRow()));
insertAct = contextMenu->addAction(QIcon(":/images/insert.png"),
QString("插入行"),this,SLOT(insertRow()));
removeAct = contextMenu->addAction(QIcon(":/images/remove.png"),
QString("删除所选行"),this,SLOT(removeSelectedRows()));
contextMenu->addSeparator();
submitAct = contextMenu->addAction(QIcon(":/images/save.png"),
QString("提交更新"),this,SLOT(submit()));
cancelAct = contextMenu->addAction(QIcon(":/images/cancel.png"),
QString("取消更新"),model,SLOT(revertAll()));
}
void ShowTableWidget::init_toolbar()
{
toolbar = new QToolBar(this);
toolbar->addAction(appendAct);
toolbar->addAction(insertAct);
toolbar->addAction(removeAct);
toolbar->addAction(submitAct);
toolbar->addAction(cancelAct);
toolbar->setMaximumHeight(24);
}
void ShowTableWidget::appendRow()
{
model->insertRow(model->rowCount());
}
void ShowTableWidget::submit()
{
bool ret = model->submitAll();
if(!ret){
QSqlError se = model->lastError();
QString err = QString("database:%1\ndriver:%2\nerror number:%3\nerror type:%4\nerror text:%5")
.arg(se.databaseText())
.arg(se.driverText())
.arg(se.number())
.arg(se.type()).arg(se.text());
QMessageBox::warning(this,QString("数据保存错误"),
err);
}
}
void ShowTableWidget::insertRow()
{
QModelIndexList list = view->selectionModel()->selectedIndexes();
if(list.count()>0){
int i= list.at(0).row();
model->insertRow(i);
}else{
model->insertRow(view->currentIndex().row());
}
}
void ShowTableWidget::removeSelectedRows()
{
QModelIndexList list = view->selectionModel()->selectedRows();
model->removeRows(list.at(0).row(),list.count());
// for(int i=0;i<list.count();i++){
// int row = list.at(i).row();
// model->removeRow(row);
// }
//QMessageBox::information(this,QString("debug"),
// QString("删除所选行"));
}
void ShowTableWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
isDirty = true;
}
void ShowTableWidget::rowsInserted(const QModelIndex &parent, int start, int end)
{
isDirty = true;
}
void ShowTableWidget::rowsRemoved(const QModelIndex &parent, int start, int end)
{
isDirty = true;
}
void ShowTableWidget::showContextMenu(QPoint p)
{
contextMenu->exec(QCursor::pos());
}
bool ShowTableWidget::maybeSave()
{
if(isDirty){
QMessageBox::StandardButton ret;
QString db = QSqlDatabase::database(con).databaseName();
ret = QMessageBox::warning(this, QString("警告"),
QString("'%1.%2' 已经更改.是否保存?").arg(db).arg(table),
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return model->submitAll();
else if (ret == QMessageBox::Cancel)
return false;
}
return true;
}
void ShowTableWidget::closeEvent(QCloseEvent *event)
{
if(maybeSave()){
event->accept();
deleteObject(table,con);
}else{
event->ignore();
}
}