Skip to content

Commit

Permalink
Add "Screenshot on ..." checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Sep 21, 2024
1 parent 18b97cb commit 3ff65ca
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 23 deletions.
118 changes: 107 additions & 11 deletions res/settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
<rect>
<x>0</x>
<y>0</y>
<width>285</width>
<height>294</height>
<width>284</width>
<height>334</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>285</width>
<height>294</height>
<width>284</width>
<height>334</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>285</width>
<height>294</height>
<width>284</width>
<height>334</height>
</size>
</property>
<property name="font">
Expand All @@ -41,7 +41,7 @@
<x>-3</x>
<y>-3</y>
<width>291</width>
<height>301</height>
<height>341</height>
</rect>
</property>
<property name="currentIndex">
Expand Down Expand Up @@ -88,7 +88,7 @@
<rect>
<x>10</x>
<y>40</y>
<width>141</width>
<width>261</width>
<height>24</height>
</rect>
</property>
Expand All @@ -107,7 +107,7 @@
<rect>
<x>10</x>
<y>70</y>
<width>151</width>
<width>261</width>
<height>16</height>
</rect>
</property>
Expand Down Expand Up @@ -158,7 +158,7 @@
<rect>
<x>10</x>
<y>120</y>
<width>151</width>
<width>261</width>
<height>16</height>
</rect>
</property>
Expand Down Expand Up @@ -210,7 +210,7 @@
<rect>
<x>10</x>
<y>220</y>
<width>181</width>
<width>261</width>
<height>24</height>
</rect>
</property>
Expand All @@ -224,6 +224,102 @@
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="screenshot_on_split_checkbox">
<property name="geometry">
<rect>
<x>10</x>
<y>280</y>
<width>131</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Screenshot on Split</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="screenshot_on_start_checkbox">
<property name="geometry">
<rect>
<x>10</x>
<y>240</y>
<width>131</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Screenshot on Start</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="screenshot_on_reset_checkbox">
<property name="geometry">
<rect>
<x>10</x>
<y>260</y>
<width>131</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Screenshot on Reset</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="screenshot_on_skip_checkbox">
<property name="geometry">
<rect>
<x>140</x>
<y>260</y>
<width>131</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Screenshot on Skip</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="screenshot_on_undo_checkbox">
<property name="geometry">
<rect>
<x>140</x>
<y>240</y>
<width>131</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Screenshot on Undo</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="screenshot_on_pause_checkbox">
<property name="geometry">
<rect>
<x>140</x>
<y>280</y>
<width>131</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Screenshot on Pause</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
</widget>
<widget class="QWidget" name="image_settings_tab">
<attribute name="title">
Expand Down
2 changes: 2 additions & 0 deletions src/AutoControlledThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def run(self):
break
except EOFError:
continue
if line in self._autosplit_ref.settings_dict["screenshot_on"]:
self._autosplit_ref.screenshot_signal.emit()
match line:
# This is for use in a Development environment
case "kill":
Expand Down
4 changes: 3 additions & 1 deletion src/AutoSplitImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ def compare_with_capture(self, default: "AutoSplit | int", capture: MatLike | No

if not is_valid_image(self.byte_array):
return 0.0
resized_capture = cv2.resize(capture, self.byte_array.shape[1::-1], interpolation=cv2.INTER_NEAREST)
resized_capture = cv2.resize(
capture, self.byte_array.shape[1::-1], interpolation=cv2.INTER_NEAREST
)

return get_comparison_method_by_index(
self.__get_comparison_method_index(default),
Expand Down
14 changes: 8 additions & 6 deletions src/hotkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
SET_HOTKEY_TEXT = "Set Hotkey"
PRESS_A_KEY_TEXT = "Press a key..."

Commands = Literal["split", "start", "pause", "reset", "skip", "undo"]
CommandStr = Literal["split", "start", "pause", "reset", "skip", "undo"]
Hotkey = Literal[
"split",
"reset",
Expand Down Expand Up @@ -80,16 +80,18 @@ def after_setting_hotkey(autosplit: "AutoSplit"):
getattr(autosplit.SettingsWidget, f"set_{hotkey}_hotkey_button").setEnabled(True)


def send_command(autosplit: "AutoSplit", command: Commands):
# Note: Rather than having the start image able to also reset the timer,
# having the reset image check be active at all time would be a better, more organic solution,
# but that is dependent on migrating to an observer pattern (#219) and
# being able to reload all images.
def send_command(autosplit: "AutoSplit", command: CommandStr):
if command in autosplit.settings_dict["screenshot_on"]:
autosplit.screenshot_signal.emit()
match command:
case _ if autosplit.is_auto_controlled:
if command == "start" and autosplit.settings_dict["start_also_resets"]:
print("reset", flush=True)
print(command, flush=True)
# Note: Rather than having the start image able to also reset the timer, having
# the reset image check be active at all time would be a better, more organic solution.
# But that is dependent on migrating to an observer pattern (#219) and
# being able to reload all images.
case "start" if autosplit.settings_dict["start_also_resets"]:
_send_hotkey(autosplit.settings_dict["reset_hotkey"])
case "reset":
Expand Down
34 changes: 31 additions & 3 deletions src/menu_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import webbrowser
from functools import partial
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, Any, Literal, cast
from urllib.error import URLError
from urllib.request import urlopen

Expand All @@ -23,7 +23,7 @@
get_all_video_capture_devices,
)
from gen import about, design, settings as settings_ui, update_checker
from hotkeys import HOTKEYS, HOTKEYS_WHEN_AUTOCONTROLLED, set_hotkey
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:
Expand All @@ -38,6 +38,15 @@
+ "\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."""
Expand Down Expand Up @@ -326,7 +335,7 @@ def __select_screenshot_directory(self):
)

def __setup_bindings(self):
"""Hotkey initial values and bindings."""
# 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(
Expand All @@ -344,6 +353,21 @@ def __setup_bindings(self):
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.add(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"])
Expand Down Expand Up @@ -478,6 +502,10 @@ def get_default_settings_from_ui(autosplit: "AutoSplit"):
"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(),
Expand Down
7 changes: 5 additions & 2 deletions src/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import error_messages
from capture_method import CAPTURE_METHODS, CaptureMethodEnum, Region, change_capture_method
from gen import design
from hotkeys import HOTKEYS, Hotkey, remove_all_hotkeys, set_hotkey
from hotkeys import HOTKEYS, CommandStr, Hotkey, remove_all_hotkeys, set_hotkey
from menu_bar import open_settings
from utils import auto_split_directory

Expand Down Expand Up @@ -40,6 +40,7 @@ class UserProfileDict(TypedDict):
split_image_directory: str
screenshot_directory: str
open_screenshot: bool
screenshot_on: set[CommandStr]
captured_window_title: str
capture_region: Region

Expand Down Expand Up @@ -72,6 +73,7 @@ def copy():
split_image_directory="",
screenshot_directory="",
open_screenshot=True,
screenshot_on=set(),
captured_window_title="",
capture_region=Region(x=0, y=0, width=1, height=1),
)
Expand Down Expand Up @@ -122,7 +124,7 @@ def __load_settings_from_file(autosplit: "AutoSplit", load_settings_file_path: s
autosplit.show_error_signal.emit(error_messages.old_version_settings_file)
return False

# Allow seemlessly reloading the entire settings widget
# Allow seamlessly reloading the entire settings widget
settings_widget_was_open = False
settings_widget = cast(QtWidgets.QWidget | None, autosplit.SettingsWidget)
if settings_widget:
Expand All @@ -137,6 +139,7 @@ def __load_settings_from_file(autosplit: "AutoSplit", load_settings_file_path: s
loaded_settings = DEFAULT_PROFILE | cast(UserProfileDict, toml.load(file))

# TODO: Data Validation / fallbacks ?
loaded_settings["screenshot_on"] = set(loaded_settings["screenshot_on"])
autosplit.settings_dict = UserProfileDict(**loaded_settings)
autosplit.last_saved_settings = deepcopy(autosplit.settings_dict)

Expand Down

0 comments on commit 3ff65ca

Please sign in to comment.