diff options
author | supermerill <merill@fr.fr> | 2019-08-24 11:29:33 +0300 |
---|---|---|
committer | supermerill <merill@fr.fr> | 2019-08-24 12:01:57 +0300 |
commit | fa1274d9b10cdfd8ed310fe458fc10f4eb2d1c4f (patch) | |
tree | 7f6f3ba1c429997a2a2955f000a2a1eb59ed1b09 | |
parent | f61087df02e950e4843c4e5af2251d83f978923b (diff) |
better error-free enum fields1.42.2
-rw-r--r-- | src/slic3r/GUI/Field.cpp | 147 | ||||
-rw-r--r-- | src/slic3r/GUI/Field.hpp | 5 |
2 files changed, 80 insertions, 72 deletions
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<class T> +int Choice::idx_from_enum_value(int val) { + if (!m_opt.enum_values.empty()) { + std::string key; + t_config_enum_values map_names = ConfigOptionEnum<T>::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<InfillPattern>::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<InfillPattern>(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<SeamPosition>::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<SeamPosition>(val); } + else if (m_opt_id.compare("gcode_flavor") == 0) + val = idx_from_enum_value<GCodeFlavor>(val); + else if (m_opt_id.compare("support_material_pattern") == 0) + val = idx_from_enum_value<SupportMaterialPattern>(val); + else if (m_opt_id.compare("seam_position") == 0) + val = idx_from_enum_value<SeamPosition>(val); + else if (m_opt_id.compare("host_type") == 0) + val = idx_from_enum_value<PrintHostType>(val); + else if (m_opt_id.compare("infill_dense_algo") == 0) + val = idx_from_enum_value<DenseInfillAlgo>(val); + else if (m_opt_id.compare("no_perimeter_unsupported_algo") == 0) + val = idx_from_enum_value<NoPerimeterUnsupportedAlgo>(val); + else if (m_opt_id.compare("wipe_advanced_algo") == 0) + val = idx_from_enum_value<WipeAlgo>(val); + else if (m_opt_id.compare("support_material_contact_distance_type") == 0) + val = idx_from_enum_value<SupportZDistanceType>(val); + else if (m_opt_id.compare("display_orientation") == 0) + val = idx_from_enum_value<SLADisplayOrientation>(val); + else if (m_opt_id.compare("support_pillar_connection_mode") == 0) + val = idx_from_enum_value<SLAPillarConnectionMode>(val); field->SetSelection(val); break; } @@ -920,6 +926,19 @@ void Choice::set_values(const std::vector<std::string>& values) m_disable_change_event = false; } +template<class T> +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<T>::get_enum_values(); + int value = map_names.at(key); + + m_value = static_cast<T>(value); + } + else + m_value = static_cast<T>(m_opt.default_value.get()->getInt()); +} + boost::any& Choice::get_value() { wxBitmapComboBox* field = dynamic_cast<wxBitmapComboBox*>(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<InfillPattern>::get_enum_values(); - int value = map_names.at(key); - - m_value = static_cast<InfillPattern>(value); - } - else - m_value = static_cast<InfillPattern>(0); - } + convert_to_enum_value<InfillPattern>(ret_enum); else if (m_opt_id.compare("gcode_flavor") == 0) - m_value = static_cast<GCodeFlavor>(ret_enum); + convert_to_enum_value<GCodeFlavor>(ret_enum); else if (m_opt_id.compare("support_material_pattern") == 0) - m_value = static_cast<SupportMaterialPattern>(ret_enum); + convert_to_enum_value<SupportMaterialPattern>(ret_enum); else if (m_opt_id.compare("seam_position") == 0) - m_value = static_cast<SeamPosition>(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<SeamPosition>::get_enum_values(); - int value = map_names.at(key); - m_value = static_cast<SeamPosition>(value); - } else - m_value = static_cast<SeamPosition>(3); - } else if (m_opt_id.compare("host_type") == 0) - m_value = static_cast<PrintHostType>(ret_enum); + convert_to_enum_value<SeamPosition>(ret_enum); + else if (m_opt_id.compare("perimeter_loop_seam") == 0) + convert_to_enum_value<SeamPosition>(ret_enum); + else if (m_opt_id.compare("host_type") == 0) + convert_to_enum_value<PrintHostType>(ret_enum); else if (m_opt_id.compare("infill_dense_algo") == 0) - m_value = static_cast<DenseInfillAlgo>(ret_enum); + convert_to_enum_value<DenseInfillAlgo>(ret_enum); else if (m_opt_id.compare("no_perimeter_unsupported_algo") == 0) - m_value = static_cast<NoPerimeterUnsupportedAlgo>(ret_enum); + convert_to_enum_value<NoPerimeterUnsupportedAlgo>(ret_enum); else if (m_opt_id.compare("wipe_advanced_algo") == 0) - m_value = static_cast<WipeAlgo>(ret_enum); + convert_to_enum_value<WipeAlgo>(ret_enum); else if (m_opt_id.compare("support_material_contact_distance_type") == 0) - m_value = static_cast<SupportZDistanceType>(ret_enum); + convert_to_enum_value<SupportZDistanceType>(ret_enum); else if (m_opt_id.compare("display_orientation") == 0) - m_value = static_cast<SLADisplayOrientation>(ret_enum); + convert_to_enum_value<SLADisplayOrientation>(ret_enum); else if (m_opt_id.compare("support_pillar_connection_mode") == 0) - m_value = static_cast<SLAPillarConnectionMode>(ret_enum); + convert_to_enum_value<SLAPillarConnectionMode>(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<class T> void convert_to_enum_value(int idx_val); + template<class T> 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) {} |