-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
277 lines (225 loc) · 6.89 KB
/
Copy pathtimer.cpp
File metadata and controls
277 lines (225 loc) · 6.89 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
#include <QInputDialog>
#include <QMessageBox>
#include "timer.h"
#include "ui_timer.h"
#include "LanguageParser.h"
#include <QDebug>
Timer::Timer(QWidget *parent) :
QDialog(parent),
ui(new Ui::Timer)
{
ui->setupUi(this);
ui->startTimer->setEnabled(false);
ui->stopTimer->setEnabled(false);
ui->timeEdit->setDisplayFormat("hh:mm:ss ap");
}
Timer::~Timer()
{
delete ui;
}
void Timer::setRef(Timer* c)
{
ref = c;
}
void Timer::setAQ(std::vector<Alert*>* alertsQueue)
{
aQ = alertsQueue;
}
void Timer::setMutex(QMutex* m)
{
mtx = m;
}
void Timer::setCurrentListWidgetItem(QListWidgetItem* tItem)
{
currentTItem = tItem;
}
Alert* Timer::getAlert(QListWidgetItem* tItem)
{
auto it = alertWidgetMap.find(tItem);
if (it != alertWidgetMap.end())
{
return it->second;
}
return nullptr;
}
std::string Timer::timeToAlert(QDateTime inputAlertTime)
{
std::string str_time = "";
int days_left;
int hours_left;
int minutes_left;
int seconds_left;
QDateTime now = QDateTime::currentDateTime();
QDateTime alertDateTime = QDateTime::currentDateTime();
QDate alertDate = alertDateTime.date();
alertDate.setDate(inputAlertTime.date().year(), inputAlertTime.date().month(), inputAlertTime.date().day());
alertDateTime.setDate(alertDate);
QTime alertTime = alertDateTime.time();
alertTime.setHMS(inputAlertTime.time().hour(), inputAlertTime.time().minute(), inputAlertTime.time().second());
alertDateTime.setTime(alertTime);
int secondsToInputTime = now.secsTo(alertDateTime); //Gets the amount of seconds to the alert
if(secondsToInputTime < 0) {
str_time = "Event already done";
return str_time;
}
minutes_left = (secondsToInputTime / 60); //Gets the amount of full minutes able to be made
hours_left = (minutes_left / 60); //Gets the amount of full hours able to be made
days_left = (hours_left / 24); //Gets the amount of full days able to be made
hours_left = hours_left % 24; //Gets the left over hours not able to be made into days
minutes_left = minutes_left % 60; //Gets the left over minutes not able to be made into hours
seconds_left = secondsToInputTime % 60; //Gets leftover seconds
if(days_left > 0) {
str_time += std::to_string(days_left);
str_time += " days, ";
}
str_time += std::to_string(hours_left);
str_time += " hours, ";
str_time += std::to_string(minutes_left);
str_time += " minutes, ";
str_time += std::to_string(seconds_left);
str_time += " seconds left.";
return str_time;
}
void Timer::on_timeEdit_userTimeChanged(const QTime &time)
{
ui->stopTimer->setEnabled(false);
ui->startTimer->setEnabled(true);
QDateTime now = QDateTime::currentDateTime();
QDateTime endDateTime = QDateTime::currentDateTime();
endDateTime.setTime(time);
int secondsToInputTime = now.time().secsTo(endDateTime.time());
if (secondsToInputTime <= 0) // timer is being set for the following day
{
endDateTime = endDateTime.addDays(1);
}
endtime.setDate(endDateTime.date());
endtime.setTime(time);
}
void Timer::on_startTimer_clicked()
{
bool ok;
QString inputdescr = QInputDialog::getText(0,"New Timer",
"Enter Timer Name", QLineEdit::Normal, QString(), &ok);
if (!ok || inputdescr == "")
{
ui->startTimer->setEnabled(false);
ui->stopTimer->setEnabled(false);
return;
}
QListWidgetItem* listAlert = new QListWidgetItem;
listAlert->setText(inputdescr);
ui->listWidget->addItem(listAlert);
std::string inputdescr_str = inputdescr.toStdString();
Alert* newAlert = new Alert(inputdescr_str, endtime);
bool acquired = false;
while(!acquired)
{
if(mtx->try_lock())
{
acquired = true;
aQ->push_back(newAlert);
mtx->unlock();
}
}
alertWidgetMap.insert({listAlert, newAlert});
ui->startTimer->setEnabled(false);
}
void Timer::on_listWidget_itemClicked(QListWidgetItem *item)
{
setCurrentListWidgetItem(item);
ui->startTimer->setEnabled(false);
ui->stopTimer->setEnabled(true);
}
void Timer::on_stopTimer_clicked()
{
QMessageBox::StandardButton reply = QMessageBox::question(this,
"Stop Timer", "Are you sure you want to cancel this timer?",
QMessageBox::Yes | QMessageBox:: No);
if(reply == QMessageBox::No)
{
ui->stopTimer->setEnabled(false);
return;
}
Alert* oldAlert = getAlert(currentTItem);
bool acquired = false;
while(!acquired)
{
if(mtx->try_lock())
{
acquired = true;
aQ->erase(std::find(aQ->begin(), aQ->end(), oldAlert));
delete oldAlert;
mtx->unlock();
}
}
alertWidgetMap.erase(currentTItem);
currentTItem->setHidden(true);
ui->stopTimer->setEnabled(false);
delete currentTItem;
}
void Timer::remove_tItem(Alert* a)
{
QListWidgetItem* finished_tItem = nullptr;
bool found = false;
for (auto it = alertWidgetMap.begin(); it != alertWidgetMap.end(); ++it)
{
if (it->second == a)
{
found = true;
finished_tItem = it->first;
alertWidgetMap.erase(it->first);
break;
}
if (found) { break; }
}
if (finished_tItem != nullptr)
{
delete finished_tItem;
}
delete a;
}
void Timer::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
ui->stopTimer->setEnabled(false);
ui->startTimer->setEnabled(false);
Alert *a = getAlert(item);
//AlertTime t = a->getAlertTime();
QDateTime t = a->getAlertTime();
QString str_time = QString::fromStdString(timeToAlert(t));
QMessageBox::information(0,"Timer:", str_time);
}
void Timer::voice_create_timer(int inputSeconds, QString name)
{
qDebug() << "in create: " << name;
QDateTime now = QDateTime::currentDateTime();
QDateTime endDateTime = QDateTime::currentDateTime();
endDateTime = now.addSecs(inputSeconds);
endtime = endDateTime;
qDebug() << "attribute set";
std::string inputdescr_str;
if(name == "") {
std::string inputdescr_str = "New Timer";
} else {
std::string inputdescr_str = name.toStdString();
}
QString inputdescr = QString::fromStdString(inputdescr_str);
QListWidgetItem* listAlert = new QListWidgetItem;
listAlert->setText(name);
ui->listWidget->addItem(listAlert);
qDebug() << "widget added";
Alert* newAlert = new Alert(inputdescr_str, endtime);
bool acquired = false;
while(!acquired)
{
if(mtx->try_lock())
{
acquired = true;
aQ->push_back(newAlert);
mtx->unlock();
}
}
alertWidgetMap.insert({listAlert, newAlert});
ui->stopTimer->setEnabled(false);
ui->startTimer->setEnabled(false);
qDebug() << "widget mapped";
}