Skip to content

Commit

Permalink
Add file type filters to QFileDialogs based on selected tool type (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsavol authored Dec 20, 2024
1 parent 53e29bd commit 053d22c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions spine_items/tool/widgets/tool_specification_editor_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit 053d22c

Please sign in to comment.