-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbinfodialog.cpp
More file actions
198 lines (152 loc) · 5.29 KB
/
dbinfodialog.cpp
File metadata and controls
198 lines (152 loc) · 5.29 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
#include "dbinfodialog.h"
#include "dbutil.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QTableView>
#include <QSpinBox>
#include <QTimer>
#include <QHeaderView>
#include <QDialogButtonBox>
#include <QDebug>
DBInfoDialog::DBInfoDialog(QWidget *parent) :
QDialog(parent)
{
updateIntervalTimer = new QTimer(this);
updateIntervalTimer->setInterval(1000);
userListModel = new ConnectedUserListModel(this);
createWidgets();
layoutWidgets();
retranslateUi();
connectSlots();
updateIntervalTimer->start();
}
void DBInfoDialog::createWidgets()
{
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal,this);
lblUserInfo = new QLabel(this);
lblVersionInfo = new QLabel(this);
tvUserList = new QTableView(this);
tvUserList->setModel(userListModel);
tvUserList->horizontalHeader()->setStretchLastSection(true);
tvUserList->setSelectionMode(QAbstractItemView::NoSelection);
tvUserList->setSelectionBehavior(QAbstractItemView::SelectRows);
lblUserListTitle = new QLabel(this);
spinUpdateInterval= new QSpinBox(this);
spinUpdateInterval->setRange(0,300);
spinUpdateInterval->setValue(1);
spinUpdateInterval->setSingleStep(1);
spinUpdateInterval->setSpecialValueText(tr("nunca"));
spinUpdateInterval->setKeyboardTracking(true);
lblUpdateInterval = new QLabel(this);
lblUpdateInterval->setBuddy(spinUpdateInterval);
}
void DBInfoDialog::layoutWidgets()
{
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(lblUserInfo);
mainLayout->addWidget(lblVersionInfo);
QHBoxLayout *hLayout1 = new QHBoxLayout;
hLayout1->addWidget(lblUserListTitle);
hLayout1->addStretch();
hLayout1->addWidget(lblUpdateInterval);
hLayout1->addWidget(spinUpdateInterval);
mainLayout->addLayout(hLayout1);
mainLayout->addWidget(tvUserList);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
}
void DBInfoDialog::retranslateUi()
{
setWindowTitle(tr("Informacion sobre la conexion"));
lblUserInfo->setText(tr("Conexion:\n%1\n").arg(psqlSessionInfo()));
lblVersionInfo->setText(tr("Servidor PostgreSQL:\n%1\n").arg(psqlVersion()));
lblUserListTitle->setText(tr("Usuarios conectados:"));
lblUpdateInterval->setText(tr("Actualizar:"));
spinUpdateInterval->setSuffix(tr(" seg."));
}
void DBInfoDialog::connectSlots()
{
connect(spinUpdateInterval,SIGNAL(valueChanged(int)),this,SLOT(setUpdateInterval(int)));
connect(updateIntervalTimer,SIGNAL(timeout()),userListModel, SLOT(update()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(userListModel,SIGNAL(updated()),tvUserList,SLOT(resizeColumnsToContents()));
connect(userListModel,SIGNAL(updated()),tvUserList,SLOT(resizeRowsToContents()));
}
void DBInfoDialog::setUpdateInterval(int t)
{
if(t==0){
updateIntervalTimer->stop();
return;
}
updateIntervalTimer->setInterval(t*1000);
updateIntervalTimer->start();
}
void ConnectedUserListModel::update()
{
QString q("SELECT pid,"
" usename,"
" datname,"
" date_trunc('second', now()-backend_start) as conectado,"
" application_name,"
" client_addr,"
" CASE"
" WHEN state ILIKE 'idle' THEN 'Ocioso'"
" WHEN state ILIKE 'active' THEN 'Activo'"
" ELSE state END"
" FROM pg_stat_activity");
queryModel->setQuery(q, QSqlDatabase::database("default"));
emit updated();
}
QVariant ConnectedUserListModel::data(const QModelIndex &idx, int role) const
{
if (!idx.isValid()){
return QVariant();
}
int col = idx.column();
if((Qt::TextAlignmentRole==role)){
switch(col){
case 6: // estado
return QVariant(Qt::AlignLeft|Qt::AlignVCenter);
default:
return QVariant(Qt::AlignCenter|Qt::AlignVCenter);
}
}
if((Qt::DisplayRole==role)){
return queryModel->data(idx,role);
}
return QVariant();
}
QVariant ConnectedUserListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if((Qt::DisplayRole==role) && (Qt::Horizontal==orientation)){
if(section<m_horizontalHeaders.length())
return QVariant(m_horizontalHeaders.at(section));
}
if((Qt::TextAlignmentRole==role)){
return QVariant(Qt::AlignVCenter|Qt::AlignHCenter);
}
if((Qt::DecorationRole==role)&&(Qt::Horizontal==orientation)){
switch(section){
case 0:
return QVariant(QIcon(":/resources/pixmaps/params.png"));
case 1:
return QVariant(QIcon(":/resources/pixmaps/user.png"));
default:
;
}
}
return QAbstractTableModel::headerData(section,orientation,role);
}
int ConnectedUserListModel::columnCount(const QModelIndex &parent) const
{
return queryModel->columnCount(parent);
}
int ConnectedUserListModel::rowCount(const QModelIndex &parent) const
{
return queryModel->rowCount(parent);
}
ConnectedUserListModel::ConnectedUserListModel(QObject *parent) : QAbstractTableModel(parent)
{
m_horizontalHeaders << tr("PID") << tr("Usuario") << tr("Base de datos") << tr("Duracion") << tr("Aplicacion") << tr("Desde") << tr("Estado") ;
queryModel = new QSqlQueryModel(this);
}