-
Notifications
You must be signed in to change notification settings - Fork 20
/
Actions.py
495 lines (402 loc) · 16.2 KB
/
Actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import wx
from wx.lib.wordwrap import wordwrap
from roundbutton import RoundButton
from HighPrecisionTimeEdit import HighPrecisionTimeEdit
import datetime
import Model
import Utils
import ChipReader
import OutputStreamer
from FtpWriteFile import realTimeFtpPublish
import Properties
from Undo import undo
import Checklist
from Clock import Clock
from CountdownClock import CountdownClock, EVT_COUNTDOWN
from Properties import PropertiesDialog
undoResetTimer = None
def StartRaceNow( page=_('Record') ):
global undoResetTimer
if undoResetTimer and undoResetTimer.IsRunning():
undoResetTimer.Stop()
undoResetTimer = None
ChipReader.chipReaderCur.reset( Model.race.chipReaderType if Model.race else None )
undo.clear()
undo.pushState()
with Model.LockRace() as race:
if race is None:
return
if not race.enableJChipIntegration:
race.resetStartClockOnFirstTag = False
Model.resetCache()
race.startRaceNow()
isTimeTrial = race.isTimeTrial
OutputStreamer.writeRaceStart()
# Refresh the main window and switch to the specified pane.
mainWin = Utils.getMainWin()
if mainWin is not None:
mainWin.showPageName( page )
mainWin.updateLapCounter()
mainWin.refresh()
if isTimeTrial:
mainWin.menuPublishHtmlTTStart()
# For safety, clear the undo stack after 8 seconds.
undoResetTimer = wx.CallLater( 8000, undo.clear )
if race.ftpUploadDuringRace:
realTimeFtpPublish.publishEntry( True )
def GetNowSeconds():
t = datetime.datetime.now()
return t.hour * 60 * 60 + t.minute * 60 + t.second
class StartRaceAtTime( wx.Dialog ):
def __init__( self, parent, id = wx.ID_ANY ):
super().__init__( parent, id, _("Start Race at Time:"),
style=wx.DEFAULT_DIALOG_STYLE|wx.TAB_TRAVERSAL )
self.startSeconds = None
self.timer = None
self.futureRaceTime = None
race = Model.getRace()
autoStartLabel = wx.StaticText( self, label = _('Automatically Start Race at:') )
# Make sure we don't suggest a start time in the past.
startSeconds = Utils.StrToSeconds( race.scheduledStart ) * 60 # race.scheduledStart has no seconds.
nowSeconds = GetNowSeconds()
if startSeconds < nowSeconds:
startOffset = 3 * 60
startSeconds = nowSeconds - nowSeconds % startOffset
startSeconds = nowSeconds + startOffset
autoStartTimeSize = wx.Size(80,-1)
self.autoStartTime = HighPrecisionTimeEdit( self, display_seconds=False, seconds=startSeconds, size=autoStartTimeSize )
self.pagesLabel = wx.StaticText( self, label=_('After Start, Switch to:') )
mainWin = Utils.getMainWin()
if mainWin:
pageNames = [name for a, b, name in mainWin.attrClassName]
else:
pageNames = [
_('Actions'),
_('Record'),
_('Results'),
_('Passings'),
_('RiderDetail'),
_('Chart'),
_('Animation'),
_('Recommendations'),
_('Categories'),
_('Properties'),
_('Primes'),
_('Situation'),
_('LapCounter'),
]
pageNames = pageNames[1:] # Skip the Actions screen.
self.pages = wx.Choice( self, choices=pageNames )
self.pages.SetSelection( 0 ) # Record screen.
self.countdown = CountdownClock( self, size=(400,400), tFuture=None )
self.countdown.SetBackgroundColour( wx.WHITE )
self.Bind( EVT_COUNTDOWN, self.onCountdown )
self.okBtn = wx.Button( self, wx.ID_OK, label=_('Start at Above Time') )
self.Bind( wx.EVT_BUTTON, self.onOK, self.okBtn )
self.start30 = wx.Button( self, label=_('Start in 30s') )
self.start30.Bind( wx.EVT_BUTTON, lambda event: self.startInFuture(event, 30) )
self.start60 = wx.Button( self, label=_('Start in 60s') )
self.start60.Bind( wx.EVT_BUTTON, lambda event: self.startInFuture(event, 60) )
self.cancelBtn = wx.Button( self, wx.ID_CANCEL )
self.Bind( wx.EVT_BUTTON, self.onCancel, self.cancelBtn )
vs = wx.BoxSizer( wx.VERTICAL )
border = 8
fgs = wx.FlexGridSizer( cols=4, vgap=8, hgap=8 )
fgs.AddGrowableCol( 1 )
fgs.Add( autoStartLabel, flag=wx.ALIGN_CENTER_VERTICAL )
fgs.Add( self.autoStartTime, flag=wx.ALIGN_CENTER_VERTICAL )
fgs.Add( self.pagesLabel, flag=wx.ALIGN_CENTER_VERTICAL )
fgs.Add( self.pages, flag=wx.ALIGN_CENTER_VERTICAL )
vs.Add( fgs, flag=wx.EXPAND|wx.ALL, border=8 )
hs = wx.BoxSizer( wx.HORIZONTAL )
hs.Add( self.okBtn, border = border, flag=wx.ALL )
hs.Add( self.start30, flag=wx.TOP|wx.BOTTOM|wx.RIGHT, border = border )
hs.Add( self.start60, flag=wx.TOP|wx.BOTTOM|wx.RIGHT, border = border)
self.okBtn.SetDefault()
hs.AddStretchSpacer()
hs.Add( self.cancelBtn, flag=wx.ALL, border = border )
vs.Add( hs, flag=wx.EXPAND )
vs.Add( self.countdown, 1, border = border, flag=wx.ALL|wx.EXPAND )
self.SetSizerAndFit( vs )
self.CentreOnParent(wx.BOTH)
wx.CallAfter( self.SetFocus )
wx.CallLater( 100, self.autoStartTime.SetSize, autoStartTimeSize )
def startInFuture( self, event, seconds ):
startSeconds = GetNowSeconds() + seconds
dt = wx.DateTime()
dt.SetToCurrent()
dt.SetHour( startSeconds//(60*60) )
dt.SetMinute( (startSeconds//60)%60 )
self.autoStartTime.SetValue( dt )
return self.onOK( event, startSeconds )
def onCountdown( self, event ):
StartRaceNow( self.pages.GetStringSelection() )
self.startTime = self.futureRaceTime
self.EndModal( wx.ID_OK )
def onOK( self, event, startSeconds = None ):
startTime = self.autoStartTime.GetSeconds()
self.startSeconds = startTime if startSeconds is None else startSeconds
if self.startSeconds < GetNowSeconds():
Utils.MessageOK(
None,
'\n\n'.join( [_('Scheduled Start Time is in the Past'),_('Please enter a Scheduled Start Time in the Future.')] ),
_('Scheduled Start Time is in the Past')
)
return
dateToday = datetime.date.today()
self.futureRaceTime = datetime.datetime(
year=dateToday.year, month=dateToday.month, day=dateToday.day,
hour=0, minute=0, second=0
) + datetime.timedelta( seconds = self.startSeconds )
self.countdown.Start( self.futureRaceTime )
# Disable buttons and switch to countdown state.
self.okBtn.Enable( False )
self.start30.Enable( False )
self.start60.Enable( False )
self.autoStartTime.Enable( False )
def onCancel( self, event ):
self.countdown.Stop()
self.EndModal( wx.ID_CANCEL )
#-------------------------------------------------------------------------------------------
StartText = '\n'.join(_('Start Race').split(maxsplit=1))
FinishText = '\n'.join(_('Finish Race').split(maxsplit=1))
class Actions( wx.Panel ):
iResetStartClockOnFirstTag = 1
iSkipFirstTagRead = 2
def __init__( self, parent, id = wx.ID_ANY ):
super().__init__(parent, id)
self.SetBackgroundColour( wx.Colour(255,255,255) )
ps = wx.BoxSizer( wx.VERTICAL )
self.splitter = wx.SplitterWindow( self, wx.VERTICAL )
ps.Add( self.splitter, 1, flag=wx.EXPAND )
self.SetSizer( ps )
#---------------------------------------------------------------------------------------------
self.leftPanel = wx.Panel( self.splitter )
bs = wx.BoxSizer( wx.VERTICAL )
self.leftPanel.SetSizer( bs )
self.leftPanel.SetBackgroundColour( wx.Colour(255,255,255) )
self.leftPanel.Bind( wx.EVT_SIZE, self.setWrappedRaceInfo )
buttonSize = 220
self.button = RoundButton( self.leftPanel, size=(buttonSize, buttonSize) )
self.button.SetLabel( FinishText )
self.button.SetFontToFitLabel()
self.button.SetForegroundColour( wx.Colour(128,128,128) )
self.Bind(wx.EVT_BUTTON, self.onPress, self.button )
self.clock = Clock( self.leftPanel, size=(190,190), checkFunc=self.updateClock )
self.clock.SetBackgroundColour( wx.WHITE )
self.raceIntro = wx.StaticText( self.leftPanel, label = '' )
self.raceIntro.SetFont( wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.NORMAL) )
self.chipTimingOptions = wx.RadioBox(
self.leftPanel,
label = _("Chip Timing Options"),
majorDimension = 1,
choices = Properties.RfidProperties.choices,
style = wx.RA_SPECIFY_COLS
)
self.Bind( wx.EVT_RADIOBOX, self.onChipTimingOptions, self.chipTimingOptions )
self.settingsButton = wx.BitmapButton( self.leftPanel, bitmap=Utils.GetPngBitmap('settings-icon.png') )
self.settingsButton.SetToolTip( wx.ToolTip(_('Properties Shortcut')) )
self.settingsButton.Bind( wx.EVT_BUTTON, self.onShowProperties )
self.startRaceTimeCheckBox = wx.CheckBox(self.leftPanel, label = _('Start Race Automatically at Future Time'))
hsSettings = wx.BoxSizer( wx.HORIZONTAL )
hsSettings.Add( self.settingsButton, flag=wx.ALIGN_CENTER_VERTICAL )
hsSettings.Add( self.startRaceTimeCheckBox, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=12 )
border = 8
hs = wx.BoxSizer( wx.HORIZONTAL )
hs.Add(self.button, border=border, flag=wx.LEFT|wx.TOP)
hs.Add(self.raceIntro, 1, border=border, flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND)
bs.Add( hs, border=border, flag=wx.ALL )
hsClock = wx.BoxSizer(wx.HORIZONTAL)
hsClock.AddSpacer( 26 )
hsClock.Add( self.clock )
hsClock.Add(hsSettings, border=4, flag=wx.LEFT )
bs.Add( hsClock, border=4, flag=wx.ALL )
bs.Add(self.chipTimingOptions, border=border, flag=wx.ALL)
#---------------------------------------------------------------------------------------------
self.rightPanel = wx.Panel( self.splitter )
self.rightPanel.SetBackgroundColour( wx.Colour(255,255,255) )
checklistTitle = wx.StaticText( self.rightPanel, label = _('Checklist:') )
checklistTitle.SetFont( wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) )
self.checklist = Checklist.Checklist( self.rightPanel )
hsSub = wx.BoxSizer( wx.VERTICAL )
hsSub.Add( checklistTitle, 0, flag=wx.ALL, border = 4 )
hsSub.Add( self.checklist, 1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border = 4 )
self.rightPanel.SetSizer( hsSub )
#---------------------------------------------------------------------------------------------
self.splitter.SplitVertically( self.leftPanel, self.rightPanel )
self.splitter.SetMinimumPaneSize( 100 )
wx.CallAfter( self.refresh )
wx.CallAfter( self.splitter.SetSashPosition, 650 )
wx.CallAfter( self.GetSizer().Layout )
def setWrappedRaceInfo( self, event = None ):
wrapWidth = self.leftPanel.GetClientSize()[0] - self.button.GetClientSize()[0] - 20
dc = wx.WindowDC( self.raceIntro )
dc.SetFont( self.raceIntro.GetFont() )
label = wordwrap( Model.race.getRaceIntro() if Model.race else '', wrapWidth, dc )
self.raceIntro.SetLabel( label )
self.leftPanel.GetSizer().Layout()
if event:
event.Skip()
def updateChipTimingOptions( self ):
if not Model.race:
return
iSelection = self.chipTimingOptions.GetSelection()
race = Model.race
race.resetStartClockOnFirstTag = bool(iSelection == self.iResetStartClockOnFirstTag)
race.skipFirstTagRead = bool(iSelection == self.iSkipFirstTagRead)
def updateClock( self ):
mainWin = Utils.getMainWin()
return not mainWin or mainWin.isShowingPage(self)
def onShowProperties( self, event ):
if not Model.race:
Utils.MessageOK(self, _("You must have a valid race. Open or New a race first."), _("No Valid Race"), iconMask=wx.ICON_ERROR)
return
if not hasattr(self, 'propertiesDialog'):
self.propertiesDialog = PropertiesDialog( self, showFileFields=False, updateProperties=True, size=(600,400) )
else:
self.propertiesDialog.properties.refresh( forceUpdate = True )
self.propertiesDialog.properties.setPage( 'raceOptionsProperties' )
if self.propertiesDialog.ShowModal() == wx.ID_OK:
self.propertiesDialog.properties.doCommit()
Utils.refresh()
def onChipTimingOptions( self, event ):
if not Model.race:
return
self.updateChipTimingOptions()
def onPress( self, event ):
if not Model.race:
return
with Model.LockRace() as race:
running = race.isRunning()
if running:
self.onFinishRace( event )
return
self.updateChipTimingOptions()
if getattr(Model.race, 'enableJChipIntegration', False):
try:
externalFields = race.excelLink.getFields()
externalInfo = race.excelLink.read()
except Exception:
externalFields = []
externalInfo = {}
if not externalInfo:
Utils.MessageOK(
self,
'\n\n'.join( [
_('Cannot Start.'),
_('This race uses RFID.'),
_('An Excel file containing the Bibs and RFID tags is required, and it is either unconfigured or unreadable.'),
_('You must have a valid Excel file, or turn off the RFID option in "Properties|RFID" to enter bibs manually.'),
_('See CrosMgr Help "RFID" for more details.'),
] ),
_('Excel Sheet Read Error'),
wx.ICON_ERROR
)
return
if not any( field.startswith('Tag') for field in externalFields) or not any( field.startswith('Bib') for field in externalFields ):
Utils.MessageOK(
self,
'\n\n'.join( [
_('Cannot Start.'),
_('This race uses RFID.'),
_('The linked Excel file must have a Bib and Tag column.'),
_('You must fix your Excel file, or turn off the RFID option in "Properties|RFID".'),
_('See CrosMgr Help "RFID" for more details.'),
] ),
_('Excel Sheet missing Tag column'),
wx.ICON_ERROR
)
return
if self.startRaceTimeCheckBox.IsChecked():
self.onStartRaceTime( event )
else:
self.onStartRace( event )
def onStartRace( self, event ):
if Model.race and Utils.MessageOKCancel(self, _('Start Race Now?'), _('Start Race')):
StartRaceNow()
def onStartRaceTime( self, event ):
if Model.race is None:
return
with StartRaceAtTime(self) as dlg:
dlg.ShowModal()
def onFinishRace( self, event, confirm=True ):
if Model.race is None:
return
if confirm and not Utils.MessageOKCancel(self, _('Finish Race Now?'), _('Finish Race')):
return
with Model.LockRace() as race:
race.finishRaceNow()
if race.numLaps is None:
race.numLaps = race.getMaxLap()
Model.resetCache()
Utils.writeRace()
self.refresh()
mainWin = Utils.getMainWin()
if mainWin:
mainWin.refresh()
OutputStreamer.writeRaceFinish()
OutputStreamer.StopStreamer()
try:
ChipReader.chipReaderCur.StopListener()
except Exception:
pass
if getattr(Model.race, 'ftpUploadDuringRace', False):
realTimeFtpPublish.publishEntry( True )
def commit( self ):
self.checklist.commit()
def refresh( self ):
self.clock.Start()
self.button.Enable( False )
self.startRaceTimeCheckBox.Enable( False )
self.settingsButton.Enable( False )
self.button.SetLabel( StartText )
self.button.SetForegroundColour( wx.Colour(100,100,100) )
self.chipTimingOptions.SetSelection( 0 )
self.chipTimingOptions.Enable( False )
with Model.LockRace() as race:
if race:
self.settingsButton.Enable( True )
# Adjust the chip recording options for TT.
if getattr(race, 'isTimeTrial', False):
race.resetStartClockOnFirstTag = False
race.skipFirstTagRead = False
if getattr(race, 'resetStartClockOnFirstTag', True):
self.chipTimingOptions.SetSelection( self.iResetStartClockOnFirstTag )
elif getattr(race, 'skipFirstTagRead', False):
self.chipTimingOptions.SetSelection( self.iSkipFirstTagRead )
if race.startTime is None:
self.button.Enable( True )
self.button.SetLabel( StartText )
self.button.SetForegroundColour( wx.Colour(0,128,0) )
self.startRaceTimeCheckBox.Enable( True )
self.startRaceTimeCheckBox.Show( True )
self.chipTimingOptions.Enable( getattr(race, 'enableJChipIntegration', False) )
self.chipTimingOptions.Show( getattr(race, 'enableJChipIntegration', False) )
elif race.isRunning():
self.button.Enable( True )
self.button.SetLabel( FinishText )
self.button.SetForegroundColour( wx.Colour(128,0,0) )
self.startRaceTimeCheckBox.Enable( False )
self.startRaceTimeCheckBox.Show( False )
self.chipTimingOptions.Enable( False )
self.chipTimingOptions.Show( False )
# Adjust the time trial display options.
if getattr(race, 'isTimeTrial', False):
self.chipTimingOptions.Enable( False )
self.chipTimingOptions.Show( False )
self.GetSizer().Layout()
self.setWrappedRaceInfo()
self.checklist.refresh()
mainWin = Utils.getMainWin()
if mainWin is not None:
mainWin.updateRaceClock()
if __name__ == '__main__':
app = wx.App(False)
mainWin = wx.Frame(None,title="CrossMan", size=(1024,600))
actions = Actions(mainWin)
Model.newRace()
Model.race.enableJChipIntegration = False
Model.race.isTimeTrial = False
actions.refresh()
mainWin.Show()
app.MainLoop()