AddEventDialog.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "AddEventDialog.h"
  27. #include <LibCore/DateTime.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/ComboBox.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGUI/Layout.h>
  33. #include <LibGUI/Painter.h>
  34. #include <LibGUI/SpinBox.h>
  35. #include <LibGUI/TextBox.h>
  36. #include <LibGUI/Widget.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibGfx/Color.h>
  39. #include <LibGfx/Font.h>
  40. AddEventDialog::AddEventDialog(RefPtr<Calendar> calendar, Core::DateTime date_time, Window* parent_window)
  41. : Dialog(parent_window)
  42. , m_calendar(calendar)
  43. , m_date_time(date_time)
  44. {
  45. resize(158, 100);
  46. set_title("Add Event");
  47. set_resizable(false);
  48. auto& widget = set_main_widget<GUI::Widget>();
  49. widget.set_fill_with_background_color(true);
  50. widget.set_layout<GUI::VerticalBoxLayout>();
  51. auto& top_container = widget.add<GUI::Widget>();
  52. top_container.set_layout<GUI::VerticalBoxLayout>();
  53. top_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  54. top_container.set_preferred_size(0, 45);
  55. top_container.layout()->set_margins({ 4, 4, 4, 4 });
  56. auto make_label = [&](const StringView& text, GUI::Widget& widget, bool bold = false) {
  57. auto& label = widget.add<GUI::Label>(text);
  58. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  59. label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  60. label.set_preferred_size(0, 14);
  61. if (bold)
  62. label.set_font(Gfx::Font::default_bold_font());
  63. };
  64. make_label("Add title & date:", top_container, true);
  65. auto& event_title_textbox = top_container.add<GUI::TextBox>();
  66. event_title_textbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  67. event_title_textbox.set_preferred_size(0, 20);
  68. auto& middle_container = widget.add<GUI::Widget>();
  69. middle_container.set_layout<GUI::HorizontalBoxLayout>();
  70. middle_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  71. middle_container.set_preferred_size(0, 25);
  72. middle_container.layout()->set_margins({ 4, 4, 4, 4 });
  73. auto& starting_month_combo = middle_container.add<GUI::ComboBox>();
  74. starting_month_combo.set_only_allow_values_from_model(true);
  75. starting_month_combo.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  76. starting_month_combo.set_preferred_size(50, 20);
  77. starting_month_combo.set_model(MonthListModel::create());
  78. starting_month_combo.set_selected_index(m_date_time.month() - 1);
  79. auto& starting_day_combo = middle_container.add<GUI::SpinBox>();
  80. starting_day_combo.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  81. starting_day_combo.set_preferred_size(40, 20);
  82. starting_day_combo.set_value(m_date_time.day());
  83. starting_day_combo.set_min(1);
  84. auto& starting_year_combo = middle_container.add<GUI::SpinBox>();
  85. starting_year_combo.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  86. starting_year_combo.set_preferred_size(55, 20);
  87. starting_year_combo.set_range(0, 9999);
  88. starting_year_combo.set_value(m_date_time.year());
  89. widget.layout()->add_spacer();
  90. auto& button_container = widget.add<GUI::Widget>();
  91. button_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  92. button_container.set_preferred_size(0, 20);
  93. button_container.set_layout<GUI::HorizontalBoxLayout>();
  94. button_container.layout()->add_spacer();
  95. auto& ok_button = button_container.add<GUI::Button>("OK");
  96. ok_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  97. ok_button.set_preferred_size(80, 20);
  98. ok_button.on_click = [this](auto) {
  99. dbg() << "TODO: Add event icon on specific tile";
  100. done(Dialog::ExecOK);
  101. };
  102. event_title_textbox.set_focus(true);
  103. }
  104. AddEventDialog::~AddEventDialog()
  105. {
  106. }
  107. AddEventDialog::MonthListModel::MonthListModel()
  108. {
  109. }
  110. AddEventDialog::MonthListModel::~MonthListModel()
  111. {
  112. }
  113. void AddEventDialog::MonthListModel::update()
  114. {
  115. }
  116. int AddEventDialog::MonthListModel::row_count(const GUI::ModelIndex&) const
  117. {
  118. return 12;
  119. }
  120. String AddEventDialog::MonthListModel::column_name(int column) const
  121. {
  122. switch (column) {
  123. case Column::Month:
  124. return "Month";
  125. default:
  126. ASSERT_NOT_REACHED();
  127. }
  128. }
  129. GUI::Variant AddEventDialog::MonthListModel::data(const GUI::ModelIndex& index, Role role) const
  130. {
  131. auto& month = Calendar::name_of_month(index.row() + 1);
  132. if (role == Role::Display) {
  133. switch (index.column()) {
  134. case Column::Month:
  135. return month;
  136. default:
  137. ASSERT_NOT_REACHED();
  138. }
  139. }
  140. return {};
  141. }