Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Utils
import Model
from AddExcelInfo import AddExcelInfo
from DistanceCellEditor import DistanceCellEditor

from Undo import undo
from ReorderableGrid import ReorderableGrid
Expand Down Expand Up @@ -349,6 +350,7 @@ def __init__( self, parent, id = wx.ID_ANY ):

self.boolCols = set()
self.choiceCols = set()
self.floatCols = set()
self.readOnlyCols = set()
self.dependentCols = set()

Expand Down Expand Up @@ -413,11 +415,12 @@ def __init__( self, parent, id = wx.ID_ANY ):
attr.SetAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
self.readOnlyCols.add( col )
self.dependentCols.add( col )

elif fieldName in ['distance', 'firstLapDistance'] :
attr.SetEditor( GridCellFloatEditor(7, 3) )
attr.SetEditor( DistanceCellEditor() )
attr.SetRenderer( gridlib.GridCellFloatRenderer(7, 3) )
attr.SetAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
self.floatCols.add (col )
self.dependentCols.add( col )

elif fieldName == 'distanceType':
Expand Down Expand Up @@ -509,7 +512,10 @@ def onGridLeftClick( self, event ):
def onCellSelected( self, event ):
self.rowCur = event.GetRow()
self.colCur = event.GetCol()
if self.colCur in self.choiceCols or self.colCur in self.boolCols:

if self.colCur in self.floatCols:
return
elif self.colCur in self.choiceCols or self.colCur in self.boolCols:
wx.CallAfter( self.grid.EnableCellEditControl )
elif self.colCur == self.iCol['setLaps']:
race = Model.race
Expand Down
48 changes: 48 additions & 0 deletions DistanceCellEditor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import re

import wx.grid as gridlib

class DistanceCellEditor(gridlib.GridCellFloatEditor):
def __init__(self, *args, **kwargs):
kwargs.setdefault('width', 7)
kwargs.setdefault('precision', 3)
super().__init__(*args, **kwargs)

def EndEdit(self, row, col, grid, oldval):
new_value = self.Control.GetValue()
new_value = self.clean_float(new_value)

import sys
if new_value is not None and float(new_value) == -sys.float_info.max:
new_value = ''
elif new_value is None:
new_value = ''

if oldval == new_value:
return None

self.Control.SetValue(new_value)
return new_value

@staticmethod
def clean_float(float_str:str) -> str | None:
if not float_str:
return None
cleaned_str = float_str
cleaned_str = cleaned_str.replace(',', '.')
# Use regex to keep only digits, decimal points, and minus signs
cleaned_str = re.sub(r'[^0-9.-]', '', cleaned_str)
cleaned_str = cleaned_str.count('.') > 1 and cleaned_str.replace('.', '', 1) or cleaned_str
return cleaned_str

def BeginEdit(self, row, col, grid):
currentValue = grid.GetCellValue(row, col)
try:
parsed = self.clean_float(currentValue)
except ValueError:
return None
if parsed is None:
parsed = ''
self.Control.SetValue(parsed)
self.Control.SetFocus()
self.Control.SetSelection(0, self.Control.GetLastPosition())