-
Notifications
You must be signed in to change notification settings - Fork 5
/
guesttable.cpp
191 lines (162 loc) · 6.47 KB
/
guesttable.cpp
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
#include "mainwindow.h"
QWidget * MainWindow::guesttable(){
QWidget *table = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
QWidget *header = new QWidget(this);
header->setFixedHeight(50);
QHBoxLayout *headerlayout = new QHBoxLayout();
searchField = new QLineEdit();
searchField->setPlaceholderText("Search");
searchField->setFixedWidth(300);
searchField->setClearButtonEnabled(true);
QPushButton *searchButton = new QPushButton();
searchButton->setText("Search");
editButton = new QPushButton();
editButton->setText("Edit");
noEditButton = new QPushButton();
noEditButton->setText("Stop Editing");
noEditButton->adjustSize();
noEditButton->hide();
QLabel *sortlabel = new QLabel("Sort By");
QComboBox *sortby = new QComboBox();
sortby->addItem("Name");
sortby->addItem("Check-in");
sortby->addItem("Check-out");
QComboBox *order = new QComboBox();
order->addItem("Ascending");
order->addItem("Descending");
QLabel *statusLabel = new QLabel("Status");
QComboBox *status = new QComboBox();
status->addItem("Active");
status->addItem("Inactive");
headerlayout->addWidget(searchField);
headerlayout->addWidget(searchButton);
headerlayout->addWidget(editButton);
headerlayout->addWidget(noEditButton);
headerlayout->addStretch(1);
headerlayout->addWidget(sortlabel);
headerlayout->addWidget(sortby);
headerlayout->addWidget(order);
headerlayout->addWidget(statusLabel);
headerlayout->addWidget(status);
header->setLayout(headerlayout);
model = new QSqlTableModel();
model->setTable("guests");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setParent(this);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Horizontal, tr("Room No"));
model->setHeaderData(2, Qt::Horizontal, tr("Name"));
model->setHeaderData(3, Qt::Horizontal, tr("Email"));
model->setHeaderData(4, Qt::Horizontal, tr("Contact"));
model->setHeaderData(5, Qt::Horizontal, tr("Address"));
model->setHeaderData(6, Qt::Horizontal, tr("Checkin"));
model->setHeaderData(7, Qt::Horizontal, tr("Checkout"));
model->setHeaderData(8, Qt::Horizontal, tr("Identity"));
model->setHeaderData(9, Qt::Horizontal, tr("Room Type"));
model->setHeaderData(10, Qt::Horizontal, tr("Total"));
model->setHeaderData(11, Qt::Horizontal, tr("Paid"));
model->setHeaderData(12, Qt::Horizontal, tr("Due"));
model->setHeaderData(13, Qt::Horizontal, tr("Status"));
model->setHeaderData(14, Qt::Horizontal, tr("Packages"));
connect(sortby, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
[=](int index){
switch (index){
case 0:
model->setSort(2,Qt::AscendingOrder);
model->select();
break;
case 1:
model->setSort(6,Qt::AscendingOrder);
model->select();
break;
case 2:
model->setSort(7,Qt::AscendingOrder);
model->select();
break;
case 6:
model->setSort(6,Qt::AscendingOrder);
model->select();
break;
case 7:
model->setSort(7,Qt::AscendingOrder);
model->select();
break;
default:
model->select();
break;
}
});
connect(searchButton,SIGNAL(clicked(bool)),this,SLOT(search()));
connect(searchField,SIGNAL(returnPressed()),this,SLOT(search()));
connect(searchField,SIGNAL(editingFinished()),this,SLOT(search()));
connect(editButton,SIGNAL(clicked()),this,SLOT(editTable()));
connect(noEditButton,SIGNAL(clicked()),this,SLOT(noEditTable()));
connect(order, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
[=](int index){
int sortIndex = sortby->currentIndex();
switch (index){
case 0:
model->setSort(sortIndex,Qt::AscendingOrder);
model->select();
break;
case 1:
model->setSort(sortIndex,Qt::DescendingOrder);
model->select();
break;
default:
model->select();
break;
}
});
connect(status, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
[=](int index){
// model->setFilterCol
switch (index){
case 0:
model->setFilter("status='Active'");
model->select();
break;
case 1:
model->setFilter("status='InActive'");
model->select();
break;
default:
model->select();
break;
}
});
QTableView *view = new QTableView();
view->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
view->setModel(model);
view->hideColumn(0);
layout->addWidget(header);
layout->addWidget(view);
table->setLayout(layout);
// table->show();
return table;
}
void MainWindow::search(){
QString query = searchField->text();
if(query!= NULL){
model->setFilter("name='"+query+"' or contact='"+query+"' or address='"+query+"' or email='"+query+"' or room_no ='"+query+"'");
model->select();
}else{
model->setFilter(NULL);
model->select();
}
}
void MainWindow::editTable(){
model->setEditStrategy(QSqlTableModel::OnFieldChange);
QMessageBox::warning(this->searchField,"Edit Enabled","Edit mode is enabled, operate carefully.");
editButton->hide();
noEditButton->show();
}
void MainWindow::noEditTable(){
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
QMessageBox::information(this->searchField,"Edit Enabled","Edit Mode Is Enabled, operate carefully.");
QMessageBox::information(this->searchField,"Edit Disabled","Edit mode is disabled, All Changes are saved.");
noEditButton->hide();
editButton->show();
}