Use either wxCombobox or wxChoice depending on underlying config choice. (port UI change from master)

#4462
This commit is contained in:
Joseph Lenox 2018-11-10 10:02:51 -06:00 committed by Joseph Lenox
parent affa0d7553
commit 2dd94ee672
3 changed files with 46 additions and 21 deletions

View File

@ -15,9 +15,11 @@
#include "wx/checkbox.h" #include "wx/checkbox.h"
#include "wx/textctrl.h" #include "wx/textctrl.h"
#include "wx/combobox.h" #include "wx/combobox.h"
#include "wx/choice.h"
#include "wx/arrstr.h" #include "wx/arrstr.h"
#include "wx/stattext.h" #include "wx/stattext.h"
#include "wx/sizer.h" #include "wx/sizer.h"
#include "wx/ctrlsub.h"
#include <wx/colour.h> #include <wx/colour.h>
#include <wx/clrpicker.h> #include <wx/clrpicker.h>
#include <wx/slider.h> #include <wx/slider.h>
@ -230,7 +232,7 @@ private:
class UI_Choice : public UI_Window { class UI_Choice : public UI_Window {
public: public:
UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id = wxID_ANY); UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id = wxID_ANY);
~UI_Choice() { _choice->Destroy(); } ~UI_Choice() { window->Destroy(); }
std::string get_string() override; std::string get_string() override;
@ -240,7 +242,8 @@ public:
std::function<void (const std::string&, std::string value)> on_change {nullptr}; std::function<void (const std::string&, std::string value)> on_change {nullptr};
/// Returns a bare pointer to the underlying combobox, usually for test interface /// Returns a bare pointer to the underlying combobox, usually for test interface
wxComboBox* choice() { return this->_choice; } wxChoice* choice() { return this->_choice; }
wxComboBox* combo() { return this->_combo; }
protected: protected:
virtual std::string LogChannel() override { return "UI_Choice"s; } virtual std::string LogChannel() override { return "UI_Choice"s; }
@ -250,7 +253,8 @@ protected:
} }
} }
private: private:
wxComboBox* _choice {nullptr}; wxComboBox* _combo {nullptr};
wxChoice* _choice {nullptr};
}; };

View File

@ -5,39 +5,60 @@ namespace Slic3r { namespace GUI {
UI_Choice::UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id) : UI_Window(parent, _opt) { UI_Choice::UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id) : UI_Window(parent, _opt) {
int style {0}; int style {0};
style |= wxTE_PROCESS_ENTER; style |= wxTE_PROCESS_ENTER;
if (opt.gui_type.size() > 0 && opt.gui_type.compare("select_open"s)) style |= wxCB_READONLY;
/// Load the values /// Load the values
auto values {wxArrayString()}; auto values {wxArrayString()};
for (auto v : opt.enum_values) values.Add(wxString(v)); for (auto v : opt.enum_values) values.Add(wxString(v));
_choice = new wxComboBox(parent, id, if (opt.gui_type.size() > 0 && opt.gui_type.compare("select_open"s)) {
_choice = new wxChoice(parent, id,
wxDefaultPosition, _default_size(), values, style);
_choice->Bind(wxEVT_CHOICE, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); });
window = _choice;
} else {
_combo = new wxComboBox(parent, id,
(opt.default_value != nullptr ? opt.default_value->getString() : ""), (opt.default_value != nullptr ? opt.default_value->getString() : ""),
wxDefaultPosition, _default_size(), values, style); wxDefaultPosition, _default_size(), values, style);
window = _choice; _combo->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); });
window = _combo;
}
_choice->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); window->Bind(wxEVT_TEXT_ENTER, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); });
_choice->Bind(wxEVT_TEXT_ENTER, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); window->Bind(wxEVT_KILL_FOCUS, [this](wxFocusEvent& e) { if (this->on_kill_focus != nullptr) {this->on_kill_focus(""); 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() { std::string UI_Choice::get_string() {
if (_combo != nullptr) {
if (opt.enum_values.size() > 0) {
auto idx = this->_combo->GetSelection();
if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx);
}
return this->_combo->GetValue().ToStdString();
} else {
if (opt.enum_values.size() > 0) { if (opt.enum_values.size() > 0) {
auto idx = this->_choice->GetSelection(); auto idx = this->_choice->GetSelection();
if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx); if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx);
} }
return this->_choice->GetValue().ToStdString(); return std::string("");
}
} }
void UI_Choice::set_value(boost::any value) { void UI_Choice::set_value(boost::any value) {
auto result {std::find(opt.enum_values.cbegin(), opt.enum_values.cend(), boost::any_cast<std::string>(value))}; auto result {std::find(opt.enum_values.cbegin(), opt.enum_values.cend(), boost::any_cast<std::string>(value))};
if (_combo != nullptr) {
if (result == opt.enum_values.cend()) { if (result == opt.enum_values.cend()) {
this->_choice->SetValue(wxString(boost::any_cast<std::string>(value))); this->_combo->SetValue(wxString(boost::any_cast<std::string>(value)));
} else { } else {
auto idx = std::distance(opt.enum_values.cbegin(), result);
this->_combo->SetSelection(idx);
}
} else {
if (result != opt.enum_values.cend()) {
auto idx = std::distance(opt.enum_values.cbegin(), result); auto idx = std::distance(opt.enum_values.cbegin(), result);
this->_choice->SetSelection(idx); this->_choice->SetSelection(idx);
} }
}
} }
} } // Namespace Slic3r::GUI } } // Namespace Slic3r::GUI

View File

@ -70,14 +70,14 @@ SCENARIO( "UI_Choice: default values from options") {
} }
} }
WHEN( "I set the string value to another item in the enumeration") { WHEN( "I set the string value to another item in the enumeration") {
test_field.choice()->SetValue("C"s); test_field.combo()->SetValue("C"s);
THEN( "get_string() returns the matching item in ConfigOptionDef") { THEN( "get_string() returns the matching item in ConfigOptionDef") {
REQUIRE(test_field.get_string() == "C"s); REQUIRE(test_field.get_string() == "C"s);
REQUIRE(test_field.choice()->FindString(simple_option.enum_values[1]) == 1); REQUIRE(test_field.choice()->FindString(simple_option.enum_values[1]) == 1);
} }
} }
WHEN( "I set the string value to another item that is not in the enumeration") { WHEN( "I set the string value to another item that is not in the enumeration") {
test_field.choice()->SetValue("F"s); test_field.combo()->SetValue("F"s);
THEN( "get_string() returns the matching item in ConfigOptionDef") { THEN( "get_string() returns the matching item in ConfigOptionDef") {
REQUIRE(test_field.get_string() == "F"s); REQUIRE(test_field.get_string() == "F"s);
REQUIRE(test_field.choice()->GetSelection() == wxNOT_FOUND); REQUIRE(test_field.choice()->GetSelection() == wxNOT_FOUND);