-
Notifications
You must be signed in to change notification settings - Fork 6
/
main_window.py
449 lines (409 loc) · 17.3 KB
/
main_window.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
# 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/>.
"""The main window."""
import os
import sys
from tkinter import FALSE, HORIZONTAL, Menu, StringVar, TclError, Tk, Widget, font, ttk
from PIL import ImageTk
import widgets
from model import Model
from tooltip import ToolTip
_PADDING = 2
_TXT_X_PAD = 5
_TXT_Y_PAD = 1
_TXT_PAD = (_TXT_X_PAD, _TXT_Y_PAD)
class View(ttk.Frame):
"""Main window view definition."""
def __init__(self, root: Tk, vm: Model):
"""Create the main window.
:param root: The Tk root window
:param vm: The application model
"""
super().__init__(root)
self._root = root
self._vm = vm
root.title("Wahoo! Results")
# Common screen sizes: HD=1366x768 FHD=1920x1080
# Fix the window to 1/2 the size of the screen so that it's manageable
root.resizable(False, False)
root.geometry(f"{1366//2}x{768//2}")
bundle_dir = getattr(
sys, "_MEIPASS", os.path.abspath(os.path.dirname(__file__))
)
icon_file = os.path.abspath(os.path.join(bundle_dir, "media", "wr-icon.ico"))
root.iconphoto(True, ImageTk.PhotoImage(file=icon_file)) # type: ignore
try:
root.iconbitmap(icon_file)
except TclError: # On linux, we can't set a Windows icon file
pass
# Insert ourselves into the main window
self.pack(side="top", fill="both", expand=True)
self._build_menu()
# App close button is same as Exit menu option
root.protocol("WM_DELETE_WINDOW", vm.menu_exit.run)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
book = ttk.Notebook(self)
book.grid(column=0, row=0, sticky="news")
book.add(
_configTab(book, self._vm), text="Configuration", underline=0, sticky="news"
)
book.add(
_dirsTab(book, self._vm), text="Directories", underline=0, sticky="news"
)
book.add(_runTab(book, self._vm), text="Run", underline=0, sticky="news")
book.enable_traversal() # So that Alt-<letter> switches tabs
statusbar = ttk.Frame(self, padding=_PADDING)
statusbar.grid(column=0, row=1, sticky="news")
statusbar.columnconfigure(0, weight=1)
ttk.Label(
statusbar, textvariable=self._vm.version, justify="right", relief="sunken"
).grid(column=1, row=0, sticky="news")
statustext = ttk.Label(
statusbar,
textvariable=self._vm.statustext,
justify="left",
relief="sunken",
foreground="blue",
)
statustext.grid(column=0, row=0, sticky="news")
statustext.bind("<Button-1>", lambda *_: self._vm.statusclick.run())
style = ttk.Style()
style.configure("TCombobox", padding=_TXT_PAD) # Font drop-downs
style.configure("TLabel", padding=_TXT_PAD)
style.configure("TEntry", padding=_TXT_PAD)
style.configure("TSpinbox", padding=_TXT_PAD)
style.configure("TButton", padding=_PADDING)
style.configure("TLabelframe", padding=_PADDING)
def _build_menu(self) -> None:
"""Create the dropdown menus."""
self._root.option_add("*tearOff", FALSE) # We don't use tear-off menus
menubar = Menu(self)
self._root["menu"] = menubar
# File menu
file_menu = Menu(menubar)
menubar.add_cascade(menu=file_menu, label="File", underline=0)
file_menu.add_command(
label="Save scoreboard...",
underline=0,
command=self._vm.menu_save_scoreboard.run,
)
file_menu.add_command(
label="Export template...",
underline=0,
command=self._vm.menu_export_template.run,
)
file_menu.add_separator()
file_menu.add_command(label="Exit", underline=1, command=self._vm.menu_exit.run)
# Help menu
help_menu = Menu(menubar, name="help")
menubar.add_cascade(menu=help_menu, label="Help", underline=0)
help_menu.add_command(
label="Documentation", underline=0, command=self._vm.menu_docs.run
)
help_menu.add_command(
label="About", underline=0, command=self._vm.menu_about.run
)
class _configTab(ttk.Frame):
def __init__(self, parent: Widget, vm: Model) -> None:
super().__init__(parent)
self._vm = vm
self.columnconfigure(0, weight=1, uniform="same1")
self.columnconfigure(1, weight=1, uniform="same1")
self.rowconfigure(0, weight=1)
self._appearance(self).grid(column=0, row=0, rowspan=2, sticky="news")
self._options_frame(self).grid(column=1, row=0, sticky="news")
self._preview(self).grid(column=1, row=1, sticky="news")
def _appearance(self, parent: Widget) -> Widget: # noqa: PLR0915
mainframe = ttk.LabelFrame(parent, text="Appearance")
txt_frame = ttk.Frame(mainframe)
txt_frame.pack(side="top", fill="x")
txt_frame.columnconfigure(1, weight=1) # col 1 gets any extra space
ttk.Label(txt_frame, text="Main font:", anchor="e").grid(
column=0, row=0, sticky="news"
)
main_dd = ttk.Combobox(
txt_frame, textvariable=self._vm.font_normal, values=sorted(font.families())
)
main_dd.grid(column=1, row=0, sticky="news", pady=_PADDING)
ToolTip(main_dd, "Main font used for scoreboard text")
# Update dropdown if textvar is changed
self._vm.font_normal.trace_add(
"write", lambda *_: main_dd.set(self._vm.font_normal.get())
)
# Set initial value
main_dd.set(self._vm.font_normal.get())
ttk.Label(txt_frame, text="Time font:", anchor="e").grid(
column=0, row=1, sticky="news"
)
time_dd = ttk.Combobox(
txt_frame, textvariable=self._vm.font_time, values=sorted(font.families())
)
time_dd.grid(column=1, row=1, sticky="news", pady=_PADDING)
ToolTip(
time_dd, "Font for displaying the times - Recommended: fixed-width font"
)
# Update dropdown if textvar is changed
self._vm.font_time.trace_add(
"write", lambda *_: time_dd.set(self._vm.font_time.get())
)
# Set initial value
time_dd.set(self._vm.font_time.get())
ttk.Label(txt_frame, text="Title:", anchor="e").grid(
column=0, row=2, sticky="news"
)
hentry = ttk.Entry(txt_frame, textvariable=self._vm.title)
hentry.grid(column=1, row=2, sticky="news", pady=_PADDING)
ToolTip(hentry, "Title text to display")
ttk.Label(txt_frame, text="Text spacing:", anchor="e").grid(
column=0, row=3, sticky="news"
)
spspin = ttk.Spinbox(
txt_frame,
from_=0.8,
to=2.0,
increment=0.05,
width=4,
format="%0.2f",
textvariable=self._vm.text_spacing,
)
spspin.grid(column=1, row=3, sticky="nws", pady=_PADDING)
ToolTip(spspin, "Vertical space between text lines")
ttk.Separator(mainframe, orient=HORIZONTAL).pack(side="top", fill="x", pady=10)
colorframe = ttk.Frame(mainframe)
colorframe.pack(side="top", fill="x")
colorframe.columnconfigure(1, weight=1)
colorframe.columnconfigure(3, weight=1)
colorframe.columnconfigure(5, weight=1)
# 1st col
ttk.Label(colorframe, text="Heading:", anchor="e").grid(
column=0, row=0, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_title).grid(
column=1, row=0, sticky="nws", pady=_PADDING
)
ttk.Label(colorframe, text="Event:", anchor="e").grid(
column=0, row=1, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_event).grid(
column=1, row=1, sticky="nws", pady=_PADDING
)
# 2nd col
ttk.Label(colorframe, text="1st place:", anchor="e").grid(
column=2, row=0, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_first).grid(
column=3, row=0, sticky="nws", pady=_PADDING
)
ttk.Label(colorframe, text="2nd place:", anchor="e").grid(
column=2, row=1, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_second).grid(
column=3, row=1, sticky="nws", pady=_PADDING
)
ttk.Label(colorframe, text="3rd place:", anchor="e").grid(
column=2, row=2, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_third).grid(
column=3, row=2, sticky="nws", pady=_PADDING
)
# 3rd col
ttk.Label(colorframe, text="Odd rows:", anchor="e").grid(
column=4, row=0, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_odd).grid(
column=5, row=0, sticky="nws", pady=_PADDING
)
ttk.Label(colorframe, text="Even rows:", anchor="e").grid(
column=4, row=1, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_even).grid(
column=5, row=1, sticky="nws", pady=_PADDING
)
ttk.Label(colorframe, text="Background:", anchor="e").grid(
column=4, row=2, sticky="news"
)
widgets.ColorButton2(colorframe, color_var=self._vm.color_bg).grid(
column=5, row=2, sticky="nws", pady=_PADDING
)
ttk.Separator(mainframe, orient=HORIZONTAL).pack(side="top", fill="x", pady=10)
bgframelabels = ttk.Frame(mainframe)
bgframelabels.pack(side="top", fill="x")
bgframelabels.columnconfigure(1, weight=1)
ttk.Label(bgframelabels, text="Image brightness:", anchor="e").grid(
column=0, row=0, sticky="news"
)
bg_bgight_spin = ttk.Spinbox(
bgframelabels,
from_=0,
to=100,
increment=5,
width=4,
textvariable=self._vm.brightness_bg,
)
bg_bgight_spin.grid(column=1, row=0, sticky="nws", pady=_PADDING)
ToolTip(bg_bgight_spin, "Brightness of the background image (percent: 0-100)")
ttk.Label(bgframelabels, text="Background image:", anchor="e").grid(
column=0, row=1, sticky="news"
)
self._bg_img_label = StringVar()
bg_img_label = ttk.Label(
bgframelabels, textvariable=self._bg_img_label, anchor="w", relief="sunken"
)
bg_img_label.grid(column=1, row=1, sticky="news")
ToolTip(bg_img_label, "Scoreboard background image - Recommended: 1280x720")
self._vm.image_bg.trace_add(
"write",
lambda *_: self._bg_img_label.set(
os.path.basename(self._vm.image_bg.get())[-20:]
),
)
self._vm.image_bg.set(self._vm.image_bg.get())
bgframebtns = ttk.Frame(mainframe)
bgframebtns.pack(side="top", fill="x")
ttk.Button(bgframebtns, text="Import...", command=self._vm.bg_import.run).pack(
side="left", fill="both", expand=1, padx=_PADDING, pady=_PADDING
)
ttk.Button(bgframebtns, text="Clear", command=self._vm.bg_clear.run).pack(
side="left", fill="both", expand=1, padx=_PADDING, pady=_PADDING
)
return mainframe
def _options_frame(self, parent: Widget) -> Widget:
opt_frame = ttk.LabelFrame(parent, text="Options")
ttk.Label(opt_frame, text="Lanes:", anchor="e").grid(
column=0, row=0, sticky="news"
)
lspin = ttk.Spinbox(
opt_frame,
from_=6,
to=10,
increment=1,
width=3,
textvariable=self._vm.num_lanes,
)
lspin.grid(column=1, row=0, sticky="news", pady=_PADDING)
ToolTip(lspin, "Number of lanes to display")
ttk.Label(opt_frame, text="Minimum times:", anchor="e").grid(
column=0, row=1, sticky="news"
)
tspin = ttk.Spinbox(
opt_frame,
from_=1,
to=3,
increment=1,
width=3,
textvariable=self._vm.min_times,
)
tspin.grid(column=1, row=1, sticky="news", pady=_PADDING)
ToolTip(
tspin,
"Lanes with fewer than this number of raw times will"
+ " display dashes instead of a time",
)
ttk.Label(opt_frame, text="Time threshold:", anchor="e").grid(
column=0, row=2, sticky="news"
)
thresh = ttk.Spinbox(
opt_frame,
from_=0.01,
to=9.99,
increment=0.1,
width=4,
textvariable=self._vm.time_threshold,
)
thresh.grid(column=1, row=2, sticky="news", pady=_PADDING)
ToolTip(
thresh,
"Lanes with any raw times that differ from the final"
+ " time more than this amount will display dashes",
)
return opt_frame
def _preview(self, parent: Widget) -> Widget:
frame = ttk.LabelFrame(parent, text="Scoreboard preview")
frame.columnconfigure(0, weight=1)
widgets.Preview(frame, self._vm.appearance_preview).grid(column=0, row=0)
ToolTip(frame, "Mockup of how the scoreboard will look")
return frame
class _dirsTab(ttk.Frame):
def __init__(self, parent: Widget, vm: Model) -> None:
super().__init__(parent)
self._vm = vm
self.columnconfigure(0, weight=1, uniform="same1")
self.columnconfigure(1, weight=1, uniform="same1")
self.rowconfigure(0, weight=1)
self._start_list(self).grid(column=0, row=0, sticky="news", padx=1, pady=1)
self._race_results(self).grid(column=1, row=0, sticky="news", padx=1, pady=1)
def _start_list(self, parent: Widget) -> Widget:
frame = ttk.LabelFrame(parent, text="Start lists")
dirsel = widgets.DirSelection(frame, self._vm.dir_startlist)
dirsel.grid(column=0, row=0, sticky="news", padx=1, pady=1)
ToolTip(dirsel, "Directory where start list (*.scb) files are located")
sltv = widgets.StartListTreeView(frame, self._vm.startlist_contents)
sltv.grid(column=0, row=1, sticky="news", padx=1, pady=1)
ToolTip(sltv, "List of events found in the start list directory")
expbtn = ttk.Button(
frame,
padding=(8, 0),
text="Export events to Dolphin...",
command=self._vm.dolphin_export.run,
)
expbtn.grid(column=0, row=2, padx=1, pady=1)
ToolTip(
expbtn,
'Create "dolphin_events.csv" event list for import'
+ " into CTS Dolphin software",
)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(1, weight=1)
return frame
def _race_results(self, parent: Widget) -> Widget:
frame = ttk.LabelFrame(parent, text="Race results")
widgets.DirSelection(frame, self._vm.dir_results).grid(
column=0, row=0, sticky="news", padx=1, pady=1
)
widgets.RaceResultTreeView(frame, self._vm.results_contents).grid(
column=0, row=1, sticky="news", padx=1, pady=1
)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(1, weight=1)
return frame
class _runTab(ttk.Frame):
def __init__(self, parent: Widget, vm: Model) -> None:
super().__init__(parent)
self._vm = vm
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.rowconfigure(0, weight=1)
self._cc_selector(self).grid(column=1, row=0, sticky="news")
self._preview(self).grid(column=1, row=1, sticky="news")
latestres = widgets.RaceResultView(self, self._vm.latest_result)
latestres.grid(column=0, row=0, rowspan=2, sticky="news")
ToolTip(latestres, "Raw data from the latest race result")
def _cc_selector(self, parent: Widget) -> Widget:
frame = ttk.LabelFrame(parent, text="Available Chromecasts")
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
ccs = widgets.ChromcastSelector(frame, self._vm.cc_status)
ccs.grid(column=0, row=0, sticky="news")
ToolTip(ccs, "Chromecasts that have been detected. Click to toggle.")
return frame
def _preview(self, parent: Widget) -> Widget:
frame = ttk.LabelFrame(parent, text="Scoreboard preview")
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
widgets.Preview(frame, self._vm.scoreboard).grid(column=0, row=0)
ToolTip(frame, "Current contents of the scoreboard")
return frame