diff --git a/src/slic3r/GUI/Field.cpp b/src/slic3r/GUI/Field.cpp index d416104fb..19f0edcbe 100644 --- a/src/slic3r/GUI/Field.cpp +++ b/src/slic3r/GUI/Field.cpp @@ -807,6 +807,32 @@ void Choice::set_value(const std::string& value, bool change_event) //! Redunda m_disable_change_event = false; } +template +int Choice::idx_from_enum_value(int val) { + if (!m_opt.enum_values.empty()) { + std::string key; + t_config_enum_values map_names = ConfigOptionEnum::get_enum_values(); + for (auto it : map_names) { + if (val == it.second) { + key = it.first; + break; + } + } + + size_t idx = 0; + for (auto el : m_opt.enum_values) + { + if (el.compare(key) == 0) + break; + ++idx; + } + + return idx == m_opt.enum_values.size() ? 0 : idx; + } + else + return 0; +} + void Choice::set_value(const boost::any& value, bool change_event) { m_disable_change_event = !change_event; @@ -846,50 +872,30 @@ void Choice::set_value(const boost::any& value, bool change_event) if (m_opt_id == "top_fill_pattern" || m_opt_id == "bottom_fill_pattern" || m_opt_id == "solid_fill_pattern" || m_opt_id == "fill_pattern" || m_opt_id == "support_fill_pattern") { - if (!m_opt.enum_values.empty()) { - std::string key; - t_config_enum_values map_names = ConfigOptionEnum::get_enum_values(); - for (auto it : map_names) { - if (val == it.second) { - key = it.first; - break; - } - } - - size_t idx = 0; - for (auto el : m_opt.enum_values) - { - if (el.compare(key) == 0) - break; - ++idx; - } - - val = idx == m_opt.enum_values.size() ? 0 : idx; - } - else - val = 0; + val = idx_from_enum_value(val); } else if (m_opt_id.compare("perimeter_loop_seam") == 0) { - if (!m_opt.enum_values.empty()) { - std::string key; - t_config_enum_values map_names = ConfigOptionEnum::get_enum_values(); - for (auto it : map_names) { - if (val == it.second) { - key = it.first; - break; - } - } - - size_t idx = 0; - for (auto el : m_opt.enum_values) { - if (el.compare(key) == 0) - break; - ++idx; - } - - val = idx == m_opt.enum_values.size() ? 0 : idx; - } else - val = 3; + val = idx_from_enum_value(val); } + else if (m_opt_id.compare("gcode_flavor") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("support_material_pattern") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("seam_position") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("host_type") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("infill_dense_algo") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("no_perimeter_unsupported_algo") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("wipe_advanced_algo") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("support_material_contact_distance_type") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("display_orientation") == 0) + val = idx_from_enum_value(val); + else if (m_opt_id.compare("support_pillar_connection_mode") == 0) + val = idx_from_enum_value(val); field->SetSelection(val); break; } @@ -920,6 +926,19 @@ void Choice::set_values(const std::vector& values) m_disable_change_event = false; } +template +void Choice::convert_to_enum_value(int ret_enum) { + if (!m_opt.enum_values.empty()) { + std::string key = m_opt.enum_values[ret_enum]; + t_config_enum_values map_names = ConfigOptionEnum::get_enum_values(); + int value = map_names.at(key); + + m_value = static_cast(value); + } + else + m_value = static_cast(m_opt.default_value.get()->getInt()); +} + boost::any& Choice::get_value() { wxBitmapComboBox* field = dynamic_cast(window); @@ -937,45 +956,29 @@ boost::any& Choice::get_value() int ret_enum = field->GetSelection(); if (m_opt_id == "top_fill_pattern" || m_opt_id == "bottom_fill_pattern" || m_opt_id == "solid_fill_pattern" || m_opt_id == "support_material_interface_pattern" || m_opt_id == "fill_pattern") - { - if (!m_opt.enum_values.empty()) { - std::string key = m_opt.enum_values[ret_enum]; - t_config_enum_values map_names = ConfigOptionEnum::get_enum_values(); - int value = map_names.at(key); - - m_value = static_cast(value); - } - else - m_value = static_cast(0); - } + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("gcode_flavor") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("support_material_pattern") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("seam_position") == 0) - m_value = static_cast(ret_enum); - else if (m_opt_id.compare("perimeter_loop_seam") == 0) { - if (!m_opt.enum_values.empty()) { - std::string key = m_opt.enum_values[ret_enum]; - t_config_enum_values map_names = ConfigOptionEnum::get_enum_values(); - int value = map_names.at(key); - m_value = static_cast(value); - } else - m_value = static_cast(3); - } else if (m_opt_id.compare("host_type") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); + else if (m_opt_id.compare("perimeter_loop_seam") == 0) + convert_to_enum_value(ret_enum); + else if (m_opt_id.compare("host_type") == 0) + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("infill_dense_algo") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("no_perimeter_unsupported_algo") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("wipe_advanced_algo") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("support_material_contact_distance_type") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("display_orientation") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); else if (m_opt_id.compare("support_pillar_connection_mode") == 0) - m_value = static_cast(ret_enum); + convert_to_enum_value(ret_enum); } else if (m_opt.gui_type == "f_enum_open") { const int ret_enum = field->GetSelection(); diff --git a/src/slic3r/GUI/Field.hpp b/src/slic3r/GUI/Field.hpp index 6c16f90f2..8e8ad3795 100644 --- a/src/slic3r/GUI/Field.hpp +++ b/src/slic3r/GUI/Field.hpp @@ -376,6 +376,11 @@ public: class Choice : public Field { using Field::Field; int m_width{ 15 }; +protected: + //used by get_value when it's an enum + //convert the value from the select to the enum value. store it in m_value + template void convert_to_enum_value(int idx_val); + template int idx_from_enum_value(int enum_val); public: Choice(const ConfigOptionDef& opt, const t_config_option_key& id) : Field(opt, id) {} Choice(wxWindow* parent, const ConfigOptionDef& opt, const t_config_option_key& id) : Field(parent, opt, id) {}