Files
Chronos/sources/setdaydialog.cpp
2022-10-16 17:36:47 +02:00

115 lines
3.4 KiB
C++

#include "setdaydialog.h"
#include "ui_setdaydialog.h"
SetDayDialog::SetDayDialog(Day *d, bool isNotValidable, QWidget *parent) :
QDialog(parent),
ui(new Ui::SetDayDialog)
{
ui->setupUi(this);
copy = new Day(d);
ui->validateButton->setEnabled(!isNotValidable);
ui->validateButton->setVisible(!isNotValidable);
this->d = d;
init();
}
SetDayDialog::~SetDayDialog()
{
delete copy;
delete ui;
}
void SetDayDialog::init() {
ui->start_edit->setTime(copy->get_start());
ui->end_edit->setTime(copy->get_end());
ui->notWorkingCheckbox->setCheckState((copy->not_working()) ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
lockControls(copy->not_working());
updateBreakList();
connect(ui->start_edit, &QTimeEdit::timeChanged, this, &SetDayDialog::compute_time);
connect(ui->end_edit, &QTimeEdit::timeChanged, this, &SetDayDialog::compute_time);
connect(ui->validateButton, &QPushButton::clicked, this, &SetDayDialog::validate_and_accept);
connect(ui->pushButton, &QPushButton::clicked, this, &SetDayDialog::accept);
connect(ui->addBreakButton, &QPushButton::clicked, this, &SetDayDialog::add_break_point);
connect(ui->removeBreakButton, &QPushButton::clicked, this, &SetDayDialog::remove_break_point);
connect(ui->breakList, &QListWidget::currentRowChanged, this, &SetDayDialog::break_selected);
connect(ui->notWorkingCheckbox, &QCheckBox::stateChanged, this, &SetDayDialog::change_not_working);
ui->total_label->setText(Tools::double_to_string_time(d->get_total()));
}
void SetDayDialog::updateBreakList()
{
ui->breakList->clear();
foreach (BreakPoint *bp, copy->getBreaks()) {
ui->breakList->addItem(QString("%1 -> %2").arg(bp->getStart().toString("hh:mm"), bp->getEnd().toString("hh:mm")));
}
}
void SetDayDialog::lockControls(bool lock)
{
ui->addBreakButton->setDisabled(lock);
ui->start_edit->setDisabled(lock);
ui->end_edit->setDisabled(lock);
ui->breakList->setDisabled(lock);
if (ui->breakList->currentRow() != -1) {
ui->removeBreakButton->setDisabled(lock);
}
}
void SetDayDialog::compute_time() {
copy->set_validate(false);
copy->set_start(ui->start_edit->time());
copy->set_end(ui->end_edit->time());
ui->total_label->setText(Tools::double_to_string_time(copy->get_total()));
}
void SetDayDialog::accept()
{
d->update(copy);
QDialog::accept();
}
void SetDayDialog::validate_and_accept() {
copy->set_validate(!copy->not_working());
this->accept();
}
void SetDayDialog::add_break_point()
{
BreakDialog bd(this);
int result = bd.exec();
if (result == QDialog::Accepted) {
BreakPoint bp = bd.get_result();
auto breaks = copy->getBreaks();
breaks.append(new BreakPoint(bp));
copy->setBreaks(breaks);
updateBreakList();
compute_time();
}
}
void SetDayDialog::remove_break_point()
{
int i = ui->breakList->currentRow();
if (i > -1) {
auto breaks = copy->getBreaks();
BreakPoint *bp = breaks.at(i);
breaks.removeAt(i);
delete bp;
copy->setBreaks(breaks);
updateBreakList();
compute_time();
}
}
void SetDayDialog::break_selected(int i)
{
ui->removeBreakButton->setEnabled(i > -1);
}
void SetDayDialog::change_not_working(int state)
{
copy->set_not_working(state != 0);
lockControls(copy->not_working());
compute_time();
}