From 74e5909b1ca7a2ce39c15c98d77aa281c4ce6a59 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Fri, 13 Nov 2026 15:10:30 -0500 Subject: [PATCH] Select main.py directly in the TOPAZ reduction dialog The Reduction > TOPAZ action used a directory selector, which shows files greyed out and led users to believe they could not load main.py. Replace it with a file selector filtered to main.py that automatically selects main.py whenever the user enters a directory containing it. The launch behavior is unchanged: main.py is run with mantidpython from its parent directory. Closes #8 Co-Authored-By: Claude Opus 5.5 --- src/NeuXtalViz/application.py | 74 ++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/src/NeuXtalViz/application.py b/src/NeuXtalViz/application.py index 9104f73..105cb4a 100644 --- a/src/NeuXtalViz/application.py +++ b/src/NeuXtalViz/application.py @@ -247,37 +247,55 @@ def show_about_dialog(self): def topaz_reduction_GUI(self): """ - Prompt for a TOPAZ reduction directory and launch its main.py. - - Opens a directory selection dialog (starting from the last used - TOPAZ path), and if the chosen directory contains a - ``main.py`` script, launches it with ``mantidpython``. Shows a - warning if ``main.py`` is missing, or a critical error dialog - if launching fails. + Prompt for a TOPAZ reduction main.py and launch it. + + Opens a file selection dialog (starting from the last used + TOPAZ path) filtered to ``main.py``, and launches the chosen + script with ``mantidpython`` from its parent directory. Whenever + the user enters a directory containing ``main.py``, the file is + selected automatically. Shows a warning if the selection is not + a ``main.py`` file, or a critical error dialog if launching + fails. """ - directory = QFileDialog.getExistingDirectory( - self, "Select Directory", self._topaz_path + dialog = QFileDialog(self, "Select TOPAZ reduction main.py") + # The directoryEntered signal is only emitted by Qt's own dialog. + dialog.setOption(QFileDialog.DontUseNativeDialog, True) + dialog.setFileMode(QFileDialog.ExistingFile) + dialog.setNameFilters( + ["TOPAZ reduction script (main.py)", "All files (*)"] ) - if directory: - self._topaz_path = directory - main_py_path = os.path.join(directory, "main.py") - if os.path.isfile(main_py_path): - try: - subprocess.Popen( - ["mantidpython", main_py_path], - cwd=directory, - ) - except OSError as e: - QMessageBox.critical( - self, "Error", f"Failed to execute main.py:\n{e}" - ) - else: - QMessageBox.warning( - self, - "Warning", - "The selected directory does not contain main.py.", - ) + def select_main_py(directory): + if os.path.isfile(os.path.join(directory, "main.py")): + dialog.selectFile("main.py") + + dialog.directoryEntered.connect(select_main_py) + dialog.setDirectory(self._topaz_path) + select_main_py(self._topaz_path) + + if not dialog.exec(): + return + + main_py_path = dialog.selectedFiles()[0] + + directory = os.path.dirname(main_py_path) + self._topaz_path = directory + if os.path.basename(main_py_path) != "main.py" or not os.path.isfile( + main_py_path + ): + QMessageBox.warning( + self, + "Warning", + "Please select the main.py script of a TOPAZ reduction.", + ) + return + + try: + subprocess.Popen(["mantidpython", main_py_path], cwd=directory) + except OSError as e: + QMessageBox.critical( + self, "Error", f"Failed to execute main.py:\n{e}" + ) def shelxle_GUI(self): """