Extended "get_value" to Choice & TextCtrl fields.

Extended "change_opt_value".
This commit is contained in:
YuSanka 2018-01-09 13:52:01 +01:00
parent 59432d50ff
commit 129bd898cd
5 changed files with 93 additions and 40 deletions

View file

@ -31,6 +31,41 @@ namespace Slic3r { namespace GUI {
return std::regex_match(string, regex_pattern); return std::regex_match(string, regex_pattern);
} }
boost::any Field::get_value_by_opt_type(wxString str, ConfigOptionType type)
{
boost::any ret_val;
switch (m_opt.type){
case coInt:
case coPercent:
if (m_opt.type == coPercent) str.RemoveLast();
ret_val = wxAtoi(str);
break;
case coPercents:
case coFloats:
case coFloat:{
double val;
str.ToCDouble(&val);
ret_val = val;
break; }
case coString:
case coStrings:
ret_val = str.ToStdString();
break;
case coFloatOrPercent:{
if (str.Last() == '%')
str.RemoveLast();
double val;
str.ToCDouble(&val);
ret_val = val;
break;
}
default:
break;
}
return ret_val;
}
void TextCtrl::BUILD() { void TextCtrl::BUILD() {
auto size = wxSize(wxDefaultSize); auto size = wxSize(wxDefaultSize);
if (m_opt.height >= 0) size.SetHeight(m_opt.height); if (m_opt.height >= 0) size.SetHeight(m_opt.height);
@ -107,7 +142,14 @@ namespace Slic3r { namespace GUI {
// recast as a wxWindow to fit the calling convention // recast as a wxWindow to fit the calling convention
window = dynamic_cast<wxWindow*>(temp); window = dynamic_cast<wxWindow*>(temp);
}
boost::any TextCtrl::get_value()
{
wxString ret_str = static_cast<wxTextCtrl*>(window)->GetValue();
boost::any ret_val = get_value_by_opt_type(ret_str, m_opt.type);
return ret_val;
} }
void TextCtrl::enable() { dynamic_cast<wxTextCtrl*>(window)->Enable(); dynamic_cast<wxTextCtrl*>(window)->SetEditable(true); } void TextCtrl::enable() { dynamic_cast<wxTextCtrl*>(window)->Enable(); dynamic_cast<wxTextCtrl*>(window)->SetEditable(true); }
@ -260,21 +302,6 @@ void Choice::set_selection()
dynamic_cast<wxComboBox*>(window)->SetSelection(idx); dynamic_cast<wxComboBox*>(window)->SetSelection(idx);
break; break;
} }
// case coString:{
// text_value = static_cast<const ConfigOptionString*>(opt.default_value)->value;
//
// auto idx = 0;
// for (auto el : opt.enum_values)
// {
// if (el.compare(text_value) == 0)
// break;
// ++idx;
// }
// idx == opt.enum_values.size() ?
// dynamic_cast<wxComboBox*>(window)->SetValue(text_value) :
// dynamic_cast<wxComboBox*>(window)->SetSelection(idx);
// break;
// }
case coStrings:{ case coStrings:{
text_value = static_cast<const ConfigOptionStrings*>(m_opt.default_value)->values.at(0); text_value = static_cast<const ConfigOptionStrings*>(m_opt.default_value)->values.at(0);
@ -361,6 +388,18 @@ void Choice::set_values(const std::vector<std::string> values)
m_disable_change_event = false; m_disable_change_event = false;
} }
boost::any Choice::get_value()
{
boost::any ret_val;
wxString ret_str = static_cast<wxComboBox*>(window)->GetValue();
ret_val = m_opt.type == coEnum ?
static_cast<wxComboBox*>(window)->GetSelection() :
get_value_by_opt_type(ret_str, m_opt.type);
return ret_val;
}
void ColourPicker::BUILD() void ColourPicker::BUILD()
{ {
auto size = wxSize(wxDefaultSize); auto size = wxSize(wxDefaultSize);

View file

@ -80,6 +80,7 @@ public:
virtual wxWindow* getWindow() { return nullptr; } virtual wxWindow* getWindow() { return nullptr; }
bool is_matched(std::string string, std::string pattern); bool is_matched(std::string string, std::string pattern);
boost::any get_value_by_opt_type(wxString str, ConfigOptionType type);
/// Factory method for generating new derived classes. /// Factory method for generating new derived classes.
template<class T> template<class T>
@ -117,7 +118,7 @@ public:
dynamic_cast<wxTextCtrl*>(window)->SetValue(boost::any_cast<wxString>(value)); dynamic_cast<wxTextCtrl*>(window)->SetValue(boost::any_cast<wxString>(value));
} }
boost::any get_value() override { return boost::any(dynamic_cast<wxTextCtrl*>(window)->GetValue()); } boost::any get_value() override;
virtual void enable(); virtual void enable();
virtual void disable(); virtual void disable();
@ -191,9 +192,7 @@ public:
void set_value(const std::string value); void set_value(const std::string value);
void set_value(boost::any value); void set_value(boost::any value);
void set_values(const std::vector<std::string> values); void set_values(const std::vector<std::string> values);
boost::any get_value() override { boost::any get_value() override;
return boost::any(dynamic_cast<wxComboBox*>(window)->GetValue());
}
void enable() override { dynamic_cast<wxComboBox*>(window)->Enable(); }; void enable() override { dynamic_cast<wxComboBox*>(window)->Enable(); };
void disable() override{ dynamic_cast<wxComboBox*>(window)->Disable(); }; void disable() override{ dynamic_cast<wxComboBox*>(window)->Disable(); };
@ -210,14 +209,13 @@ public:
wxWindow* window{ nullptr }; wxWindow* window{ nullptr };
void BUILD() override; void BUILD() override;
// void set_selection();
void set_value(const std::string value) { void set_value(const std::string value) {
dynamic_cast<wxColourPickerCtrl*>(window)->SetColour(value); dynamic_cast<wxColourPickerCtrl*>(window)->SetColour(value);
} }
void set_value(boost::any value) { void set_value(boost::any value) {
dynamic_cast<wxColourPickerCtrl*>(window)->SetColour(boost::any_cast<std::string>(value)); dynamic_cast<wxColourPickerCtrl*>(window)->SetColour(boost::any_cast<std::string>(value));
} }
// void set_values(const std::vector<std::string> values);
boost::any get_value() override { boost::any get_value() override {
return boost::any(dynamic_cast<wxColourPickerCtrl*>(window)->GetColour()); return boost::any(dynamic_cast<wxColourPickerCtrl*>(window)->GetColour());
} }

View file

@ -196,27 +196,36 @@ void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, b
{ {
try{ try{
switch (config.def()->get(opt_key)->type){ switch (config.def()->get(opt_key)->type){
case coFloatOrPercent: case coFloatOrPercent:{
const auto &val = *config.option<ConfigOptionFloatOrPercent>(opt_key);
config.set_key_value(opt_key, new ConfigOptionFloatOrPercent(boost::any_cast</*ConfigOptionFloatOrPercent*/double>(value), val.percent));
break;}
case coPercent: case coPercent:
case coFloat: config.set_key_value(opt_key, new ConfigOptionPercent(boost::any_cast</*ConfigOptionPercent*/double>(value)));
{ break;
case coFloat:{
double& val = config.opt_float(opt_key); double& val = config.opt_float(opt_key);
val = boost::any_cast<double>(value); val = boost::any_cast<double>(value);
break; break;
} }
// case coPercents: case coPercents:
// case coFloats: case coFloats:{
double& val = config.opt_float(opt_key, 0);
val = boost::any_cast<double>(value);
break;
}
case coString: case coString:
// opt = new ConfigOptionString(config.opt_string(opt_key)); config.set_key_value(opt_key, new ConfigOptionString(boost::any_cast<std::string>(value)));
break; break;
case coStrings: case coStrings:
break; break;
case coBool: case coBool:
config.set_key_value(opt_key, new ConfigOptionBool(boost::any_cast<bool>(value))); config.set_key_value(opt_key, new ConfigOptionBool(boost::any_cast<bool>(value)));
break; break;
case coBools: case coBools:{
// opt = new ConfigOptionBools(0, config.opt_bool(opt_key)); //! 0? ConfigOptionBools* vec_new = new ConfigOptionBools{ boost::any_cast<bool>(value) };
break; config.option<ConfigOptionBools>(opt_key)->set_at(vec_new, 0, 0);
break;}
case coInt: case coInt:
config.set_key_value(opt_key, new ConfigOptionInt(boost::any_cast<int>(value))); config.set_key_value(opt_key, new ConfigOptionInt(boost::any_cast<int>(value)));
break; break;
@ -234,7 +243,7 @@ void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, b
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
int i = 0;//no reason, just experiment
} }
catch (...) catch (...)
{ {

View file

@ -231,7 +231,9 @@ void ConfigOptionsGroup::on_change_OG(t_config_option_key opt_id, boost::any val
} }
else { else {
if (opt_index == -1) { if (opt_index == -1) {
change_opt_value(*m_config, opt_key, field_value); // change_opt_value(*m_config, opt_key, field_value);
//!? why field_value?? in this case changed value will be lose! No?
change_opt_value(*m_config, opt_key, value);
} }
else { else {
// auto value = m_config->get($opt_key); // auto value = m_config->get($opt_key);

View file

@ -150,7 +150,11 @@ void Tab::load_config(DynamicPrintConfig config)
for(auto opt_key : m_config.diff(config)) { for(auto opt_key : m_config.diff(config)) {
switch ( config.def()->get(opt_key)->type ){ switch ( config.def()->get(opt_key)->type ){
case coFloatOrPercent: case coFloatOrPercent:
change_opt_value(m_config, opt_key, config.option<ConfigOptionFloatOrPercent>(opt_key)->value);
break;
case coPercent: case coPercent:
change_opt_value(m_config, opt_key, config.option<ConfigOptionPercent>(opt_key)->value);
break;
case coFloat: case coFloat:
change_opt_value(m_config, opt_key, config.opt_float(opt_key)); change_opt_value(m_config, opt_key, config.opt_float(opt_key));
break; break;
@ -422,7 +426,8 @@ void TabPrint::update()
Freeze(); Freeze();
if ( m_config.opt_bool("spiral_vase") && if ( m_config.opt_bool("spiral_vase") &&
!(m_config.opt_int("perimeters") == 1 && m_config.opt_int("top_solid_layers") == 0 && m_config.opt_float("fill_density") == 0)) { !(m_config.opt_int("perimeters") == 1 && m_config.opt_int("top_solid_layers") == 0 && /*m_config.opt_float("fill_density") == 0*/
m_config.option<ConfigOptionPercent>("fill_density")->value == 0)) {
std::string msg_text = "The Spiral Vase mode requires:\n" std::string msg_text = "The Spiral Vase mode requires:\n"
"- one perimeter\n" "- one perimeter\n"
"- no top solid layers\n" "- no top solid layers\n"