@@ -226,7 +226,7 @@ def match_tool(model, path, iter, pathlist):
226226 except :
227227 print (_ ("tooledit_widget error: cannot select tool number" ),toolnumber )
228228
229- def add (self ,widget ,data = [1 ,0 ,0 ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,"comment" ]):
229+ def add (self ,widget ,data = [1 ,0 ,0 ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,0 ,"comment" ]):
230230 self .model .append (data )
231231 self .num_of_col += 1
232232
@@ -235,6 +235,17 @@ def set_filename(self,filename):
235235 self .toolfile = filename
236236 self .reload (None )
237237
238+ def warning_dialog (self , line_number ):
239+ message = _ ("Error in tool table line {0} in column " ).format (line_number ) + _ ("Orientation" ) + \
240+ _ (".\n Valid range is 0 - 9." )
241+ dialog = Gtk .MessageDialog (self .wTree .get_object ("window1" ),
242+ Gtk .DialogFlags .DESTROY_WITH_PARENT ,
243+ Gtk .MessageType .ERROR , Gtk .ButtonsType .OK ,
244+ message )
245+ dialog .show ()
246+ dialog .run ()
247+ dialog .destroy ()
248+
238249 # Reload the tool file into display
239250 def reload (self ,widget ):
240251 self .hash_code = self .md5sum (self .toolfile )
@@ -247,18 +258,23 @@ def reload(self,widget):
247258 return
248259 logfile = open (self .toolfile , "r" ).readlines ()
249260 self .toolinfo = []
261+ line_number = 0
250262 for rawline in logfile :
251263 # strip the comments from line and add directly to array
252264 # if index = -1 the delimiter ; is missing - clear comments
253265 index = rawline .find (";" )
266+ # skip lines beginning with a semicolon
267+ if index == 0 :
268+ continue
254269 comment = ''
255270 if not index == - 1 :
256271 comment = (rawline [index + 1 :])
257272 comment = comment .rstrip ("\n " )
258273 line = rawline .rstrip (comment )
259274 else :
260275 line = rawline
261- array = [0 ,0 ,0 ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,comment ]
276+ line_number += 1
277+ array = [0 ,0 ,0 ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,0 ,comment ]
262278 toolinfo_flag = False
263279 # search beginning of each word for keyword letters
264280 # offset 0 is the checkbutton so ignore it
@@ -278,11 +294,21 @@ def reload(self,widget):
278294 array [offset ]= int (word .lstrip (i ))
279295 except :
280296 print (_ ("Tooledit widget int error" ))
297+ elif offset == 15 :
298+ try :
299+ # Accept also float for 'orientation' for backward compatibility
300+ value = int (float (word .lstrip (i )))
301+ array [offset ] = value
302+ if value not in range (10 ):
303+ self .warning_dialog (line_number )
304+ break
305+ except :
306+ print (_ ("Tooledit widget float error" ))
281307 else :
282308 try :
283309 array [offset ]= locale .format ("%10.4f" , float (word .lstrip (i )))
284310 except :
285- print (_ ("Tooledit_widget float error" ))
311+ print (_ ("Tooledit widget float error" ))
286312 break
287313 if toolinfo_flag :
288314 self .toolinfo = array
@@ -292,16 +318,26 @@ def reload(self,widget):
292318 # Note we have to save the float info with a decimal even if the locale uses a comma
293319 def save (self ,widget ):
294320 if self .toolfile == None :return
321+ liststore = self .model
322+ # pre check before saving the file
323+ # if not done before, the file will be saved only until the erroneous line and the rest will be lost
324+ line_number = 0
325+ for row in liststore :
326+ values = [ value for value in row ]
327+ line_number += 1
328+ if values [15 ] > 9 :
329+ self .warning_dialog (line_number )
330+ return
331+
295332 file = open (self .toolfile , "w" )
296333 #print self.toolfile
297- liststore = self .model
298334 for row in liststore :
299335 values = [ value for value in row ]
300336 #print values
301337 line = ""
302338 for num ,i in enumerate (values ):
303339 if num == 0 : continue
304- elif num in (1 ,2 ): # tool# pocket#
340+ elif num in (1 ,2 , 15 ): # tool#, pocket#, orientation
305341 line = line + "%s%d " % (KEYWORDS [num ], i )
306342 elif num == 16 : # comments
307343 test = i .strip ()
@@ -421,17 +457,26 @@ def col_editted(self, widget, path, new_text, col, filter):
421457 elif filter == 'tool' :
422458 (store_path ,) = self .tool_filter .convert_path_to_child_path (path )
423459 path = store_path
424-
460+
425461 if col in (1 ,2 ):
426462 try :
427463 self .model [path ][col ] = int (new_text )
428464 except :
429465 pass
430- elif col in range (3 ,16 ):
466+ # validate input for float columns
467+ elif col in range (3 ,15 ):
431468 try :
432469 self .model [path ][col ] = locale .format ("%10.4f" ,locale .atof (new_text ))
433470 except :
434471 pass
472+ # validate input for orientation: check if int and valid range
473+ elif col == 15 :
474+ try :
475+ value = int (new_text )
476+ if value in range (10 ):
477+ self .model [path ][col ] = value
478+ except :
479+ pass
435480 elif col == 16 :
436481 try :
437482 self .model [path ][col ] = (new_text )
0 commit comments