Parse Point Fields Whichever Decimal Separator They Show

Point fields write X and Y with the UI locale's decimal separator but read them back with ToDouble, which uses the C locale's. When the two differ, any value with decimals fails with Invalid numeric and resets. Read them with string_to_double_any_separator, which converts the other separator to the C locale's before ToDouble, so every input ToDouble accepted before still parses.
This commit is contained in:
Hanif Koh committed 2026-11-19 17:19:15 +08:00
1 parent 8ae85dd072
commit 5289aa9d00
3 files changed
+38 -2

No files matched your search

+10 -2
View File
@@ -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."));
+2
View File
@@ -59,6 +59,8 @@ using t_change = std::function<void(const t_config_option_key&, const boost::any
using t_back_to_init = std::function<void(const std::string&)>;
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<Vec2d>& values);
wxString get_formatted_tooltip_text(const ConfigOptionDef& opt, const t_config_option_key& id);
@@ -3,11 +3,14 @@
#include "test_utils.hpp"
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <wx/numformatter.h>
#include <wx/string.h>
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));
}