forked from AlphaKnot/PlanItTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarmwidget.cpp
236 lines (169 loc) · 6.68 KB
/
alarmwidget.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
#include "alarmwidget.h"
AlarmWidget::AlarmWidget(QWidget *parent) : QWidget(parent)
{
QFontDatabase::addApplicationFont(":/fonts/ARCADECLASSIC.TTF");
QPalette pal = QPalette();
pal.setColor(QPalette::WindowText,Qt::cyan);
add_alarm = new QPushButton("Add",this);
remove_alarm = new QPushButton("Delete",this);
add_alarm->autoFillBackground();
remove_alarm->autoFillBackground();
add_alarm->setFont(QFont("arcadeclassic",20)); // not case sensitive
remove_alarm->setFont(QFont("arcadeclassic",20));
remove_alarm->setStyleSheet(" QPushButton{background-color: black ; color: white}");
add_alarm->setStyleSheet(" QPushButton{background-color: black ; color: white}");
remove_alarm->setPalette(pal);
add_alarm->setPalette(pal);
hour_box = new QComboBox();
hour_box->setStyleSheet("QComboBox { background-color: black; color:white }");
hour_box->addItem(QString("1"));
hour_box->addItem(QString("2"));
hour_box->addItem(QString("3"));
hour_box->addItem(QString("4"));
hour_box->addItem(QString("5"));
hour_box->addItem(QString("6"));
hour_box->addItem(QString("7"));
hour_box->addItem(QString("8"));
hour_box->addItem(QString("9"));
hour_box->addItem(QString("10"));
hour_box->addItem(QString("11"));
hour_box->addItem(QString("12"));
minute_box = new QComboBox();
for(int i = 0 ; i!=60; i++){
minute_box->addItem(QString::fromStdString(std::to_string(i)));
}
AM_PM_Box = new QComboBox();
AM_PM_Box->addItem(QString("AM"));
AM_PM_Box->addItem(QString("PM"));
hour_box->setStyleSheet("QComboBox { background-color: black; color:white }");
hour_box->setFont(QFont("arcadeclassic"));
minute_box->setStyleSheet("QComboBox { background-color: black; color:white }");
minute_box->setFont(QFont("arcadeclassic"));
AM_PM_Box->setStyleSheet("QComboBox { background-color: black; color:white }");
AM_PM_Box->setFont(QFont("arcadeclassic"));
alarmList = new QListWidget(parent);
connect(add_alarm, &QPushButton::clicked, this, &AlarmWidget::addButtonClicked);
connect(remove_alarm, &QPushButton::clicked, this, &AlarmWidget::removeButtonClicked);
connect(alarmList, &QListWidget::itemClicked, this,&AlarmWidget::itemClicked);
alarmList->setPalette(pal);
alarmList->setFont(QFont("arcadeclassic",20));
alarmList->setStyleSheet("QListWidget {background-color:black} QListWidget::item:text {background-color:black; color: white} QListWidget::item {border-bottom:1px solid white;} QListWidget::item:selected { background-color:white; color:black; }");
std::string path = "alarmTime.csv";
std::string* pathptr = &path;
bool value = checkFile(*pathptr);
if(value == true){
readTimeSlots(*pathptr, alarmList );
}
QGridLayout* layout = new QGridLayout(this);
layout->addWidget(remove_alarm,0,1);
layout->addWidget(add_alarm,0,3);
layout->addWidget(alarmList,4,2);
layout->setColumnStretch(2,3);
layout->setRowStretch(4,4);
layout->addWidget(hour_box,5,0);
layout->addWidget(minute_box,5,1);
layout->addWidget(AM_PM_Box,5,3);
resize(700,450);
}
void AlarmWidget::addButtonClicked(){
int newstdhour;
std::string minout;
if(AM_PM_Box->currentText()!=QString("") && minute_box->currentText() !=QString("") && hour_box->currentText()!=QString("")){
if (AM_PM_Box->currentText() == QString("PM")) {
QString newHour = hour_box->currentText();
newstdhour = newHour.toInt();
if (newstdhour!=12) {
newstdhour+=12;
}
}
// edge case
else if (AM_PM_Box->currentText() == QString("AM") && hour_box->currentText() == "12") newstdhour = 0;
else newstdhour = hour_box->currentText().toInt();
std::ofstream myfile;
myfile.open ("alarmTime.csv",std::ios::app);
int minbox = minute_box->currentText().toInt();
if (minbox<10) {
minout= "0"+std::to_string(minbox);
} else {
minout = minute_box->currentText().toStdString();
}
std::string writeout = std::to_string(newstdhour) +"," + minout +"\n";
myfile << writeout;
myfile.close();
std::string fileloca = "alarmTime.csv";
readTimeSlots(fileloca, alarmList);
}
}
void AlarmWidget::removeButtonClicked(){
char filename[14] = "alarmTime.csv";
std::string filenamestdString = "alarmTime.csv";
int lineNumber = alarmList->currentRow();
if (lineNumber!=0) {
alarmList->removeItemWidget(alarmList->selectedItems().at(0));
delete_line(filename, lineNumber-1);
readTimeSlots(filenamestdString, alarmList);
}
if (lineNumber==0) {
readTimeSlots(filenamestdString, alarmList);
}
}
void AlarmWidget::itemClicked(){
}
bool AlarmWidget::checkFile (std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}
void AlarmWidget::readTimeSlots(std::string& name, QListWidget* alarmList){
std::string line;
std::string fileDir = name.c_str();
std::ifstream myfile (fileDir);
alarmList->clear();
QListWidgetItem *temp = new QListWidgetItem("Alarm Lists");
temp->setTextAlignment(Qt::AlignCenter);
alarmList->addItem(temp);
while(getline(myfile,line)){
std::string finalString = "";
std::vector<std::string> parsedcsv;
boost::split(parsedcsv, line, boost::is_any_of(","));
for (int i = 0; i < parsedcsv.size(); i++){
if (i%2 ==0) {
int hour = std::stoi( parsedcsv[i] );
hour = hour % 24;
if (hour == 0) finalString.append("00:");
else finalString.append(std::to_string(hour)+ ":");
} else {
finalString.append(parsedcsv[i]);
std::cout<< finalString << std::endl;
Times.push_back(finalString);
QListWidgetItem *temp = new QListWidgetItem((tr(finalString.c_str())));
temp->setTextAlignment(Qt::AlignCenter);
alarmList->addItem(temp);
}
}
}
}
void AlarmWidget::delete_line(char* file_name, int n)
{
std::ifstream is("alarmTime.csv");
std::ofstream ofs;
ofs.open("temp.txt", std::ofstream::out);
char c;
int line_no = 0;
while (is.get(c))
{
if (line_no != n) ofs << c;
if (c == '\n') line_no++;
}
// closing output file
ofs.close();
// closing input file
is.close();
// remove the original file
remove(file_name);
// rename the file
rename("temp.txt", file_name);
}
// helper method to check if CSV database file exists.
std::vector<std::string> AlarmWidget::getTimes(){
return Times;
}