From 1c72c6e908b027c0a1a5d2f1442c72a551672da3 Mon Sep 17 00:00:00 2001 From: Matthew Mott Date: Tue, 25 Jan 2022 21:21:15 +0000 Subject: [PATCH] #5872: "Show skins" button is persisted in registry Use registry::bindWidget() to persist the toggle button value for the next Model Selector invocation. The button is now also hidden when the Model Selector is shown in models-only mode, to avoid confusion (the button would have no effect in this case). --- libs/registry/Widgets.h | 136 ++++++++++----------- radiant/ui/modelselector/ModelSelector.cpp | 27 ++-- radiant/ui/modelselector/ModelSelector.h | 1 + 3 files changed, 82 insertions(+), 82 deletions(-) diff --git a/libs/registry/Widgets.h b/libs/registry/Widgets.h index b5660e345c..6a3082e37b 100644 --- a/libs/registry/Widgets.h +++ b/libs/registry/Widgets.h @@ -13,131 +13,119 @@ namespace registry { +namespace detail +{ + +template +void setWidgetValueIfKeyExists(const std::string& key, Widget_T& widget) +{ + if (GlobalRegistry().keyExists(key)) + widget.SetValue(registry::getValue(key)); +} + +} // namespace detail + /** * Various bind() overloads to let widgets export their values to the registry * as soon as they fire their changes signal. The value * will be initialised with the one currently present in the registry. * - * Note: due to the use of lambdas it's not possible to disconnect - * the widget's after calling bind(). The widget will keep writing + * Note: due to the use of lambdas it's not possible to disconnect + * the widget's after calling bind(). The widget will keep writing * its value to the registry, unless it's destroyed. */ inline void bindWidget(wxSpinCtrlDouble* spinCtrl, const std::string& key) { - // Set initial value then connect to changed signal - if (GlobalRegistry().keyExists(key)) - { - spinCtrl->SetValue(registry::getValue(key)); - } - - spinCtrl->Bind(wxEVT_SPINCTRLDOUBLE, [=] (wxSpinDoubleEvent& ev) - { - registry::setValue(key, spinCtrl->GetValue()); - ev.Skip(); - }); + detail::setWidgetValueIfKeyExists(key, *spinCtrl); + spinCtrl->Bind(wxEVT_SPINCTRLDOUBLE, [=](wxSpinDoubleEvent& ev) { + registry::setValue(key, spinCtrl->GetValue()); + ev.Skip(); + }); } inline void bindWidget(wxTextCtrl* text, const std::string& key) { - // Set initial value then connect to changed signal - if (GlobalRegistry().keyExists(key)) - { - text->SetValue(registry::getValue(key)); - } - - text->Bind(wxEVT_TEXT, [=] (wxCommandEvent& ev) - { - registry::setValue(key, text->GetValue().ToStdString()); - ev.Skip(); - }); + detail::setWidgetValueIfKeyExists(key, *text); + text->Bind(wxEVT_TEXT, [=](wxCommandEvent& ev) { + registry::setValue(key, text->GetValue().ToStdString()); + ev.Skip(); + }); } inline void bindWidget(wxCheckBox* checkbox, const std::string& key) { - // Set initial value then connect to changed signal - if (GlobalRegistry().keyExists(key)) - { - checkbox->SetValue(registry::getValue(key)); - } - - checkbox->Bind(wxEVT_CHECKBOX, [=] (wxCommandEvent& ev) - { - registry::setValue(key, checkbox->GetValue() ? "1" : "0"); - ev.Skip(); - }); + detail::setWidgetValueIfKeyExists(key, *checkbox); + checkbox->Bind(wxEVT_CHECKBOX, [=](wxCommandEvent& ev) { + registry::setValue(key, checkbox->GetValue() ? "1" : "0"); + ev.Skip(); + }); } inline void bindWidget(wxToggleButton* toggleButton, const std::string& key) { - // Set initial value then connect to changed signal - if (GlobalRegistry().keyExists(key)) - { - toggleButton->SetValue(registry::getValue(key)); - } - - toggleButton->Bind(wxEVT_TOGGLEBUTTON, [=](wxCommandEvent& ev) - { - registry::setValue(key, toggleButton->GetValue() ? "1" : "0"); - ev.Skip(); - }); + detail::setWidgetValueIfKeyExists(key, *toggleButton); + toggleButton->Bind(wxEVT_TOGGLEBUTTON, [=](wxCommandEvent& ev) { + registry::setValue(key, toggleButton->GetValue() ? "1" : "0"); + ev.Skip(); + }); } // ------------- Variants supporting registry::Buffer --------------------- -inline void bindWidgetToBufferedKey(wxCheckBox* checkbox, const std::string& key, +inline void bindWidgetToBufferedKey(wxCheckBox* checkbox, const std::string& key, Buffer& buffer, sigc::signal& resetSignal) { // Set initial value then connect to changed signal checkbox->SetValue(registry::getValue(key) == "1"); checkbox->Bind(wxEVT_CHECKBOX, [=, &buffer] (wxCommandEvent& ev) - { - buffer.set(key, checkbox->GetValue() ? "1" : "0"); + { + buffer.set(key, checkbox->GetValue() ? "1" : "0"); ev.Skip(); }); resetSignal.connect([=, &buffer] { if (buffer.keyExists(key)) { checkbox->SetValue(registry::getValue(key) == "1"); } }); } -inline void bindWidgetToBufferedKey(wxSlider* slider, const std::string& key, +inline void bindWidgetToBufferedKey(wxSlider* slider, const std::string& key, Buffer& buffer, sigc::signal& resetSignal, int factor) { // Set initial value then connect to changed signal slider->SetValue(registry::getValue(key) * factor); slider->Bind(wxEVT_SCROLL_CHANGED, [=, &buffer] (wxScrollEvent& ev) - { - buffer.set(key, string::to_string(static_cast(slider->GetValue()) / factor)); + { + buffer.set(key, string::to_string(static_cast(slider->GetValue()) / factor)); ev.Skip(); }); slider->Bind(wxEVT_SCROLL_THUMBTRACK, [=, &buffer] (wxScrollEvent& ev) - { - buffer.set(key, string::to_string(static_cast(slider->GetValue()) / factor)); + { + buffer.set(key, string::to_string(static_cast(slider->GetValue()) / factor)); ev.Skip(); }); resetSignal.connect([=, &buffer] - { - if (buffer.keyExists(key)) - { - slider->SetValue(registry::getValue(key) * factor); - } + { + if (buffer.keyExists(key)) + { + slider->SetValue(registry::getValue(key) * factor); + } }); } -inline void bindWidgetToBufferedKey(wxChoice* choice, const std::string& key, +inline void bindWidgetToBufferedKey(wxChoice* choice, const std::string& key, Buffer& buffer, sigc::signal& resetSignal, bool storeValueNotIndex) { // Set initial value then connect to changed signal - choice->Select(storeValueNotIndex ? + choice->Select(storeValueNotIndex ? choice->FindString(registry::getValue(key)): registry::getValue(key)); choice->Bind(wxEVT_CHOICE, [=, &buffer] (wxCommandEvent& ev) - { + { if (storeValueNotIndex) { - buffer.set(key, choice->GetStringSelection().ToStdString()); + buffer.set(key, choice->GetStringSelection().ToStdString()); } else { @@ -150,15 +138,15 @@ inline void bindWidgetToBufferedKey(wxChoice* choice, const std::string& key, resetSignal.connect([=, &buffer] { if (buffer.keyExists(key)) - { - choice->Select(storeValueNotIndex ? + { + choice->Select(storeValueNotIndex ? choice->FindString(registry::getValue(key)): registry::getValue(key)); } }); } -inline void bindWidgetToBufferedKey(wxTextCtrl* entry, const std::string& key, +inline void bindWidgetToBufferedKey(wxTextCtrl* entry, const std::string& key, Buffer& buffer, sigc::signal& resetSignal) { // Set initial value then connect to changed signal @@ -168,7 +156,7 @@ inline void bindWidgetToBufferedKey(wxTextCtrl* entry, const std::string& key, } entry->Bind(wxEVT_TEXT, [=, &buffer] (wxCommandEvent& ev) - { + { buffer.set(key, entry->GetValue().ToStdString()); ev.Skip(); }); @@ -176,13 +164,13 @@ inline void bindWidgetToBufferedKey(wxTextCtrl* entry, const std::string& key, resetSignal.connect([=, &buffer] { if (buffer.keyExists(key)) - { + { entry->SetValue(registry::getValue(key)); } }); } -inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key, +inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key, Buffer& buffer, sigc::signal& resetSignal) { // Set initial value then connect to changed signal @@ -191,7 +179,7 @@ inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key spinCtrl->SetValue(registry::getValue(key)); } - spinCtrl->Bind(wxEVT_SPINCTRL, [=, &buffer] (wxSpinEvent& ev) + spinCtrl->Bind(wxEVT_SPINCTRL, [=, &buffer] (wxSpinEvent& ev) { buffer.set(key, string::to_string(spinCtrl->GetValue())); ev.Skip(); @@ -200,13 +188,13 @@ inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key resetSignal.connect([=, &buffer] { if (buffer.keyExists(key)) - { + { spinCtrl->SetValue(registry::getValue(key)); } }); } -inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::string& key, +inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::string& key, Buffer& buffer, sigc::signal& resetSignal) { // Set initial value then connect to changed signal @@ -215,7 +203,7 @@ inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::strin spinCtrl->SetValue(registry::getValue(key)); } - spinCtrl->Bind(wxEVT_SPINCTRLDOUBLE, [=, &buffer] (wxSpinDoubleEvent& ev) + spinCtrl->Bind(wxEVT_SPINCTRLDOUBLE, [=, &buffer] (wxSpinDoubleEvent& ev) { buffer.set(key, string::to_string(spinCtrl->GetValue())); ev.Skip(); @@ -224,7 +212,7 @@ inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::strin resetSignal.connect([=, &buffer] { if (buffer.keyExists(key)) - { + { spinCtrl->SetValue(registry::getValue(key)); } }); diff --git a/radiant/ui/modelselector/ModelSelector.cpp b/radiant/ui/modelselector/ModelSelector.cpp index c73cfc43fb..1a775bdf28 100644 --- a/radiant/ui/modelselector/ModelSelector.cpp +++ b/radiant/ui/modelselector/ModelSelector.cpp @@ -28,6 +28,7 @@ #include "wxutil/dataview/ResourceTreeViewToolbar.h" #include "wxutil/Bitmap.h" #include "ui/UserInterfaceModule.h" +#include "registry/Widgets.h" #include @@ -41,6 +42,7 @@ namespace const std::string RKEY_BASE = "user/ui/modelSelector/"; const std::string RKEY_SPLIT_POS = RKEY_BASE + "splitPos"; + const std::string RKEY_SHOW_SKINS = RKEY_BASE + "showSkinsInTree"; } // Constructor. @@ -185,9 +187,17 @@ ModelSelectorResult ModelSelector::showAndBlock(const std::string& curModel, bool showOptions, bool showSkins) { - _treeView->SetShowSkins(showSkins); - _treeView->SetSelectedFullname(curModel); + // Hide the Show Skins button if skins should not be shown for this invocation + if (showSkins) { + _showSkinsBtn->Show(); + _treeView->SetShowSkins(_showSkinsBtn->GetValue()); + } + else { + _showSkinsBtn->Hide(); + _treeView->SetShowSkins(false); + } + _treeView->SetSelectedFullname(curModel); showInfoForSelectedModel(); _showOptions = showOptions; @@ -263,13 +273,14 @@ wxWindow* ModelSelector::setupTreeViewToolbar(wxWindow* parent) // Set up the top treeview toolbar, including a custom button to enable/disable the showing of // skins in the tree. auto* toolbar = new wxutil::ResourceTreeViewToolbar(parent, _treeView); - auto* showSkinsBtn = new wxBitmapToggleButton(toolbar, wxID_ANY, - wxutil::GetLocalBitmap("skin16.png")); - showSkinsBtn->SetValue(true); - showSkinsBtn->SetToolTip(_("List model skins in the tree underneath their associated models")); - showSkinsBtn->Bind(wxEVT_TOGGLEBUTTON, + _showSkinsBtn = new wxBitmapToggleButton(toolbar, wxID_ANY, + wxutil::GetLocalBitmap("skin16.png")); + _showSkinsBtn->SetValue(true); + _showSkinsBtn->SetToolTip(_("List model skins in the tree underneath their associated models")); + _showSkinsBtn->Bind(wxEVT_TOGGLEBUTTON, [this](auto& ev) { _treeView->SetShowSkins(ev.IsChecked()); }); - toolbar->GetRightSizer()->Add(showSkinsBtn, wxSizerFlags().Border(wxLEFT, 6)); + registry::bindWidget(_showSkinsBtn, RKEY_SHOW_SKINS); + toolbar->GetRightSizer()->Add(_showSkinsBtn, wxSizerFlags().Border(wxLEFT, 6)); return toolbar; } diff --git a/radiant/ui/modelselector/ModelSelector.h b/radiant/ui/modelselector/ModelSelector.h index 55a48d52e6..b29adca694 100644 --- a/radiant/ui/modelselector/ModelSelector.h +++ b/radiant/ui/modelselector/ModelSelector.h @@ -51,6 +51,7 @@ class ModelSelector: public wxutil::DialogBase, private wxutil::XmlResourceBased // Main tree view with model hierarchy ModelTreeView* _treeView; + wxToggleButton* _showSkinsBtn = nullptr; // Key/value table for model information wxutil::KeyValueTable* _infoTable;