Files
Chronos/sources/setdaydialog.cpp

94 lines
2.7 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());
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);
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::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(true);
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);
}