From 3c1f0b64fef45760a0234ac6b676ca9042dc7d0d Mon Sep 17 00:00:00 2001 From: pascal Date: Sun, 8 Dec 2024 18:51:32 +0100 Subject: [PATCH] implement TimeRangeEditor pb_begin_to_last_end --- src/timerangeeditor.cpp | 21 ++++++++++++++++----- src/timerangeeditor.h | 3 ++- src/views/perioddetailview.cpp | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/timerangeeditor.cpp b/src/timerangeeditor.cpp index 28b5582..f48531d 100644 --- a/src/timerangeeditor.cpp +++ b/src/timerangeeditor.cpp @@ -2,26 +2,37 @@ #include "application.h" #include "datetimeselector.h" +#include "intervalmodel.h" #include "ui_timerangeeditor.h" #include -TimeRangeEditor::TimeRangeEditor(QWidget* const parent) +TimeRangeEditor::TimeRangeEditor(const IntervalModel& interval_model, QWidget* const parent) : QDialog(parent), m_ui(std::make_unique()) { m_ui->setupUi(this); connect(m_ui->cb_has_end, &QCheckBox::toggled, this, &TimeRangeEditor::update_enabledness); - connect(m_ui->pb_begin_to_last_end, &QPushButton::clicked, this, []() {}); + connect(m_ui->pb_begin_to_last_end, &QPushButton::clicked, this, [&interval_model, this]() { + auto ends_view = + interval_model.intervals() | std::views::transform(&Interval::end) | std::views::filter(&QDateTime::isValid); + if (const std::vector ends(ends_view.begin(), ends_view.end()); ends.empty()) { + QMessageBox::critical(this, "Error", "No end intervals were found"); + } else { + const auto end = std::ranges::max_element(ends); + m_ui->de_begin->setDate(end->date()); + m_ui->te_begin->setTime(end->time()); + } + }); connect(m_ui->pb_begin_to_now, &QPushButton::clicked, this, [this]() { - m_ui->te_begin->setDate(Application::current_date_time().date()); - m_ui->de_begin->setTime(Application::current_date_time().time()); + m_ui->te_begin->setTime(Application::current_date_time().time()); + m_ui->de_begin->setDate(Application::current_date_time().date()); }); connect(m_ui->pb_end_to_begin, &QPushButton::clicked, this, [this]() { m_ui->te_end->setTime(m_ui->te_begin->time()); m_ui->sp_end_offset->setValue(0); }); connect(m_ui->pb_end_to_now, &QPushButton::clicked, this, [this]() { - const auto offset = m_ui->te_begin->date().daysTo(Application::current_date_time().date()); + const auto offset = m_ui->de_begin->date().daysTo(Application::current_date_time().date()); m_ui->sp_end_offset->setValue(offset); m_ui->te_end->setTime(Application::current_date_time().time()); }); diff --git a/src/timerangeeditor.h b/src/timerangeeditor.h index 4538e20..f3aaafe 100644 --- a/src/timerangeeditor.h +++ b/src/timerangeeditor.h @@ -3,6 +3,7 @@ #include #include +class IntervalModel; namespace Ui { class TimeRangeEditor; @@ -13,7 +14,7 @@ class TimeRangeEditor : public QDialog Q_OBJECT public: - explicit TimeRangeEditor(QWidget* parent = nullptr); + explicit TimeRangeEditor(const IntervalModel& interval_model, QWidget* parent = nullptr); ~TimeRangeEditor() override; void set_range(const QDateTime& begin, const QDateTime& end); diff --git a/src/views/perioddetailview.cpp b/src/views/perioddetailview.cpp index 73f30af..8b945f8 100644 --- a/src/views/perioddetailview.cpp +++ b/src/views/perioddetailview.cpp @@ -259,7 +259,7 @@ void PeriodDetailView::show_table_context_menu(const QPoint& pos) void PeriodDetailView::edit_date_time(const QModelIndex& index) const { - TimeRangeEditor e; + TimeRangeEditor e{time_sheet()->interval_model()}; auto& interval = *time_sheet()->interval_model().intervals().at(index.row()); e.set_range(interval.begin(), interval.end()); if (e.exec() == QDialog::Accepted) {