Skip to content

Commit 1736443

Browse files
authored
Merge pull request #132 from Avasam/Make-Save-Settings-more-intuitive
Make Save Settings more intuitive
2 parents 822c8aa + 93b089f commit 1736443

File tree

3 files changed

+66
-64
lines changed

3 files changed

+66
-64
lines changed

src/AutoControlledWorker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def run(self):
3535
self.autosplit.reset_signal.emit()
3636
elif line.startswith("settings"):
3737
# Allow for any split character between "settings" and the path
38-
self.autosplit.load_settings_file_path = line[9:]
39-
settings.load_settings(self.autosplit, load_settings_from_livesplit=True)
38+
settings.load_settings(self.autosplit, line[9:])
4039
# TODO: Not yet implemented in AutoSplit Integration
4140
# elif line == 'pause':
4241
# self.pause_signal.emit()

src/AutoSplit.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,15 @@ class AutoSplit(QMainWindow, design.Ui_MainWindow):
101101
window_text = ""
102102
selection = Rect()
103103
last_saved_settings: list[Union[str, float, int, bool]] = []
104-
save_settings_file_path = ""
105-
load_settings_file_path = ""
106104
live_image_function_on_open = True
107105
split_image_number = 0
108106
split_images_and_loop_number: list[tuple[AutoSplitImage, int]] = []
109107
split_groups: list[list[int]] = []
110108

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

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

223222
if not self.is_auto_controlled:
224-
settings.load_settings(self, load_settings_on_open=True)
223+
settings.load_settings_on_open(self)
225224

226225
self.show()
227226

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

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

850-
self.highest_similarity = 0.0
851-
852850
def closeEvent(self, a0: Optional[QtGui.QCloseEvent] = None):
853851
"""
854852
Exit safely when closing the window
@@ -876,18 +874,18 @@ def exit_program():
876874
settings_file_name = "Untitled" \
877875
if self.last_successfully_loaded_settings_file_path is None \
878876
else os.path.basename(self.last_successfully_loaded_settings_file_path)
879-
warning_message = f"Do you want to save changes made to settings file {settings_file_name}?"
880877

881878
warning = QMessageBox.warning(
882879
self,
883880
"AutoSplit",
884-
warning_message,
881+
f"Do you want to save changes made to settings file {settings_file_name}?",
885882
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel)
886883

887884
if warning is QMessageBox.StandardButton.Yes:
888-
# TODO: Don't close if user cancelled the save
889-
settings.save_settings_as(self)
890-
exit_program()
885+
if settings.save_settings(self):
886+
exit_program()
887+
else:
888+
a0.ignore()
891889
if warning is QMessageBox.StandardButton.No:
892890
exit_program()
893891
if warning is QMessageBox.StandardButton.Cancel:

src/settings_file.py

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -60,46 +60,53 @@ def have_settings_changed(autosplit: AutoSplit):
6060

6161

6262
def save_settings(autosplit: AutoSplit):
63+
"""
64+
@return: The save settings filepath. Or None if "Save Settings As" is cancelled
65+
"""
6366
if not autosplit.last_successfully_loaded_settings_file_path:
64-
save_settings_as(autosplit)
65-
else:
66-
autosplit.last_saved_settings = get_save_settings_values(autosplit)
67-
# save settings to a .pkl file
68-
with open(autosplit.last_successfully_loaded_settings_file_path, "wb") as file:
69-
pickle.dump(autosplit.last_saved_settings, file)
67+
return save_settings_as(autosplit)
68+
69+
autosplit.last_saved_settings = get_save_settings_values(autosplit)
70+
# Save settings to a .pkl file
71+
with open(autosplit.last_successfully_loaded_settings_file_path, "wb") as file:
72+
pickle.dump(autosplit.last_saved_settings, file)
73+
return autosplit.last_successfully_loaded_settings_file_path
7074

7175

7276
def save_settings_as(autosplit: AutoSplit):
77+
"""
78+
@return: The save settings filepath selected. Empty if cancelled
79+
"""
7380
# User picks save destination
74-
autosplit.save_settings_file_path = QtWidgets.QFileDialog.getSaveFileName(
81+
save_settings_file_path = QtWidgets.QFileDialog.getSaveFileName(
7582
autosplit,
7683
"Save Settings As",
77-
os.path.join(auto_split_directory, "settings.pkl"),
84+
autosplit.last_successfully_loaded_settings_file_path
85+
or os.path.join(auto_split_directory, "settings.pkl"),
7886
"PKL (*.pkl)")[0]
7987

8088
# If user cancels save destination window, don't save settings
81-
if not autosplit.save_settings_file_path:
82-
return
89+
if not save_settings_file_path:
90+
return ""
8391

8492
autosplit.last_saved_settings = get_save_settings_values(autosplit)
8593

86-
# save settings to a .pkl file
87-
with open(autosplit.save_settings_file_path, "wb") as file:
94+
# Save settings to a .pkl file
95+
with open(save_settings_file_path, "wb") as file:
8896
pickle.dump(autosplit.last_saved_settings, file)
8997

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

94101

95-
def __load_settings_from_file(autosplit: AutoSplit):
102+
def __load_settings_from_file(autosplit: AutoSplit, load_settings_file_path: str):
96103
try:
97-
with open(autosplit.load_settings_file_path, "rb") as file:
104+
with open(load_settings_file_path, "rb") as file:
98105
settings: list[Any] = RestrictedUnpickler(file).load()
99106
settings_count = len(settings)
100107
if settings_count < 18:
101108
autosplit.show_error_signal.emit(error_messages.old_version_settings_file)
102-
return
109+
return False
103110
# v1.3-1.4 settings. Add default pause_key and auto_start_on_reset_setting
104111
if settings_count == 18:
105112
settings.insert(9, "")
@@ -110,11 +117,11 @@ def __load_settings_from_file(autosplit: AutoSplit):
110117
# v1.6.X settings
111118
elif settings_count != 21:
112119
autosplit.show_error_signal.emit(error_messages.invalid_settings)
113-
return
120+
return False
114121
autosplit.last_loaded_settings = settings
115122
except (FileNotFoundError, MemoryError, pickle.UnpicklingError):
116123
autosplit.show_error_signal.emit(error_messages.invalid_settings)
117-
return
124+
return False
118125

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

151159

152160
def load_settings(
153161
autosplit: AutoSplit,
154-
load_settings_on_open: bool = False,
155-
load_settings_from_livesplit: bool = False
162+
from_path: str = ""
156163
):
157-
if load_settings_on_open:
158-
settings_files = [
159-
file for file
160-
in os.listdir(auto_split_directory)
161-
if file.endswith(".pkl")]
162-
163-
# find all .pkls in AutoSplit folder, error if there is none or more than 1
164-
if len(settings_files) < 1:
165-
error_messages.no_settings_file_on_open()
166-
autosplit.last_loaded_settings = []
167-
return
168-
if len(settings_files) > 1:
169-
error_messages.too_many_settings_files_on_open()
170-
autosplit.last_loaded_settings = []
171-
return
172-
autosplit.load_settings_file_path = os.path.join(auto_split_directory, settings_files[0])
173-
174-
elif not load_settings_on_open and not load_settings_from_livesplit:
175-
autosplit.load_settings_file_path = QtWidgets.QFileDialog.getOpenFileName(
176-
autosplit,
177-
"Load Settings",
178-
os.path.join(auto_split_directory, "settings.pkl"),
179-
"PKL (*.pkl)")[0]
180-
if not autosplit.load_settings_file_path:
181-
return
182-
183-
__load_settings_from_file(autosplit)
184-
185-
autosplit.last_successfully_loaded_settings_file_path = autosplit.load_settings_file_path
164+
load_settings_file_path = from_path or QtWidgets.QFileDialog.getOpenFileName(
165+
autosplit,
166+
"Load Settings",
167+
os.path.join(auto_split_directory, "settings.pkl"),
168+
"PKL (*.pkl)")[0]
169+
if not (load_settings_file_path and __load_settings_from_file(autosplit, load_settings_file_path)):
170+
return
171+
172+
autosplit.last_successfully_loaded_settings_file_path = load_settings_file_path
186173
autosplit.check_live_image()
187174
autosplit.load_start_image()
188175

189176

177+
def load_settings_on_open(autosplit: AutoSplit):
178+
settings_files = [
179+
file for file
180+
in os.listdir(auto_split_directory)
181+
if file.endswith(".pkl")]
182+
183+
# find all .pkls in AutoSplit folder, error if there is none or more than 1
184+
if len(settings_files) < 1:
185+
error_messages.no_settings_file_on_open()
186+
autosplit.last_loaded_settings = []
187+
return
188+
if len(settings_files) > 1:
189+
error_messages.too_many_settings_files_on_open()
190+
autosplit.last_loaded_settings = []
191+
return
192+
load_settings(autosplit, os.path.join(auto_split_directory, settings_files[0]))
193+
194+
190195
def load_check_for_updates_on_open(autosplit: AutoSplit):
191196
"""
192197
Retrieve the "Check For Updates On Open" QSettings and set the checkbox state

0 commit comments

Comments
 (0)