Calendar: Use GML for AddEventDialog

This commit is contained in:
lanmonster 2023-10-06 14:24:37 -07:00 committed by Andreas Kling
parent 1a6affc64b
commit 97cc5b2da4
Notes: sideshowbarker 2024-07-17 08:37:36 +09:00
3 changed files with 134 additions and 77 deletions

View file

@ -6,6 +6,7 @@
*/
#include "AddEventDialog.h"
#include <Applications/Calendar/AddEventDialogGML.h>
#include <LibCore/DateTime.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
@ -34,103 +35,61 @@ AddEventDialog::AddEventDialog(Core::DateTime date_time, EventManager& event_man
m_date_time = Core::DateTime::create(m_date_time.year(), m_date_time.month(), m_date_time.day(), 12, 0);
auto widget = set_main_widget<GUI::Widget>();
widget->set_fill_with_background_color(true);
widget->set_layout<GUI::VerticalBoxLayout>();
widget->load_from_gml(add_event_dialog_gml).release_value_but_fixme_should_propagate_errors();
auto& top_container = widget->add<GUI::Widget>();
top_container.set_layout<GUI::VerticalBoxLayout>(4);
top_container.set_fixed_height(45);
auto event_title_textbox = widget->find_descendant_of_type_named<GUI::TextBox>("event_title_textbox");
event_title_textbox->set_focus(true);
auto& add_label = top_container.add<GUI::Label>("Add title & date:"_string);
add_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
add_label.set_fixed_height(14);
add_label.set_font(Gfx::FontDatabase::default_font().bold_variant());
auto starting_month_combo = widget->find_descendant_of_type_named<GUI::ComboBox>("start_month");
starting_month_combo->set_model(MonthListModel::create());
starting_month_combo->set_selected_index(m_date_time.month() - 1);
auto& event_title_textbox = top_container.add<GUI::TextBox>();
event_title_textbox.set_name("event_title_textbox");
event_title_textbox.set_fixed_height(20);
auto starting_day_combo = widget->find_descendant_of_type_named<GUI::SpinBox>("start_day");
starting_day_combo->set_value(m_date_time.day());
auto& middle_container = widget->add<GUI::Widget>();
middle_container.set_layout<GUI::HorizontalBoxLayout>(4);
middle_container.set_fixed_height(25);
auto starting_year_combo = widget->find_descendant_of_type_named<GUI::SpinBox>("start_year");
starting_year_combo->set_value(m_date_time.year());
auto& time_container = widget->add<GUI::Widget>();
time_container.set_layout<GUI::HorizontalBoxLayout>(4);
time_container.set_fixed_height(25);
auto starting_hour_combo = widget->find_descendant_of_type_named<GUI::SpinBox>("start_hour");
starting_hour_combo->set_value(m_date_time.hour());
auto& starting_month_combo = middle_container.add<GUI::ComboBox>();
starting_month_combo.set_only_allow_values_from_model(true);
starting_month_combo.set_fixed_size(50, 20);
starting_month_combo.set_model(MonthListModel::create());
starting_month_combo.set_selected_index(m_date_time.month() - 1);
auto starting_minute_combo = widget->find_descendant_of_type_named<GUI::SpinBox>("start_minute");
starting_minute_combo->set_value(m_date_time.minute());
auto& starting_day_combo = middle_container.add<GUI::SpinBox>();
starting_day_combo.set_fixed_size(40, 20);
starting_day_combo.set_range(1, m_date_time.days_in_month());
starting_day_combo.set_value(m_date_time.day());
auto starting_meridiem_combo = widget->find_descendant_of_type_named<GUI::ComboBox>("start_meridiem");
starting_meridiem_combo->set_model(MeridiemListModel::create());
starting_meridiem_combo->set_selected_index(0);
auto& starting_year_combo = middle_container.add<GUI::SpinBox>();
starting_year_combo.set_fixed_size(55, 20);
starting_year_combo.set_range(0, 9999);
starting_year_combo.set_value(m_date_time.year());
auto& starting_hour_combo = time_container.add<GUI::SpinBox>();
starting_hour_combo.set_fixed_size(50, 20);
starting_hour_combo.set_range(1, 12);
starting_hour_combo.set_value(m_date_time.hour());
auto& starting_minute_combo = time_container.add<GUI::SpinBox>();
starting_minute_combo.set_fixed_size(40, 20);
starting_minute_combo.set_range(0, 59);
starting_minute_combo.set_value(m_date_time.minute());
auto& starting_meridiem_combo = time_container.add<GUI::ComboBox>();
starting_meridiem_combo.set_only_allow_values_from_model(true);
starting_meridiem_combo.set_fixed_size(55, 20);
starting_meridiem_combo.set_model(MeridiemListModel::create());
starting_meridiem_combo.set_selected_index(0);
widget->add_spacer();
auto& button_container = widget->add<GUI::Widget>();
button_container.set_fixed_height(20);
button_container.set_layout<GUI::HorizontalBoxLayout>();
button_container.add_spacer();
auto& ok_button = button_container.add<GUI::Button>("OK"_string);
ok_button.set_fixed_size(80, 20);
ok_button.on_click = [&](auto) {
auto ok_button = widget->find_descendant_of_type_named<GUI::Button>("ok_button");
ok_button->on_click = [&](auto) {
add_event_to_calendar().release_value_but_fixme_should_propagate_errors();
done(ExecResult::OK);
};
auto update_starting_day_range = [&starting_day_combo, &starting_year_combo, &starting_month_combo]() {
auto year = starting_year_combo.value();
auto month = starting_month_combo.selected_index();
auto year = starting_year_combo->value();
auto month = starting_month_combo->selected_index();
starting_day_combo.set_range(1, days_in_month(year, month + 1));
starting_day_combo->set_range(1, days_in_month(year, month + 1));
};
starting_year_combo.on_change = [update_starting_day_range](auto) { update_starting_day_range(); };
starting_month_combo.on_change = [update_starting_day_range](auto, auto) { update_starting_day_range(); };
starting_year_combo->on_change = [update_starting_day_range](auto) { update_starting_day_range(); };
starting_month_combo->on_change = [update_starting_day_range](auto, auto) { update_starting_day_range(); };
auto update_combo_values = [&]() {
auto year = starting_year_combo.value();
auto month = starting_month_combo.selected_index() + 1;
auto day = starting_day_combo.value();
auto hour = starting_hour_combo.value();
auto minute = starting_minute_combo.value();
auto year = starting_year_combo->value();
auto month = starting_month_combo->selected_index() + 1;
auto day = starting_day_combo->value();
auto hour = starting_hour_combo->value();
auto minute = starting_minute_combo->value();
m_date_time = Core::DateTime::create(year, month, day, hour, minute);
};
starting_year_combo.on_change = [update_combo_values](auto) { update_combo_values(); };
starting_month_combo.on_change = [update_combo_values](auto, auto) { update_combo_values(); };
starting_day_combo.on_change = [update_combo_values](auto) { update_combo_values(); };
starting_hour_combo.on_change = [update_combo_values](auto) { update_combo_values(); };
starting_minute_combo.on_change = [update_combo_values](auto) { update_combo_values(); };
event_title_textbox.set_focus(true);
starting_year_combo->on_change = [update_combo_values](auto) { update_combo_values(); };
starting_month_combo->on_change = [update_combo_values](auto, auto) { update_combo_values(); };
starting_day_combo->on_change = [update_combo_values](auto) { update_combo_values(); };
starting_hour_combo->on_change = [update_combo_values](auto) { update_combo_values(); };
starting_minute_combo->on_change = [update_combo_values](auto) { update_combo_values(); };
}
ErrorOr<void> AddEventDialog::add_event_to_calendar()

View file

@ -0,0 +1,97 @@
@GUI::Widget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [4]
}
@GUI::Widget {
fill_with_background_color: true
fixed_height: 45
layout: @GUI::VerticalBoxLayout {
spacing: 4
}
@GUI::Label {
text: "Add title & date:"
text_alignment: "CenterLeft"
fixed_height: 14
font_weight: "Bold"
}
@GUI::TextBox {
name: "event_title_textbox"
fixed_height: 20
}
}
@GUI::Widget {
fill_with_background_color: true
fixed_height: 25
layout: @GUI::HorizontalBoxLayout {
spacing: 4
}
@GUI::ComboBox {
name: "start_month"
model_only: true
fixed_size: [50, 20]
}
@GUI::SpinBox {
name: "start_day"
fixed_size: [40, 20]
min: 1
}
@GUI::SpinBox {
name: "start_year"
fixed_size: [55, 20]
min: 0
max: 9999
}
}
@GUI::Widget {
fill_with_background_color: true
fixed_height: 25
layout: @GUI::HorizontalBoxLayout {
spacing: 4
}
@GUI::SpinBox {
name: "start_hour"
fixed_size: [50, 20]
min: 1
max: 12
}
@GUI::SpinBox {
name: "start_minute"
fixed_size: [40, 20]
min: 1
max: 59
}
@GUI::ComboBox {
name: "start_meridiem"
model_only: true
fixed_size: [55, 20]
}
}
@GUI::Layout::Spacer {}
@GUI::Widget {
fill_with_background_color: true
fixed_height: 20
layout: @GUI::HorizontalBoxLayout {}
@GUI::Layout::Spacer {}
@GUI::Button {
name: "ok_button"
text: "OK"
fixed_size: [80, 20]
}
}
}

View file

@ -4,6 +4,7 @@ serenity_component(
)
stringify_gml(CalendarWindow.gml CalendarWindowGML.h calendar_window_gml)
stringify_gml(AddEventDialog.gml AddEventDialogGML.h add_event_dialog_gml)
set(SOURCES
AddEventDialog.cpp
@ -14,7 +15,7 @@ set(SOURCES
)
set(GENERATED_SOURCES
CalendarWindowGML.h
CalendarWindowGML.h AddEventDialogGML.h
)
serenity_app(Calendar ICON app-calendar)