diff --git a/src/slic3r/GUI/Field.cpp b/src/slic3r/GUI/Field.cpp index f2d58882a9..3f3886a1ca 100644 --- a/src/slic3r/GUI/Field.cpp +++ b/src/slic3r/GUI/Field.cpp @@ -126,6 +126,14 @@ wxString double_to_string(double const value, const int max_precision /*= 4*/) return s; } +bool string_to_double_any_separator(wxString str, double* value) +{ + // The text may carry the UI locale's separator or the C locale's, and the two can differ. + const char dec_sep = is_decimal_separator_point() ? '.' : ','; + str.Replace(dec_sep == '.' ? ',' : '.', dec_sep, false); + return str.ToDouble(value); +} + wxString get_thumbnail_string(const Vec2d& value) { wxString ret_str = wxString::Format("%.2fx%.2f", value[0], value[1]); @@ -2899,8 +2907,8 @@ void PointCtrl::set_value(const boost::any& value, bool change_event) boost::any& PointCtrl::get_value() { double x, y; - if (!x_textctrl->GetValue().ToDouble(&x) || - !y_textctrl->GetValue().ToDouble(&y)) + if (!string_to_double_any_separator(x_textctrl->GetValue(), &x) || + !string_to_double_any_separator(y_textctrl->GetValue(), &y)) { set_value(m_value.empty() ? Vec2d(0.0, 0.0) : m_value, true); show_error(m_parent, _L("Invalid numeric.")); diff --git a/src/slic3r/GUI/Field.hpp b/src/slic3r/GUI/Field.hpp index acdf0c21a2..72c3e31705 100644 --- a/src/slic3r/GUI/Field.hpp +++ b/src/slic3r/GUI/Field.hpp @@ -59,6 +59,8 @@ using t_change = std::function; wxString double_to_string(double const value, const int max_precision = 4); +// Parses a number shown in a field, whichever decimal separator it uses. +bool string_to_double_any_separator(wxString str, double* value); wxString get_thumbnail_string(const Vec2d& value); wxString get_thumbnails_string(const std::vector& values); wxString get_formatted_tooltip_text(const ConfigOptionDef& opt, const t_config_option_key& id); diff --git a/tests/slic3rutils/test_field_number_text.cpp b/tests/slic3rutils/test_field_number_text.cpp index 22ec5687fb..889802e0d4 100644 --- a/tests/slic3rutils/test_field_number_text.cpp +++ b/tests/slic3rutils/test_field_number_text.cpp @@ -3,11 +3,14 @@ #include "test_utils.hpp" #include +#include +#include #include #include using namespace Slic3r; using namespace Slic3r::GUI; +using Catch::Matchers::WithinAbs; TEST_CASE("Numbers drop trailing zeros when the C locale writes a comma and the UI locale a point", "[Field]") { @@ -22,3 +25,26 @@ TEST_CASE("Numbers drop trailing zeros when the C locale writes a comma and the CHECK(double_to_string(0.42) == wxString("0.42")); CHECK(double_to_string(100.) == wxString("100")); } + +TEST_CASE("Field numbers parse back whichever decimal separator they show", "[Field]") +{ + CommaNumericLocale comma; + CNumericLocalesSetter outer; + if (!comma.apply()) + SKIP("no locale with a comma decimal separator is installed"); + + double value = 0.; + // As point fields write it, in the UI locale. + REQUIRE(string_to_double_any_separator(wxNumberFormatter::ToString(0.5, 2, wxNumberFormatter::Style_None), &value)); + CHECK_THAT(value, WithinAbs(0.5, 1e-12)); + REQUIRE(string_to_double_any_separator("0.25", &value)); + CHECK_THAT(value, WithinAbs(0.25, 1e-12)); + REQUIRE(string_to_double_any_separator("0,25", &value)); + CHECK_THAT(value, WithinAbs(0.25, 1e-12)); + REQUIRE(string_to_double_any_separator("-1,5", &value)); + CHECK_THAT(value, WithinAbs(-1.5, 1e-12)); + // Accepted by ToDouble before, so typing it must keep working. + REQUIRE(string_to_double_any_separator("0.", &value)); + CHECK_THAT(value, WithinAbs(0., 1e-12)); + CHECK_FALSE(string_to_double_any_separator("abc", &value)); +}