From 39d2546dcd58bca1dbf55358eb6fc56bf35829b0 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 11 Apr 2012 03:43:08 +0000 Subject: [PATCH] Add a button to the preferences dialog to reset all options to the defaults Originally committed to SVN as r6690. --- aegisub/src/command/app.cpp | 2 +- aegisub/src/preferences.cpp | 32 +++++++++++++++++++++++++++++++- aegisub/src/preferences.h | 23 +++++++++++++++++++---- aegisub/src/preferences_base.cpp | 6 ++++++ aegisub/src/preferences_base.h | 8 +++++--- 5 files changed, 62 insertions(+), 9 deletions(-) diff --git a/aegisub/src/command/app.cpp b/aegisub/src/command/app.cpp index d858ac9e2..f5f257a11 100644 --- a/aegisub/src/command/app.cpp +++ b/aegisub/src/command/app.cpp @@ -238,7 +238,7 @@ struct app_options : public Command { void operator()(agi::Context *c) { try { - Preferences(c->parent).ShowModal(); + while (Preferences(c->parent).ShowModal() < 0); } catch (agi::Exception& e) { LOG_E("config/init") << "Caught exception: " << e.GetName() << " -> " << e.GetMessage(); } diff --git a/aegisub/src/preferences.cpp b/aegisub/src/preferences.cpp index 386e46abb..dc7cb5c0f 100644 --- a/aegisub/src/preferences.cpp +++ b/aegisub/src/preferences.cpp @@ -38,6 +38,7 @@ #endif #include +#include #include "preferences.h" @@ -614,6 +615,10 @@ void Preferences::AddPendingChange(Thunk const& callback) { applyButton->Enable(true); } +void Preferences::AddChangeableOption(std::string const& name) { + option_names.push_back(name); +} + void Preferences::OnOK(wxCommandEvent &event) { OnApply(event); EndModal(0); @@ -653,6 +658,25 @@ void Preferences::OnApply(wxCommandEvent &) { config::opt->Flush(); } +void Preferences::OnResetDefault(wxCommandEvent&) { + if (wxYES != wxMessageBox(_("Are you sure that you want to restore the defaults? All your settings will be overridden."), _("Restore defaults?"), wxYES_NO)) + return; + + for (std::deque::iterator it = option_names.begin(); it != option_names.end(); ++it) { + agi::OptionValue *opt = OPT_SET(*it); + if (!opt->IsDefault()) + opt->Reset(); + } + config::opt->Flush(); + + agi::hotkey::Hotkey def_hotkeys("", GET_DEFAULT_CONFIG(default_hotkey)); + hotkey::inst->SetHotkeyMap(def_hotkeys.GetHotkeyMap()); + + // Close and reopen the dialog to update all the controls with the new values + OPT_SET("Tool/Preferences/Page")->SetInt(book->GetSelection()); + EndModal(-1); +} + static void PageChanged(wxBookCtrlEvent& evt) { OPT_SET("Tool/Preferences/Page")->SetInt(evt.GetSelection()); } @@ -681,11 +705,16 @@ Preferences::Preferences(wxWindow *parent): wxDialog(parent, -1, _("Preferences" // Bottom Buttons wxStdDialogButtonSizer *stdButtonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL | wxAPPLY | wxHELP); applyButton = stdButtonSizer->GetApplyButton(); + wxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL); + wxButton *defaultButton = new wxButton(this, -1, _("&Restore Defaults")); + buttonSizer->Add(defaultButton, wxSizerFlags(0).Expand()); + buttonSizer->AddStretchSpacer(1); + buttonSizer->Add(stdButtonSizer, wxSizerFlags(0).Expand()); // Main Sizer wxSizer *mainSizer = new wxBoxSizer(wxVERTICAL); mainSizer->Add(book, wxSizerFlags(1).Expand().Border()); - mainSizer->Add(stdButtonSizer, wxSizerFlags(0).Expand().Border(wxALL & ~wxTOP)); + mainSizer->Add(buttonSizer, wxSizerFlags(0).Expand().Border(wxALL & ~wxTOP)); SetSizerAndFit(mainSizer); SetMinSize(wxSize(-1, 500)); @@ -697,6 +726,7 @@ Preferences::Preferences(wxWindow *parent): wxDialog(parent, -1, _("Preferences" Bind(wxEVT_COMMAND_BUTTON_CLICKED, &Preferences::OnOK, this, wxID_OK); Bind(wxEVT_COMMAND_BUTTON_CLICKED, &Preferences::OnApply, this, wxID_APPLY); Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::tr1::bind(&HelpButton::OpenPage, "Options"), wxID_HELP); + defaultButton->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &Preferences::OnResetDefault, this); } Preferences::~Preferences() { diff --git a/aegisub/src/preferences.h b/aegisub/src/preferences.h index 2c0d91ce6..851cdbf95 100644 --- a/aegisub/src/preferences.h +++ b/aegisub/src/preferences.h @@ -37,7 +37,7 @@ DEFINE_BASE_EXCEPTION_NOINNER(PreferencesError, agi::Exception) DEFINE_SIMPLE_EXCEPTION_NOINNER(PreferenceIncorrectType, PreferencesError, "preferences/incorrect_type") DEFINE_SIMPLE_EXCEPTION_NOINNER(PreferenceNotSupported, PreferencesError, "preferences/not_supported") -class Preferences: public wxDialog { +class Preferences : public wxDialog { public: typedef std::tr1::function Thunk; private: @@ -46,15 +46,30 @@ private: std::map pending_changes; std::deque pending_callbacks; + std::deque option_names; - void OnOK(wxCommandEvent &event); - void OnCancel(wxCommandEvent &event); - void OnApply(wxCommandEvent &event); + void OnOK(wxCommandEvent &); + void OnCancel(wxCommandEvent &); + void OnApply(wxCommandEvent &); + void OnResetDefault(wxCommandEvent&); public: Preferences(wxWindow *parent); ~Preferences(); + /// Add an option to be set when the OK or Apply button is clicked + /// @param new_value Clone of the option with the new value to copy over void SetOption(agi::OptionValue *new_value); + + /// All a function to call when the OK or Apply button is clicked + /// @param callback Function to call void AddPendingChange(Thunk const& callback); + + /// Add an option which can be changed via the dialog + /// @param name Name of the option + /// + /// This is used for resetting options to the defaults. We don't want to + /// simply revert to the default config file as a bunch of things other than + /// user options are stored in it. Perhaps that should change in the future. + void AddChangeableOption(std::string const& name); }; diff --git a/aegisub/src/preferences_base.cpp b/aegisub/src/preferences_base.cpp index 247c81fb5..227955dae 100644 --- a/aegisub/src/preferences_base.cpp +++ b/aegisub/src/preferences_base.cpp @@ -131,6 +131,7 @@ void OptionPage::CellSkip(wxFlexGridSizer *flex) { } wxControl *OptionPage::OptionAdd(wxFlexGridSizer *flex, const wxString &name, const char *opt_name, double min, double max, double inc) { + parent->AddChangeableOption(opt_name); const agi::OptionValue *opt = OPT_GET(opt_name); switch (opt->GetType()) { @@ -176,6 +177,7 @@ wxControl *OptionPage::OptionAdd(wxFlexGridSizer *flex, const wxString &name, co } void OptionPage::OptionChoice(wxFlexGridSizer *flex, const wxString &name, const wxArrayString &choices, const char *opt_name) { + parent->AddChangeableOption(opt_name); const agi::OptionValue *opt = OPT_GET(opt_name); wxComboBox *cb = new wxComboBox(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, choices, wxCB_READONLY | wxCB_DROPDOWN); @@ -214,6 +216,7 @@ wxFlexGridSizer* OptionPage::PageSizer(wxString name) { } void OptionPage::OptionBrowse(wxFlexGridSizer *flex, const wxString &name, const char *opt_name, wxControl *enabler, bool do_enable) { + parent->AddChangeableOption(opt_name); const agi::OptionValue *opt = OPT_GET(opt_name); if (opt->GetType() != agi::OptionValue::Type_String) @@ -247,6 +250,9 @@ void OptionPage::OptionFont(wxSizer *sizer, std::string opt_prefix) { const agi::OptionValue *face_opt = OPT_GET(opt_prefix + "Font Face"); const agi::OptionValue *size_opt = OPT_GET(opt_prefix + "Font Size"); + parent->AddChangeableOption(face_opt->GetName()); + parent->AddChangeableOption(size_opt->GetName()); + wxTextCtrl *font_name = new wxTextCtrl(this, -1, face_opt->GetString()); font_name->Bind(wxEVT_COMMAND_TEXT_UPDATED, StringUpdater(face_opt->GetName().c_str(), parent)); diff --git a/aegisub/src/preferences_base.h b/aegisub/src/preferences_base.h index 313e38add..1e9de73f3 100644 --- a/aegisub/src/preferences_base.h +++ b/aegisub/src/preferences_base.h @@ -21,10 +21,10 @@ class Preferences; -class OptionPage: public wxScrolled { +class OptionPage : public wxScrolled { template void Add(wxSizer *sizer, wxString const& label, T *control); -public: +protected: enum Style { PAGE_DEFAULT = 0x00000000, PAGE_SCROLL = 0x00000001, @@ -34,7 +34,6 @@ public: wxSizer *sizer; Preferences *parent; - OptionPage(wxTreebook *book, Preferences *parent, wxString name, int style = PAGE_DEFAULT); wxFlexGridSizer* PageSizer(wxString name); @@ -43,9 +42,12 @@ public: void OptionChoice(wxFlexGridSizer *flex, const wxString &name, const wxArrayString &choices, const char *opt_name); void OptionBrowse(wxFlexGridSizer *flex, const wxString &name, const char *opt_name, wxControl *enabler = 0, bool do_enable = false); void OptionFont(wxSizer *sizer, std::string opt_prefix); +public: /// Enable ctrl only when cbx is checked void EnableIfChecked(wxControl *cbx, wxControl *ctrl); /// Enable ctrl only when cbx is not checked void DisableIfChecked(wxControl *cbx, wxControl *ctrl); + + OptionPage(wxTreebook *book, Preferences *parent, wxString name, int style = PAGE_DEFAULT); };