-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwidgets.py
311 lines (271 loc) · 12 KB
/
widgets.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
# Wahoo! Results - https://github.com/JohnStrunk/wahoo-results
# Copyright (C) 2020 - John D. Strunk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
TKinter code to display a button that presents a colorpicker.
"""
import os
from tkinter import (
VERTICAL,
Canvas,
StringVar,
TclError,
Widget,
colorchooser,
filedialog,
ttk,
)
from typing import Any, Optional
import PIL.Image as PILImage
from PIL import ImageTk # type: ignore
import scoreboard
from model import (
ChromecastStatusVar,
ImageVar,
RaceResultListVar,
RaceResultVar,
StartListVar,
)
from racetimes import RawTime
TkContainer = Any
def swatch(width: int, height: int, color: str) -> ImageTk.PhotoImage:
"""Generate a color swatch"""
img = PILImage.new("RGBA", (width, height), color)
return ImageTk.PhotoImage(img)
class ColorButton2(ttk.Button): # pylint: disable=too-many-ancestors
"""Displays a button that allows choosing a color."""
SWATCH_SIZE = 12
def __init__(self, parent: Widget, color_var: StringVar):
if color_var.get() == "":
color_var.set("#000000")
self._img = swatch(self.SWATCH_SIZE, self.SWATCH_SIZE, color_var.get())
# super().__init__(parent, bg=color_var.get(), relief="solid",
# padx=9, command=self._btn_cb)
super().__init__(parent, command=self._btn_cb, image=self._img, padding=0)
self._color_var = color_var
def _on_change(_a, _b, _c):
try:
self._img = swatch(self.SWATCH_SIZE, self.SWATCH_SIZE, color_var.get())
self.configure(image=self._img)
except TclError: # configuring an invalid color throws
pass
self._color_var.trace_add("write", _on_change)
def _btn_cb(self) -> None:
(_, rgb) = colorchooser.askcolor(self._color_var.get())
if rgb is not None:
self._color_var.set(rgb)
class Preview(Canvas):
"""A widget that displays a scoreboard preview image"""
WIDTH = 320
HEIGHT = 180
def __init__(self, parent: Widget, image_var: ImageVar):
super().__init__(parent, width=self.WIDTH, height=self.HEIGHT)
self._pimage: Optional[ImageTk.PhotoImage] = None
self._image_var = image_var
image_var.trace_add("write", lambda *_: self._set_image(self._image_var.get()))
def _set_image(self, image: PILImage.Image) -> None:
"""Set the preview image"""
self.delete("all")
scaled = image.resize((self.WIDTH, self.HEIGHT))
# Note: In order for the image to display on the canvas, we need to
# keep a reference to it, so it gets assigned to _pimage even though
# it's not used anywhere else.
self._pimage = ImageTk.PhotoImage(scaled)
self.create_image(0, 0, image=self._pimage, anchor="nw")
class StartListTreeView(ttk.Frame):
"""Widget to display a set of startlists"""
def __init__(self, parent: Widget, startlist: StartListVar):
super().__init__(parent)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.tview = ttk.Treeview(self, columns=["event", "desc", "heats"])
self.tview.grid(column=0, row=0, sticky="news")
self.scroll = ttk.Scrollbar(self, orient=VERTICAL, command=self.tview.yview)
self.scroll.grid(column=1, row=0, sticky="news")
self.tview.configure(
selectmode="none", show="headings", yscrollcommand=self.scroll.set
)
self.tview.column("event", anchor="w", minwidth=40, width=40)
self.tview.heading("event", anchor="w", text="Event")
self.tview.column("desc", anchor="w", minwidth=220, width=220)
self.tview.heading("desc", anchor="w", text="Description")
self.tview.column("heats", anchor="w", minwidth=40, width=40)
self.tview.heading("heats", anchor="w", text="Heats")
self.startlist = startlist
startlist.trace_add("write", lambda *_: self._update_contents())
def _update_contents(self):
self.tview.delete(*self.tview.get_children())
local_list = self.startlist.get()
for entry in local_list:
self.tview.insert(
"",
"end",
id=str(entry.event_num),
values=[str(entry.event_num), entry.event_name, str(entry.heats)],
)
class DirSelection(ttk.Frame):
"""Directory selector widget"""
def __init__(self, parent: Widget, directory: StringVar):
super().__init__(parent)
self.dir = directory
self.columnconfigure(1, weight=1)
self.btn = ttk.Button(self, text="Browse...", command=self._handle_browse)
self.btn.grid(column=0, row=0, sticky="news")
self.dir_label = StringVar()
ttk.Label(self, textvariable=self.dir_label, relief="sunken").grid(
column=1, row=0, sticky="news"
)
self.dir.trace_add(
"write",
lambda *_: self.dir_label.set(os.path.basename(self.dir.get())[-20:]),
)
self.dir.set(self.dir.get())
def _handle_browse(self) -> None:
directory = filedialog.askdirectory(initialdir=self.dir.get())
if len(directory) == 0:
return
directory = os.path.normpath(directory)
self.dir.set(directory)
class RaceResultTreeView(ttk.Frame):
"""Widget that displays a table of completed races"""
def __init__(self, parent: Widget, racelist: RaceResultListVar):
super().__init__(parent)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.tview = ttk.Treeview(self, columns=["meet", "event", "heat", "time"])
self.tview.grid(column=0, row=0, sticky="news")
self.scroll = ttk.Scrollbar(self, orient=VERTICAL, command=self.tview.yview)
self.scroll.grid(column=1, row=0, sticky="news")
self.tview.configure(
selectmode="none", show="headings", yscrollcommand=self.scroll.set
)
self.tview.column("meet", anchor="w", minwidth=50, width=50)
self.tview.heading("meet", anchor="w", text="Meet")
self.tview.column("event", anchor="w", minwidth=50, width=50)
self.tview.heading("event", anchor="w", text="Event")
self.tview.column("heat", anchor="w", minwidth=50, width=50)
self.tview.heading("heat", anchor="w", text="Heat")
self.tview.column("time", anchor="w", minwidth=140, width=140)
self.tview.heading("time", anchor="w", text="Time")
self.racelist = racelist
racelist.trace_add("write", lambda *_: self._update_contents())
def _update_contents(self):
self.tview.delete(*self.tview.get_children())
local_list = self.racelist.get()
# Sort the list by date, descending
# https://stackoverflow.com/a/39359270
local_list.sort(key=lambda e: e.time_recorded, reverse=True)
for entry in local_list:
timetext = entry.time_recorded.strftime("%Y-%m-%d %H:%M:%S")
self.tview.insert(
"",
"end",
id=timetext,
values=[str(entry.meet_id), entry.event, entry.heat, timetext],
)
class ChromcastSelector(ttk.Frame):
"""Widget that allows enabling/disabling a set of Chromecast devices"""
def __init__(self, parent: Widget, statusvar: ChromecastStatusVar) -> None:
super().__init__(parent)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.tview = ttk.Treeview(self, columns=["enabled", "cc_name"])
self.tview.grid(column=0, row=0, sticky="news")
self.scroll = ttk.Scrollbar(self, orient=VERTICAL, command=self.tview.yview)
self.scroll.grid(column=1, row=0, sticky="news")
self.tview.configure(
selectmode="none", show="headings", yscrollcommand=self.scroll.set
)
self.tview.column("enabled", anchor="center", width=30)
self.tview.heading("enabled", anchor="center", text="Enabled")
self.tview.column("cc_name", anchor="w", minwidth=100)
self.tview.heading("cc_name", anchor="w", text="Chromecast name")
self.devstatus = statusvar
self.devstatus.trace_add("write", lambda *_: self._update_contents())
# Needs to be the ButtonRelease event because the Button event happens
# before the focus is actually set/changed.
self.tview.bind("<ButtonRelease-1>", self._item_clicked)
self._update_contents()
def _update_contents(self) -> None:
self.tview.delete(*self.tview.get_children())
local_list = self.devstatus.get()
# Sort them by name for display
local_list.sort(key=lambda d: (d.name))
for dev in local_list:
txt_status = "Yes" if dev.enabled else "No"
self.tview.insert(
"", "end", id=str(dev.uuid), values=[txt_status, dev.name]
)
def _item_clicked(self, _event) -> None:
item = self.tview.focus()
if len(item) == 0:
return
local_list = self.devstatus.get()
for dev in local_list:
if str(dev.uuid) == item:
dev.enabled = not dev.enabled
self.devstatus.set(local_list)
class RaceResultView(ttk.LabelFrame):
"""Widget that displays a RaceResult"""
def __init__(self, parent: Widget, resultvar: RaceResultVar) -> None:
super().__init__(parent, text="Latest result")
self._resultvar = resultvar
self._resultvar.trace_add("write", lambda *_: self._update())
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.tview = ttk.Treeview(
self,
columns=["lane", "t1", "t2", "t3", "final"],
selectmode="none",
show="headings",
)
self.tview.grid(column=0, row=0, sticky="news")
# Column configuration
time_width = 70
self.tview.heading("lane", anchor="e", text="Lane")
self.tview.column("lane", anchor="e", width=40)
self.tview.heading("t1", anchor="e", text="Timer #1")
self.tview.column("t1", anchor="e", width=time_width)
self.tview.heading("t2", anchor="e", text="Timer #2")
self.tview.column("t2", anchor="e", width=time_width)
self.tview.heading("t3", anchor="e", text="Timer #3")
self.tview.column("t3", anchor="e", width=time_width)
self.tview.heading("final", anchor="e", text="Final")
self.tview.column("final", anchor="e", width=time_width)
self._update()
def _update(self) -> None:
self.tview.delete(*self.tview.get_children())
result = self._resultvar.get()
for lane in range(1, 11):
if result is None:
self.tview.insert(
"", "end", id=str(lane), values=[str(lane), "", "", "", ""]
)
else:
rawtimes = result.raw_times(lane)
timestr = [
scoreboard.format_time(t) if t is not None else "" for t in rawtimes
]
final = result.final_time(lane)
if final.value == RawTime("0"):
finalstr = ""
else:
finalstr = scoreboard.format_time(final.value)
self.tview.insert(
"",
"end",
id=str(lane),
values=[str(lane), timestr[0], timestr[1], timestr[2], finalstr],
)