forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prizes.py
143 lines (118 loc) · 3.94 KB
/
Prizes.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
import wx
import os
import six
import sys
import math
import Utils
import Model
from GetResults import GetResults
from ReorderableGrid import ReorderableGrid
class Prizes( wx.Panel ):
rowsMax = 20
def __init__( self, parent, id=wx.ID_ANY, size=wx.DefaultSize ):
super(Prizes, self).__init__( parent, id, size=size )
vsOverall = wx.BoxSizer( wx.VERTICAL )
self.grid = ReorderableGrid( self )
self.grid.CreateGrid( 0, 10 )
self.grid.AutoSizeColumns( False )
self.grid.AutoSizeRows( False )
self.grid.Bind( wx.grid.EVT_GRID_CELL_CHANGED, self.onCellChange )
#---------------------------------------------------------------
vsOverall.Add( self.grid, 1, flag=wx.EXPAND|wx.ALL, border=4 )
self.SetSizer( vsOverall )
def onCellChange( self, event ):
race = Model.race
if not race:
return
row, col = event.GetRow(), event.GetCol()
if col & 1:
return
categories = race.getCategories( startWaveOnly=False, publishOnly=True )
if self.grid.GetNumberCols() != len(categories)*2:
Utils.AdjustGridSize( self.grid, self.rowsMax, len(categories)*2 )
if row >= self.grid.GetNumberRows() or col >= self.grid.GetNumberCols():
return
self.copyToRace()
try:
category = categories[col//2]
except IndexError:
return
self.grid.SetCellValue( row, col+1, self.getRecepient(self.grid.GetCellValue(row, col), row, category) )
wx.CallAfter( self.grid.AutoSizeColumns, False )
def getRecepient( self, prize, row, category ):
if not prize:
return ''
name = ''
results = GetResults( category )
try:
name = u'{}: {}'.format(results[row].num, results[row].full_name())
except IndexError:
pass
return name
def setCellPair( self, row, col, category ):
try:
prize = getattr( category, 'prizes', [] )[row]
except IndexError:
prize = ''
self.grid.SetCellValue( row, col, prize )
self.grid.SetCellValue( row, col+1, self.getRecepient(prize, row, category) )
def updateGrid( self ):
race = Model.race
if not race:
self.grid.ClearGrid()
return
categories = race.getCategories( startWaveOnly=False, publishOnly=True )
Utils.AdjustGridSize( self.grid, self.rowsMax, len(categories)*2 )
col = 0
for category in categories:
fullname = category.fullname
ib = fullname.rfind( '(' )
catname, catgender = fullname[:ib].strip(), fullname[ib:]
colName = '{}\n{}'.format( catname, catgender )
self.grid.SetColLabelValue( col, colName )
attr = wx.grid.GridCellAttr()
attr.SetReadOnly( False )
attr.SetAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
self.grid.SetColAttr( col, attr )
self.grid.SetColLabelValue( col+1, _('Recipient') )
attr = wx.grid.GridCellAttr()
attr.SetReadOnly( True )
attr.SetAlignment( wx.ALIGN_LEFT, wx.ALIGN_CENTRE )
attr.SetBackgroundColour( wx.Colour(152,251,152) )
self.grid.SetColAttr( col+1, attr )
for row in range(self.rowsMax):
self.setCellPair( row, col, category )
col += 2
self.grid.AutoSizeColumns( False ) # Resize to fit the column name.
self.grid.AutoSizeRows( False )
def refresh( self ):
self.updateGrid()
def copyToRace( self ):
race = Model.race
if not race:
return
categories = race.getCategories( startWaveOnly=False, publishOnly=True )
for i, category in enumerate(categories):
prizes = []
for row in range(self.rowsMax):
v = self.grid.GetCellValue( row, i*2 ).strip()
if not v:
break
prizes.append( v )
category.prizes = prizes
def commit( self ):
self.grid.SaveEditControlValue() # Make sure the current edit is committed.
self.grid.DisableCellEditControl()
self.copyToRace()
if __name__ == '__main__':
app = wx.App(False)
app.SetAppName("CrossMgr")
Utils.disable_stdout_buffering()
race = Model.newRace()
race._populate()
fnameRiderInfo = os.path.join(Utils.getHomeDir(), 'SimulationRiderData.xlsx')
mainWin = wx.Frame(None, title="Prizes", size=(800,700) )
prizes = Prizes( mainWin )
mainWin.Show()
prizes.refresh()
app.MainLoop()