Skip to content

Commit 5d3711d

Browse files
authored
Merge pull request #3691 from Sigma1912/Gmoccapy_Calculator_widget_decimal_handling
Gmoccapy: Make value conversion independent of locale setting
2 parents f80bfbc + ae93251 commit 5d3711d

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

lib/python/gladevcp/calculatorwidget.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Calculator( Gtk.Box ):
4646

4747
def __init__( self, *a, **kw ):
4848
Gtk.Box.__init__( self, *a, **kw )
49+
self.use_localization = False # Set True for legacay behavior using value conversion using atof()
4950
self.preset_value = None
5051
self.eval_string = ""
5152
self.font = "sans 12"
@@ -170,19 +171,22 @@ def get_preset_value( self ):
170171

171172
def compute( self ):
172173
qualified = ''
173-
# print"string:",self.eval_string
174174
temp = self.eval_string.strip( " " ).replace("Pi", "math.pi")
175+
if self.use_localization:
176+
pass
177+
else:
178+
temp = temp.replace(",", ".")
175179
# this loop adds only spaces around the mentioned operators
176180
for i in( '-', '+', '/', '*', 'math.pi', '(', ')' ):
177181
new = " %s " % i
178182
temp = temp.replace( i, new )
179183
for i in temp.split():
180-
# print ( "i in compute = ", i )
181-
try:
182-
i = str( locale.atof( i ) )
183-
# print ( "converted i in compute = ", i )
184-
except:
185-
pass
184+
if self.use_localization:
185+
try:
186+
# convert to decimal dot format
187+
i = str( locale.atof( i ) )
188+
except:
189+
pass
186190
if i.isdigit():
187191
qualified = qualified + str( float( i ) )
188192
else:
@@ -194,18 +198,16 @@ def compute( self ):
194198
b = str( eval( qualified ) )
195199
except:
196200
b = "Error"
197-
print("Calculator widget error, string:", self.eval_string, sys.exc_info()[0])
198201
self.eval_string = ''
199-
else : self.eval_string = b
200-
# if locale.localeconv()["decimal_point" = comma ,
201-
# we have to replace the internal dot by a comma,
202-
# otherwise it will be interpreted as an thousend separator
203-
try:
204-
b = locale.format_string( "%f", float( b ) ).rstrip( "0" )
205-
if b[-1] == locale.localeconv()["decimal_point"]:
206-
b = b.rstrip( locale.localeconv()["decimal_point"] )
207-
except:
208-
b = "Error"
202+
if self.use_localization:
203+
try:
204+
b = locale.format_string( "%f", float( b ) ).rstrip( "0" )
205+
if b[-1] == locale.localeconv()["decimal_point"]:
206+
b = b.rstrip( locale.localeconv()["decimal_point"] )
207+
else:
208+
self.eval_string = b
209+
except:
210+
b = "Error"
209211
self.entry.set_text( b )
210212
self.entry.set_position(len(self.eval_string))
211213

lib/python/gladevcp/offsetpage_widget.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def __init__(self, filename = None, *a, **kw):
8888
self.status = linuxcnc.stat()
8989
self.cmd = linuxcnc.command()
9090
self.hash_check = None
91+
self.use_localization = False # Set to True for float value conversion using locale settings (not recommended)
9192
self.display_units_mm = 0 # imperial
9293
self.machine_units_mm = 0 # imperial
9394
self.program_units = 0 # imperial
@@ -378,27 +379,36 @@ def system_to_p(system):
378379
if col == 11:
379380
self.store[row][15] = new_text
380381
return
382+
# for all other columns we expect a float value
383+
else:
384+
try:
385+
if self.use_localization:
386+
# using locale settings can lead to issues but we make it optional for backwards compatibility
387+
new_float = float(locale.atof(new_text))
388+
else:
389+
# this is the preferred way, allowing dot or comma as decimal symbol
390+
new_float = float(new_text.replace(',', '.'))
391+
except Exception as error:
392+
print('new_text: ', new_text, error)
393+
print(_("offsetpage widget error: unrecognized float input"))
394+
return
381395

382396
# ignore entries to the Rot column in non-wcs rows
383397
if self.store[row][0] not in ["G54", "G55", "G56", "G57", "G58", "G59", "G59.1", "G59.2", "G59.3"] and col == 10:
384398
return
385399

386400
# set the text in the table
387-
try:
388-
self.store[row][col] = "%10.4f" % locale.atof(new_text)
389-
except Exception as error:
390-
print('new_text: ', new_text, error)
391-
print(_("offsetpage widget error: unrecognized float input"))
401+
self.store[row][col] = f"{new_float:10.4f}"
392402
# make sure we switch to correct units for machine and rotational, row 2, does not get converted
393403
try:
394404
if not self.display_units_mm == self.program_units and not row == 2:
395405
if self.program_units == 1:
396406
convert = 25.4
397407
else:
398408
convert = 1.0 / 25.4
399-
qualified = float(locale.atof(new_text)) * convert
409+
qualified = new_float * convert
400410
else:
401-
qualified = float(locale.atof(new_text))
411+
qualified = new_float
402412
except:
403413
print('error')
404414
# now update linuxcnc to the change

0 commit comments

Comments
 (0)