From 4d6188e0d19c050023eec8f049278bb4899b6fdc Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Thu, 28 Jun 2018 22:46:22 -0500 Subject: [PATCH] Moved implementation of most UI_Choice methods into its own file. --- src/CMakeLists.txt | 1 + src/GUI/OptionsGroup/Field.hpp | 43 +++++------------------------- src/GUI/OptionsGroup/UI_Choice.cpp | 43 ++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 src/GUI/OptionsGroup/UI_Choice.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 83e68dfc1..e12546d51 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -260,6 +260,7 @@ IF(wxWidgets_FOUND) ${GUI_LIBDIR}/Settings.cpp ${GUI_LIBDIR}/misc_ui.cpp ${GUI_LIBDIR}/OptionsGroup/UI_NumChoice.cpp + ${GUI_LIBDIR}/OptionsGroup/UI_Choice.cpp ) target_compile_features(slic3r_gui PUBLIC cxx_std_14) #only build GUI lib if building with wx diff --git a/src/GUI/OptionsGroup/Field.hpp b/src/GUI/OptionsGroup/Field.hpp index fecedf940..0166f2d22 100644 --- a/src/GUI/OptionsGroup/Field.hpp +++ b/src/GUI/OptionsGroup/Field.hpp @@ -196,49 +196,18 @@ private: class UI_Choice : public UI_Window { public: - UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id = wxID_ANY) : UI_Window(parent, _opt) { - int style {0}; - style |= wxTE_PROCESS_ENTER; - if (opt.gui_type.size() > 0 && opt.gui_type.compare("select_open"s)) style |= wxCB_READONLY; - - /// Load the values - auto values {wxArrayString()}; - for (auto v : opt.enum_values) values.Add(wxString(v)); - - _choice = new wxComboBox(parent, id, - (opt.default_value != nullptr ? opt.default_value->getString() : ""), - wxDefaultPosition, _default_size(), values, style); - window = _choice; - - _choice->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); - _choice->Bind(wxEVT_TEXT_ENTER, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); - _choice->Bind(wxEVT_KILL_FOCUS, [this](wxFocusEvent& e) { if (this->on_kill_focus != nullptr) {this->on_kill_focus(""); this->_on_change("");} e.Skip(); }); - } - std::string get_string() override { - if (opt.enum_values.size() > 0) { - auto idx = this->_choice->GetSelection(); - if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx); - } - return this->_choice->GetValue().ToStdString(); - } + UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id = wxID_ANY); ~UI_Choice() { _choice->Destroy(); } + std::string get_string() override; - /// Returns a bare pointer to the underlying combobox, usually for test interface - wxComboBox* choice() { return this->_choice; } - - void set_value(boost::any value) override { - auto result {std::find(opt.enum_values.cbegin(), opt.enum_values.cend(), boost::any_cast(value))}; - if (result == opt.enum_values.cend()) { - this->_choice->SetValue(wxString(boost::any_cast(value))); - } else { - auto idx = std::distance(opt.enum_values.cbegin(), result); - this->_choice->SetSelection(idx); - } - } + void set_value(boost::any value) override; /// Function to call when the contents of this change. std::function on_change {nullptr}; + + /// Returns a bare pointer to the underlying combobox, usually for test interface + wxComboBox* choice() { return this->_choice; } protected: virtual std::string LogChannel() override { return "UI_Choice"s; } diff --git a/src/GUI/OptionsGroup/UI_Choice.cpp b/src/GUI/OptionsGroup/UI_Choice.cpp new file mode 100644 index 000000000..e6bf3a7cd --- /dev/null +++ b/src/GUI/OptionsGroup/UI_Choice.cpp @@ -0,0 +1,43 @@ +#include "OptionsGroup/Field.hpp" + +namespace Slic3r { namespace GUI { + +UI_Choice::UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id) : UI_Window(parent, _opt) { + int style {0}; + style |= wxTE_PROCESS_ENTER; + if (opt.gui_type.size() > 0 && opt.gui_type.compare("select_open"s)) style |= wxCB_READONLY; + + /// Load the values + auto values {wxArrayString()}; + for (auto v : opt.enum_values) values.Add(wxString(v)); + + _choice = new wxComboBox(parent, id, + (opt.default_value != nullptr ? opt.default_value->getString() : ""), + wxDefaultPosition, _default_size(), values, style); + window = _choice; + + _choice->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); + _choice->Bind(wxEVT_TEXT_ENTER, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); + _choice->Bind(wxEVT_KILL_FOCUS, [this](wxFocusEvent& e) { if (this->on_kill_focus != nullptr) {this->on_kill_focus(""); this->_on_change("");} e.Skip(); }); +} + +std::string UI_Choice::get_string() { + if (opt.enum_values.size() > 0) { + auto idx = this->_choice->GetSelection(); + if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx); + } + return this->_choice->GetValue().ToStdString(); +} + + +void UI_Choice::set_value(boost::any value) { + auto result {std::find(opt.enum_values.cbegin(), opt.enum_values.cend(), boost::any_cast(value))}; + if (result == opt.enum_values.cend()) { + this->_choice->SetValue(wxString(boost::any_cast(value))); + } else { + auto idx = std::distance(opt.enum_values.cbegin(), result); + this->_choice->SetSelection(idx); + } +} + +} } // Namespace Slic3r::GUI