Skip to content
Snippets Groups Projects
Unverified Commit 54a0611c authored by Gagik Vardanyan's avatar Gagik Vardanyan Committed by GitHub
Browse files

Merge pull request #30805 from mantidproject/28176_python_input_to_dialog

Replace pythons input to raise a QInputDialog
parents 7fca34b2 20d1859c
No related branches found
No related tags found
No related merge requests found
......@@ -7,14 +7,15 @@ Mantid Workbench Changes
New and Improved
----------------
Added Floating/On Top setting for all the windows that are opened by workbench (plots, interfaces, etc.)
- Added Floating/On Top setting for all the windows that are opened by workbench (plots, interfaces, etc.)
- New plot interactions: Double click a legend to hide it, double click a curve to open it in the plot config dialog.
- It is now possible to overplot bin data from the matrix workspace view.
- Improved the performance of the table workspace display for large datasets
- Added a sample material dialog that is accessed via the context menu in the workspace widget.
- When a workspace is renamed it now updates the relevant plot labels with the new workspace name.
- Add a checkbox to freeze the rotation in the instrument viewer in Full 3D mode.
- Calling python's `input` now raises an input dialog in the script editor and the iPython shell.
- A new empty facility with empty instrument is the default facility now, and
user has to select their choice of facility (including ISIS) and instrument for the first time
......
......@@ -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)
......@@ -27,7 +27,7 @@ from ..plugins.base import PluginWidget # noqa
# from mantidqt.utils.qt import toQSettings when readSettings/writeSettings are implemented
# should we share this with plugins.editor?
STARTUP_CODE = """from __future__ import (absolute_import, division, print_function, unicode_literals)
STARTUP_CODE = """
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
......
......@@ -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, an EOFError 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 EOFError("User input request cancelled")
# 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 unittest import TestCase
from unittest.mock import patch
from qtpy.QtWidgets import QInputDialog
from workbench.utils.io import input_qinputdialog
class IOTest(TestCase):
@patch('workbench.utils.io.QInputDialog')
def test_input_qinputdialog_setup_is_correct(self, mock_QInputDialogClass):
mock_QInputDialogClass.TextInput = QInputDialog.TextInput
mock_QInputDialog = mock_QInputDialogClass()
_ = input_qinputdialog("prompt")
mock_QInputDialog.setInputMode.assert_called_with(QInputDialog.TextInput)
mock_QInputDialog.setLabelText.assert_called_with("prompt")
@patch('workbench.utils.io.QInputDialog')
def test_input_qinputdialog_return_value_is_correct_when_dialog_accepted(self, mock_QInputDialogClass):
mock_QInputDialog = mock_QInputDialogClass()
mock_QInputDialog.exec_.return_value = True
mock_QInputDialog.textValue.return_value = "their input"
user_input = input_qinputdialog()
self.assertEqual(user_input, "their input")
@patch('workbench.utils.io.QInputDialog')
def test_input_qinputdialog_raises_RuntimeError_when_input_cancelled(self, mock_QInputDialogClass):
mock_QInputDialog = mock_QInputDialogClass()
mock_QInputDialog.exec_.return_value = False
self.assertRaises(EOFError, input_qinputdialog)
......@@ -26,6 +26,8 @@ except ImportError:
# local imports
from inspect import getfullargspec
from mantidqt.utils.asynchronous import BlockingAsyncTaskWithCallback
from mantidqt.utils.qt.qappthreadcall import QAppThreadCall
from workbench.utils.io import input_qinputdialog
class InProcessJupyterConsole(RichJupyterWidget):
......@@ -62,6 +64,9 @@ class InProcessJupyterConsole(RichJupyterWidget):
self.kernel_manager = kernel_manager
self.kernel_client = kernel_client
# Override python input to raise a QInputDialog.
kernel.raw_input = QAppThreadCall(input_qinputdialog)
def keyPressEvent(self, event):
if QApplication.keyboardModifiers() & Qt.ControlModifier and (event.key() == Qt.Key_Equal):
self.change_font_size(1)
......
......@@ -14,6 +14,7 @@ try:
except ImportError:
pass
import unittest
from unittest.mock import patch
# third-party library imports
......@@ -41,6 +42,18 @@ class InProcessJupyterConsoleTest(unittest.TestCase):
widget.kernel_manager.shutdown_kernel()
del widget
@patch('mantidqt.widgets.jupyterconsole.input_qinputdialog')
def test_construction_overrides_python_input_with_qinputdialog(self, mock_input):
widget = InProcessJupyterConsole()
kernel = widget.kernel_manager.kernel
kernel.raw_input("prompt")
mock_input.assert_called_with("prompt")
self._pre_delete_console_cleanup(widget)
widget.kernel_manager.shutdown_kernel()
del widget
def _pre_delete_console_cleanup(self, console):
"""Certain versions of qtconsole seem to raise an attribute error on
exit of the process when an event is delivered to an event filter
......
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