Skip to content

Make Save Settings more intuitive #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/AutoControlledWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def run(self):
self.autosplit.reset_signal.emit()
elif line.startswith("settings"):
# Allow for any split character between "settings" and the path
self.autosplit.load_settings_file_path = line[9:]
settings.load_settings(self.autosplit, load_settings_from_livesplit=True)
settings.load_settings(self.autosplit, line[9:])
# TODO: Not yet implemented in AutoSplit Integration
# elif line == 'pause':
# self.pause_signal.emit()
20 changes: 9 additions & 11 deletions src/AutoSplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,15 @@ class AutoSplit(QMainWindow, design.Ui_MainWindow):
window_text = ""
selection = Rect()
last_saved_settings: list[Union[str, float, int, bool]] = []
save_settings_file_path = ""
load_settings_file_path = ""
live_image_function_on_open = True
split_image_number = 0
split_images_and_loop_number: list[tuple[AutoSplitImage, int]] = []
split_groups: list[list[int]] = []

# Last loaded settings and last successful loaded settings file path to None until we try to load them
# Last loaded settings empty and last successful loaded settings file path to None until we try to load them
last_loaded_settings: list[Union[str, float, int]] = []
last_successfully_loaded_settings_file_path: Optional[str] = None
"""For when a file has never loaded, but you successfully "Save File As"."""

# Automatic timer start
highest_similarity = 0.0
Expand Down Expand Up @@ -221,7 +220,7 @@ def __init__(self, parent: Optional[QWidget] = None):
self.timer_start_image.timeout.connect(self.__start_image_function)

if not self.is_auto_controlled:
settings.load_settings(self, load_settings_on_open=True)
settings.load_settings_on_open(self)

self.show()

Expand Down Expand Up @@ -844,11 +843,10 @@ def __update_split_image(self, specific_image: Optional[AutoSplitImage] = None):
loop_tuple = self.split_images_and_loop_number[self.split_image_number]
self.image_loop_label.setText(f"Image Loop: {loop_tuple[1]}/{loop_tuple[0].loops}")

self.highest_similarity = 0.0
# need to set split below threshold to false each time an image updates.
self.split_below_threshold = False

self.highest_similarity = 0.0

def closeEvent(self, a0: Optional[QtGui.QCloseEvent] = None):
"""
Exit safely when closing the window
Expand Down Expand Up @@ -876,18 +874,18 @@ def exit_program():
settings_file_name = "Untitled" \
if self.last_successfully_loaded_settings_file_path is None \
else os.path.basename(self.last_successfully_loaded_settings_file_path)
warning_message = f"Do you want to save changes made to settings file {settings_file_name}?"

warning = QMessageBox.warning(
self,
"AutoSplit",
warning_message,
f"Do you want to save changes made to settings file {settings_file_name}?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel)

if warning is QMessageBox.StandardButton.Yes:
# TODO: Don't close if user cancelled the save
settings.save_settings_as(self)
exit_program()
if settings.save_settings(self):
exit_program()
else:
a0.ignore()
if warning is QMessageBox.StandardButton.No:
exit_program()
if warning is QMessageBox.StandardButton.Cancel:
Expand Down
107 changes: 56 additions & 51 deletions src/settings_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,53 @@ def have_settings_changed(autosplit: AutoSplit):


def save_settings(autosplit: AutoSplit):
"""
@return: The save settings filepath. Or None if "Save Settings As" is cancelled
"""
if not autosplit.last_successfully_loaded_settings_file_path:
save_settings_as(autosplit)
else:
autosplit.last_saved_settings = get_save_settings_values(autosplit)
# save settings to a .pkl file
with open(autosplit.last_successfully_loaded_settings_file_path, "wb") as file:
pickle.dump(autosplit.last_saved_settings, file)
return save_settings_as(autosplit)

autosplit.last_saved_settings = get_save_settings_values(autosplit)
# Save settings to a .pkl file
with open(autosplit.last_successfully_loaded_settings_file_path, "wb") as file:
pickle.dump(autosplit.last_saved_settings, file)
return autosplit.last_successfully_loaded_settings_file_path


def save_settings_as(autosplit: AutoSplit):
"""
@return: The save settings filepath selected. Empty if cancelled
"""
# User picks save destination
autosplit.save_settings_file_path = QtWidgets.QFileDialog.getSaveFileName(
save_settings_file_path = QtWidgets.QFileDialog.getSaveFileName(
autosplit,
"Save Settings As",
os.path.join(auto_split_directory, "settings.pkl"),
autosplit.last_successfully_loaded_settings_file_path
or os.path.join(auto_split_directory, "settings.pkl"),
"PKL (*.pkl)")[0]

# If user cancels save destination window, don't save settings
if not autosplit.save_settings_file_path:
return
if not save_settings_file_path:
return ""

autosplit.last_saved_settings = get_save_settings_values(autosplit)

# save settings to a .pkl file
with open(autosplit.save_settings_file_path, "wb") as file:
# Save settings to a .pkl file
with open(save_settings_file_path, "wb") as file:
pickle.dump(autosplit.last_saved_settings, file)

# Wording is kinda off here but this needs to be here for an edge case:
# for when a file has never loaded, but you save file as successfully.
autosplit.last_successfully_loaded_settings_file_path = autosplit.save_settings_file_path
autosplit.last_successfully_loaded_settings_file_path = save_settings_file_path
return save_settings_file_path


def __load_settings_from_file(autosplit: AutoSplit):
def __load_settings_from_file(autosplit: AutoSplit, load_settings_file_path: str):
try:
with open(autosplit.load_settings_file_path, "rb") as file:
with open(load_settings_file_path, "rb") as file:
settings: list[Any] = RestrictedUnpickler(file).load()
settings_count = len(settings)
if settings_count < 18:
autosplit.show_error_signal.emit(error_messages.old_version_settings_file)
return
return False
# v1.3-1.4 settings. Add default pause_key and auto_start_on_reset_setting
if settings_count == 18:
settings.insert(9, "")
Expand All @@ -110,11 +117,11 @@ def __load_settings_from_file(autosplit: AutoSplit):
# v1.6.X settings
elif settings_count != 21:
autosplit.show_error_signal.emit(error_messages.invalid_settings)
return
return False
autosplit.last_loaded_settings = settings
except (FileNotFoundError, MemoryError, pickle.UnpicklingError):
autosplit.show_error_signal.emit(error_messages.invalid_settings)
return
return False

autosplit.split_image_directory = settings[0]
autosplit.split_image_folder_input.setText(settings[0])
Expand Down Expand Up @@ -147,46 +154,44 @@ def __load_settings_from_file(autosplit: AutoSplit):
autosplit.live_image.setText("Reload settings after opening"
f'\n"{autosplit.window_text}"'
"\nto automatically load Live Capture")
return True


def load_settings(
autosplit: AutoSplit,
load_settings_on_open: bool = False,
load_settings_from_livesplit: bool = False
from_path: str = ""
):
if load_settings_on_open:
settings_files = [
file for file
in os.listdir(auto_split_directory)
if file.endswith(".pkl")]

# find all .pkls in AutoSplit folder, error if there is none or more than 1
if len(settings_files) < 1:
error_messages.no_settings_file_on_open()
autosplit.last_loaded_settings = []
return
if len(settings_files) > 1:
error_messages.too_many_settings_files_on_open()
autosplit.last_loaded_settings = []
return
autosplit.load_settings_file_path = os.path.join(auto_split_directory, settings_files[0])

elif not load_settings_on_open and not load_settings_from_livesplit:
autosplit.load_settings_file_path = QtWidgets.QFileDialog.getOpenFileName(
autosplit,
"Load Settings",
os.path.join(auto_split_directory, "settings.pkl"),
"PKL (*.pkl)")[0]
if not autosplit.load_settings_file_path:
return

__load_settings_from_file(autosplit)

autosplit.last_successfully_loaded_settings_file_path = autosplit.load_settings_file_path
load_settings_file_path = from_path or QtWidgets.QFileDialog.getOpenFileName(
autosplit,
"Load Settings",
os.path.join(auto_split_directory, "settings.pkl"),
"PKL (*.pkl)")[0]
if not (load_settings_file_path and __load_settings_from_file(autosplit, load_settings_file_path)):
return

autosplit.last_successfully_loaded_settings_file_path = load_settings_file_path
autosplit.check_live_image()
autosplit.load_start_image()


def load_settings_on_open(autosplit: AutoSplit):
settings_files = [
file for file
in os.listdir(auto_split_directory)
if file.endswith(".pkl")]

# find all .pkls in AutoSplit folder, error if there is none or more than 1
if len(settings_files) < 1:
error_messages.no_settings_file_on_open()
autosplit.last_loaded_settings = []
return
if len(settings_files) > 1:
error_messages.too_many_settings_files_on_open()
autosplit.last_loaded_settings = []
return
load_settings(autosplit, os.path.join(auto_split_directory, settings_files[0]))


def load_check_for_updates_on_open(autosplit: AutoSplit):
"""
Retrieve the "Check For Updates On Open" QSettings and set the checkbox state
Expand Down