better error-free enum fields

This commit is contained in:
supermerill 2019-08-24 10:29:33 +02:00
parent f61087df02
commit fa1274d9b1
2 changed files with 80 additions and 72 deletions

View File

@ -807,6 +807,32 @@ void Choice::set_value(const std::string& value, bool change_event) //! Redunda
m_disable_change_event = false; 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) void Choice::set_value(const boost::any& value, bool change_event)
{ {
m_disable_change_event = !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" 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") || m_opt_id == "fill_pattern" || m_opt_id == "support_fill_pattern")
{ {
if (!m_opt.enum_values.empty()) { val = idx_from_enum_value<InfillPattern>(val);
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;
} else if (m_opt_id.compare("perimeter_loop_seam") == 0) { } else if (m_opt_id.compare("perimeter_loop_seam") == 0) {
if (!m_opt.enum_values.empty()) { val = idx_from_enum_value<SeamPosition>(val);
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;
} }
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); field->SetSelection(val);
break; break;
} }
@ -920,6 +926,19 @@ void Choice::set_values(const std::vector<std::string>& values)
m_disable_change_event = false; 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() boost::any& Choice::get_value()
{ {
wxBitmapComboBox* field = dynamic_cast<wxBitmapComboBox*>(window); wxBitmapComboBox* field = dynamic_cast<wxBitmapComboBox*>(window);
@ -937,45 +956,29 @@ boost::any& Choice::get_value()
int ret_enum = field->GetSelection(); int ret_enum = field->GetSelection();
if (m_opt_id == "top_fill_pattern" || m_opt_id == "bottom_fill_pattern" || m_opt_id == "solid_fill_pattern" 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") || m_opt_id == "support_material_interface_pattern" || m_opt_id == "fill_pattern")
{ convert_to_enum_value<InfillPattern>(ret_enum);
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);
}
else if (m_opt_id.compare("gcode_flavor") == 0) 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) 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) else if (m_opt_id.compare("seam_position") == 0)
m_value = static_cast<SeamPosition>(ret_enum); convert_to_enum_value<SeamPosition>(ret_enum);
else if (m_opt_id.compare("perimeter_loop_seam") == 0) { else if (m_opt_id.compare("perimeter_loop_seam") == 0)
if (!m_opt.enum_values.empty()) { convert_to_enum_value<SeamPosition>(ret_enum);
std::string key = m_opt.enum_values[ret_enum]; else if (m_opt_id.compare("host_type") == 0)
t_config_enum_values map_names = ConfigOptionEnum<SeamPosition>::get_enum_values(); convert_to_enum_value<PrintHostType>(ret_enum);
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);
else if (m_opt_id.compare("infill_dense_algo") == 0) 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) 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) 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) 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) 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) 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") { else if (m_opt.gui_type == "f_enum_open") {
const int ret_enum = field->GetSelection(); const int ret_enum = field->GetSelection();

View File

@ -376,6 +376,11 @@ public:
class Choice : public Field { class Choice : public Field {
using Field::Field; using Field::Field;
int m_width{ 15 }; 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: public:
Choice(const ConfigOptionDef& opt, const t_config_option_key& id) : Field(opt, id) {} 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) {} Choice(wxWindow* parent, const ConfigOptionDef& opt, const t_config_option_key& id) : Field(parent, opt, id) {}