-
Notifications
You must be signed in to change notification settings - Fork 6
/
score_frame.py
163 lines (128 loc) · 5.21 KB
/
score_frame.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
from __future__ import division
import wx
from collections import OrderedDict
_scores = OrderedDict()
class ScoreSettingsFrame(wx.MiniFrame):
# XXX Is this scoring or rating?
def __init__ (self, *args, **kwds) :
super(ScoreSettingsFrame, self).__init__(*args, **kwds)
szr = wx.BoxSizer(wx.VERTICAL)
panel = ScoreSettingsPanel(self)
self.SetSizer(szr)
szr.Add(panel, 1, wx.EXPAND)
szr.Fit(panel)
self.panel = panel
self.sizer = szr
self.Fit()
self.Bind(wx.EVT_CLOSE, lambda evt : self.Destroy(), self)
choices = ["HIT","MISS","MAYBE"]
shortcuts = "jkl"
class ScoreSettingsPanel(wx.Panel):
def __init__ (self, *args, **kwds) :
from wxtbx import bitmaps
super(ScoreSettingsPanel, self).__init__(*args, **kwds)
# Needed for communication with the root frame.
self._root_frame = self.GetParent().GetParent()
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
self._id_text = wx.NewId()
text = wx.StaticText(self, id= self._id_text, label="")
sizer.Add(text, flag=wx.ALIGN_CENTER)
# The score buttons. XXX Would really like this to be stars or
# some such. Could then zap the static text above.
box = wx.BoxSizer(wx.HORIZONTAL)
self._id_buttons = []
self._id_accel = []
accelerators = []
for idx,i in enumerate(choices):
btn = wx.Button(self, label=i)
box.Add(btn, flag=wx.ALIGN_CENTER, border=5)
self.Bind(wx.EVT_BUTTON, self.OnScore, btn)
self._id_buttons.append(btn.GetId())
randomId = wx.NewId()
self._id_accel.append(randomId)
self.Bind(wx.EVT_MENU, self.OnKeyCombo, id=randomId)
accelerators.append((wx.ACCEL_NORMAL, ord(shortcuts[idx]), randomId))
sizer.Add(box)
accel_tbl = wx.AcceleratorTable(accelerators)
self.SetAcceleratorTable(accel_tbl)
# XXX Would like to have label and short help for the buttons.
# Buttons in wxWidgets version 2.9.1 and later support text and
# bitmap, see SetBitmap, SetBitmapLabel, SetBitmapDisabled, etc.
box = wx.BoxSizer(wx.HORIZONTAL)
self._id_previous = wx.NewId()
btn = wx.BitmapButton(
self,
id=self._id_previous,
bitmap=bitmaps.fetch_icon_bitmap('actions', '1leftarrow'),
style=wx.BORDER_NONE)
box.Add(btn, flag=wx.ALIGN_CENTER, border=5)
self.Bind(wx.EVT_BUTTON, self.OnPrevious, btn)
btn = wx.Button(self, label="Save scores")
box.Add(btn, flag=wx.ALIGN_CENTER, border=5)
self.Bind(wx.EVT_BUTTON, self.OnSave, btn)
self._id_next = wx.NewId()
btn = wx.BitmapButton(
self,
id=self._id_next,
bitmap=bitmaps.fetch_icon_bitmap('actions', '1rightarrow'),
style=wx.BORDER_NONE)
box.Add(btn, flag=wx.ALIGN_CENTER, border=5)
self.Bind(wx.EVT_BUTTON, self.OnNext, btn)
sizer.Add(box, flag=wx.ALIGN_CENTER)
# Register update events for the dynamic widgets.
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateNext, id=self._id_next)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdatePrevious, id=self._id_previous)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateText, id=self._id_text)
for i in xrange(self._root_frame.image_chooser.GetCount()):
_scores[self._root_frame.get_key(self._root_frame.image_chooser.GetClientData(i))] = None
def OnKeyCombo(self, event):
print "You pressed one of jkl"
#from IPython import embed; embed()
score = choices[self._id_accel.index(event.GetId())]
key = self._root_frame.get_key(self._root_frame.image_chooser.GetClientData(self._root_frame.image_chooser.GetSelection()))
_scores[key] = score
print key, score, "with kybd shortcut"
self.OnNext(event)
def OnNext(self, event):
self._root_frame.OnNext(event)
def OnPrevious(self, event):
self._root_frame.OnPrevious(event)
def OnScore(self, event):
score = choices[self._id_buttons.index(event.EventObject.GetId())]
key = self._root_frame.get_key(self._root_frame.image_chooser.GetClientData(self._root_frame.image_chooser.GetSelection()))
_scores[key] = score
print key, score
self.OnNext(event)
def OnSave(self, event):
dialog = wx.FileDialog(
self,
defaultDir='',
message="Save scores",
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
wildcard="Text files (*.txt)|*.txt")
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
if (path != '') :
stream = open(path, "w")
for (key, score) in _scores.iteritems():
if score is None:
print >> stream, "%s None" % (key)
else:
print >> stream, "%s %s" % (key, score)
stream.close()
print "Dumped scores to", path
def OnUpdateNext(self, event):
root_next = self._root_frame.toolbar.FindById(wx.ID_FORWARD)
event.Enable(root_next.IsEnabled())
def OnUpdatePrevious(self, event):
root_previous = self._root_frame.toolbar.FindById(wx.ID_BACKWARD)
event.Enable(root_previous.IsEnabled())
def OnUpdateText(self, event):
key = self._root_frame.get_key(self._root_frame.image_chooser.GetClientData(self._root_frame.image_chooser.GetSelection()))
if key in _scores:
event.SetText("Previous score: %s" % _scores[key])
else:
event.SetText("Not previously scored")
# Call self.Layout() to recenter the updated text.
self.Layout()