Serenity Operating System
at master 178 lines 6.0 kB view raw
1/* 2 * Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include "AddEventDialog.h" 9#include <LibCore/DateTime.h> 10#include <LibGUI/BoxLayout.h> 11#include <LibGUI/Button.h> 12#include <LibGUI/ComboBox.h> 13#include <LibGUI/Label.h> 14#include <LibGUI/Painter.h> 15#include <LibGUI/SpinBox.h> 16#include <LibGUI/TextBox.h> 17#include <LibGUI/Widget.h> 18#include <LibGUI/Window.h> 19#include <LibGfx/Color.h> 20#include <LibGfx/Font/FontDatabase.h> 21 22AddEventDialog::AddEventDialog(Core::DateTime date_time, Window* parent_window) 23 : Dialog(parent_window) 24 , m_date_time(date_time) 25{ 26 resize(158, 130); 27 set_title("Add Event"); 28 set_resizable(false); 29 set_icon(parent_window->icon()); 30 31 auto widget = set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors(); 32 widget->set_fill_with_background_color(true); 33 widget->set_layout<GUI::VerticalBoxLayout>(); 34 35 auto& top_container = widget->add<GUI::Widget>(); 36 top_container.set_layout<GUI::VerticalBoxLayout>(4); 37 top_container.set_fixed_height(45); 38 39 auto& add_label = top_container.add<GUI::Label>("Add title & date:"); 40 add_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); 41 add_label.set_fixed_height(14); 42 add_label.set_font(Gfx::FontDatabase::default_font().bold_variant()); 43 44 auto& event_title_textbox = top_container.add<GUI::TextBox>(); 45 event_title_textbox.set_fixed_height(20); 46 47 auto& middle_container = widget->add<GUI::Widget>(); 48 middle_container.set_layout<GUI::HorizontalBoxLayout>(4); 49 middle_container.set_fixed_height(25); 50 51 auto& time_container = widget->add<GUI::Widget>(); 52 time_container.set_layout<GUI::HorizontalBoxLayout>(4); 53 time_container.set_fixed_height(25); 54 55 auto& starting_month_combo = middle_container.add<GUI::ComboBox>(); 56 starting_month_combo.set_only_allow_values_from_model(true); 57 starting_month_combo.set_fixed_size(50, 20); 58 starting_month_combo.set_model(MonthListModel::create()); 59 starting_month_combo.set_selected_index(m_date_time.month() - 1); 60 61 auto& starting_day_combo = middle_container.add<GUI::SpinBox>(); 62 starting_day_combo.set_fixed_size(40, 20); 63 starting_day_combo.set_range(1, m_date_time.days_in_month()); 64 starting_day_combo.set_value(m_date_time.day()); 65 66 auto& starting_year_combo = middle_container.add<GUI::SpinBox>(); 67 starting_year_combo.set_fixed_size(55, 20); 68 starting_year_combo.set_range(0, 9999); 69 starting_year_combo.set_value(m_date_time.year()); 70 71 auto& starting_hour_combo = time_container.add<GUI::SpinBox>(); 72 starting_hour_combo.set_fixed_size(50, 20); 73 starting_hour_combo.set_range(1, 12); 74 starting_hour_combo.set_value(12); 75 76 auto& starting_minute_combo = time_container.add<GUI::SpinBox>(); 77 starting_minute_combo.set_fixed_size(40, 20); 78 starting_minute_combo.set_range(0, 59); 79 starting_minute_combo.set_value(0); 80 81 auto& starting_meridiem_combo = time_container.add<GUI::ComboBox>(); 82 starting_meridiem_combo.set_only_allow_values_from_model(true); 83 starting_meridiem_combo.set_fixed_size(55, 20); 84 starting_meridiem_combo.set_model(MeridiemListModel::create()); 85 starting_meridiem_combo.set_selected_index(0); 86 87 widget->add_spacer().release_value_but_fixme_should_propagate_errors(); 88 89 auto& button_container = widget->add<GUI::Widget>(); 90 button_container.set_fixed_height(20); 91 button_container.set_layout<GUI::HorizontalBoxLayout>(); 92 button_container.add_spacer().release_value_but_fixme_should_propagate_errors(); 93 auto& ok_button = button_container.add<GUI::Button>("OK"_short_string); 94 ok_button.set_fixed_size(80, 20); 95 ok_button.on_click = [this](auto) { 96 dbgln("TODO: Add event icon on specific tile"); 97 done(ExecResult::OK); 98 }; 99 100 auto update_starting_day_range = [&starting_day_combo, &starting_year_combo, &starting_month_combo]() { 101 auto year = starting_year_combo.value(); 102 auto month = starting_month_combo.selected_index(); 103 104 starting_day_combo.set_range(1, days_in_month(year, month + 1)); 105 }; 106 107 starting_year_combo.on_change = [update_starting_day_range](auto) { update_starting_day_range(); }; 108 starting_month_combo.on_change = [update_starting_day_range](auto, auto) { update_starting_day_range(); }; 109 110 event_title_textbox.set_focus(true); 111} 112 113int AddEventDialog::MonthListModel::row_count(const GUI::ModelIndex&) const 114{ 115 return 12; 116} 117 118int AddEventDialog::MeridiemListModel::row_count(const GUI::ModelIndex&) const 119{ 120 return 2; 121} 122 123DeprecatedString AddEventDialog::MonthListModel::column_name(int column) const 124{ 125 switch (column) { 126 case Column::Month: 127 return "Month"; 128 default: 129 VERIFY_NOT_REACHED(); 130 } 131} 132 133DeprecatedString AddEventDialog::MeridiemListModel::column_name(int column) const 134{ 135 switch (column) { 136 case Column::Meridiem: 137 return "Meridiem"; 138 default: 139 VERIFY_NOT_REACHED(); 140 } 141} 142 143GUI::Variant AddEventDialog::MonthListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const 144{ 145 constexpr Array short_month_names = { 146 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 147 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 148 }; 149 150 auto& month = short_month_names[index.row()]; 151 if (role == GUI::ModelRole::Display) { 152 switch (index.column()) { 153 case Column::Month: 154 return month; 155 default: 156 VERIFY_NOT_REACHED(); 157 } 158 } 159 return {}; 160} 161 162GUI::Variant AddEventDialog::MeridiemListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const 163{ 164 constexpr Array meridiem_names = { 165 "AM", "PM" 166 }; 167 168 auto& meridiem = meridiem_names[index.row()]; 169 if (role == GUI::ModelRole::Display) { 170 switch (index.column()) { 171 case Column::Meridiem: 172 return meridiem; 173 default: 174 VERIFY_NOT_REACHED(); 175 } 176 } 177 return {}; 178}