From 904601abbc7c97d3c4b9b8224708e51c51d860ec Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 25 Jul 2024 18:17:14 +0200 Subject: ui/tracker: implement simple saving mechanism --- ui/src/windows/tracker.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) (limited to 'ui/src') diff --git a/ui/src/windows/tracker.cpp b/ui/src/windows/tracker.cpp index 8add81f..5571aca 100644 --- a/ui/src/windows/tracker.cpp +++ b/ui/src/windows/tracker.cpp @@ -5,12 +5,19 @@ #include "turns/lang/messages.hpp" #include "turns/ui/windows/participant_editor.hpp" +#include #include #include #include +#include + +#include +#include + #include +#include #include #include @@ -43,6 +50,7 @@ namespace turns::ui::windows , m_adw{ADW_APPLICATION_WINDOW(gobj())} , m_controls{builder->get_widget("controls")} , m_empty(builder->get_widget("empty")) + , m_overlay{ADW_TOAST_OVERLAY(builder->get_widget("overlay")->gobj())} , m_stack{builder->get_widget("stack")} , m_start{builder->get_widget("start")} , m_title(ADW_WINDOW_TITLE(builder->get_widget("title")->gobj())) @@ -92,6 +100,48 @@ namespace turns::ui::windows dialog->present(this); } + auto tracker::handle_save() -> void + { + auto filters = Gio::ListStore::create(); + auto filter = Gtk::FileFilter::create(); + filter->set_name(_("Turns Files")); + filter->add_pattern("*.trns"); + filters->append(filter); + + auto dialog = Gtk::FileDialog::create(); + dialog->set_initial_name(m_file_name.value_or(_(lang::new_turn_order_file_name))); + dialog->set_filters(filters); + dialog->save(*this, sigc::bind(sigc::mem_fun(*this, &tracker::handle_save_finish), dialog)); + } + + auto tracker::handle_save_finish(Glib::RefPtr result, Glib::RefPtr dialog) -> void + try + { + auto file = dialog->save_finish(result); + file->replace_contents(m_turn_order->serialize().dump(2), m_file_tag, m_file_tag); + auto name = file->get_basename(); + auto message = std::vformat(_(lang::successfully_saved_format), std::make_format_args(name)); + auto toast = adw_toast_new(message.c_str()); + adw_toast_overlay_add_toast(m_overlay, toast); + } + catch (Gtk::DialogError const & e) + { + if (e.code() == Gtk::DialogError::FAILED) + { + auto error = e.what(); + auto message = std::vformat(_(lang::saving_failed_format), std::make_format_args(error)); + auto toast = adw_toast_new(message.c_str()); + adw_toast_overlay_add_toast(m_overlay, toast); + } + } + catch (Gio::Error const & e) + { + auto error = e.what(); + auto message = std::vformat(_(lang::saving_failed_format), std::make_format_args(error)); + auto toast = adw_toast_new(message.c_str()); + adw_toast_overlay_add_toast(m_overlay, toast); + } + auto tracker::handle_stop() -> void { auto dialog = ADW_ALERT_DIALOG(adw_alert_dialog_new("Stop turn order", "Do you want to clear the turn order?")); @@ -174,6 +224,16 @@ namespace turns::ui::windows add_action_with_parameter("delete", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &tracker::handle_delete_participant)); add_action_with_parameter("edit", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &tracker::handle_edit_participant)); } + + // win.save + // depends-on: turn_order:is_empty == false + { + auto action = add_action("save", sigc::mem_fun(*this, &tracker::handle_save)); + + Glib::Binding::bind_property(m_turn_order->is_empty(), + action->property_enabled(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + } } auto tracker::update_subtitle() -> void @@ -185,8 +245,8 @@ namespace turns::ui::windows else { auto round_number = m_turn_order->round_number() + 1; - m_subtitle = round_number == 0 ? "" : std::vformat(_("Round {}"), std::make_format_args(round_number)); + m_subtitle = round_number == 0 ? "" : std::vformat(_(lang::round_number), std::make_format_args(round_number)); } } -} // namespace turns::app::windows +} // namespace turns::ui::windows -- cgit v1.2.3