mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:27:35 +00:00
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.
This commit is contained in:
parent
5ed334e13a
commit
039114b728
5 changed files with 91 additions and 55 deletions
|
@ -2,6 +2,7 @@
|
|||
* Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com>
|
||||
* Copyright (c) 2020-2022, the SerenityOS developers.
|
||||
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
|
||||
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
|
||||
*
|
||||
* 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<String> 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 {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
* Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com>
|
||||
* Copyright (c) 2020-2022, the SerenityOS developers.
|
||||
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
|
||||
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -13,6 +14,7 @@
|
|||
#include <LibCore/DateTime.h>
|
||||
#include <LibGUI/AbstractScrollableWidget.h>
|
||||
#include <LibGUI/Frame.h>
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
|
||||
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<MonthListModel> 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<String> 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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
33
Userland/Libraries/LibGUI/DatePicker.h
Normal file
33
Userland/Libraries/LibGUI/DatePicker.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <LibGUI/Calendar.h>
|
||||
#include <LibGUI/Dialog.h>
|
||||
#include <LibGUI/Model.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
class DatePicker : public Dialog {
|
||||
C_OBJECT(DatePicker)
|
||||
|
||||
public:
|
||||
virtual ~DatePicker() override = default;
|
||||
|
||||
static Optional<Core::DateTime> 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<ComboBox> m_month_box;
|
||||
RefPtr<SpinBox> m_year_box;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue