diff --git a/lib/python/gladevcp/calculatorwidget.py b/lib/python/gladevcp/calculatorwidget.py index 60e4ef08123..9c6f4b81107 100644 --- a/lib/python/gladevcp/calculatorwidget.py +++ b/lib/python/gladevcp/calculatorwidget.py @@ -46,6 +46,7 @@ class Calculator( Gtk.Box ): def __init__( self, *a, **kw ): Gtk.Box.__init__( self, *a, **kw ) + self.use_localization = False # Set True for legacay behavior using value conversion using atof() self.preset_value = None self.eval_string = "" self.font = "sans 12" @@ -170,19 +171,22 @@ def get_preset_value( self ): def compute( self ): qualified = '' - # print"string:",self.eval_string temp = self.eval_string.strip( " " ).replace("Pi", "math.pi") + if self.use_localization: + pass + else: + temp = temp.replace(",", ".") # this loop adds only spaces around the mentioned operators for i in( '-', '+', '/', '*', 'math.pi', '(', ')' ): new = " %s " % i temp = temp.replace( i, new ) for i in temp.split(): -# print ( "i in compute = ", i ) - try: - i = str( locale.atof( i ) ) -# print ( "converted i in compute = ", i ) - except: - pass + if self.use_localization: + try: + # convert to decimal dot format + i = str( locale.atof( i ) ) + except: + pass if i.isdigit(): qualified = qualified + str( float( i ) ) else: @@ -194,18 +198,16 @@ def compute( self ): b = str( eval( qualified ) ) except: b = "Error" - print("Calculator widget error, string:", self.eval_string, sys.exc_info()[0]) self.eval_string = '' - else : self.eval_string = b - # if locale.localeconv()["decimal_point" = comma , - # we have to replace the internal dot by a comma, - # otherwise it will be interpreted as an thousend separator - try: - b = locale.format_string( "%f", float( b ) ).rstrip( "0" ) - if b[-1] == locale.localeconv()["decimal_point"]: - b = b.rstrip( locale.localeconv()["decimal_point"] ) - except: - b = "Error" + if self.use_localization: + try: + b = locale.format_string( "%f", float( b ) ).rstrip( "0" ) + if b[-1] == locale.localeconv()["decimal_point"]: + b = b.rstrip( locale.localeconv()["decimal_point"] ) + else: + self.eval_string = b + except: + b = "Error" self.entry.set_text( b ) self.entry.set_position(len(self.eval_string)) diff --git a/lib/python/gladevcp/offsetpage_widget.py b/lib/python/gladevcp/offsetpage_widget.py index 1ba0fd752ec..7e1a3fb1df8 100755 --- a/lib/python/gladevcp/offsetpage_widget.py +++ b/lib/python/gladevcp/offsetpage_widget.py @@ -88,6 +88,7 @@ def __init__(self, filename = None, *a, **kw): self.status = linuxcnc.stat() self.cmd = linuxcnc.command() self.hash_check = None + self.use_localization = False # Set to True for float value conversion using locale settings (not recommended) self.display_units_mm = 0 # imperial self.machine_units_mm = 0 # imperial self.program_units = 0 # imperial @@ -378,17 +379,26 @@ def system_to_p(system): if col == 11: self.store[row][15] = new_text return + # for all other columns we expect a float value + else: + try: + if self.use_localization: + # using locale settings can lead to issues but we make it optional for backwards compatibility + new_float = float(locale.atof(new_text)) + else: + # this is the preferred way, allowing dot or comma as decimal symbol + new_float = float(new_text.replace(',', '.')) + except Exception as error: + print('new_text: ', new_text, error) + print(_("offsetpage widget error: unrecognized float input")) + return # ignore entries to the Rot column in non-wcs rows if self.store[row][0] not in ["G54", "G55", "G56", "G57", "G58", "G59", "G59.1", "G59.2", "G59.3"] and col == 10: return # set the text in the table - try: - self.store[row][col] = "%10.4f" % locale.atof(new_text) - except Exception as error: - print('new_text: ', new_text, error) - print(_("offsetpage widget error: unrecognized float input")) + self.store[row][col] = f"{new_float:10.4f}" # make sure we switch to correct units for machine and rotational, row 2, does not get converted try: if not self.display_units_mm == self.program_units and not row == 2: @@ -396,9 +406,9 @@ def system_to_p(system): convert = 25.4 else: convert = 1.0 / 25.4 - qualified = float(locale.atof(new_text)) * convert + qualified = new_float * convert else: - qualified = float(locale.atof(new_text)) + qualified = new_float except: print('error') # now update linuxcnc to the change