Skip to content

Commit

Permalink
fix for file location persistence not saving correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
j-bryan committed Mar 5, 2024
1 parent 150860d commit 651c832
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
13 changes: 6 additions & 7 deletions package/ui/controllers/file_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,28 @@ def set_filename(self, value):
raise FileNotFoundError(f'File {value} does not exist')
self.filename = os.path.abspath(value)
self.view.filename.set(os.path.basename(value))
self.persistence.set_location(value)
if self.persistence:
self.persistence.set_location(value)

def open_selection_dialog(self):
"""
Open a file dialog to select an existing file
@In, None
@Out, None
"""
initial_dir = self.persistence.get_location() if self.persistence else None
initial_dir = self.persistence.get_location() if self.persistence else os.getcwd()
filetypes = [(self.file_type.upper(), f'*.{self.file_type.strip().lower()}') if self.file_type else ('All Files', '*.*')]
filename = filedialog.askopenfilename(initialdir=initial_dir, filetypes=filetypes)
if filename:
self.filename = filename
self.view.filename.set(os.path.basename(filename))
self.set_filename(filename)

def open_save_dialog(self):
"""
Open a file dialog to save a new file
@In, None
@Out, None
"""
initial_dir = self.persistence.get_location() if self.persistence else None
initial_dir = self.persistence.get_location() if self.persistence else os.getcwd()
filename = filedialog.asksaveasfilename(initialdir=initial_dir, defaultextension=f'.{self.file_type}')
if filename:
self.filename = filename
self.view.filename.set(os.path.basename(filename))
self.set_filename(filename)
14 changes: 11 additions & 3 deletions package/ui/controllers/file_location_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def __init__(self, path: str):
if os.path.exists(self.path):
self.read()

def __exit__(self):
def close(self):
"""
Destructor
Save and close the dotfile
@In, None
@Out, None
"""
Expand Down Expand Up @@ -62,4 +62,12 @@ def set_location(self, value):
@In, value, str, the last file location (file path or directory)
@Out, None
"""
self.rcfile['DEFAULT_DIR'] = os.path.dirname(value)
self.rcfile['DEFAULT_DIR'] = os.path.abspath(os.path.dirname(value))

def close(self):
"""
Closes the file location persistence
@In, None
@Out, None
"""
self.rcfile.close()
8 changes: 8 additions & 0 deletions package/ui/controllers/file_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ def get_sys_args_from_file_selection(self):
args.extend(self.unknown_args)
return args

def close_persistence(self):
"""
Closes the file location persistence
@In, None
@Out, None
"""
self.persistence.close()

def _parse_cli_args(self):
"""
Parse arguments provided from the command line
Expand Down
11 changes: 10 additions & 1 deletion package/ui/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, model, view):
# Bind the run button to the model
self.view.frames["run_abort"].run_button.config(command=self.run_model)
# Bind the abort button to closing the window
self.view.frames["run_abort"].abort_button.config(command=self.view.quit)
self.view.frames["run_abort"].abort_button.config(command=self.quit)

def run_model(self):
# Construct sys.argv from the file selectors
Expand All @@ -26,3 +26,12 @@ def run_model(self):

def start(self):
self.view.mainloop()

def quit(self, showdialog: bool = True):
"""
Quit the application
@In, showdialog, bool, optional, whether to show a dialog before quitting, default is True
@Out, None
"""
self.file_selection_controller.close_persistence()
self.view.quit(showdialog)
2 changes: 2 additions & 0 deletions package/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ def run_from_gui(func: Callable, **kwargs):
view = View()
controller = Controller(model, view)
controller.start()
# Let the controller know to clean up when the view is closed
controller.quit(showdialog=False)
9 changes: 4 additions & 5 deletions package/ui/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ def mainloop(self):
"""
self.root.mainloop()

def quit(self):
def quit(self, showdialog: bool = True):
"""
Quit the application
@In, None
@In, showdialog, bool, optional, whether to show a dialog before quitting, default is True
@Out, None
"""
response = askokcancel('Abort run', 'Are you sure you want to abort? '
'This will close the window and any text output will be lost.')
if response:
if not showdialog or askokcancel('Abort run', 'Are you sure you want to abort? '
'This will close the window and any text output will be lost.'):
self.root.quit()

0 comments on commit 651c832

Please sign in to comment.