-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignOutDialog.cpp
More file actions
148 lines (125 loc) · 4 KB
/
Copy pathSignOutDialog.cpp
File metadata and controls
148 lines (125 loc) · 4 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
#include "SignOutDialog.h"
#include "Common.h"
SignOutDialog::SignOutDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
ui.dateTimeEdit->setDateTime(QDateTime::currentDateTime());
this->setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
}
void SignOutDialog::showEvent(QShowEvent* event) {
ui.whoAreYouCombo->clear();
QFile file(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/" + "Log.csv");
if(!openFile(&file, this, QFile::ReadOnly | QFile::Text | QFile::ExistingOnly)) return;
bool done = false;
QTextStream in(&file);
QString line;
while (!done) {
line = in.readLine();
if (!isSignedOut(line)) {
QString first, last;
getNames(&last, &first, line);
ui.whoAreYouCombo->addItem(last + ", " + first);
}
if (in.atEnd()) done = true;
}
file.close();
}
SignOutDialog::~SignOutDialog()
{
}
void SignOutDialog::on_whoAreYouCombo_currentTextChanged(const QString& text)
{
if (ui.whoAreYouCombo->currentText() != "") {
ui.signOutBtn2->setEnabled(true);
selPerson = ui.whoAreYouCombo->currentText();
separateName(&selLast, &selFirst, text);
}
else {
ui.signOutBtn2->setEnabled(false);
}
}
void SignOutDialog::on_checkBox_stateChanged(int state)
{
if (state) {
ui.dateTimeEdit->setEnabled(true);
ui.dateTimeLbl->setEnabled(true);
}
else {
ui.dateTimeEdit->setEnabled(false);
ui.dateTimeLbl->setEnabled(false);
}
}
void SignOutDialog::on_signOutBtn2_clicked() {
if (ui.checkBox->checkState()) {
QMessageBox message;
message.setText("You are attempting to sign out with a time that is not current. Do you wish to proceed?");
message.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
if (message.exec() == QMessageBox::Cancel) return;
}
QString workedToday, workedTotal, lastReset;
if (!signPersonOut(selFirst, selLast, &workedToday)) {
QMessageBox::warning(this, "Failed to sign out", "Your exit time can not be before your entrance time");
return;
}
QDateTime date = QDateTime::fromString(lastReset, "MM-dd-yyyy hh:mm");
QMessageBox::information(this, "Thank You", "You have worked " + workedToday + " hours today.");
this->close();
}
bool SignOutDialog::signPersonOut(QString first, QString last, QString* workedToday)
{
QDateTime dateIn;
QDateTime dateOut;
/*
Sign the person out of Log.csv
*/
// Open the files and make sure they can be read
QFile file(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/" + "Log.csv");
if (!openFile(&file, this, QFile::ReadWrite | QFile::Text | QIODevice::ExistingOnly)) return false;
/*
Sign the person out of Log.csv by adding a sign out date
*/
QTextStream stream(&file);
QString line, task;
bool done = false;
int pos;
int lines = 2;
int lineNum = 0;
QString fileStr = stream.readAll();
stream.seek(0);
while (!done) {
line = stream.readLine();
if (!isSignedOut(line)) {
QString first, last, timeIn;
getAll(&last, &first, &task, &timeIn, line);
if ((selFirst == first) && (selLast == last)) {
done = true;
pos = stream.pos();
lineNum = lines;
dateIn = QDateTime::fromString(timeIn, "MM-dd-yyyy hh:mm");
}
}
lines++;
if ((stream.atEnd()) && (done == false) ) {
done = true;
QMessageBox::critical(this, "", "Your name could not be found in the log so you could not be signed out. Please write down your sign in and out times and tell whoever keeps the volunteer hours");
return true;
}
}
/*
Calculate the time worked and put it at the end of the line
*/
stream.seek(0);
if (ui.checkBox->checkState() == Qt::Checked) dateOut = ui.dateTimeEdit->dateTime();
else dateOut = QDateTime::currentDateTime();
long double diffMsec = dateIn.msecsTo(dateOut);
long double diffHour = diffMsec / 3600000; // The time worked in hours
*workedToday = QString("%1").arg(diffHour, 6, 'f', 1);
if (diffHour < 0) {
return false;
}
fileStr.insert(pos - lineNum, dateOut.toString("MM-dd-yyyy hh:mm") + ',' + QString("%1").arg(diffHour, 6, 'f', 1, '0'));
stream << fileStr;
file.close();
return true;
}