306 lines
10 KiB
C++
306 lines
10 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
init();
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete week_template;
|
|
foreach (Year *y, years) {
|
|
delete y;
|
|
}
|
|
years.clear();
|
|
delete ui;
|
|
}
|
|
|
|
QString MainWindow::get_save_file_path() {
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
if (!QDir(path).exists()) {
|
|
QDir().mkpath(path);
|
|
}
|
|
path += "/data/";
|
|
if (!QDir(path).exists()) {
|
|
QDir().mkpath(path);
|
|
}
|
|
path += SAVE_FILENAME;
|
|
return path;
|
|
}
|
|
|
|
void MainWindow::init() {
|
|
objectId.insert(ui->mon_button->objectName(), Identifier::mon);
|
|
objectId.insert(ui->tue_button->objectName(), Identifier::tue);
|
|
objectId.insert(ui->wed_button->objectName(), Identifier::wed);
|
|
objectId.insert(ui->thu_button->objectName(), Identifier::thu);
|
|
objectId.insert(ui->fri_button->objectName(), Identifier::fri);
|
|
connect(ui->aboutButton, &QPushButton::clicked, this, &MainWindow::open_about);
|
|
connect(ui->template_settings_button, &QPushButton::clicked, this, &MainWindow::edit_template);
|
|
connect(ui->today_button, &QPushButton::clicked, this, &MainWindow::set_date_to_now);
|
|
connect(ui->week_options_button, &QPushButton::clicked, this, &MainWindow::set_week_options);
|
|
connect(ui->dateEdit, &QDateEdit::dateTimeChanged, this, &MainWindow::compute_week_number);
|
|
connect(ui->mon_button, &QPushButton::clicked, this, &MainWindow::edit);
|
|
connect(ui->tue_button, &QPushButton::clicked, this, &MainWindow::edit);
|
|
connect(ui->wed_button, &QPushButton::clicked, this, &MainWindow::edit);
|
|
connect(ui->thu_button, &QPushButton::clicked, this, &MainWindow::edit);
|
|
connect(ui->fri_button, &QPushButton::clicked, this, &MainWindow::edit);
|
|
todayWeekNumber = QDate::currentDate().weekNumber();
|
|
dayOfWeek = QDate::currentDate().dayOfWeek();
|
|
saveLoaded = false;
|
|
|
|
if (QFile::exists(get_save_file_path())) {
|
|
open_save();
|
|
} else {
|
|
week_template = new Week();
|
|
Welcome w(week_template);
|
|
w.exec();
|
|
saveLoaded = true;
|
|
}
|
|
if (saveLoaded) {
|
|
set_date_to_now();
|
|
compute_time();
|
|
}
|
|
}
|
|
|
|
void MainWindow::highlightDayOfWeek() {
|
|
ui->monLabel->setText("Lundi");
|
|
ui->tueLabel->setText("Mardi");
|
|
ui->wedLabel->setText("Mercredi");
|
|
ui->thuLabel->setText("Jeudi");
|
|
ui->friLabel->setText("Vendredi");
|
|
if (todayWeekNumber == current_week->getWeekNumber()) {
|
|
ui->today_button->setDisabled(true);
|
|
switch (dayOfWeek) {
|
|
case 1: {
|
|
ui->monLabel->setText(QString("%1 📆").arg(ui->monLabel->text()));
|
|
break;
|
|
}
|
|
case 2: {
|
|
ui->tueLabel->setText(QString("%1 📆").arg(ui->tueLabel->text()));
|
|
break;
|
|
}
|
|
case 3: {
|
|
ui->wedLabel->setText(QString("%1 📆").arg(ui->wedLabel->text()));
|
|
break;
|
|
}
|
|
case 4: {
|
|
ui->thuLabel->setText(QString("%1 📆").arg(ui->thuLabel->text()));
|
|
break;
|
|
}
|
|
case 5: {
|
|
ui->friLabel->setText(QString("%1 📆").arg(ui->friLabel->text()));
|
|
break;
|
|
}
|
|
default: break;
|
|
}
|
|
} else {
|
|
ui->today_button->setDisabled(false);
|
|
}
|
|
}
|
|
|
|
void MainWindow::set_date_to_now() {
|
|
auto date = QDate::currentDate();
|
|
ui->dateEdit->setDate(date);
|
|
}
|
|
|
|
void MainWindow::open_save() {
|
|
QFile* file = new QFile(get_save_file_path());
|
|
file->open(QIODevice::ReadOnly);
|
|
auto json = QString(file->readAll());
|
|
file->close();
|
|
delete file;
|
|
|
|
QJsonObject obj = QJsonDocument::fromJson(json.toUtf8()).object();
|
|
if (obj[KEY_SAVE_FILE_VERSION] != SAVE_FILE_VERSION) {
|
|
panic_dialog("Your save file version does not match the supported version."
|
|
" Please update this file and restart the application");
|
|
}
|
|
week_template = Week::from_json(obj[KEY_TEMPLATE].toObject());
|
|
QJsonArray arr = obj[KEY_YEARS].toArray();
|
|
foreach (QJsonValue val, arr) {
|
|
Year *y = Year::from_json(val.toObject());
|
|
years[y->getNumber()] = y;
|
|
}
|
|
saveLoaded = true;
|
|
}
|
|
|
|
void MainWindow::panic_dialog(QString text) {
|
|
QMessageBox::critical(this, tr("Chronos"), text,
|
|
QMessageBox::Ok, QMessageBox::Ok);
|
|
QTimer::singleShot(0, this, &MainWindow::close);
|
|
}
|
|
|
|
void MainWindow::save_to_file() {
|
|
QJsonArray arr;
|
|
foreach (Year *y, years) {
|
|
arr.append(y->to_json());
|
|
}
|
|
QJsonObject obj {
|
|
{KEY_TEMPLATE, week_template->to_json()},
|
|
{KEY_YEARS, arr},
|
|
{KEY_SAVE_FILE_VERSION, SAVE_FILE_VERSION}
|
|
};
|
|
QJsonDocument doc(obj);
|
|
QFile *f = new QFile(get_save_file_path());
|
|
if (f->open(QIODevice::WriteOnly)) {
|
|
f->write(doc.toJson());
|
|
f->close();
|
|
}
|
|
delete f;
|
|
}
|
|
|
|
void MainWindow::compute_week_number(const QDateTime &dt) {
|
|
int n = dt.date().weekNumber();
|
|
int y = dt.date().year();
|
|
ui->label_semaine->setText(QString("Semaine : %1").arg(QString::number(n)));
|
|
if (years.contains(y)) {
|
|
current_year = years[y];
|
|
} else {
|
|
Year *nYear = new Year(y);
|
|
years[y] = nYear;
|
|
current_year = nYear;
|
|
save_to_file();
|
|
}
|
|
|
|
if (current_year->hasWeek(n)) {
|
|
current_week = current_year->getWeek(n);
|
|
} else {
|
|
current_week = new Week(week_template);
|
|
current_week->setWeekNumber(n);
|
|
current_year->addWeek(current_week);
|
|
save_to_file();
|
|
}
|
|
compute_time();
|
|
highlightDayOfWeek();
|
|
}
|
|
|
|
void MainWindow::compute_time() {
|
|
ui->mon_time_label->setText(Tools::double_to_string_time(current_week->getMon()->get_total()));
|
|
ui->tue_time_label->setText(Tools::double_to_string_time(current_week->getTue()->get_total()));
|
|
ui->wed_time_label->setText(Tools::double_to_string_time(current_week->getWed()->get_total()));
|
|
ui->thu_time_label->setText(Tools::double_to_string_time(current_week->getThu()->get_total()));
|
|
ui->fri_time_label->setText(Tools::double_to_string_time(current_week->getFri()->get_total()));
|
|
ui->total_time_label->setText(Tools::double_to_string_time(current_week->total()));
|
|
updateLabels();
|
|
updateWeekTime();
|
|
|
|
double late = 0.0;
|
|
double overtime = 0.0;
|
|
int todayWeekNumber = QDate::currentDate().weekNumber();
|
|
QMapIterator i = current_year->iterator();
|
|
while (i.hasNext()) {
|
|
Week *w = i.next().value();
|
|
if (w->getWeekNumber() <= todayWeekNumber) {
|
|
late += (week_template->total() - w->total(week_template)) - w->getTimeDeltaInHours();
|
|
overtime += (w->total() - week_template->total(week_template)) + w->getTimeDeltaInHours();
|
|
}
|
|
}
|
|
late -= current_week->getTimeDeltaInHours();
|
|
overtime += - current_week->getTimeDeltaInHours();
|
|
ui->late_time_label->setText(Tools::double_to_string_time((late > 0.0) ? late : 0.0));
|
|
ui->overtime_time_label->setText(Tools::double_to_string_time((overtime > 0.0) ? overtime : 0.0));
|
|
}
|
|
|
|
void MainWindow::updateWeekTime()
|
|
{
|
|
double t = (current_week->total(week_template) + current_week->getTimeDeltaInHours()) - week_template->total();
|
|
if (t > 0) {
|
|
ui->overtime_time_label_week->setText(Tools::double_to_string_time(t));
|
|
ui->late_time_label_week->setText("0h");
|
|
} else if(t < 0) {
|
|
ui->late_time_label_week->setText(Tools::double_to_string_time(-t));
|
|
ui->overtime_time_label_week->setText("0h");
|
|
} else {
|
|
ui->overtime_time_label_week->setText("0h");
|
|
ui->late_time_label_week->setText("0h");
|
|
}
|
|
}
|
|
|
|
void MainWindow::updateLabels() {
|
|
updateDayLabels(ui->monStartLabel, ui->monEndLabel, ui->monBreakLabel, ui->mondayValidate, ui->mondayWarning, ui->mondayNotWorking, current_week->getMon());
|
|
updateDayLabels(ui->tueStartLabel, ui->tueEndLabel, ui->tueBreakLabel, ui->tuesdayValidate, ui->tuesdayWarning, ui->tuesdayNotWorking, current_week->getTue());
|
|
updateDayLabels(ui->wedStartLabel, ui->wedEndLabel, ui->wedBreakLabel, ui->wednesdayValidate, ui->wednesdayWarning, ui->wednesdayNotWorking, current_week->getWed());
|
|
updateDayLabels(ui->thuStartLabel, ui->thuEndLabel, ui->thuBreakLabel, ui->thurdayValidate, ui->thurdayWarning, ui->thurdayNotWorking, current_week->getThu());
|
|
updateDayLabels(ui->friStartLabel, ui->friEndLabel, ui->friBreakLabel, ui->fridayValidate, ui->fridayWarning, ui->fridayNotWorking, current_week->getFri());
|
|
}
|
|
|
|
void MainWindow::updateDayLabels(QLabel *start, QLabel *end, QLabel *breaks, QFrame *validate, QFrame *warning, QFrame *not_working, Day *d)
|
|
{
|
|
not_working->setVisible(d->not_working());
|
|
if (!d->not_working()) {
|
|
start->setText(d->get_start().toString("HH:mm"));
|
|
end->setText(d->get_end().toString("HH:mm"));
|
|
breaks->setText(Tools::double_to_string_counter(d->get_time_break()));
|
|
validate->setVisible(d->get_validate());
|
|
warning->setVisible(d->has_warning());
|
|
} else {
|
|
start->setText("--:--");
|
|
end->setText("--:--");
|
|
breaks->setText("0 min.");
|
|
validate->setVisible(false);
|
|
warning->setVisible(false);
|
|
}
|
|
}
|
|
|
|
void MainWindow::edit() {
|
|
QString name = QObject::sender()->objectName();
|
|
switch (get_identifier(name)) {
|
|
case mon:
|
|
modify_value(current_week->getMon());
|
|
break;
|
|
case tue:
|
|
modify_value(current_week->getTue());
|
|
break;
|
|
case wed:
|
|
modify_value(current_week->getWed());
|
|
break;
|
|
case thu:
|
|
modify_value(current_week->getThu());
|
|
break;
|
|
case fri:
|
|
modify_value(current_week->getFri());
|
|
break;
|
|
}
|
|
save_to_file();
|
|
compute_time();
|
|
}
|
|
|
|
void MainWindow::modify_value(Day *d) {
|
|
bool isNotValidable = (current_week->getWeekNumber() > todayWeekNumber);
|
|
SetDayDialog sdd(d, isNotValidable, this);
|
|
sdd.exec();
|
|
}
|
|
|
|
Identifier MainWindow::get_identifier(QString objectName) {
|
|
return objectId[objectName];
|
|
}
|
|
|
|
void MainWindow::edit_template() {
|
|
Welcome w(week_template);
|
|
int res = w.exec();
|
|
if (res == QDialog::Accepted) {
|
|
save_to_file();
|
|
compute_time();
|
|
}
|
|
}
|
|
|
|
void MainWindow::open_about() {
|
|
AboutBox a(this);
|
|
a.exec();
|
|
}
|
|
|
|
void MainWindow::set_week_options()
|
|
{
|
|
WeekOption ow(current_week, this);
|
|
int result = ow.exec();
|
|
if (result == QDialog::Accepted) {
|
|
current_week->setTimeDelta(ow.get_time_delta());
|
|
save_to_file();
|
|
compute_time();
|
|
}
|
|
}
|