From 039114b7282daebb080ad5acb5341fcd6ac3e5c4 Mon Sep 17 00:00:00 2001 From: david072 Date: Fri, 17 Nov 2023 21:10:13 +0100 Subject: [PATCH] Calendar: Move MonthListModel into Calendar.h Previously, we had two versions of MonthListModel for the AddEventDialog and the DatePickerDialog. Now, a unified version is in the Calendar.h file, which can be used easily by anyone. Since that model and the MeridiemListModel weren't used anymore in the AddEventDialog, I have also removed them from there. --- .../Applications/Calendar/AddEventDialog.cpp | 38 +------------------ .../Applications/Calendar/AddEventDialog.h | 19 ---------- Userland/Libraries/LibGUI/Calendar.cpp | 25 ++++++++++++ Userland/Libraries/LibGUI/Calendar.h | 31 +++++++++++++++ Userland/Libraries/LibGUI/DatePicker.h | 33 ++++++++++++++++ 5 files changed, 91 insertions(+), 55 deletions(-) create mode 100644 Userland/Libraries/LibGUI/DatePicker.h diff --git a/Userland/Applications/Calendar/AddEventDialog.cpp b/Userland/Applications/Calendar/AddEventDialog.cpp index aa53c9fbbf..0bf1244ea0 100644 --- a/Userland/Applications/Calendar/AddEventDialog.cpp +++ b/Userland/Applications/Calendar/AddEventDialog.cpp @@ -45,7 +45,7 @@ AddEventDialog::AddEventDialog(Core::DateTime date_time, EventManager& event_man event_title_textbox->set_focus(true); auto starting_month_input = widget->find_descendant_of_type_named("start_month"); - starting_month_input->set_model(MonthListModel::create()); + starting_month_input->set_model(GUI::MonthListModel::create()); starting_month_input->set_selected_index(m_start_date_time.month() - 1); auto starting_day_input = widget->find_descendant_of_type_named("start_day"); @@ -65,7 +65,7 @@ AddEventDialog::AddEventDialog(Core::DateTime date_time, EventManager& event_man starting_meridiem_input->set_selected_index(0); auto ending_month_input = widget->find_descendant_of_type_named("end_month"); - ending_month_input->set_model(MonthListModel::create()); + ending_month_input->set_model(GUI::MonthListModel::create()); ending_month_input->set_selected_index(m_end_date_time.month() - 1); auto ending_day_input = widget->find_descendant_of_type_named("end_day"); @@ -172,26 +172,11 @@ ErrorOr AddEventDialog::add_event_to_calendar() return {}; } -int AddEventDialog::MonthListModel::row_count(const GUI::ModelIndex&) const -{ - return 12; -} - int AddEventDialog::MeridiemListModel::row_count(const GUI::ModelIndex&) const { return 2; } -ErrorOr AddEventDialog::MonthListModel::column_name(int column) const -{ - switch (column) { - case Column::Month: - return "Month"_string; - default: - VERIFY_NOT_REACHED(); - } -} - ErrorOr AddEventDialog::MeridiemListModel::column_name(int column) const { switch (column) { @@ -202,25 +187,6 @@ ErrorOr AddEventDialog::MeridiemListModel::column_name(int column) const } } -GUI::Variant AddEventDialog::MonthListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const -{ - constexpr Array short_month_names = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" - }; - - auto& month = short_month_names[index.row()]; - if (role == GUI::ModelRole::Display) { - switch (index.column()) { - case Column::Month: - return month; - default: - VERIFY_NOT_REACHED(); - } - } - return {}; -} - GUI::Variant AddEventDialog::MeridiemListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const { constexpr Array meridiem_names = { diff --git a/Userland/Applications/Calendar/AddEventDialog.h b/Userland/Applications/Calendar/AddEventDialog.h index 3af042f865..52871097e1 100644 --- a/Userland/Applications/Calendar/AddEventDialog.h +++ b/Userland/Applications/Calendar/AddEventDialog.h @@ -31,25 +31,6 @@ private: ErrorOr add_event_to_calendar(); - class MonthListModel final : public GUI::Model { - public: - enum Column { - Month, - __Count, - }; - - static NonnullRefPtr create() { return adopt_ref(*new MonthListModel); } - virtual ~MonthListModel() override = default; - - virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override; - virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Count; } - virtual ErrorOr column_name(int) const override; - virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override; - - private: - MonthListModel() = default; - }; - class MeridiemListModel final : public GUI::Model { public: enum Column { diff --git a/Userland/Libraries/LibGUI/Calendar.cpp b/Userland/Libraries/LibGUI/Calendar.cpp index e02b94edc1..043e65c326 100644 --- a/Userland/Libraries/LibGUI/Calendar.cpp +++ b/Userland/Libraries/LibGUI/Calendar.cpp @@ -2,6 +2,7 @@ * Copyright (c) 2019-2020, Ryan Grieb * Copyright (c) 2020-2022, the SerenityOS developers. * Copyright (c) 2022, Tobias Christiansen + * Copyright (c) 2023, David Ganz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -857,4 +858,28 @@ bool Calendar::is_day_in_weekend(DayOfWeek day) return day_index < weekend_end_index; } +ErrorOr MonthListModel::column_name(int column) const +{ + switch (column) { + case Column::Month: + return "Month"_string; + default: + VERIFY_NOT_REACHED(); + } +} + +GUI::Variant MonthListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const +{ + auto const& month = (m_mode == MonthListModel::DisplayMode::Short ? AK::short_month_names : AK::long_month_names)[index.row()]; + if (role == GUI::ModelRole::Display) { + switch (index.column()) { + case Column::Month: + return month; + default: + VERIFY_NOT_REACHED(); + } + } + return {}; +} + } diff --git a/Userland/Libraries/LibGUI/Calendar.h b/Userland/Libraries/LibGUI/Calendar.h index 36d0a0c77a..c98bae6985 100644 --- a/Userland/Libraries/LibGUI/Calendar.h +++ b/Userland/Libraries/LibGUI/Calendar.h @@ -2,6 +2,7 @@ * Copyright (c) 2019-2020, Ryan Grieb * Copyright (c) 2020-2022, the SerenityOS developers. * Copyright (c) 2022, Tobias Christiansen + * Copyright (c) 2023, David Ganz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -13,6 +14,7 @@ #include #include #include +#include #include namespace GUI { @@ -165,4 +167,33 @@ private: int m_weekend_length { 2 }; }; +class MonthListModel final : public GUI::Model { +public: + enum DisplayMode { + Short, + Long, + }; + + enum Column { + Month, + __Count, + }; + + static NonnullRefPtr create(DisplayMode mode = DisplayMode::Short) { return adopt_ref(*new MonthListModel(mode)); } + virtual ~MonthListModel() override = default; + + virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 12; } + virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Count; } + virtual ErrorOr column_name(int) const override; + virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override; + +private: + MonthListModel(DisplayMode mode) + : m_mode(mode) + { + } + + DisplayMode m_mode; +}; + } diff --git a/Userland/Libraries/LibGUI/DatePicker.h b/Userland/Libraries/LibGUI/DatePicker.h new file mode 100644 index 0000000000..aaee165553 --- /dev/null +++ b/Userland/Libraries/LibGUI/DatePicker.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023, David Ganz + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace GUI { + +class DatePicker : public Dialog { + C_OBJECT(DatePicker) + +public: + virtual ~DatePicker() override = default; + + static Optional show(Window* parent_window, String title, Core::DateTime focused_date = Core::DateTime::now()); + +private: + explicit DatePicker(Window* parent_window, String const& title, Core::DateTime focused_date = Core::DateTime::now()); + + Core::DateTime m_selected_date; + RefPtr m_month_box; + RefPtr m_year_box; +}; + +}