Skip to content
Snippets Groups Projects
Commit 1475aa3b authored by Joseph Torsney's avatar Joseph Torsney
Browse files

Replace python builtin input with qinputdialog re28176

parent cd82403a
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@
"""
Defines the QMainWindow of the application and the main() entry point.
"""
import builtins
import os
from mantid.api import FrameworkManager
......@@ -39,6 +40,7 @@ from workbench.config import CONF # noqa
from workbench.plotting.globalfiguremanager import GlobalFigureManager # noqa
from workbench.utils.windowfinder import find_all_windows_that_are_savable # noqa
from workbench.utils.workspacehistorygeneration import get_all_workspace_history_from_ads # noqa
from workbench.utils.io import input_qinputdialog
from workbench.projectrecovery.projectrecovery import ProjectRecovery # noqa
from workbench.utils.recentlyclosedscriptsmenu import RecentlyClosedScriptsMenu # noqa
from mantidqt.utils.asynchronous import BlockingAsyncTaskWithCallback # noqa
......@@ -197,6 +199,8 @@ class MainWindow(QMainWindow):
self.readSettings(CONF)
self.config_updated()
self.override_python_input()
def post_mantid_init(self):
"""Run any setup that requires mantid
to have been initialized
......@@ -752,3 +756,7 @@ class MainWindow(QMainWindow):
for widget in self.widgets:
if hasattr(widget, 'writeSettings'):
widget.writeSettings(settings)
def override_python_input(self):
"""Replace python input with a call to a qinputdialog"""
builtins.input = QAppThreadCall(input_qinputdialog)
......@@ -15,6 +15,7 @@ from functools import partial
from mantid.api import FrameworkManagerImpl
from mantid.kernel import ConfigService, UsageService, version_str as mantid_version_str
from mantidqt.utils.qt import plugins
from mantidqt.utils.qt.qappthreadcall import QAppThreadCall
# Find Qt plugins for development builds on some platforms
plugins.setup_library_paths()
......@@ -28,6 +29,7 @@ from workbench.app.resources import qCleanupResources # noqa
from workbench.config import APPNAME, ORG_DOMAIN, ORGANIZATION # noqa
from workbench.plugins.exception_handler import exception_logger # noqa
from workbench.widgets.about.presenter import AboutPresenter # noqa
from workbench.utils.io import input_qinputdialog
# Constants
SYSCHECK_INTERVAL = 50
......
......@@ -337,6 +337,14 @@ class MainWindowTest(unittest.TestCase):
}
self.assertDictEqual(expected_interfaces, all_interfaces)
@patch('workbench.app.mainwindow.input_qinputdialog')
def test_override_python_input_replaces_input_with_qinputdialog(self, mock_input):
self.main_window.override_python_input()
input("prompt")
mock_input.assert_called_with("prompt")
if __name__ == '__main__':
unittest.main()
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
# This file is part of the mantidworkbench package
from qtpy.QtWidgets import QInputDialog
def input_qinputdialog(prompt: str = "") -> str:
"""
Raises a QInputDialog with a given prompt and returns the user input as a string.
If the user cancels the dialog, a RuntimeError is raised.
Intended to be used to override python's `input` function to be more user friendly.
"""
dlg = QInputDialog()
dlg.setInputMode(QInputDialog.TextInput)
dlg.setLabelText(str(prompt) if prompt is not None else "")
accepted = dlg.exec_()
if accepted:
return dlg.textValue()
else:
raise RuntimeError("User input request cancelled")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment