Add a button to the preferences dialog to reset all options to the defaults

Originally committed to SVN as r6690.
This commit is contained in:
Thomas Goyne 2012-04-11 03:43:08 +00:00
parent 3ae96b59b9
commit 39d2546dcd
5 changed files with 62 additions and 9 deletions

View File

@ -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();
}

View File

@ -38,6 +38,7 @@
#endif
#include <libaegisub/exception.h>
#include <libaegisub/hotkey.h>
#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<std::string>::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() {

View File

@ -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<void ()> Thunk;
private:
@ -46,15 +46,30 @@ private:
std::map<std::string, agi::OptionValue*> pending_changes;
std::deque<Thunk> pending_callbacks;
std::deque<std::string> 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);
};

View File

@ -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));

View File

@ -21,10 +21,10 @@
class Preferences;
class OptionPage: public wxScrolled<wxPanel> {
class OptionPage : public wxScrolled<wxPanel> {
template<class T>
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);
};