Added ConfigDef::get_default_value<>() template,

refactored calling code to use it.
This commit is contained in:
bubnikv 2019-05-03 18:30:58 +02:00
parent a61e833536
commit 09d80b1b2a
3 changed files with 21 additions and 21 deletions

View file

@ -1011,8 +1011,10 @@ public:
// What type? bool, int, string etc.
ConfigOptionType type = coNone;
// Default value of this option. The default value object is owned by ConfigDef, it is released in its destructor.
Slic3r::clonable_ptr<const ConfigOption> default_value = nullptr;
Slic3r::clonable_ptr<const ConfigOption> default_value;
void set_default_value(const ConfigOption* ptr) { this->default_value = Slic3r::clonable_ptr<const ConfigOption>(ptr); }
template<typename T>
const T* get_default_value() const { return static_cast<const T*>(this->default_value.get()); }
// Usually empty.
// Special values - "i_enum_open", "f_enum_open" to provide combo box for int or float selection,

View file

@ -544,14 +544,12 @@ PageDiameters::PageDiameters(ConfigWizard *parent)
{
spin_nozzle->SetDigits(2);
spin_nozzle->SetIncrement(0.1);
const auto &def_nozzle = *print_config_def.get("nozzle_diameter");
auto *default_nozzle = dynamic_cast<const ConfigOptionFloats*>(def_nozzle.default_value.get());
auto *default_nozzle = print_config_def.get("nozzle_diameter")->get_default_value<ConfigOptionFloats>();
spin_nozzle->SetValue(default_nozzle != nullptr && default_nozzle->size() > 0 ? default_nozzle->get_at(0) : 0.5);
spin_filam->SetDigits(2);
spin_filam->SetIncrement(0.25);
const auto &def_filam = *print_config_def.get("filament_diameter");
auto *default_filam = dynamic_cast<const ConfigOptionFloats*>(def_filam.default_value.get());
auto *default_filam = print_config_def.get("filament_diameter")->get_default_value<ConfigOptionFloats>();
spin_filam->SetValue(default_filam != nullptr && default_filam->size() > 0 ? default_filam->get_at(0) : 3.0);
append_text(_(L("Enter the diameter of your printer's hot end nozzle.")));
@ -596,13 +594,13 @@ PageTemperatures::PageTemperatures(ConfigWizard *parent)
spin_extr->SetIncrement(5.0);
const auto &def_extr = *print_config_def.get("temperature");
spin_extr->SetRange(def_extr.min, def_extr.max);
auto *default_extr = dynamic_cast<const ConfigOptionInts*>(def_extr.default_value.get());
auto *default_extr = def_extr.get_default_value<ConfigOptionInts>();
spin_extr->SetValue(default_extr != nullptr && default_extr->size() > 0 ? default_extr->get_at(0) : 200);
spin_bed->SetIncrement(5.0);
const auto &def_bed = *print_config_def.get("bed_temperature");
spin_bed->SetRange(def_bed.min, def_bed.max);
auto *default_bed = dynamic_cast<const ConfigOptionInts*>(def_bed.default_value.get());
auto *default_bed = def_bed.get_default_value<ConfigOptionInts>();
spin_bed->SetValue(default_bed != nullptr && default_bed->size() > 0 ? default_bed->get_at(0) : 0);
append_text(_(L("Enter the temperature needed for extruding your filament.")));

View file

@ -203,7 +203,7 @@ void TextCtrl::BUILD() {
case coFloatOrPercent:
{
text_value = double_to_string(m_opt.default_value->getFloat());
if (static_cast<const ConfigOptionFloatOrPercent*>(m_opt.default_value.get())->percent)
if (m_opt.get_default_value<ConfigOptionFloatOrPercent>()->percent)
text_value += "%";
break;
}
@ -218,19 +218,19 @@ void TextCtrl::BUILD() {
case coFloat:
{
double val = m_opt.type == coFloats ?
static_cast<const ConfigOptionFloats*>(m_opt.default_value.get())->get_at(m_opt_idx) :
m_opt.get_default_value<ConfigOptionFloats>()->get_at(m_opt_idx) :
m_opt.type == coFloat ?
m_opt.default_value->getFloat() :
static_cast<const ConfigOptionPercents*>(m_opt.default_value.get())->get_at(m_opt_idx);
m_opt.get_default_value<ConfigOptionPercents>()->get_at(m_opt_idx);
text_value = double_to_string(val);
break;
}
case coString:
text_value = static_cast<const ConfigOptionString*>(m_opt.default_value.get())->value;
text_value = m_opt.get_default_value<ConfigOptionString>()->value;
break;
case coStrings:
{
const ConfigOptionStrings *vec = static_cast<const ConfigOptionStrings*>(m_opt.default_value.get());
const ConfigOptionStrings *vec = m_opt.get_default_value<ConfigOptionStrings>();
if (vec == nullptr || vec->empty()) break; //for the case of empty default value
text_value = vec->get_at(m_opt_idx);
break;
@ -373,8 +373,8 @@ void CheckBox::BUILD() {
bool check_value = m_opt.type == coBool ?
m_opt.default_value->getBool() : m_opt.type == coBools ?
static_cast<const ConfigOptionBools*>(m_opt.default_value.get())->get_at(m_opt_idx) :
false;
m_opt.get_default_value<ConfigOptionBools>()->get_at(m_opt_idx) :
false;
// Set Label as a string of at least one space simbol to correct system scaling of a CheckBox
auto temp = new wxCheckBox(m_parent, wxID_ANY, wxString(" "), wxDefaultPosition, size);
@ -427,7 +427,7 @@ void SpinCtrl::BUILD() {
break;
case coInts:
{
const ConfigOptionInts *vec = static_cast<const ConfigOptionInts*>(m_opt.default_value.get());
const ConfigOptionInts *vec = m_opt.get_default_value<ConfigOptionInts>();
if (vec == nullptr || vec->empty()) break;
for (size_t id = 0; id < vec->size(); ++id)
{
@ -629,7 +629,7 @@ void Choice::set_selection()
break;
}
case coEnum:{
int id_value = static_cast<const ConfigOptionEnum<SeamPosition>*>(m_opt.default_value.get())->value; //!!
int id_value = m_opt.get_default_value<ConfigOptionEnum<SeamPosition>>()->value; //!!
field->SetSelection(id_value);
break;
}
@ -649,7 +649,7 @@ void Choice::set_selection()
break;
}
case coStrings:{
text_value = static_cast<const ConfigOptionStrings*>(m_opt.default_value.get())->get_at(m_opt_idx);
text_value = m_opt.get_default_value<ConfigOptionStrings>()->get_at(m_opt_idx);
size_t idx = 0;
for (auto el : m_opt.enum_values)
@ -886,7 +886,7 @@ void ColourPicker::BUILD()
if (m_opt.width >= 0) size.SetWidth(m_opt.width*m_em_unit);
// Validate the color
wxString clr_str(static_cast<const ConfigOptionStrings*>(m_opt.default_value.get())->get_at(m_opt_idx));
wxString clr_str(m_opt.get_default_value<ConfigOptionStrings>()->get_at(m_opt_idx));
wxColour clr(clr_str);
if (! clr.IsOk()) {
clr = wxTransparentColour;
@ -920,7 +920,7 @@ void PointCtrl::BUILD()
const wxSize field_size(4 * m_em_unit, -1);
auto default_pt = static_cast<const ConfigOptionPoints*>(m_opt.default_value.get())->values.at(0);
auto default_pt = m_opt.get_default_value<ConfigOptionPoints>()->values.at(0);
double val = default_pt(0);
wxString X = val - int(val) == 0 ? wxString::Format(_T("%i"), int(val)) : wxNumberFormatter::ToString(val, 2, wxNumberFormatter::Style_None);
val = default_pt(1);
@ -1019,7 +1019,7 @@ void StaticText::BUILD()
if (m_opt.height >= 0) size.SetHeight(m_opt.height*m_em_unit);
if (m_opt.width >= 0) size.SetWidth(m_opt.width*m_em_unit);
const wxString legend(static_cast<const ConfigOptionString*>(m_opt.default_value.get())->value);
const wxString legend(m_opt.get_default_value<ConfigOptionString>()->value);
auto temp = new wxStaticText(m_parent, wxID_ANY, legend, wxDefaultPosition, size, wxST_ELLIPSIZE_MIDDLE);
temp->SetFont(Slic3r::GUI::wxGetApp().normal_font());
temp->SetBackgroundStyle(wxBG_STYLE_PAINT);
@ -1054,7 +1054,7 @@ void SliderCtrl::BUILD()
auto temp = new wxBoxSizer(wxHORIZONTAL);
auto def_val = static_cast<const ConfigOptionInt*>(m_opt.default_value.get())->value;
auto def_val = m_opt.get_default_value<ConfigOptionInt>()->value;
auto min = m_opt.min == INT_MIN ? 0 : m_opt.min;
auto max = m_opt.max == INT_MAX ? 100 : m_opt.max;