-
Notifications
You must be signed in to change notification settings - Fork 1
/
addstaff.cpp
266 lines (199 loc) · 7.73 KB
/
addstaff.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
#include "addstaff.h"
#include "ui_addstaff.h"
#include<QRegExpValidator>
#include "cao_panel.h"
#include <QString>
#include <QFile>
#include <QtDebug>
#include <QSqlRecord>
#include <QFileDialog>
#include <QMessageBox>
QString staff_filename;
addStaff::addStaff(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::addStaff)
{
ui->setupUi(this);
/*Validator::Ayush Malla
QRegExp rxSfrstname(SFRSTNAME_RX);
QRegExp rxSmidname(SMIDNAME_RX);
QRegExp rxSlastname(SLASTNAME_RX);
QRegExp rxSpaddress(SPADDRESS_RX);
QRegExp rxStaddress(STADDRESS_RX);
QRegExp rxSphone(SPHONE_RX);
QRegExp rxSemail(SEMAIL_RX);
QRegExp rxSsalary(SSALARY_RX);
QRegExp rxSpostion(SPOSTION_RX);
QRegExp rxQualification1(QUALIFICATION1_RX);
QRegExp rxQualification2(QUALIFICATION2_RX);
QRegExp rxQualification3(QUALIFICATION3_RX);
QRegExp rxQualification4(QUALIFICATION4_RX);
QRegExp rxQualification5(QUALIFICATION5_RX);
QRegExpValidator *Vsfn = new QRegExpValidator(rxSfrstname,this);
ui->Sfrstname->setValidator(Vsfn);
QRegExpValidator *Vsmn=new QRegExpValidator(rxSmidname,this);
ui->Smidname->setValidator(Vsmn);
QRegExpValidator *Vsln=new QRegExpValidator(rxSlastname,this);
ui->Slastname->setValidator(Vsln);
QRegExpValidator *Vspa=new QRegExpValidator(rxSpaddress);
ui->Spaddress->setValidator(Vspa);
QRegExpValidator *Vsta =new QRegExpValidator(rxStaddress);
ui->Staddress->setValidator(Vsta);
QRegExpValidator *Vsp=new QRegExpValidator(rxSphone);
ui->Sphone->setValidator(Vsp);
QRegExpValidator *Vse=new QRegExpValidator(rxSemail);
ui->Semail->setValidator(Vse);
QRegExpValidator *Vss=new QRegExpValidator(rxSsalary);
ui->Ssalary->setValidator(Vss);
QRegExpValidator *Vspo=new QRegExpValidator(rxSpostion);
ui->Sposition->setValidator(Vspo);
QRegExpValidator *Vq1=new QRegExpValidator(rxQualification1);
ui->Qualification1->setValidator(Vq1);
QRegExpValidator *Vq2=new QRegExpValidator(rxQualification2);
ui->Qualification2->setValidator(Vq2);
QRegExpValidator *Vq3=new QRegExpValidator(rxQualification3);
ui->Qualification3->setValidator(Vq3);
QRegExpValidator *Vq4=new QRegExpValidator(rxQualification4);
ui->Qualification4->setValidator(Vq4);
QRegExpValidator *Vq5=new QRegExpValidator(rxQualification5);
ui->Qualification5->setValidator(Vq5);
*/
staff_filename =":/img/na.jpg";
//Database Connection
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("data.sqlite3");
db.open();
// set a "cursor"
query = QSqlQuery(db);
query.exec("SELECT * FROM Staff");
if (query.isActive()){
qDebug()<<"Connection Successful";
query.first();
}else{
qDebug()<< "Database connection:Error";
}
}
QString addStaff::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;
}
addStaff::~addStaff()
{
delete ui;
db.close();
}
void addStaff::on_sFormSubmit_clicked()
{
//Inserting,Updating and Deleting Records of the Staffs
if (ui->Sfrstname->text().trimmed() == ""){
ui->statusbar->showMessage("Staff Name is Compulsory!");
}else{
QSqlQuery updateQuery = QSqlQuery(db);
updateQuery.prepare("INSERT INTO Staff (Firstname, Middlename, Lastname,"
"PermanentAdd,TemporaryAdd,"
"Email,Position,Salary,Description,"
"StaffPhone,"
"Qualification1,Qualification2,Qualification3,Qualification4,Qualification5,image,sex) "
"VALUES (:Firstname, :Middlename, :Lastname,"
":PermanentAdd,:TemporaryAdd,"
":Email,:Position,:Salary,:Description,"
":StaffPhone,"
":Qualification1,:Qualification2,:Qualification3,:Qualification4,:Qualification5,:image,:sex) ");
//Updating Staff Information after entering it manually
//Inserting Staff Information
updateQuery.bindValue(":Id", query.value(0));
updateQuery.bindValue(":Firstname", encrypt(ui->Sfrstname->text()));
updateQuery.bindValue(":Middlename", encrypt(ui->Smidname->text()));
updateQuery.bindValue(":Lastname", encrypt(ui->Slastname->text()));
updateQuery.bindValue(":PermanentAdd", encrypt(ui->Spaddress->text()));
updateQuery.bindValue(":TemporaryAdd",encrypt(ui->Staddress->text()));
updateQuery.bindValue(":Email", encrypt(ui->Semail->text()));
updateQuery.bindValue(":Position", encrypt(ui->Sposition->text()));
updateQuery.bindValue(":Description", encrypt(ui->StaffDescription->toPlainText()));
updateQuery.bindValue(":Salary", encrypt(ui->Ssalary->text()));
updateQuery.bindValue(":StaffPhone", encrypt(ui->Sphone->text()));
updateQuery.bindValue(":image", staff_filename);
updateQuery.bindValue(":Qualification1", encrypt(ui->Qualification1->text()));
updateQuery.bindValue(":Qualification2", encrypt(ui->Qualification2->text()));
updateQuery.bindValue(":Qualification3", encrypt(ui->Qualification3->text()));
updateQuery.bindValue(":Qualification4", encrypt(ui->Qualification4->text()));
updateQuery.bindValue(":Qualification5", encrypt(ui->Qualification5->text()));
if (ui->sFemale->isChecked())
{
updateQuery.bindValue(":sex",2);
}
else if(ui->sMale->isChecked())
{
updateQuery.bindValue(":sex",1);
}
else
{
updateQuery.bindValue(":sex",3);
}
//Error Message
QMessageBox msgBox;
if(!updateQuery.exec())
{
msgBox.setText("Failed to Add.Please Try Again!!!");
msgBox.exec();
}
else
{
msgBox.setText("Successfully Added");
msgBox.exec();
}
query.exec("SELECT * FROM Staff");
query.next();
query.last();
}
}
void addStaff::on_chooseImage_clicked()
{
// qDebug() << filename;
staff_filename = QFileDialog::getOpenFileName(this,tr("Choose Image"),"",tr("Images(*.png *.jpg *.jpeg)"));
if (QString::compare(staff_filename, QString()) != 0)
{
QImage image;
bool valid = image.load(staff_filename);
if(valid)
{
image = image.scaledToWidth(ui->pic->width(), Qt::SmoothTransformation);
ui->pic->setPixmap(QPixmap::fromImage(image));
}
}
}
void addStaff::on_addmore_clicked()
{
ui->Qualification1->clear();
ui->Qualification2->clear();
ui->Qualification3->clear();
ui->Qualification4->clear();
ui->Qualification5->clear();
ui->Semail->clear();
ui->Sfrstname->clear();
ui->Slastname->clear();
ui->Smidname->clear();
ui->Spaddress->clear();
ui->Sphone->clear();
ui->Sposition->clear();
ui->Ssalary->clear();
ui->Staddress->clear();
ui->StaffDescription->clear();
staff_filename = ":/img/na.jpg";
ui->pic->setPixmap(QPixmap(":/img/na.jpg") );
}
void addStaff::on_back_clicked()
{
hide();
QWidget *backToCao = new Cao_panel;
backToCao-> showMaximized();
}