From 59432d50ffb93daac4d2b77904a20de187a79b6a Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 9 Jan 2018 09:41:07 +0100 Subject: [PATCH] To OptionsGroup added "reload_config" to reload configurations after changes in any fields & "get_config_value" to get current option value from config. In Field extended "set_value" to Choice. In PrintConfig added default_value to "post_process". --- xs/src/libslic3r/PrintConfig.cpp | 3 +- xs/src/slic3r/GUI/Field.cpp | 32 ++++++++- xs/src/slic3r/GUI/Field.hpp | 8 +-- xs/src/slic3r/GUI/GUI.cpp | 3 +- xs/src/slic3r/GUI/OptionsGroup.cpp | 112 +++++++++++++++++++++++++++++ xs/src/slic3r/GUI/OptionsGroup.hpp | 4 ++ xs/src/slic3r/GUI/Tab.cpp | 12 +++- xs/src/slic3r/GUI/Tab.h | 6 +- 8 files changed, 168 insertions(+), 12 deletions(-) diff --git a/xs/src/libslic3r/PrintConfig.cpp b/xs/src/libslic3r/PrintConfig.cpp index 9029145361..c2fbd11670 100644 --- a/xs/src/libslic3r/PrintConfig.cpp +++ b/xs/src/libslic3r/PrintConfig.cpp @@ -999,7 +999,8 @@ PrintConfigDef::PrintConfigDef() def->gui_flags = "serialized"; def->multiline = true; def->full_width = true; - def->height = 60; + def->height = 60; + def->default_value = new ConfigOptionStrings{ "" }; def = this->add("printer_notes", coString); def->label = "Printer notes"; diff --git a/xs/src/slic3r/GUI/Field.cpp b/xs/src/slic3r/GUI/Field.cpp index a4c1fdf966..a43551d734 100644 --- a/xs/src/slic3r/GUI/Field.cpp +++ b/xs/src/slic3r/GUI/Field.cpp @@ -47,7 +47,7 @@ namespace Slic3r { namespace GUI { text_value += "%"; } else - wxNumberFormatter::ToString(m_opt.default_value->getFloat(), 2); + text_value = wxNumberFormatter::ToString(m_opt.default_value->getFloat(), 2); break; } case coPercent: @@ -312,6 +312,36 @@ void Choice::set_value(const std::string value) //! Redundant? m_disable_change_event = false; } +void Choice::set_value(boost::any value) +{ + switch (m_opt.type){ + case coInt: + case coFloat: + case coPercent: + case coStrings:{ + wxString text_value = boost::any_cast(value); + auto idx = 0; + for (auto el : m_opt.enum_values) + { + if (el.compare(text_value) == 0) + break; + ++idx; + } + if (m_opt.type == coPercent) text_value += "%"; + idx == m_opt.enum_values.size() ? + dynamic_cast(window)->SetValue(text_value) : + dynamic_cast(window)->SetSelection(idx); + break; + } + case coEnum:{ + dynamic_cast(window)->SetSelection(boost::any_cast(value)); + break; + } + default: + break; + } +} + //! it's needed for _update_serial_ports() void Choice::set_values(const std::vector values) { diff --git a/xs/src/slic3r/GUI/Field.hpp b/xs/src/slic3r/GUI/Field.hpp index b36e2fe159..bd7f3efff7 100644 --- a/xs/src/slic3r/GUI/Field.hpp +++ b/xs/src/slic3r/GUI/Field.hpp @@ -114,7 +114,7 @@ public: dynamic_cast(window)->SetValue(wxString(value)); } virtual void set_value(boost::any value) { - dynamic_cast(window)->SetValue(boost::any_cast(value)); + dynamic_cast(window)->SetValue(boost::any_cast(value)); } boost::any get_value() override { return boost::any(dynamic_cast(window)->GetValue()); } @@ -166,7 +166,7 @@ public: dynamic_cast(window)->SetValue(value); } void set_value(boost::any value) { - dynamic_cast(window)->SetValue(boost::any_cast(value)); + dynamic_cast(window)->SetValue(boost::any_cast(value)); } boost::any get_value() override { return boost::any(dynamic_cast(window)->GetValue()); @@ -189,9 +189,7 @@ public: void set_selection(); void set_value(const std::string value); - void set_value(boost::any value) { - dynamic_cast(window)->SetValue(boost::any_cast(value)); - } + void set_value(boost::any value); void set_values(const std::vector values); boost::any get_value() override { return boost::any(dynamic_cast(window)->GetValue()); diff --git a/xs/src/slic3r/GUI/GUI.cpp b/xs/src/slic3r/GUI/GUI.cpp index 57b209d152..73f1433a54 100644 --- a/xs/src/slic3r/GUI/GUI.cpp +++ b/xs/src/slic3r/GUI/GUI.cpp @@ -201,8 +201,7 @@ void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, b case coFloat: { double& val = config.opt_float(opt_key); - std::string str = boost::any_cast(value); - val = boost::lexical_cast(str); + val = boost::any_cast(value); break; } // case coPercents: diff --git a/xs/src/slic3r/GUI/OptionsGroup.cpp b/xs/src/slic3r/GUI/OptionsGroup.cpp index 784e9b6546..d5a98a1211 100644 --- a/xs/src/slic3r/GUI/OptionsGroup.cpp +++ b/xs/src/slic3r/GUI/OptionsGroup.cpp @@ -3,6 +3,7 @@ #include #include +#include namespace Slic3r { namespace GUI { @@ -243,5 +244,116 @@ void ConfigOptionsGroup::on_change_OG(t_config_option_key opt_id, boost::any val OptionsGroup::on_change_OG(opt_id, value); } +void ConfigOptionsGroup::reload_config(){ + for (std::map< std::string, std::pair >::iterator it = m_opt_map.begin(); it != m_opt_map.end(); ++it) { + auto opt_id = it->first; + std::string opt_key = m_opt_map.at(opt_id).first; + int opt_index = m_opt_map.at(opt_id).second; + auto option = m_options.at(opt_id); + set_value(opt_id, config_value(opt_key, opt_index, option.gui_flags.compare("serialized") == 0 )); + } +} + +boost::any ConfigOptionsGroup::config_value(std::string opt_key, int opt_index, bool deserialize){ + + if (deserialize) { + // Want to edit a vector value(currently only multi - strings) in a single edit box. + // Aggregate the strings the old way. + // Currently used for the post_process config value only. + if (opt_index != -1) + throw std::out_of_range("Can't deserialize option indexed value"); +// return join(';', m_config->get(opt_key)}); + return get_config_value(*m_config, opt_key); + } + else { +// return opt_index == -1 ? m_config->get(opt_key) : m_config->get_at(opt_key, opt_index); + return get_config_value(*m_config, opt_key, opt_index); + } +} + +boost::any ConfigOptionsGroup::get_config_value(DynamicPrintConfig& config, std::string opt_key, int opt_index/* = -1*/) +{ + boost::any ret; + wxString text_value = wxString(""); + const ConfigOptionDef* opt = config.def()->get(opt_key); + switch (opt->type){ + case coFloatOrPercent:{ + const auto &value = *config.option(opt_key); + if (value.percent) + { + text_value = wxString::Format(_T("%i"), int(value.value)); + text_value += "%"; + } + else + text_value = wxNumberFormatter::ToString(value.value, 2); + ret = text_value; + break; + } + case coPercent:{ + double val = config.option(opt_key)->value; + text_value = wxString::Format(_T("%i"), int(val)); + ret = text_value;// += "%"; + } + break; + case coPercents: + case coFloats:{ + double val = config.opt_float(opt_key, 0/*opt_index*/); + ret = val - int(val) == 0 ? + wxString::Format(_T("%i"), int(val)) : + wxNumberFormatter::ToString(val, 2); + } + break; + case coFloat: + ret = wxNumberFormatter::ToString(config.opt_float(opt_key), 2); + break; + case coString: + ret = static_cast(config.opt_string(opt_key)); + break; + case coStrings: + if (config.option(opt_key)->values.empty()) + ret = text_value; + else + ret = static_cast(config.opt_string(opt_key, static_cast(0)/*opt_index*/)); + break; + case coBool: + ret = config.opt_bool(opt_key); + break; + case coBools: + ret = config.opt_bool(opt_key, 0/*opt_index*/); + break; + case coInt: + ret = config.opt_int(opt_key); + break; + case coInts: + ret = config.opt_int(opt_key, 0/*opt_index*/); + break; + case coEnum:{ + if (opt_key.compare("external_fill_pattern") == 0 || + opt_key.compare("fill_pattern") == 0 || + opt_key.compare("external_fill_pattern") == 0 ){ + ret = static_cast(config.option>(opt_key)->value); + } + else if (opt_key.compare("gcode_flavor") == 0 ){ + ret = static_cast(config.option>(opt_key)->value); + } + else if (opt_key.compare("support_material_pattern") == 0){ + ret = static_cast(config.option>(opt_key)->value); + } + else if (opt_key.compare("seam_position") == 0) + ret = static_cast(config.option>(opt_key)->value); + } + break; + case coPoints:{ + const auto &value = *config.option(opt_key); + ret = value.values.at(0); + } + break; + case coNone: + default: + break; + } + return ret; +} + } // GUI } // Slic3r diff --git a/xs/src/slic3r/GUI/OptionsGroup.hpp b/xs/src/slic3r/GUI/OptionsGroup.hpp index bd8140276f..f395e95b4b 100644 --- a/xs/src/slic3r/GUI/OptionsGroup.hpp +++ b/xs/src/slic3r/GUI/OptionsGroup.hpp @@ -162,6 +162,10 @@ public: } void on_change_OG(t_config_option_key opt_id, boost::any value) override; + void reload_config(); + boost::any config_value(std::string opt_key, int opt_index, bool deserialize); + // return option value from config + boost::any get_config_value(DynamicPrintConfig& config, std::string opt_key, int opt_index = -1); }; }} diff --git a/xs/src/slic3r/GUI/Tab.cpp b/xs/src/slic3r/GUI/Tab.cpp index ea69bafbf4..0444bf3f1e 100644 --- a/xs/src/slic3r/GUI/Tab.cpp +++ b/xs/src/slic3r/GUI/Tab.cpp @@ -105,7 +105,7 @@ void Tab::create_preset_tab(PresetBundle *preset_bundle) // Possible %params keys: no_controller build(); rebuild_page_tree(); -// _update(); + update(); } PageShp Tab::add_options_page(wxString title, std::string icon, bool is_extruder_pages/* = false*/) @@ -186,11 +186,19 @@ void Tab::load_config(DynamicPrintConfig config) if (modified) { update_dirty(); //# Initialize UI components with the config values. -// _reload_config(); + reload_config(); update(); } } +// Reload current $self->{config} (aka $self->{presets}->edited_preset->config) into the UI fields. +void Tab::reload_config(){ + Freeze(); + for (auto page : m_pages) + page->reload_config(); + Thaw(); +} + void Tab::load_key_value(std::string opt_key, std::vector value) { // # To be called by custom widgets, load a value into a config, diff --git a/xs/src/slic3r/GUI/Tab.h b/xs/src/slic3r/GUI/Tab.h index 4e4d41b15d..b133d4d763 100644 --- a/xs/src/slic3r/GUI/Tab.h +++ b/xs/src/slic3r/GUI/Tab.h @@ -63,6 +63,10 @@ public: wxString title() const { return m_title; } size_t iconID() const { return m_iconID; } void set_config(DynamicPrintConfig* config_in) { m_config = config_in; } + void reload_config(){ + for (auto group: m_optgroups) + group->reload_config(); + } ConfigOptionsGroupShp new_optgroup(std::string title, int noncommon_label_width = -1); }; @@ -139,7 +143,7 @@ public: virtual void update() = 0; void update_dirty(); void load_config(DynamicPrintConfig config); - + void reload_config(); }; //Slic3r::GUI::Tab::Print;