Skip to content

Commit

Permalink
#5872: "Show skins" button is persisted in registry
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Matthew Mott committed Jan 25, 2022
1 parent a109bd7 commit 1c72c6e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 82 deletions.
136 changes: 62 additions & 74 deletions libs/registry/Widgets.h
Expand Up @@ -13,131 +13,119 @@
namespace registry
{

namespace detail
{

template <typename Value_T, typename Widget_T>
void setWidgetValueIfKeyExists(const std::string& key, Widget_T& widget)
{
if (GlobalRegistry().keyExists(key))
widget.SetValue(registry::getValue<Value_T>(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<double>(key));
}

spinCtrl->Bind(wxEVT_SPINCTRLDOUBLE, [=] (wxSpinDoubleEvent& ev)
{
registry::setValue(key, spinCtrl->GetValue());
ev.Skip();
});
detail::setWidgetValueIfKeyExists<double>(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<std::string>(key));
}

text->Bind(wxEVT_TEXT, [=] (wxCommandEvent& ev)
{
registry::setValue(key, text->GetValue().ToStdString());
ev.Skip();
});
detail::setWidgetValueIfKeyExists<std::string>(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<bool>(key));
}

checkbox->Bind(wxEVT_CHECKBOX, [=] (wxCommandEvent& ev)
{
registry::setValue(key, checkbox->GetValue() ? "1" : "0");
ev.Skip();
});
detail::setWidgetValueIfKeyExists<bool>(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<bool>(key));
}

toggleButton->Bind(wxEVT_TOGGLEBUTTON, [=](wxCommandEvent& ev)
{
registry::setValue(key, toggleButton->GetValue() ? "1" : "0");
ev.Skip();
});
detail::setWidgetValueIfKeyExists<bool>(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<void>& resetSignal)
{
// Set initial value then connect to changed signal
checkbox->SetValue(registry::getValue<std::string>(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<std::string>(key) == "1"); } });
}

inline void bindWidgetToBufferedKey(wxSlider* slider, const std::string& key,
inline void bindWidgetToBufferedKey(wxSlider* slider, const std::string& key,
Buffer& buffer, sigc::signal<void>& resetSignal, int factor)
{
// Set initial value then connect to changed signal
slider->SetValue(registry::getValue<float>(key) * factor);

slider->Bind(wxEVT_SCROLL_CHANGED, [=, &buffer] (wxScrollEvent& ev)
{
buffer.set(key, string::to_string(static_cast<float>(slider->GetValue()) / factor));
{
buffer.set(key, string::to_string(static_cast<float>(slider->GetValue()) / factor));
ev.Skip();
});
slider->Bind(wxEVT_SCROLL_THUMBTRACK, [=, &buffer] (wxScrollEvent& ev)
{
buffer.set(key, string::to_string(static_cast<float>(slider->GetValue()) / factor));
{
buffer.set(key, string::to_string(static_cast<float>(slider->GetValue()) / factor));
ev.Skip();
});

resetSignal.connect([=, &buffer]
{
if (buffer.keyExists(key))
{
slider->SetValue(registry::getValue<float>(key) * factor);
}
{
if (buffer.keyExists(key))
{
slider->SetValue(registry::getValue<float>(key) * factor);
}
});
}

inline void bindWidgetToBufferedKey(wxChoice* choice, const std::string& key,
inline void bindWidgetToBufferedKey(wxChoice* choice, const std::string& key,
Buffer& buffer, sigc::signal<void>& resetSignal, bool storeValueNotIndex)
{
// Set initial value then connect to changed signal
choice->Select(storeValueNotIndex ?
choice->Select(storeValueNotIndex ?
choice->FindString(registry::getValue<std::string>(key)):
registry::getValue<int>(key));

choice->Bind(wxEVT_CHOICE, [=, &buffer] (wxCommandEvent& ev)
{
{
if (storeValueNotIndex)
{
buffer.set(key, choice->GetStringSelection().ToStdString());
buffer.set(key, choice->GetStringSelection().ToStdString());
}
else
{
Expand All @@ -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<std::string>(key)):
registry::getValue<int>(key));
}
});
}

inline void bindWidgetToBufferedKey(wxTextCtrl* entry, const std::string& key,
inline void bindWidgetToBufferedKey(wxTextCtrl* entry, const std::string& key,
Buffer& buffer, sigc::signal<void>& resetSignal)
{
// Set initial value then connect to changed signal
Expand All @@ -168,21 +156,21 @@ inline void bindWidgetToBufferedKey(wxTextCtrl* entry, const std::string& key,
}

entry->Bind(wxEVT_TEXT, [=, &buffer] (wxCommandEvent& ev)
{
{
buffer.set(key, entry->GetValue().ToStdString());
ev.Skip();
});

resetSignal.connect([=, &buffer]
{
if (buffer.keyExists(key))
{
{
entry->SetValue(registry::getValue<std::string>(key));
}
});
}

inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key,
inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key,
Buffer& buffer, sigc::signal<void>& resetSignal)
{
// Set initial value then connect to changed signal
Expand All @@ -191,7 +179,7 @@ inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key
spinCtrl->SetValue(registry::getValue<int>(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();
Expand All @@ -200,13 +188,13 @@ inline void bindWidgetToBufferedKey(wxSpinCtrl* spinCtrl, const std::string& key
resetSignal.connect([=, &buffer]
{
if (buffer.keyExists(key))
{
{
spinCtrl->SetValue(registry::getValue<int>(key));
}
});
}

inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::string& key,
inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::string& key,
Buffer& buffer, sigc::signal<void>& resetSignal)
{
// Set initial value then connect to changed signal
Expand All @@ -215,7 +203,7 @@ inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::strin
spinCtrl->SetValue(registry::getValue<double>(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();
Expand All @@ -224,7 +212,7 @@ inline void bindWidgetToBufferedKey(wxSpinCtrlDouble* spinCtrl, const std::strin
resetSignal.connect([=, &buffer]
{
if (buffer.keyExists(key))
{
{
spinCtrl->SetValue(registry::getValue<double>(key));
}
});
Expand Down
27 changes: 19 additions & 8 deletions radiant/ui/modelselector/ModelSelector.cpp
Expand Up @@ -28,6 +28,7 @@
#include "wxutil/dataview/ResourceTreeViewToolbar.h"
#include "wxutil/Bitmap.h"
#include "ui/UserInterfaceModule.h"
#include "registry/Widgets.h"

#include <functional>

Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions radiant/ui/modelselector/ModelSelector.h
Expand Up @@ -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;
Expand Down

0 comments on commit 1c72c6e

Please sign in to comment.