-
Notifications
You must be signed in to change notification settings - Fork 1
/
viewstfrecord.cpp
316 lines (240 loc) · 8.19 KB
/
viewstfrecord.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
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
#include "viewstfrecord.h"
#include "ui_viewstfrecord.h"
#include "cao_panel.h"
#include <QString>
#include <QFile>
#include <QtDebug>
#include <QSqlRecord>
#include <QFileDialog>
#include <QMessageBox>
int staff_id;
viewstfrecord::viewstfrecord(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::viewstfrecord)
{
ui->setupUi(this);
QString sid = QString::number(staff_id); //converting int to string
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("data.sqlite3");
db.open();
// set a "cursor"
if(staff_id != 0)
{
query = QSqlQuery(db);
query.exec("SELECT * FROM Staff WHERE Id = '"+sid+"'");
if (query.isActive()){
qDebug()<<"Connection Successful";
query.first();
FillForm();
}else{
qDebug()<< "Database connection:Error";
}
newrecord =false;
}
else
{
query = QSqlQuery(db);
query.exec("SELECT * FROM Staff");
if (query.isActive()){
qDebug()<<"Connection Successful";
query.first();
FillForm();
}else{
qDebug()<< "Database connection:Error";
}
newrecord =false;
}
}
QString viewstfrecord::decrypt(QString encrypted)
{
//Decryption
std::reverse(encrypted.begin(),encrypted.end());
QString decrypted = encrypted;
int size_original = encrypted.size();
int i = 0;
while(size_original--)
{
decrypted[i] = encrypted[i].unicode() - size_original - 1;
i++;
}
return decrypted;
}
void viewstfrecord::FillForm()
{
//Staff General Information
ui->Sfrstname->setText(decrypt(query.value(1).toString()));
ui->Smidname->setText(decrypt(query.value(2).toString()));
ui->Slastname->setText(decrypt(query.value(3).toString()));
ui->Sposition->setText(decrypt(query.value(9).toString()));
ui->StaffDescription->setText(decrypt(query.value(8).toString()));
ui->Semail->setText(decrypt(query.value(6).toString()));
ui->Ssalary->setText(decrypt(query.value(10).toString()));
ui->Sphone->setText(decrypt(query.value(11).toString()));
int sex = query.value(17).toInt(); //sex column is in 21
switch(sex) // 1 is for male,2 for female and 3 for others
{
case 1:
//male
//qDebug() << "MAle";
ui->sMale->setChecked(true);
break;
case 2:
//female
//qDebug() << "female";
ui->sFemale->setChecked(true);
break;
case 3:
//Others
qDebug() << "Others";
ui->sothers->setChecked(true);
break;
default:
qDebug() << "Not in DB";
}
//Staff Qualification
ui->Qualification1->setText(decrypt(query.value(7).toString()));
ui->Qualification2->setText(decrypt(query.value(12).toString()));
ui->Qualification3->setText(decrypt(query.value(13).toString()));
ui->Qualification4->setText(decrypt(query.value(14).toString()));
ui->Qualification5->setText(decrypt(query.value(15).toString()));
//Staff's Address
ui->Spaddress->setText(decrypt(query.value(4).toString()));
ui->Staddress->setText(decrypt(query.value(5).toString()));
//Loading image of student
QString filename = query.value(16).toString(); ///Path of the image
// qDebug() << filename;
QImage photo;
bool valid = photo.load(filename);
if(valid)
{
photo = photo.scaledToWidth(ui->pic->width(), Qt::SmoothTransformation);
ui->pic->setPixmap(QPixmap::fromImage(photo));
}
}
viewstfrecord::~viewstfrecord()
{
delete ui;
db.close();
}
void viewstfrecord::on_Next_clicked()
{
if(!query.next())
query.first();
FillForm();
}
void viewstfrecord::on_Previous_clicked()
{
if (!query.previous())
query.last();
FillForm();
}
void viewstfrecord::on_Back_clicked()
{
hide();
QWidget *panel = new Cao_panel;
panel-> show();
}
QString viewstfrecord::encrypt(QString input)
{
QString encrypted = input;
int size_original = input.size();
int i = 0;
while(size_original--)
{
encrypted[i] = input[i].unicode() + size_original + 1;
i++;
}
std::reverse(encrypted.begin(),encrypted.end());
return encrypted;
}
void viewstfrecord::on_Search_clicked()
{
QString keyword = ui->searchBox->text();
qDebug() << keyword;
QString sortValue = ui->sortBy->currentText();
qDebug() << sortValue;
//keyword = QString::toupper(keyword);
keyword =keyword.toUpper();
keyword = encrypt(keyword);
query = QSqlQuery(db);
if (sortValue == "firstname" )
{
int num_rows;
//get the number of rows
if(!query.exec("SELECT count(Id) as num_rows FROM Staff WHERE upper(Firstname) = '"+keyword+"'")) qDebug() << query.lastError().text();
//query.first();
query.first();
num_rows = query.value(0).toInt();
QMessageBox msgBox;
QString text= "Number of records found is:" + QString::number(num_rows);
msgBox.setText(text);
msgBox.exec();
if(!query.exec("SELECT * FROM Staff WHERE upper(Firstname) = '"+keyword+"' ORDER BY Id")) qDebug() << query.lastError().text();
query.first();
FillForm();
}
else if(sortValue == "email")
{
int num_rows;
//get the number of rows
if(!query.exec("SELECT count(Id) as num_rows FROM Staff WHERE upper(Email) = '"+keyword+"'")) qDebug() << query.lastError().text();
//query.first();
query.first();
num_rows = query.value(0).toInt();
QMessageBox msgBox;
QString text= "Number of records found is:" + QString::number(num_rows);
msgBox.setText(text);
msgBox.exec();
if(!query.exec("SELECT * FROM Staff WHERE upper(Email) = '"+keyword+"' ORDER BY Id")) qDebug() << query.lastError().text();
query.first();
FillForm();
}
else if(sortValue == "phone")
{
int num_rows;
//get the number of rows
if(!query.exec("SELECT count(Id) as num_rows FROM Staff WHERE upper(StaffPhone) = '"+keyword+"'")) qDebug() << query.lastError().text();
//query.first();
query.first();
num_rows = query.value(0).toInt();
QMessageBox msgBox;
QString text= "Number of records found is:" + QString::number(num_rows);
msgBox.setText(text);
msgBox.exec();
if(!query.exec("SELECT * FROM Staff WHERE upper(StaffPhone) = '"+keyword+"' ORDER BY Id")) qDebug() << query.lastError().text();
query.first();
FillForm();
}
else if(sortValue == "position")
{
int num_rows;
//get the number of rows
if(!query.exec("SELECT count(Id) as num_rows FROM Staff WHERE upper(Position) = '"+keyword+"'")) qDebug() << query.lastError().text();
//query.first();
query.first();
num_rows = query.value(0).toInt();
QMessageBox msgBox;
QString text= "Number of records found is:" + QString::number(num_rows);
msgBox.setText(text);
msgBox.exec();
if(!query.exec("SELECT * FROM Staff WHERE upper(Position) = '"+keyword+"' ORDER BY Id")) qDebug() << query.lastError().text();
query.first();
FillForm();
}
else if(sortValue == "lastname")
{
int num_rows;
//get the number of rows
if(!query.exec("SELECT count(Id) as num_rows FROM Staff WHERE upper(Lastname) = '"+keyword+"'")) qDebug() << query.lastError().text();
//query.first();
query.first();
num_rows = query.value(0).toInt();
QMessageBox msgBox;
QString text= "Number of records found is:" + QString::number(num_rows);
msgBox.setText(text);
msgBox.exec();
if(!query.exec("SELECT * FROM Staff WHERE upper(Lastname) = '"+keyword+"' ORDER BY Id")) qDebug() << query.lastError().text();
query.first();
FillForm();
}
}