AddEventDialog.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #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. AddEventDialog::AddEventDialog(Core::DateTime date_time, Window* parent_window)
  21. : Dialog(parent_window)
  22. , m_date_time(date_time)
  23. {
  24. resize(158, 130);
  25. set_title("Add Event");
  26. set_resizable(false);
  27. set_icon(parent_window->icon());
  28. auto widget = set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors();
  29. widget->set_fill_with_background_color(true);
  30. widget->set_layout<GUI::VerticalBoxLayout>();
  31. auto& top_container = widget->add<GUI::Widget>();
  32. top_container.set_layout<GUI::VerticalBoxLayout>(4);
  33. top_container.set_fixed_height(45);
  34. auto& add_label = top_container.add<GUI::Label>("Add title & date:"_string.release_value_but_fixme_should_propagate_errors());
  35. add_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  36. add_label.set_fixed_height(14);
  37. add_label.set_font(Gfx::FontDatabase::default_font().bold_variant());
  38. auto& event_title_textbox = top_container.add<GUI::TextBox>();
  39. event_title_textbox.set_fixed_height(20);
  40. auto& middle_container = widget->add<GUI::Widget>();
  41. middle_container.set_layout<GUI::HorizontalBoxLayout>(4);
  42. middle_container.set_fixed_height(25);
  43. auto& time_container = widget->add<GUI::Widget>();
  44. time_container.set_layout<GUI::HorizontalBoxLayout>(4);
  45. time_container.set_fixed_height(25);
  46. auto& starting_month_combo = middle_container.add<GUI::ComboBox>();
  47. starting_month_combo.set_only_allow_values_from_model(true);
  48. starting_month_combo.set_fixed_size(50, 20);
  49. starting_month_combo.set_model(MonthListModel::create());
  50. starting_month_combo.set_selected_index(m_date_time.month() - 1);
  51. auto& starting_day_combo = middle_container.add<GUI::SpinBox>();
  52. starting_day_combo.set_fixed_size(40, 20);
  53. starting_day_combo.set_range(1, m_date_time.days_in_month());
  54. starting_day_combo.set_value(m_date_time.day());
  55. auto& starting_year_combo = middle_container.add<GUI::SpinBox>();
  56. starting_year_combo.set_fixed_size(55, 20);
  57. starting_year_combo.set_range(0, 9999);
  58. starting_year_combo.set_value(m_date_time.year());
  59. auto& starting_hour_combo = time_container.add<GUI::SpinBox>();
  60. starting_hour_combo.set_fixed_size(50, 20);
  61. starting_hour_combo.set_range(1, 12);
  62. starting_hour_combo.set_value(12);
  63. auto& starting_minute_combo = time_container.add<GUI::SpinBox>();
  64. starting_minute_combo.set_fixed_size(40, 20);
  65. starting_minute_combo.set_range(0, 59);
  66. starting_minute_combo.set_value(0);
  67. auto& starting_meridiem_combo = time_container.add<GUI::ComboBox>();
  68. starting_meridiem_combo.set_only_allow_values_from_model(true);
  69. starting_meridiem_combo.set_fixed_size(55, 20);
  70. starting_meridiem_combo.set_model(MeridiemListModel::create());
  71. starting_meridiem_combo.set_selected_index(0);
  72. widget->add_spacer().release_value_but_fixme_should_propagate_errors();
  73. auto& button_container = widget->add<GUI::Widget>();
  74. button_container.set_fixed_height(20);
  75. button_container.set_layout<GUI::HorizontalBoxLayout>();
  76. button_container.add_spacer().release_value_but_fixme_should_propagate_errors();
  77. auto& ok_button = button_container.add<GUI::Button>("OK"_short_string);
  78. ok_button.set_fixed_size(80, 20);
  79. ok_button.on_click = [this](auto) {
  80. dbgln("TODO: Add event icon on specific tile");
  81. done(ExecResult::OK);
  82. };
  83. auto update_starting_day_range = [&starting_day_combo, &starting_year_combo, &starting_month_combo]() {
  84. auto year = starting_year_combo.value();
  85. auto month = starting_month_combo.selected_index();
  86. starting_day_combo.set_range(1, days_in_month(year, month + 1));
  87. };
  88. starting_year_combo.on_change = [update_starting_day_range](auto) { update_starting_day_range(); };
  89. starting_month_combo.on_change = [update_starting_day_range](auto, auto) { update_starting_day_range(); };
  90. event_title_textbox.set_focus(true);
  91. }
  92. int AddEventDialog::MonthListModel::row_count(const GUI::ModelIndex&) const
  93. {
  94. return 12;
  95. }
  96. int AddEventDialog::MeridiemListModel::row_count(const GUI::ModelIndex&) const
  97. {
  98. return 2;
  99. }
  100. DeprecatedString AddEventDialog::MonthListModel::column_name(int column) const
  101. {
  102. switch (column) {
  103. case Column::Month:
  104. return "Month";
  105. default:
  106. VERIFY_NOT_REACHED();
  107. }
  108. }
  109. DeprecatedString AddEventDialog::MeridiemListModel::column_name(int column) const
  110. {
  111. switch (column) {
  112. case Column::Meridiem:
  113. return "Meridiem";
  114. default:
  115. VERIFY_NOT_REACHED();
  116. }
  117. }
  118. GUI::Variant AddEventDialog::MonthListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  119. {
  120. constexpr Array short_month_names = {
  121. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  122. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  123. };
  124. auto& month = short_month_names[index.row()];
  125. if (role == GUI::ModelRole::Display) {
  126. switch (index.column()) {
  127. case Column::Month:
  128. return month;
  129. default:
  130. VERIFY_NOT_REACHED();
  131. }
  132. }
  133. return {};
  134. }
  135. GUI::Variant AddEventDialog::MeridiemListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  136. {
  137. constexpr Array meridiem_names = {
  138. "AM", "PM"
  139. };
  140. auto& meridiem = meridiem_names[index.row()];
  141. if (role == GUI::ModelRole::Display) {
  142. switch (index.column()) {
  143. case Column::Meridiem:
  144. return meridiem;
  145. default:
  146. VERIFY_NOT_REACHED();
  147. }
  148. }
  149. return {};
  150. }