-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowview.cpp
More file actions
119 lines (97 loc) · 2.72 KB
/
showview.cpp
File metadata and controls
119 lines (97 loc) · 2.72 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
#include <QtGui>
#include <QtSql>
#include "showview.h"
struct ShowViewObjects{
QMap<QPair<QString,QString>,ShowViewWidget*> widgets;
};
ShowViewObjects* ShowViewWidget::objs=0;
ShowViewWidget* ShowViewWidget::createObject(const QString &view_name,
const QString &constr,
bool &isExist)
{
QPair<QString,QString> pair;
pair.first = view_name;
pair.second = constr;
if(objs->widgets.contains(pair))
{
isExist = true;
return objs->widgets[pair];
}else{
isExist = false;
ShowViewWidget *widget = new ShowViewWidget;
widget->init(view_name,constr);
objs->widgets.insert(pair,widget);
return widget;
}
}
void ShowViewWidget::deleteObject(const QString &table_name, const QString &constr)
{
QPair<QString,QString> pair;
pair.first = table_name;
pair.second = constr;
objs->widgets.remove(pair);
}
void ShowViewWidget::initObjContainer()
{
objs = new ShowViewObjects;
}
void ShowViewWidget::delObjContainer()
{
objs->widgets.clear();
delete objs;
}
QList<QWidget*> ShowViewWidget::dbViews(const QString &constr)
{
QList<QPair<QString,QString> > keys = objs->widgets.keys();
QPair<QString,QString> pair;
QStringList tables;
foreach(pair,keys){
if(pair.second==constr){
tables.append(pair.first);
}
}
QList<QWidget*> ret;
foreach(QString table,tables){
pair.first = table;
pair.second = constr;
ShowViewWidget *widget = objs->widgets.find(pair).value();
ret.append(widget);
}
return ret;
}
QList<ShowViewWidget*> ShowViewWidget::allTables()
{
return objs->widgets.values();
}
ShowViewWidget::ShowViewWidget()
{
}
void ShowViewWidget::init(const QString &view_name,
const QString &constr)
{
view = view_name;
con = constr;
dbname = QSqlDatabase::database(con).databaseName();
init_model();
init_table();
setWindowTitle(QString("ÊÓͼ%1.%2").arg(dbname).arg(view));
}
void ShowViewWidget::init_table(){
setModel(model);
resizeRowsToContents();
}
void ShowViewWidget::init_model(){
QSqlDatabase db = QSqlDatabase::database(con);
model = new QSqlQueryModel;
model->setQuery(QString("SELECT * FROM %1").arg(view),db);
QSqlRecord record = db.record(view);
for(int i=0;i<record.count();++i)
{
model->setHeaderData(i,Qt::Horizontal,record.fieldName(i));
}
}
void ShowViewWidget::closeEvent(QCloseEvent *event)
{
event->accept();
deleteObject(view,con);
}