From 45ac53efa1917fa960f297fe26afa782f6bce920 Mon Sep 17 00:00:00 2001
From: YuSanka <yusanka@gmail.com>
Date: Fri, 26 Mar 2021 19:01:10 +0100
Subject: [PATCH] Code refactoring to reduce switch statements on
 ConfigOptionEnum<> templates

---
 src/slic3r/GUI/Field.cpp | 72 ++++++++--------------------------------
 src/slic3r/GUI/GUI.cpp   | 35 +------------------
 2 files changed, 15 insertions(+), 92 deletions(-)

diff --git a/src/slic3r/GUI/Field.cpp b/src/slic3r/GUI/Field.cpp
index 8e3fff06d..1fe28eb34 100644
--- a/src/slic3r/GUI/Field.cpp
+++ b/src/slic3r/GUI/Field.cpp
@@ -1112,28 +1112,17 @@ void Choice::set_value(const boost::any& value, bool change_event)
 		int val = boost::any_cast<int>(value);
 		if (m_opt_id == "top_fill_pattern" || m_opt_id == "bottom_fill_pattern" || m_opt_id == "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;
-					}
+			std::string key;
+			const 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 == key)
-						break;
-					++idx;
-				}
-
-				val = idx == m_opt.enum_values.size() ? 0 : idx;
-			}
-			else
-				val = 0;
+			const std::vector<std::string>& values = m_opt.enum_values;
+			auto it = std::find(values.begin(), values.end(), key);
+			val = it == values.end() ? 0 : it - values.begin();
 		}
 		field->SetSelection(val);
 		break;
@@ -1199,45 +1188,12 @@ boost::any& Choice::get_value()
 
 	if (m_opt.type == coEnum)
 	{
-		int ret_enum = field->GetSelection();
-		if (m_opt_id == "top_fill_pattern" || m_opt_id == "bottom_fill_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);
+		if (m_opt_id == "top_fill_pattern" || m_opt_id == "bottom_fill_pattern" || m_opt_id == "fill_pattern") {
+			const std::string& key = m_opt.enum_values[field->GetSelection()];
+			m_value = int(ConfigOptionEnum<InfillPattern>::get_enum_values().at(key));
 		}
-		else if (m_opt_id.compare("ironing_type") == 0)
-			m_value = static_cast<IroningType>(ret_enum);
-        else if (m_opt_id.compare("fuzzy_skin") == 0)
-            m_value = static_cast<FuzzySkinType>(ret_enum);
-		else if (m_opt_id.compare("gcode_flavor") == 0)
-			m_value = static_cast<GCodeFlavor>(ret_enum);
-		else if (m_opt_id.compare("machine_limits_usage") == 0)
-			m_value = static_cast<MachineLimitsUsage>(ret_enum);
-		else if (m_opt_id.compare("support_material_pattern") == 0)
-			m_value = static_cast<SupportMaterialPattern>(ret_enum);
-        else if (m_opt_id.compare("support_material_interface_pattern") == 0)
-            m_value = static_cast<SupportMaterialInterfacePattern>(ret_enum);
-        else if (m_opt_id.compare("support_material_style") == 0)
-            m_value = static_cast<SupportMaterialStyle>(ret_enum);
-		else if (m_opt_id.compare("seam_position") == 0)
-			m_value = static_cast<SeamPosition>(ret_enum);
-		else if (m_opt_id.compare("host_type") == 0)
-			m_value = static_cast<PrintHostType>(ret_enum);
-		else if (m_opt_id.compare("display_orientation") == 0)
-			m_value = static_cast<SLADisplayOrientation>(ret_enum);
-        else if (m_opt_id.compare("support_pillar_connection_mode") == 0)
-            m_value = static_cast<SLAPillarConnectionMode>(ret_enum);
-		else if (m_opt_id == "printhost_authorization_type")
-			m_value = static_cast<AuthorizationType>(ret_enum);
-        else if (m_opt_id == "brim_type")
-            m_value = static_cast<BrimType>(ret_enum);
+		else
+			m_value = field->GetSelection();
 	}
     else if (m_opt.gui_type == ConfigOptionDef::GUIType::f_enum_open || m_opt.gui_type == ConfigOptionDef::GUIType::i_enum_open) {
         const int ret_enum = field->GetSelection();
diff --git a/src/slic3r/GUI/GUI.cpp b/src/slic3r/GUI/GUI.cpp
index 1d3a85dc0..f3a8f6ba1 100644
--- a/src/slic3r/GUI/GUI.cpp
+++ b/src/slic3r/GUI/GUI.cpp
@@ -177,43 +177,10 @@ void change_opt_value(DynamicPrintConfig& config, const t_config_option_key& opt
 			}
 			break;
 		case coEnum:{
-#if 0
 			auto *opt = opt_def->default_value.get()->clone();
-			opt->setInt(0);
+			opt->setInt(boost::any_cast<int>(value));
 			config.set_key_value(opt_key, opt);
-#else
-			if (opt_key == "top_fill_pattern" ||
-				opt_key == "bottom_fill_pattern" ||
-				opt_key == "fill_pattern")
-				config.set_key_value(opt_key, new ConfigOptionEnum<InfillPattern>(boost::any_cast<InfillPattern>(value)));
-			else if (opt_key.compare("ironing_type") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<IroningType>(boost::any_cast<IroningType>(value))); 
-			else if (opt_key.compare("fuzzy_skin") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<FuzzySkinType>(boost::any_cast<FuzzySkinType>(value))); 
-			else if (opt_key.compare("gcode_flavor") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<GCodeFlavor>(boost::any_cast<GCodeFlavor>(value)));
-			else if (opt_key.compare("machine_limits_usage") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<MachineLimitsUsage>(boost::any_cast<MachineLimitsUsage>(value)));
-			else if (opt_key.compare("support_material_pattern") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<SupportMaterialPattern>(boost::any_cast<SupportMaterialPattern>(value)));
-			else if (opt_key.compare("support_material_interface_pattern") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<SupportMaterialInterfacePattern>(boost::any_cast<SupportMaterialInterfacePattern>(value)));
-			else if (opt_key.compare("support_material_style") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<SupportMaterialStyle>(boost::any_cast<SupportMaterialStyle>(value)));
-			else if (opt_key.compare("seam_position") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<SeamPosition>(boost::any_cast<SeamPosition>(value)));
-			else if (opt_key.compare("host_type") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<PrintHostType>(boost::any_cast<PrintHostType>(value)));
-			else if (opt_key.compare("display_orientation") == 0)
-				config.set_key_value(opt_key, new ConfigOptionEnum<SLADisplayOrientation>(boost::any_cast<SLADisplayOrientation>(value)));
-            else if(opt_key.compare("support_pillar_connection_mode") == 0)
-                config.set_key_value(opt_key, new ConfigOptionEnum<SLAPillarConnectionMode>(boost::any_cast<SLAPillarConnectionMode>(value)));
-            else if(opt_key == "printhost_authorization_type")
-                config.set_key_value(opt_key, new ConfigOptionEnum<AuthorizationType>(boost::any_cast<AuthorizationType>(value)));
-            else if(opt_key == "brim_type")
-                config.set_key_value(opt_key, new ConfigOptionEnum<BrimType>(boost::any_cast<BrimType>(value)));
 			}
-#endif
 			break;
 		case coPoints:{
 			if (opt_key == "bed_shape" || opt_key == "thumbnails") {