Fixed incorrect locales handling in the UI (Field, ObjectManipulation, etc)

This commit is contained in:
Lukas Matena 2021-05-24 12:22:46 +02:00
parent c05b8210f2
commit 4960b125c5
6 changed files with 56 additions and 28 deletions

View File

@ -1388,17 +1388,21 @@ static void focus_event(wxFocusEvent& e, wxTextCtrl* ctrl, double def_value)
{
e.Skip();
wxString str = ctrl->GetValue();
// Replace the first occurence of comma in decimal number.
bool was_replace = str.Replace(",", ".", false) > 0;
const char dec_sep = is_decimal_separator_point() ? '.' : ',';
const char dec_sep_alt = dec_sep == '.' ? ',' : '.';
// Replace the first incorrect separator in decimal number.
bool was_replaced = str.Replace(dec_sep_alt, dec_sep, false) != 0;
double val = 0.0;
if (!str.ToCDouble(&val)) {
if (!str.ToDouble(&val)) {
if (val == 0.0)
val = def_value;
ctrl->SetValue(double_to_string(val));
show_error(nullptr, _L("Invalid numeric input."));
ctrl->SetFocus();
}
else if (was_replace)
else if (was_replaced)
ctrl->SetValue(double_to_string(val));
}
@ -1447,12 +1451,12 @@ PageDiameters::PageDiameters(ConfigWizard *parent)
void PageDiameters::apply_custom_config(DynamicPrintConfig &config)
{
double val = 0.0;
diam_nozzle->GetValue().ToCDouble(&val);
diam_nozzle->GetValue().ToDouble(&val);
auto *opt_nozzle = new ConfigOptionFloats(1, val);
config.set_key_value("nozzle_diameter", opt_nozzle);
val = 0.0;
diam_filam->GetValue().ToCDouble(&val);
diam_filam->GetValue().ToDouble(&val);
auto * opt_filam = new ConfigOptionFloats(1, val);
config.set_key_value("filament_diameter", opt_filam);

View File

@ -2162,7 +2162,7 @@ static void upgrade_text_entry_dialog(wxTextEntryDialog* dlg, double min = -1.0,
bool disable = textctrl->IsEmpty();
if (!disable && min >= 0.0 && max >= 0.0) {
double value = -1.0;
if (!textctrl->GetValue().ToCDouble(&value)) // input value couldn't be converted to double
if (!textctrl->GetValue().ToDouble(&value)) // input value couldn't be converted to double
disable = true;
else
disable = value < min - epsilon() || value > max + epsilon(); // is input value is out of valid range ?
@ -2231,7 +2231,7 @@ static double get_value_to_jump(double active_value, double min_z, double max_z,
return -1.0;
double value = -1.0;
return dlg.GetValue().ToCDouble(&value) ? value : -1.0;
return dlg.GetValue().ToDouble(&value) ? value : -1.0;
}
void Control::add_code_as_tick(Type type, int selected_extruder/* = -1*/)

View File

@ -98,9 +98,14 @@ ExtruderSequenceDialog::ExtruderSequenceDialog(const DoubleSlider::ExtrudersSequ
return;
}
str.Replace(",", ".", false);
char dec_sep = '.';
if (! is_decimal_separator_point()) {
str.Replace(".", ",", false);
dec_sep = ',';
}
double val;
if (str == "." || !str.ToCDouble(&val) || val <= 0.0)
if (str == dec_sep || !str.ToDouble(&val) || val <= 0.0)
val = 3.0; // default value
if (fabs(m_sequence.interval_by_layers - val) < 0.001)

View File

@ -37,12 +37,13 @@ wxString double_to_string(double const value, const int max_precision /*= 4*/)
// with the exception that here one sets the decimal separator explicitely to dot.
// If number is in scientific format, trailing zeroes belong to the exponent and cannot be removed.
if (s.find_first_of("eE") == wxString::npos) {
const size_t posDecSep = s.find(".");
char dec_sep = is_decimal_separator_point() ? '.' : ',';
const size_t posDecSep = s.find(dec_sep);
// No decimal point => removing trailing zeroes irrelevant for integer number.
if (posDecSep != wxString::npos) {
// Find the last character to keep.
size_t posLastNonZero = s.find_last_not_of("0");
// If it's the decimal separator itself, don't keep it neither.
// If it's the decimal separator itself, don't keep it either.
if (posLastNonZero == posDecSep)
-- posLastNonZero;
s.erase(posLastNonZero + 1);
@ -236,16 +237,22 @@ void Field::get_value_by_opt_type(wxString& str, const bool check_value/* = true
m_value = double(m_opt.min);
break;
}
double val;
// Replace the first occurence of comma in decimal number.
str.Replace(",", ".", false);
if (str == ".")
double val;
const char dec_sep = is_decimal_separator_point() ? '.' : ',';
const char dec_sep_alt = dec_sep == '.' ? ',' : '.';
// Replace the first incorrect separator in decimal number.
if (str.Replace(dec_sep_alt, dec_sep, false) != 0)
set_value(str, false);
if (str == dec_sep)
val = 0.0;
else
{
if (m_opt.nullable && str == na_value())
val = ConfigOptionFloatsNullable::nil_value();
else if (!str.ToCDouble(&val))
else if (!str.ToDouble(&val))
{
if (!check_value) {
m_value.clear();
@ -293,14 +300,18 @@ void Field::get_value_by_opt_type(wxString& str, const bool check_value/* = true
if (m_opt.type == coFloatOrPercent && !str.IsEmpty() && str.Last() != '%')
{
double val = 0.;
// Replace the first occurence of comma in decimal number.
str.Replace(",", ".", false);
const char dec_sep = is_decimal_separator_point() ? '.' : ',';
const char dec_sep_alt = dec_sep == '.' ? ',' : '.';
// Replace the first incorrect separator in decimal number.
if (str.Replace(dec_sep_alt, dec_sep, false) != 0)
set_value(str, false);
// remove space and "mm" substring, if any exists
str.Replace(" ", "", true);
str.Replace("m", "", true);
if (!str.ToCDouble(&val))
if (!str.ToDouble(&val))
{
if (!check_value) {
m_value.clear();
@ -1478,7 +1489,7 @@ boost::any& PointCtrl::get_value()
!y_textctrl->GetValue().ToDouble(&y))
{
set_value(m_value.empty() ? Vec2d(0.0, 0.0) : m_value, true);
show_error(m_parent, _L("Invalid numeric input."));
show_error(m_parent, _L("Invalid numeric input."));
}
else
if (m_opt.min > x || x > m_opt.max ||

View File

@ -397,12 +397,16 @@ coordf_t LayerRangeEditor::get_value()
wxString str = GetValue();
coordf_t layer_height;
// Replace the first occurence of comma in decimal number.
str.Replace(",", ".", false);
const char dec_sep = is_decimal_separator_point() ? '.' : ',';
const char dec_sep_alt = dec_sep == '.' ? ',' : '.';
// Replace the first incorrect separator in decimal number.
if (str.Replace(dec_sep_alt, dec_sep, false) != 0)
SetValue(str);
if (str == ".")
layer_height = 0.0;
else {
if (!str.ToCDouble(&layer_height) || layer_height < 0.0f) {
if (!str.ToDouble(&layer_height) || layer_height < 0.0f) {
show_error(m_parent, _L("Invalid numeric input."));
SetValue(double_to_string(layer_height));
}

View File

@ -1095,15 +1095,19 @@ double ManipulationEditor::get_value()
wxString str = GetValue();
double value;
// Replace the first occurence of comma in decimal number.
str.Replace(",", ".", false);
const char dec_sep = is_decimal_separator_point() ? '.' : ',';
const char dec_sep_alt = dec_sep == '.' ? ',' : '.';
// Replace the first incorrect separator in decimal number.
if (str.Replace(dec_sep_alt, dec_sep, false) != 0)
SetValue(str);
if (str == ".")
value = 0.0;
if ((str.IsEmpty() || !str.ToCDouble(&value)) && !m_valid_value.IsEmpty()) {
if ((str.IsEmpty() || !str.ToDouble(&value)) && !m_valid_value.IsEmpty()) {
str = m_valid_value;
SetValue(str);
str.ToCDouble(&value);
str.ToDouble(&value);
}
return value;