-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
221 lines (185 loc) · 6.71 KB
/
mainwindow.cpp
File metadata and controls
221 lines (185 loc) · 6.71 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
/**
* @author Iagoh Ribeiro Lima
* @date 11/29/2021
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Hide the task widget to clean the tasks section
ui->task->hide();
//ui->descriptionField->setEchoMode(QLineEdit::Normal);
//ui->descriptionField->echoMode();
//Connection between the behavior of the Submit button and the slot that adds the task to the list.
QObject::connect(ui->submitButton, &QPushButton::clicked, this, &MainWindow::addTask);
//Connection to enable a task to be added via the return button.
QObject::connect(ui->descriptionField, &QLineEdit::returnPressed, ui->submitButton, &QPushButton::click);
//Connection to enable the creation of Exclusive checkboxes.
QObject::connect(ui->high, &QCheckBox::clicked, this, [=]{editCheckBox(ui->high);});
QObject::connect(ui->medium, &QCheckBox::clicked, this, [=]{editCheckBox(ui->medium);});
QObject::connect(ui->low, &QCheckBox::clicked, this, [=]{editCheckBox(ui->low);});
QObject::connect(ui->none, &QCheckBox::clicked, this, [=]{editCheckBox(ui->none);});
//Connection to add to the save button behavior.
QObject::connect(ui->saveButton, &QPushButton::clicked, this, &MainWindow::saveTasks);
}
MainWindow::~MainWindow()
{
delete ui;
taskList.clear();
}
void MainWindow::addTask()
{
/**
* @brief The tasks will be added in a Grid layout. Thus, for each row of the grid,
* a group of elements of a task will be added. Each column will be a task element
* in that row.
*
* Furthermore, the task's checkbox will be customized according to its priority.
*/
QGridLayout* layout = qobject_cast<QGridLayout*>(ui->gridLayout->layout());
int row = ui->gridLayout->rowCount();
if (ui->descriptionField->text().isEmpty())
{
/**
* @note If the description field is empty, you cannot add a task.
*/
QMessageBox msgBox;
msgBox.setText("The Description field cannot be empty.");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
else
{
Task* newTask = new Task ();
newTask->getDescription()->setText(ui->descriptionField->text());
if(ui->high->isChecked())
{
newTask->editCheckBox("High");
}
else if (ui->medium->isChecked())
{
newTask->editCheckBox("Medium");
}
else if (ui->low->isChecked())
{
newTask->editCheckBox("Low");
}
else
{
newTask->editCheckBox("None");
}
layout->addWidget(newTask->getCheckBox(), row, 0);
layout->addWidget(newTask->getDescription(), row, 1);
layout->addWidget(newTask->getEditButton(), row, 2);
layout->addWidget(newTask->getDeleteButton(), row, 3);
//The tasks will be added to a list to make it easier to save them to a file.
taskList.append(newTask);
//The buttons of a Task are connected to the functions responsible for their respective behaviors.
QObject::connect(
newTask->getDeleteButton(), &QPushButton::clicked,
this, [=]{removeTask(newTask);});
QObject::connect(
newTask->getEditButton(), &QPushButton::clicked,
this, [=]{editTask(newTask);});
}
}
void MainWindow::removeTask(Task* object)
{
/**
* @brief Responsible for deleting the task from the
* interface and from the task list
*/
taskList.removeAll(object);
delete object;
}
void MainWindow::editTask(Task* object)
{
/**
* @brief Responsible for generating a QDialog for
* the user to choose the color of the task.
*/
QInputDialog colorDialog = new QInputDialog();
QStringList colorList = {" ", "Red", "Yellow", "Green"};
QString color = colorDialog.getItem(this, "Select a background color for the task",
"Color: ",
colorList);
object->editDescription(color);
}
void MainWindow::editCheckBox(QCheckBox* checkBox)
{
/**
* @brief Transform the Priority checkboxes into exclusive checkboxes.
*/
if (checkBox->text().toUpper() == "HIGH")
{
ui->medium->setChecked(false);
ui->low->setChecked(false);
ui->none->setChecked(false);
}
else if (checkBox->text().toUpper() == "MEDIUM")
{
ui->high->setChecked(false);
ui->low->setChecked(false);
ui->none->setChecked(false);
}
else if (checkBox->text().toUpper() == "LOW")
{
ui->high->setChecked(false);
ui->medium->setChecked(false);
ui->none->setChecked(false);
}
else if (checkBox->text().toUpper() == "NONE")
{
ui->high->setChecked(false);
ui->medium->setChecked(false);
ui->low->setChecked(false);
}
}
void MainWindow::saveTasks()
{
/**
* @brief The tasks will be saved per line in a .txt file.
* The task's checkbox status, description and current background color
* will be saved.
*/
QDir directory = (QDir::currentPath());
QFile file = directory.filePath("TasksList.txt");
QString message = QString("The activities are being saved in: %1").arg(directory.path());
qInfo() << message;
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
/**
* @note If it is not possible to open the file,
* a screen will be presented to the user.
*/
QMessageBox msgBox;
msgBox.setText("The Tasks could not be saved.");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
}
else
{
/**
* @note scrolls through the list to get the task data to be saved.
*/
for(Task* taskElem:qAsConst(taskList))
{
QString line = QString("State: %1; Description: %2; BackgroundDescription: rgb(%3, %4, %5);\n")
.arg(taskElem->CheckBox->isChecked())
.arg(taskElem->Description->text())
.arg(taskElem->Description->palette().background().color().red())
.arg(taskElem->Description->palette().background().color().green())
.arg(taskElem->Description->palette().background().color().blue());
QTextStream out(&file);
out << line;
}
file.close();
qInfo() << "The Tasks have been successfully saved.";
}
}