-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
400 lines (362 loc) · 11.8 KB
/
mainwindow.cpp
File metadata and controls
400 lines (362 loc) · 11.8 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::populate_table(TableSelection const t) {
this->model = new CSqlRelationalTableModel(ui->main_result_count_label, this);
// this->model->setEditStrategy(QSqlTableModel::OnFieldChange);
this->ui->main_tableView->setModel(this->model);
switch (t) {
case Keys:
this->load_keys_table();
break;
case People:
this->load_people_table();
break;
case Leases:
this->load_leases_table();
break;
default:
this->load_keys_table();
break;
};
}
void MainWindow::load_keys_table() {
this->model->setTable("key_data");
this->model->setRelation(2, QSqlRelation("rooms", "id", "name"));
this->model->setRelation(3, QSqlRelation("users", "id", "name"));
this->model->setHeaderData(0, Qt::Horizontal, tr("Id"));
this->model->setHeaderData(1, Qt::Horizontal, tr("Key Type"));
this->model->setHeaderData(2, Qt::Horizontal, tr("Room"));
this->model->setHeaderData(3, Qt::Horizontal, tr("Current Holder"));
this->model->setHeaderData(4, Qt::Horizontal, tr("Storage Location"));
this->setup_search_combobox(TableSelection::Keys);
this->current_table_selection = TableSelection::Keys;
}
void MainWindow::load_people_table() {
this->model->setTable("users");
this->model->setHeaderData(0, Qt::Horizontal, tr("Id"));
this->model->setHeaderData(1, Qt::Horizontal, tr("Name"));
this->setup_search_combobox(TableSelection::People);
this->current_table_selection = TableSelection::People;
}
void MainWindow::load_leases_table() {
this->model->setTable("leases");
// this->model->setHeaderData(0, Qt::Horizontal, tr("Id"));
this->model->setHeaderData(1, Qt::Horizontal, tr("Key ID"));
this->model->setHeaderData(2, Qt::Horizontal, tr("Start"));
this->model->setHeaderData(3, Qt::Horizontal, tr("End"));
this->model->setHeaderData(4, Qt::Horizontal, tr("Username"));
this->model->setRelation(4, QSqlRelation("users", "id", "name"));
this->setup_search_combobox(TableSelection::Leases);
this->current_table_selection = TableSelection::Leases;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
this->current_table_selection = TableSelection::Keys;
ui->setupUi(this);
this->last_sorted_header = 0;
this->header_sort_state = HeaderSortState::None;
this->db = new DBHandle();
this->populate_table(this->current_table_selection);
ui->main_tableView->setItemDelegate(
new QSqlRelationalDelegate(ui->main_tableView));
ui->main_tableView->resizeColumnsToContents();
ui->main_tableView->sortByColumn(0, Qt::SortOrder::AscendingOrder);
QHeaderView *h = ui->main_tableView->horizontalHeader();
connect(h, SIGNAL(sectionClicked(int)), this,
SLOT(onHeaderClicked(int const)));
// QSqlQuery q;
ui->main_keysButton->setCheckable(true);
ui->main_peopleButton->setCheckable(true);
ui->main_leaseButton->setCheckable(true);
ui->main_keysButton->setChecked(true);
ui->main_peopleButton->setChecked(false);
ui->main_leaseButton->setChecked(false);
this->tab_buttons.push_back(this->ui->main_keysButton);
this->tab_buttons.push_back(this->ui->main_peopleButton);
this->tab_buttons.push_back(this->ui->main_leaseButton);
std::cout << "Dank memes" << std::endl;
}
void MainWindow::release_all_tabs() {
for (QPushButton *q : this->tab_buttons) {
q->setChecked(false);
}
}
void MainWindow::onHeaderClicked(int const logical_index) {
std::cout << "on header clicked" << std::endl;
QHeaderView *h = ui->main_tableView->horizontalHeader();
if (this->last_sorted_header == logical_index &&
this->header_sort_state == HeaderSortState::Ascending) {
h->setSortIndicator(logical_index, Qt::DescendingOrder);
this->header_sort_state = HeaderSortState::Descending;
} else {
h->setSortIndicator(logical_index, Qt::AscendingOrder);
this->header_sort_state = HeaderSortState::Ascending;
}
this->last_sorted_header = logical_index;
ui->main_tableView->sortByColumn(logical_index);
}
MainWindow::~MainWindow() {
delete db;
delete ui;
}
void MainWindow::on_main_pushButton_clicked() {
AddKey *addkey = new AddKey(this->model);
addkey->show();
}
void MainWindow::on_main_addPermissionGroup_clicked() {
CreatePermissionGroup *cpg = new CreatePermissionGroup(this->model);
cpg->show();
}
void MainWindow::gen_search_sql_query_people() {
QString s;
QString t = this->ui->main_searchEntry->text();
switch (this->from_qstring(this->ui->main_comboBox->currentText())) {
case Name:
s = "users.name LIKE '%" + t + "%'";
break;
default:
break;
};
this->model->setFilter(s);
std::cout << this->model->query().lastQuery().toStdString() << std::endl;
}
void MainWindow::gen_search_sql_query_leases() {
QString s;
QString t = this->ui->main_searchEntry->text();
switch (this->from_qstring(this->ui->main_comboBox->currentText())) {
// SELECT leases."id",relTblAl_1.name AS
// users_name_2,relTblAl_2.name,leases."end_ts" FROM leases,users
// relTblAl_1,rooms relTblAl_2 WHERE (leases."key_id"=relTblAl_1.id AND
// leases."start_ts"=relTblAl_2.id)
case Any:
s = "relTblAl_1.name LIKE '%" + t + "%' OR relTblAl_2.name LIKE '%" + t +
"%' OR leases.key_id LIKE '%" + t + "%' OR leases.id LIKE '%" + t +
"%'";
break;
case CurrentHolder:
s = "relTblAl_1.name LIKE '%" + t + "%'";
break;
case Room:
s = "relTblAl_2.name LIKE '%" + t + "%'";
break;
case KeyID:
s = "leases.key_id IS '%" + t + "%'";
break;
// Todo
// Accomodate alternative timedate formats
case StartsBefore:
s = " leases.start_ts <= '" + t + "'";
break;
case StartsAfter:
s = "leases.start_ts >= '" + t + "'";
break;
case EndsBefore:
s = "leases.end_ts <= '" + t + "'";
break;
case EndsAfter:
s = "leases.end_ts >= '" + t + "'";
break;
//
default:
std::cout << "185" << std::endl;
abort();
};
this->model->setFilter(s);
std::cout << this->model->query().lastQuery().toStdString() << std::endl;
std::cout << this->model->query().lastError().text().toStdString()
<< std::endl;
}
void MainWindow::gen_search_sql_query_keys() {
QString s;
QString t = this->ui->main_searchEntry->text();
switch (from_qstring(this->ui->main_comboBox->currentText())) {
case Any:
s = "key_type LIKE '%" + t + "%' " + "OR relTblAl_2.name LIKE '%" + t +
"%' " + "OR relTblAl_3.name LIKE '%" + t + "%' " +
"OR storage_location LIKE '%" + t + "%' ";
break;
case KeyType:
s = "key_type LIKE '%" + t + "%'";
break;
case Room:
s = "relTblAl_2.name LIKE '%" + t + "%'";
break;
case CurrentHolder:
s = "relTblAl_3.name LIKE '%" + t + "%'";
break;
case StorageLocation:
s = "storage_location LIKE '%" + t + "%'";
break;
default:
std::cout << "139 err" << std::endl;
break;
}
this->model->setFilter(s);
std::cout << this->model->query().lastQuery().toStdString() << std::endl;
}
void MainWindow::search() {
switch (this->current_table_selection) {
case Keys:
this->gen_search_sql_query_keys();
break;
case People:
this->gen_search_sql_query_people();
break;
case Leases:
this->gen_search_sql_query_leases();
break;
default:
std::cout << "Error1" << std::endl;
break;
}
}
void MainWindow::on_main_filterButton_clicked() { this->search(); }
void MainWindow::setup_search_combobox(TableSelection const t) {
// hack
// main_comboBox->count() not right?
for (int i = 0; i < 50; i++) {
ui->main_comboBox->removeItem(0);
}
switch (t) {
case TableSelection::Keys:
this->setup_keys_search_combobox();
break;
case TableSelection::People:
this->setup_people_search_combobox();
break;
case TableSelection::Leases:
this->setup_leases_search_combobox();
break;
default:
std::cout << "Error2" << std::endl;
abort();
break;
}
// ui->main_comboBox->currentText() = "";
}
void MainWindow::setup_keys_search_combobox() {
ui->main_comboBox->addItem("Any");
ui->main_comboBox->addItem("Key Type");
ui->main_comboBox->addItem("Room");
ui->main_comboBox->addItem("Current Holder");
ui->main_comboBox->addItem("Storage Location");
}
void MainWindow::setup_people_search_combobox() {
ui->main_comboBox->addItem("Name");
}
void MainWindow::setup_leases_search_combobox() {
ui->main_comboBox->addItem("Any");
// ui->main_comboBox->addItem("Lease Id");
ui->main_comboBox->addItem("Key ID");
ui->main_comboBox->addItem("Leased to");
ui->main_comboBox->addItem("Room");
ui->main_comboBox->addItem("Starts before");
ui->main_comboBox->addItem("Starts after");
ui->main_comboBox->addItem("Ends before");
ui->main_comboBox->addItem("Ends after");
}
inline FilterSelection MainWindow::from_qstring(QString const qs) const {
if (qs == "Any")
return Any;
if (qs == "Key Type")
return KeyType;
if (qs == "Room")
return Room;
if (qs == "Current Holder" || qs == "Leased to")
return CurrentHolder;
if (qs == "Storage Location")
return StorageLocation;
if (qs == "Name")
return Name;
if (qs == "Starts before")
return StartsBefore;
if (qs == "Starts after")
return StartsAfter;
if (qs == "Ends after")
return EndsBefore;
if (qs == "Ends before")
return EndsAfter;
if (qs == "KeyID" || qs == "Key ID")
return KeyID;
// Crash fix?
std::cout << "Unable to determine switch for " << qs.toStdString()
<< std::endl;
abort();
}
void MainWindow::on_main_pushButtonClearSearch_clicked() {
this->model->setFilter(QString());
this->ui->main_comboBox->clear();
this->setup_search_combobox(this->current_table_selection);
this->ui->main_searchEntry->clear();
this->model->select();
}
void MainWindow::on_main_keysButton_clicked() {
if (this->current_table_selection != TableSelection::Keys) {
this->release_all_tabs();
ui->main_keysButton->setChecked(true);
this->populate_table(TableSelection::Keys);
this->model->select();
}
}
void MainWindow::on_main_peopleButton_clicked() {
if (this->current_table_selection != TableSelection::People) {
this->release_all_tabs();
ui->main_peopleButton->setChecked(true);
this->populate_table(TableSelection::People);
this->model->select();
}
}
void MainWindow::on_main_searchEntry_returnPressed() { this->search(); }
void MainWindow::on_main_leaseButton_clicked() {
if (this->current_table_selection != TableSelection::Leases) {
this->release_all_tabs();
ui->main_leaseButton->setChecked(true);
this->populate_table(TableSelection::Leases);
this->model->select();
}
}
void MainWindow::on_main_pushButton_add_person_clicked() {
AddPerson *addperson = new AddPerson(this->model);
addperson->show();
}
void MainWindow::on_main_pushButton_add_lease_clicked()
{
AddLease *leases = new AddLease(this->model);
leases->show();
}
void MainWindow::on_main_overdueButton_clicked() {
this->on_main_leaseButton_clicked();
QDateTime dt = dt.currentDateTime();
ui->main_searchEntry->setText(dt.toString());
ui->main_comboBox->setCurrentIndex(__OVERDUE_OFFSET);
this->on_main_filterButton_clicked();
}
void MainWindow::on_main_deleteCurrent_clicked() {
QItemSelectionModel *m = ui->main_tableView->selectionModel();
if (!m->hasSelection()) {
return;
}
QModelIndexList row = m->selectedRows();
for (QModelIndex i : row) {
int k = i.data().toInt();
QSqlQuery q;
switch (current_table_selection) {
case TableSelection::Keys:
q.prepare("DELETE FROM key_data WHERE id = :id");
q.bindValue(":id", k);
break;
case TableSelection::People:
q.prepare("DELETE FROM users WHERE id = :id");
q.bindValue(":id", k);
break;
case TableSelection::Leases:
q.prepare("DELETE FROM leases WHERE id = :id");
q.bindValue(":id", k);
break;
}
q.exec();
}
QSqlRelationalTableModel *_m =
static_cast<QSqlRelationalTableModel *>(ui->main_tableView->model());
_m->select();
}