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".
This commit is contained in:
YuSanka 2018-01-09 09:41:07 +01:00
parent 16458e070a
commit 59432d50ff
8 changed files with 168 additions and 12 deletions

View file

@ -1000,6 +1000,7 @@ PrintConfigDef::PrintConfigDef()
def->multiline = true;
def->full_width = true;
def->height = 60;
def->default_value = new ConfigOptionStrings{ "" };
def = this->add("printer_notes", coString);
def->label = "Printer notes";

View file

@ -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<wxString>(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<wxComboBox*>(window)->SetValue(text_value) :
dynamic_cast<wxComboBox*>(window)->SetSelection(idx);
break;
}
case coEnum:{
dynamic_cast<wxComboBox*>(window)->SetSelection(boost::any_cast<int>(value));
break;
}
default:
break;
}
}
//! it's needed for _update_serial_ports()
void Choice::set_values(const std::vector<std::string> values)
{

View file

@ -166,7 +166,7 @@ public:
dynamic_cast<wxSpinCtrl*>(window)->SetValue(value);
}
void set_value(boost::any value) {
dynamic_cast<wxSpinCtrl*>(window)->SetValue(boost::any_cast<std::string>(value));
dynamic_cast<wxSpinCtrl*>(window)->SetValue(boost::any_cast<int>(value));
}
boost::any get_value() override {
return boost::any(dynamic_cast<wxSpinCtrl*>(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<wxComboBox*>(window)->SetValue(boost::any_cast<std::string>(value));
}
void set_value(boost::any value);
void set_values(const std::vector<std::string> values);
boost::any get_value() override {
return boost::any(dynamic_cast<wxComboBox*>(window)->GetValue());

View file

@ -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<std::string>(value);
val = boost::lexical_cast<double>(str);
val = boost::any_cast<double>(value);
break;
}
// case coPercents:

View file

@ -3,6 +3,7 @@
#include <utility>
#include <wx/tooltip.h>
#include <wx/numformatter.h>
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<std::string, int> >::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<ConfigOptionFloatOrPercent>(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<ConfigOptionPercent>(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<wxString>(config.opt_string(opt_key));
break;
case coStrings:
if (config.option<ConfigOptionStrings>(opt_key)->values.empty())
ret = text_value;
else
ret = static_cast<wxString>(config.opt_string(opt_key, static_cast<unsigned int>(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<int>(config.option<ConfigOptionEnum<InfillPattern>>(opt_key)->value);
}
else if (opt_key.compare("gcode_flavor") == 0 ){
ret = static_cast<int>(config.option<ConfigOptionEnum<GCodeFlavor>>(opt_key)->value);
}
else if (opt_key.compare("support_material_pattern") == 0){
ret = static_cast<int>(config.option<ConfigOptionEnum<SupportMaterialPattern>>(opt_key)->value);
}
else if (opt_key.compare("seam_position") == 0)
ret = static_cast<int>(config.option<ConfigOptionEnum<SeamPosition>>(opt_key)->value);
}
break;
case coPoints:{
const auto &value = *config.option<ConfigOptionPoints>(opt_key);
ret = value.values.at(0);
}
break;
case coNone:
default:
break;
}
return ret;
}
} // GUI
} // Slic3r

View file

@ -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);
};
}}

View file

@ -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<std::string> value)
{
// # To be called by custom widgets, load a value into a config,

View file

@ -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;