-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminwidget.cpp
More file actions
204 lines (169 loc) · 6.81 KB
/
Copy pathadminwidget.cpp
File metadata and controls
204 lines (169 loc) · 6.81 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
#include "adminwidget.h"
AdminWidget::AdminWidget(QSqlDatabase& database,
QStatusBar *statusBar, QWidget *parent)
:QWidget(parent), mainWindowStatusBar(statusBar), m_db(database)
{
setupLayout();
setupModels();
setupUserTableView();
connect(applyButton, SIGNAL(clicked()), this, SLOT(onApplyButtonClicked()));
connect(changeFileNameToolButton, SIGNAL(clicked()), this, SLOT(onChangeFileNameToolButtonClicked()));
connect(revertButton, SIGNAL(clicked()), this, SLOT(onRevertButtonClicked()));
connect(addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
connect(editButton, SIGNAL(clicked()), this, SLOT(editEntry()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteEntry()));
connect(userTableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editEntry()));
}
void AdminWidget::setupLayout()
{
QLabel *fileNamelabel = new QLabel(tr("&File Name: "));
fileNameLineEdit = new QLineEdit;
fileNamelabel->setBuddy(fileNameLineEdit);
QSettings settings(Globals::OrganizationName, Globals::ApplicationName);
QString fileName = settings.value(Globals::FileDbDestination, "").toString();
fileNameLineEdit->setText(fileName);
changeFileNameToolButton = new QToolButton;
changeFileNameToolButton->setText(tr("..."));
changeFileNameToolButton->setToolTip(tr("Change the file name or path"));
QLabel *usersLabel = new QLabel(tr("Users"));
userTableView = new QTableView;
applyButton = new QPushButton(tr("Apply"));
applyButton->setToolTip(tr("Apply changes for database file name and directroy"));
revertButton = new QPushButton(tr("Revert"));
addButton = new QPushButton(tr("&Add"));
editButton = new QPushButton(tr("&Edit"));
deleteButton = new QPushButton(tr("Delete"));
QHBoxLayout *fileButtonsLayout = new QHBoxLayout;
fileButtonsLayout->addWidget(revertButton);
fileButtonsLayout->addWidget(applyButton);
QGridLayout *topLayout = new QGridLayout;
topLayout->addWidget(fileNamelabel, 0, 0);
topLayout->addWidget(fileNameLineEdit, 0, 1);
topLayout->addWidget(changeFileNameToolButton, 0, 2);
topLayout->addLayout(fileButtonsLayout, 1, 1, Qt::AlignRight);
QHBoxLayout *userButtonLayout = new QHBoxLayout;
userButtonLayout->addStretch();
userButtonLayout->addWidget(addButton);
userButtonLayout->addWidget(editButton);
userButtonLayout->addWidget(deleteButton);
QVBoxLayout *bottomLayout = new QVBoxLayout;
bottomLayout->addWidget(usersLabel);
bottomLayout->addWidget(userTableView);
bottomLayout->addLayout(userButtonLayout);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addLayout(bottomLayout);
setLayout(mainLayout);
}
void AdminWidget::updateTableView()
{
setupModels();
}
void AdminWidget::setupModels()
{
userModel = new QSqlRelationalTableModel(this, m_db);
userModel->setTable("users");
userModel->setRelation(Role, QSqlRelation("roles", "id", "type"));
userModel->setHeaderData(Username, Qt::Horizontal, tr("Username"));
userModel->setHeaderData(Email, Qt::Horizontal, tr("Email"));
userModel->setHeaderData(Role, Qt::Horizontal, tr("Role"));
userModel->select();
}
void AdminWidget::setupUserTableView()
{
userTableView->setModel(userModel);
userTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
userTableView->setColumnHidden(Id, true);
userTableView->setColumnHidden(HashedPassword, true);
userTableView->setColumnHidden(Salt, true);
userTableView->horizontalHeader()->setStretchLastSection(true);
userTableView->resizeColumnsToContents();
}
void AdminWidget::onApplyButtonClicked()
{
QSettings settings(Globals::OrganizationName, Globals::ApplicationName);
QString oldFileName = settings.value(Globals::FileDbDestination, "").toString();
bool fileExists = QFile::exists(fileNameLineEdit->text());
if(fileExists)
{
settings.setValue(Globals::FileDbDestination, fileNameLineEdit->text());
mainWindowStatusBar->showMessage(tr("File name and directory successfully changed."));
}
else
{
QMessageBox::information(this, tr("File Validation"),
tr("File: %1 doesn't exist.").arg(fileNameLineEdit->text()));
}
QString newFileName = settings.value(Globals::FileDbDestination, "").toString();
if(oldFileName != newFileName)
{
QMessageBox::information(this, tr("Restart"),
tr("You need to retart the application for the changes to take places"));
}
}
void AdminWidget::onChangeFileNameToolButtonClicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Database file"),
".", tr("Database File (*.db)"));
if(!fileName.isEmpty())
{
fileNameLineEdit->setText(fileName);
}
}
void AdminWidget::onRevertButtonClicked()
{
QSettings settings(Globals::OrganizationName, Globals::ApplicationName);
QString fileName = settings.value(Globals::FileDbDestination, "").toString();
fileNameLineEdit->setText(fileName);
}
void AdminWidget::deleteEntry()
{
QModelIndexList indexes = userTableView->selectionModel()->selectedIndexes();
QModelIndex index;
if (indexes.isEmpty())
return;
int ret = QMessageBox::information(this, tr("Deleting user(s)"),
tr("Are you sure you want to delete user(s)?"),
QMessageBox::Cancel, QMessageBox::Yes);
if(ret == QMessageBox::Yes)
{
foreach(index, indexes)
{
QString role = userModel->record(index.row()).field("type").value().toString();
if(role != "Administrator")
{
userModel->removeRow(index.row());
mainWindowStatusBar->showMessage(tr("User(s) has been successfully deleted"));
}
else
{
QMessageBox::information(this, tr("Admin deletion"),
tr("Sorry, you can not delete an admin user"));
return;
}
}
}
}
void AdminWidget::addEntry()
{
UserDialog dlg(userModel, this);
dlg.setWindowTitle(tr("Add User"));
if(dlg.exec())
{
mainWindowStatusBar->showMessage(tr("User has been successfully added"));
}
}
void AdminWidget::editEntry()
{
QModelIndex index = userTableView->selectionModel()->currentIndex();
QSqlRecord record = userModel->record(index.row());
if(index.isValid())
{
UserDialog dlg(userModel, record, index.row(), this);
dlg.setWindowTitle(tr("Edit User"));
if(dlg.exec())
{
mainWindowStatusBar->showMessage(tr("User has been successfully updated"));
}
}
}