DatePicker.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Button.h>
  7. #include <LibGUI/ComboBox.h>
  8. #include <LibGUI/DatePicker.h>
  9. #include <LibGUI/DatePickerDialogGML.h>
  10. #include <LibGUI/ImageWidget.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/SpinBox.h>
  13. #include <LibGUI/TextBox.h>
  14. namespace GUI {
  15. DatePicker::DatePicker(Window* parent_window, String const& title, Core::DateTime focused_date)
  16. : Dialog(parent_window)
  17. , m_selected_date(focused_date)
  18. {
  19. if (parent_window)
  20. set_icon(parent_window->icon());
  21. set_resizable(false);
  22. set_title(title.to_byte_string());
  23. auto widget = set_main_widget<Widget>();
  24. widget->load_from_gml(date_picker_dialog_gml).release_value_but_fixme_should_propagate_errors();
  25. auto& calendar = *widget->find_descendant_of_type_named<GUI::Calendar>("calendar_view");
  26. calendar.on_tile_click = [&]() {
  27. m_selected_date = calendar.selected_date();
  28. m_month_box->set_selected_index(m_selected_date.month() - 1, AllowCallback::No);
  29. m_year_box->set_value(static_cast<int>(m_selected_date.year()), AllowCallback::No);
  30. };
  31. calendar.on_tile_doubleclick = [&]() {
  32. m_selected_date = calendar.selected_date();
  33. done(ExecResult::OK);
  34. };
  35. calendar.set_selected_date(focused_date);
  36. calendar.update_tiles(focused_date.year(), focused_date.month());
  37. m_month_box = widget->find_descendant_of_type_named<GUI::ComboBox>("month_box");
  38. m_month_box->set_model(GUI::MonthListModel::create(GUI::MonthListModel::DisplayMode::Long));
  39. m_month_box->set_selected_index(focused_date.month() - 1, AllowCallback::No);
  40. m_month_box->on_change = [&](ByteString const&, ModelIndex const& index) {
  41. m_selected_date.set_time(static_cast<int>(m_selected_date.year()), index.row() + 1);
  42. calendar.set_selected_date(m_selected_date);
  43. calendar.update_tiles(m_selected_date.year(), m_selected_date.month());
  44. calendar.update();
  45. };
  46. m_year_box = widget->find_descendant_of_type_named<GUI::SpinBox>("year_box");
  47. m_year_box->set_value(static_cast<int>(focused_date.year()), AllowCallback::No);
  48. m_year_box->on_change = [&](int year) {
  49. m_selected_date.set_time(year, static_cast<int>(m_selected_date.month()));
  50. calendar.set_selected_date(m_selected_date);
  51. calendar.update_tiles(m_selected_date.year(), m_selected_date.month());
  52. calendar.update();
  53. };
  54. auto& ok_button = *widget->find_descendant_of_type_named<GUI::Button>("ok_button");
  55. ok_button.on_click = [&](auto) {
  56. dbgln("GUI::DatePicker: OK button clicked");
  57. m_selected_date = calendar.selected_date();
  58. done(ExecResult::OK);
  59. };
  60. auto& cancel_button = *widget->find_descendant_of_type_named<GUI::Button>("cancel_button");
  61. cancel_button.on_click = [this](auto) {
  62. dbgln("GUI::DatePicker: Cancel button clicked");
  63. done(ExecResult::Cancel);
  64. };
  65. }
  66. Optional<Core::DateTime> DatePicker::show(Window* parent_window, String title, Core::DateTime focused_date)
  67. {
  68. auto box = DatePicker::construct(parent_window, title, focused_date);
  69. if (box->exec() == ExecResult::OK) {
  70. return box->m_selected_date;
  71. }
  72. return {};
  73. }
  74. }