-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmenu_bar.py
More file actions
518 lines (456 loc) · 21.5 KB
/
menu_bar.py
File metadata and controls
518 lines (456 loc) · 21.5 KB
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import json
import sys
import webbrowser
from functools import partial
from typing import TYPE_CHECKING, Any, Literal, cast
from urllib.error import URLError
from urllib.request import urlopen
from packaging.version import parse as version_parse
from PySide6 import QtCore, QtWidgets
from PySide6.QtCore import Qt
from PySide6.QtGui import QBrush, QPalette
from PySide6.QtWidgets import QFileDialog
from typing_extensions import override
import error_messages
import user_profile
from capture_method import (
CAPTURE_METHODS,
CameraInfo,
CaptureMethodEnum,
change_capture_method,
get_all_video_capture_devices,
)
from gen import about, design, settings as settings_ui, update_checker
from hotkeys import HOTKEYS, HOTKEYS_WHEN_AUTOCONTROLLED, CommandStr, set_hotkey
from utils import AUTOSPLIT_VERSION, GITHUB_REPOSITORY, ONE_SECOND, decimal, fire_and_forget
if TYPE_CHECKING:
from AutoSplit import AutoSplit
HALF_BRIGHTNESS = 128
LINUX_SCREENSHOT_SUPPORT = (
"\n\n----------------------------------------------------\n\n"
+ error_messages.WAYLAND_WARNING
# Keep in sync with README.md#Capture_Method_Linux
+ '\n"scrot" must be installed to use SCReenshOT. '
+ "\nRun: sudo apt-get install scrot"
) if sys.platform == "linux" else "" # fmt: skip
_DEBUG_SCREENSHOT_COMMANDS: tuple[CommandStr, ...] = (
"split",
"start",
"reset",
"undo",
"skip",
"pause",
)
class __AboutWidget(QtWidgets.QWidget, about.Ui_AboutAutoSplitWidget): # noqa: N801 # Private class
"""About Window."""
def __init__(self):
super().__init__()
self.setupUi(self)
self.created_by_label.setOpenExternalLinks(True)
self.donate_button_label.setOpenExternalLinks(True)
self.version_label.setText(f"Version: {AUTOSPLIT_VERSION}")
self.show()
def open_about(autosplit: "AutoSplit"):
if not autosplit.AboutWidget or cast(QtWidgets.QWidget, autosplit.AboutWidget).isHidden():
autosplit.AboutWidget = __AboutWidget()
class __UpdateCheckerWidget(QtWidgets.QWidget, update_checker.Ui_UpdateChecker): # noqa: N801 # Private class
def __init__(
self,
latest_version: str,
design_window: design.Ui_MainWindow,
*,
check_on_open: bool = False,
):
super().__init__()
self.setupUi(self)
self.current_version_number_label.setText(AUTOSPLIT_VERSION)
self.latest_version_number_label.setText(latest_version)
self.left_button.clicked.connect(self.open_update)
self.do_not_ask_again_checkbox.stateChanged.connect(self.do_not_ask_me_again_state_changed)
self.design_window = design_window
if version_parse(latest_version) > version_parse(AUTOSPLIT_VERSION):
self.do_not_ask_again_checkbox.setVisible(check_on_open)
self.left_button.setFocus()
self.show()
elif not check_on_open:
self.update_status_label.setText("You are on the latest AutoSplit version.")
self.go_to_download_label.setVisible(False)
self.left_button.setVisible(False)
self.right_button.setText("OK")
self.do_not_ask_again_checkbox.setVisible(False)
self.show()
def open_update(self):
webbrowser.open(f"https://github.com/{GITHUB_REPOSITORY}/releases/latest")
self.close()
def do_not_ask_me_again_state_changed(self):
user_profile.set_check_for_updates_on_open(
self.design_window,
self.do_not_ask_again_checkbox.isChecked(),
)
def open_update_checker(autosplit: "AutoSplit", latest_version: str, *, check_on_open: bool):
if (
not autosplit.UpdateCheckerWidget
or cast(QtWidgets.QWidget, autosplit.UpdateCheckerWidget).isHidden()
):
autosplit.UpdateCheckerWidget = __UpdateCheckerWidget(
latest_version,
autosplit,
check_on_open=check_on_open,
)
def view_help():
webbrowser.open(f"https://github.com/{GITHUB_REPOSITORY}/blob/main/docs/tutorial.md")
class __CheckForUpdatesThread(QtCore.QThread): # noqa: N801 # Private class
def __init__(self, autosplit: "AutoSplit", *, check_on_open: bool):
super().__init__()
self._autosplit_ref = autosplit
self.check_on_open = check_on_open
@override
def run(self):
try:
with urlopen(
f"https://api.github.com/repos/{GITHUB_REPOSITORY}/releases/latest",
timeout=30,
) as response:
json_response: dict[str, str] = json.loads(response.read())
latest_version = json_response["name"].split("v")[1]
self._autosplit_ref.update_checker_widget_signal.emit(
latest_version,
self.check_on_open,
)
except (URLError, KeyError):
if not self.check_on_open:
self._autosplit_ref.show_error_signal.emit(error_messages.check_for_updates)
def about_qt():
webbrowser.open("https://wiki.qt.io/About_Qt")
def about_qt_for_python():
webbrowser.open("https://wiki.qt.io/Qt_for_Python")
def check_for_updates(autosplit: "AutoSplit", *, check_on_open: bool = False):
autosplit.CheckForUpdatesThread = __CheckForUpdatesThread(
autosplit,
check_on_open=check_on_open,
)
autosplit.CheckForUpdatesThread.start()
class __SettingsWidget(QtWidgets.QWidget, settings_ui.Ui_SettingsWidget): # noqa: N801 # Private class
def __init__(self, autosplit: "AutoSplit"):
super().__init__()
self.__video_capture_devices: list[CameraInfo] = []
"""
Used to temporarily store the existing cameras,
we don't want to call `get_all_video_capture_devices` again
and possibly have a different result
"""
self.setupUi(self)
# Fix Fusion Dark Theme's tabs content looking weird because it's using the button role
window_color = self.palette().color(QPalette.ColorRole.Window)
if window_color.red() < HALF_BRIGHTNESS:
brush = QBrush(window_color)
brush.setStyle(Qt.BrushStyle.SolidPattern)
palette = QPalette()
palette.setBrush(QPalette.ColorGroup.Active, QPalette.ColorRole.Button, brush)
palette.setBrush(QPalette.ColorGroup.Inactive, QPalette.ColorRole.Button, brush)
palette.setBrush(QPalette.ColorGroup.Disabled, QPalette.ColorRole.Button, brush)
self.settings_tabs.setPalette(palette)
self._autosplit_ref = autosplit
self.__set_readme_link()
# Don't autofocus any particular field
self.setFocus()
# region Build the Capture method combobox # fmt: skip
capture_method_values = CAPTURE_METHODS.values()
self.__set_all_capture_devices()
# TODO: Word-wrapping works, but there's lots of extra padding to the right.
# Raise issue upstream
#
# list_view = QtWidgets.QListView()
# list_view.setWordWrap(True)
# list_view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
# list_view.setFixedWidth(self.capture_method_combobox.width())
# self.capture_method_combobox.setView(list_view)
self.capture_method_combobox.addItems([
f"- {method.name} ({method.short_description})" for method in capture_method_values
])
self.capture_method_combobox.setToolTip(
"\n\n".join(
f"{method.name} :\n{method.description}" for method in capture_method_values
)
+ LINUX_SCREENSHOT_SUPPORT
)
# endregion
self.__setup_bindings()
self.show()
def __update_default_threshold(self, value: Any):
self.__set_value("default_similarity_threshold", value)
self._autosplit_ref.table_current_image_threshold_label.setText(
decimal(self._autosplit_ref.split_image.get_similarity_threshold(self._autosplit_ref))
if self._autosplit_ref.split_image
else "-"
)
self._autosplit_ref.table_reset_image_threshold_label.setText(
decimal(self._autosplit_ref.reset_image.get_similarity_threshold(self._autosplit_ref))
if self._autosplit_ref.reset_image
else "-"
)
def __set_value(self, key: str, value: Any):
self._autosplit_ref.settings_dict[key] = value
def get_capture_device_index(self, capture_device_id: int):
"""Returns 0 if the capture_device_id is invalid."""
try:
return [
device.device_id # fmt: skip
for device in self.__video_capture_devices
].index(capture_device_id)
except ValueError:
return 0
def __enable_capture_device_if_its_selected_method(
self,
selected_capture_method: str | CaptureMethodEnum | None = None,
):
if selected_capture_method is None:
selected_capture_method = self._autosplit_ref.settings_dict["capture_method"]
is_video_capture_device = selected_capture_method == CaptureMethodEnum.VIDEO_CAPTURE_DEVICE
self.capture_device_combobox.setEnabled(is_video_capture_device)
if is_video_capture_device:
self.capture_device_combobox.setCurrentIndex(
self.get_capture_device_index(
self._autosplit_ref.settings_dict["capture_device_id"]
)
)
else:
self.capture_device_combobox.setPlaceholderText('Select "Video Capture Device" above')
self.capture_device_combobox.setCurrentIndex(-1)
def __capture_method_changed(self):
selected_capture_method = CAPTURE_METHODS.get_method_by_index(
self.capture_method_combobox.currentIndex()
)
self.__enable_capture_device_if_its_selected_method(selected_capture_method)
change_capture_method(selected_capture_method, self._autosplit_ref)
return selected_capture_method
def __capture_device_changed(self):
device_index = self.capture_device_combobox.currentIndex()
if device_index == -1:
return
capture_device = self.__video_capture_devices[device_index]
self._autosplit_ref.settings_dict["capture_device_name"] = capture_device.name
self._autosplit_ref.settings_dict["capture_device_id"] = capture_device.device_id
if (
self._autosplit_ref.settings_dict["capture_method"]
== CaptureMethodEnum.VIDEO_CAPTURE_DEVICE
):
# Re-initializes the VideoCaptureDeviceCaptureMethod
change_capture_method(CaptureMethodEnum.VIDEO_CAPTURE_DEVICE, self._autosplit_ref)
def __fps_limit_changed(self, value: int):
value = self.fps_limit_spinbox.value()
self._autosplit_ref.settings_dict["fps_limit"] = value
self._autosplit_ref.timer_live_image.setInterval(int(ONE_SECOND / value))
@fire_and_forget
def __set_all_capture_devices(self):
self.__video_capture_devices = get_all_video_capture_devices()
if len(self.__video_capture_devices) > 0:
for i in range(self.capture_device_combobox.count()):
self.capture_device_combobox.removeItem(i)
self.capture_device_combobox.addItems([
f"* {device.name}"
+ (f" [{device.backend}]" if device.backend else "")
+ (" (occupied)" if device.occupied else "")
for device in self.__video_capture_devices
])
self.__enable_capture_device_if_its_selected_method()
else:
self.capture_device_combobox.setPlaceholderText("No device found.")
def __set_readme_link(self):
self.custom_image_settings_info_label.setText(
self.custom_image_settings_info_label.text().format(GITHUB_REPOSITORY=GITHUB_REPOSITORY)
)
# HACK: This is a workaround because custom_image_settings_info_label
# simply will not open links with a left click no matter what we tried.
self.readme_link_button.clicked.connect(
lambda: webbrowser.open(f"https://github.com/{GITHUB_REPOSITORY}#readme")
)
self.readme_link_button.setStyleSheet("border: 0px; background-color:rgba(0,0,0,0%);")
if sys.platform == "linux":
geometry = self.readme_link_button.geometry()
# In-button font has different width so "README" doesn't fit -.-
self.readme_link_button.setText("#DOC#")
self.readme_link_button.setGeometry(
QtCore.QRect(116, 220, geometry.width(), geometry.height())
)
def __select_screenshot_directory(self):
self._autosplit_ref.settings_dict["screenshot_directory"] = (
QFileDialog.getExistingDirectory(
self,
"Select Screenshots Directory",
self._autosplit_ref.settings_dict["screenshot_directory"]
or self._autosplit_ref.settings_dict["split_image_directory"],
)
)
self.screenshot_directory_input.setText(
self._autosplit_ref.settings_dict["screenshot_directory"]
)
def __setup_bindings(self):
# Hotkey initial values and bindings
for hotkey in HOTKEYS:
hotkey_input: QtWidgets.QLineEdit = getattr(self, f"{hotkey}_input")
set_hotkey_hotkey_button: QtWidgets.QPushButton = getattr(
self,
f"set_{hotkey}_hotkey_button",
)
hotkey_input.setText(self._autosplit_ref.settings_dict.get(f"{hotkey}_hotkey", ""))
# Make it very clear that hotkeys are not used when auto-controlled
if self._autosplit_ref.is_auto_controlled and hotkey not in HOTKEYS_WHEN_AUTOCONTROLLED:
set_hotkey_hotkey_button.setEnabled(False)
hotkey_input.setEnabled(False)
else:
set_hotkey_hotkey_button.clicked.connect(
partial(set_hotkey, self._autosplit_ref, hotkey=hotkey)
)
# Debug screenshot selection checkboxes initial values and bindings
screenshot_on_setting = self._autosplit_ref.settings_dict["screenshot_on"]
for command in _DEBUG_SCREENSHOT_COMMANDS:
checkbox: QtWidgets.QCheckBox = getattr(self, f"screenshot_on_{command}_checkbox")
checkbox.setChecked(command in screenshot_on_setting)
def add_or_del(checked: Literal[0, 2], command: CommandStr = command):
if checked:
screenshot_on_setting.append(command)
else:
screenshot_on_setting.remove(command)
checkbox.stateChanged.connect(add_or_del)
# region Set initial values
# Capture Settings
self.fps_limit_spinbox.setValue(self._autosplit_ref.settings_dict["fps_limit"])
self.live_capture_region_checkbox.setChecked(
self._autosplit_ref.settings_dict["live_capture_region"]
)
self.capture_method_combobox.setCurrentIndex(
CAPTURE_METHODS.get_index(self._autosplit_ref.settings_dict["capture_method"])
)
# No self.capture_device_combobox.setCurrentIndex
# It'll set itself asynchronously in self.__set_all_capture_devices()
self.screenshot_directory_input.setText(
self._autosplit_ref.settings_dict["screenshot_directory"]
)
self.open_screenshot_checkbox.setChecked(
self._autosplit_ref.settings_dict["open_screenshot"]
)
# Image Settings
self.default_comparison_method_combobox.setCurrentIndex(
self._autosplit_ref.settings_dict["default_comparison_method"]
)
self.default_similarity_threshold_spinbox.setValue(
self._autosplit_ref.settings_dict["default_similarity_threshold"]
)
self.default_delay_time_spinbox.setValue(
self._autosplit_ref.settings_dict["default_delay_time"]
)
self.default_pause_time_spinbox.setValue(
self._autosplit_ref.settings_dict["default_pause_time"]
)
self.loop_splits_checkbox.setChecked(self._autosplit_ref.settings_dict["loop_splits"])
self.start_also_resets_checkbox.setChecked(
self._autosplit_ref.settings_dict["start_also_resets"]
)
self.enable_auto_reset_image_checkbox.setChecked(
self._autosplit_ref.settings_dict["enable_auto_reset"]
)
# endregion
# region Binding
# Capture Settings
self.fps_limit_spinbox.valueChanged.connect(self.__fps_limit_changed)
self.live_capture_region_checkbox.stateChanged.connect(
lambda: self.__set_value(
"live_capture_region",
self.live_capture_region_checkbox.isChecked(),
)
)
self.capture_method_combobox.currentIndexChanged.connect(
lambda: self.__set_value("capture_method", self.__capture_method_changed())
)
self.capture_device_combobox.currentIndexChanged.connect(self.__capture_device_changed)
self.screenshot_directory_browse_button.clicked.connect(self.__select_screenshot_directory)
self.open_screenshot_checkbox.stateChanged.connect(
lambda: self.__set_value("open_screenshot", self.open_screenshot_checkbox.isChecked())
)
# Image Settings
self.default_comparison_method_combobox.currentIndexChanged.connect(
lambda: self.__set_value(
"default_comparison_method",
self.default_comparison_method_combobox.currentIndex(),
)
)
self.default_similarity_threshold_spinbox.valueChanged.connect(
lambda: self.__update_default_threshold(
self.default_similarity_threshold_spinbox.value()
)
)
self.default_delay_time_spinbox.valueChanged.connect(
lambda: self.__set_value("default_delay_time", self.default_delay_time_spinbox.value())
)
self.default_pause_time_spinbox.valueChanged.connect(
lambda: self.__set_value("default_pause_time", self.default_pause_time_spinbox.value())
)
self.loop_splits_checkbox.stateChanged.connect(
lambda: self.__set_value("loop_splits", self.loop_splits_checkbox.isChecked())
)
self.start_also_resets_checkbox.stateChanged.connect(
lambda: self.__set_value(
"start_also_resets",
self.start_also_resets_checkbox.isChecked(),
)
)
self.enable_auto_reset_image_checkbox.stateChanged.connect(
lambda: self.__set_value(
"enable_auto_reset",
self.enable_auto_reset_image_checkbox.isChecked(),
)
)
# endregion
def open_settings(autosplit: "AutoSplit"):
if not autosplit.SettingsWidget or cast(QtWidgets.QWidget, autosplit.SettingsWidget).isHidden():
autosplit.SettingsWidget = __SettingsWidget(autosplit)
def get_default_settings_from_ui(autosplit: "AutoSplit"):
temp_dialog = QtWidgets.QWidget()
default_settings_dialog = settings_ui.Ui_SettingsWidget()
default_settings_dialog.setupUi(temp_dialog)
default_settings: user_profile.UserProfileDict = {
"split_hotkey": default_settings_dialog.split_input.text(),
"reset_hotkey": default_settings_dialog.reset_input.text(),
"undo_split_hotkey": default_settings_dialog.undo_split_input.text(),
"skip_split_hotkey": default_settings_dialog.skip_split_input.text(),
"pause_hotkey": default_settings_dialog.pause_input.text(),
"screenshot_hotkey": default_settings_dialog.screenshot_input.text(),
"toggle_auto_reset_image_hotkey": (
default_settings_dialog.toggle_auto_reset_image_input.text()
),
"fps_limit": default_settings_dialog.fps_limit_spinbox.value(),
"live_capture_region": default_settings_dialog.live_capture_region_checkbox.isChecked(),
"capture_method": CAPTURE_METHODS.get_method_by_index(
default_settings_dialog.capture_method_combobox.currentIndex()
),
"capture_device_id": default_settings_dialog.capture_device_combobox.currentIndex(),
"capture_device_name": "",
"default_comparison_method": (
default_settings_dialog.default_comparison_method_combobox.currentIndex()
),
"default_similarity_threshold": (
default_settings_dialog.default_similarity_threshold_spinbox.value()
),
"default_delay_time": default_settings_dialog.default_delay_time_spinbox.value(),
"default_pause_time": default_settings_dialog.default_pause_time_spinbox.value(),
"loop_splits": default_settings_dialog.loop_splits_checkbox.isChecked(),
"start_also_resets": default_settings_dialog.start_also_resets_checkbox.isChecked(),
"enable_auto_reset": default_settings_dialog.enable_auto_reset_image_checkbox.isChecked(),
"split_image_directory": autosplit.split_image_folder_input.text(),
"screenshot_directory": default_settings_dialog.screenshot_directory_input.text(),
"open_screenshot": default_settings_dialog.open_screenshot_checkbox.isChecked(),
"screenshot_on": [
getattr(default_settings_dialog, f"screenshot_on_{command}_checkbox").isChecked()
for command in _DEBUG_SCREENSHOT_COMMANDS
],
"captured_window_title": "",
"capture_region": {
"x": autosplit.x_spinbox.value(),
"y": autosplit.y_spinbox.value(),
"width": autosplit.width_spinbox.value(),
"height": autosplit.height_spinbox.value(),
},
}
del temp_dialog
return default_settings