From 053d22c2c2b861357ad0fc38f75bbaddb5f89b7c Mon Sep 17 00:00:00 2001 From: ptsavol <43600314+ptsavol@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:20:16 +0200 Subject: [PATCH] Add file type filters to QFileDialogs based on selected tool type (#241) Re spine-tools/Spine-Toolbox#2961 --- .../tool_specification_editor_window.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/spine_items/tool/widgets/tool_specification_editor_window.py b/spine_items/tool/widgets/tool_specification_editor_window.py index 3472270e..2cb32d34 100644 --- a/spine_items/tool/widgets/tool_specification_editor_window.py +++ b/spine_items/tool/widgets/tool_specification_editor_window.py @@ -922,8 +922,9 @@ def _start_dir(self): @Slot(bool) def browse_main_program_file(self, _=False): """Opens a file dialog where user can select the path of the main program file.""" + filter = self._get_filetype_filter() # noinspection PyCallByClass, PyTypeChecker, PyArgumentList - answer = QFileDialog.getOpenFileName(self, "Select existing main program file", self._start_dir(), "*.*") + answer = QFileDialog.getOpenFileName(self, "Select existing main program file", self._start_dir(), filter) file_path = answer[0] existing_file_paths = [ os.path.join(self.includes_main_path, i) @@ -949,8 +950,9 @@ def browse_main_program_file(self, _=False): @Slot(bool) def new_main_program_file(self, _=False): """Creates a new blank main program file.""" + filter = self._get_filetype_filter() # noinspection PyCallByClass - answer = QFileDialog.getSaveFileName(self, "Create new main program file", self._start_dir()) + answer = QFileDialog.getSaveFileName(self, "Create new main program file", self._start_dir(), filter) file_path = answer[0] existing_file_paths = [os.path.join(self.includes_main_path, i) for i in self.spec_dict.get("includes", [])] if not file_path: # Cancel button clicked @@ -1042,6 +1044,18 @@ def add_dropped_program_files(self, file_paths): """Adds dropped file paths to Source files list.""" self.add_program_files(*file_paths) + def _get_filetype_filter(self): + """Returns a filter for the QFileDialog based on the selected tool spec type.""" + tooltype = self.spec_dict.get("tooltype", "") + if tooltype == "python": + return "Python (*.py);;*.*" + elif tooltype == "julia": + return "Julia (*.jl);;*.*" + elif tooltype == "gams": + return "Gams (*.gms);;*.*" + else: + return "*.*" + def _validate_additional_program_files(self, new_files, old_program_files): valid_files = [] dupes = []