AddEventDialog.cpp 7.6 KB

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