diff --git a/qt/python/CMakeLists.txt b/qt/python/CMakeLists.txt index 08e5cbf0865bb638d2154ff7447b5cc4852c6dca..75a35fe1da10ce8988dbe861de2d1d48721fb7f1 100644 --- a/qt/python/CMakeLists.txt +++ b/qt/python/CMakeLists.txt @@ -42,10 +42,10 @@ if ( ENABLE_WORKBENCH ) mantidqt/utils/test/test_qt_utils.py - mantidqt/widgets/codeeditor/test_codeeditor.py - mantidqt/widgets/codeeditor/test_executablecodeeditor.py - mantidqt/widgets/codeeditor/test_execution.py - mantidqt/widgets/codeeditor/test_multifileeditor.py + mantidqt/widgets/codeeditor/test/test_codeeditor.py + mantidqt/widgets/codeeditor/test/test_interpreter.py + mantidqt/widgets/codeeditor/test/test_execution.py + mantidqt/widgets/codeeditor/test/test_multifileeditor.py mantidqt/widgets/test/test_algorithmselector.py mantidqt/widgets/test/test_jupyterconsole.py diff --git a/qt/python/mantidqt/widgets/codeeditor/editor.py b/qt/python/mantidqt/widgets/codeeditor/editor.py index df9669c8217387d09c5e7d044cc140b163ecb4ba..5065fbe518bc6aeb0e6641c39d820b14815a1271 100644 --- a/qt/python/mantidqt/widgets/codeeditor/editor.py +++ b/qt/python/mantidqt/widgets/codeeditor/editor.py @@ -24,103 +24,5 @@ from qtpy.QtWidgets import QStatusBar, QTabWidget, QVBoxLayout, QWidget from mantidqt.utils.qt import import_qtlib from .execution import PythonCodeExecution -DEFAULT_EDITOR_LANGUAGE = "Python" - -EXECUTION_CLS = PythonCodeExecution - -IDLE_STATUS_MSG = "Status: Idle" - -RUNNING_STATUS_MSG = "Status: Running" - # Import single-file editor from C++ wrapping CodeEditor = import_qtlib('_widgetscore', 'mantidqt.widgets', 'ScriptEditor') - - -class ExecutableCodeEditor(QWidget): - - def __init__(self, language=DEFAULT_EDITOR_LANGUAGE, parent=None): - """ - - :param language: Language for syntax highlighting - :param user_globals: Dictionary for global context of execution. - :param user_locals: Dictionary for local context of execution - :param parent: A parent QWidget - """ - super(ExecutableCodeEditor, self).__init__(parent) - - # layout - self.editor = CodeEditor(language, self) - self.status = QStatusBar(self) - layout = QVBoxLayout() - layout.addWidget(self.editor) - layout.addWidget(self.status) - self.setLayout(layout) - layout.setContentsMargins(0, 0, 0, 0) - - # presenter - self._presenter = ExecutableCodeEditorPresenter(self, PythonCodeExecution()) - - def execute_all_async(self): - self._presenter.req_execute_all_async() - - def set_editor_readonly(self, ro): - self.editor.setReadOnly(ro) - - def set_status_message(self, msg): - self.status.showMessage(msg) - - -class ExecutableCodeEditorPresenter(QObject): - """Presenter part of MVP to control actions on the editor""" - - def __init__(self, view, model): - super(ExecutableCodeEditorPresenter, self).__init__() - self.view = view - self.model = model - - # connect signals - self.model.sig_exec_success.connect(self.on_exec_success) - self.model.sig_exec_error.connect(self.on_exec_error) - - # starts idle - self.view.set_status_message(IDLE_STATUS_MSG) - - def req_execute_all_async(self): - text = self.view.editor.text() - if not text: - return - self.view.set_editor_readonly(True) - self.view.set_status_message(RUNNING_STATUS_MSG) - return self.model.execute_async(text) - - def on_exec_success(self): - self.view.set_status_message(IDLE_STATUS_MSG) - - def on_exec_error(self): - self.view.set_status_message(IDLE_STATUS_MSG) - - -class MultiFileCodeEditor(QWidget): - """Provides a tabbed widget for editing multiple files""" - - def __init__(self, parent=None): - super(MultiFileCodeEditor, self).__init__(parent) - - # layout - self.editors = QTabWidget(self) - self.editors.setMovable(True) - layout = QVBoxLayout() - layout.addWidget(self.editors) - self.setLayout(layout) - layout.setContentsMargins(0, 0, 0, 0) - - # add a single editor by default - self.append_new_editor(DEFAULT_EDITOR_LANGUAGE) - - @property - def editor_count(self): - return self.editors.count() - - def append_new_editor(self, language): - title = "New" - self.editors.addTab(ExecutableCodeEditor(language, self.editors), title) diff --git a/qt/python/mantidqt/widgets/codeeditor/interpreter.py b/qt/python/mantidqt/widgets/codeeditor/interpreter.py new file mode 100644 index 0000000000000000000000000000000000000000..3fe50625a1277e969409c494be1542139462a174 --- /dev/null +++ b/qt/python/mantidqt/widgets/codeeditor/interpreter.py @@ -0,0 +1,89 @@ +# This file is part of the mantidqt package +# +# Copyright (C) 2017 mantidproject +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +from __future__ import (absolute_import, unicode_literals) + +# 3rd party imports +from qtpy.QtCore import QObject +from qtpy.QtWidgets import QStatusBar, QVBoxLayout, QWidget + +# local imports +from mantidqt.widgets.codeeditor.editor import CodeEditor +from mantidqt.widgets.codeeditor.execution import PythonCodeExecution + +IDLE_STATUS_MSG = "Status: Idle" + +RUNNING_STATUS_MSG = "Status: Running" + + +class PythonFileInterpreter(QWidget): + + def __init__(self, parent=None): + """ + :param parent: A parent QWidget + """ + super(PythonFileInterpreter, self).__init__(parent) + + # layout + self.editor = CodeEditor("Python", self) + self.status = QStatusBar(self) + layout = QVBoxLayout() + layout.addWidget(self.editor) + layout.addWidget(self.status) + self.setLayout(layout) + layout.setContentsMargins(0, 0, 0, 0) + + # presenter + self._presenter = PythonFileInterpreterPresenter(self, PythonCodeExecution()) + + def execute_all_async(self): + self._presenter.req_execute_all_async() + + def set_editor_readonly(self, ro): + self.editor.setReadOnly(ro) + + def set_status_message(self, msg): + self.status.showMessage(msg) + + +class PythonFileInterpreterPresenter(QObject): + """Presenter part of MVP to control actions on the editor""" + + def __init__(self, view, model): + super(PythonFileInterpreterPresenter, self).__init__() + self.view = view + self.model = model + + # connect signals + self.model.sig_exec_success.connect(self.on_exec_success) + self.model.sig_exec_error.connect(self.on_exec_error) + + # starts idle + self.view.set_status_message(IDLE_STATUS_MSG) + + def req_execute_all_async(self): + text = self.view.editor.text() + if not text: + return + self.view.set_editor_readonly(True) + self.view.set_status_message(RUNNING_STATUS_MSG) + return self.model.execute_async(text) + + def on_exec_success(self): + self.view.set_status_message(IDLE_STATUS_MSG) + + def on_exec_error(self): + self.view.set_status_message(IDLE_STATUS_MSG) diff --git a/qt/python/mantidqt/widgets/codeeditor/multifileinterpreter.py b/qt/python/mantidqt/widgets/codeeditor/multifileinterpreter.py new file mode 100644 index 0000000000000000000000000000000000000000..89c201db9a3c375412379e11ed4e04c90d425aa2 --- /dev/null +++ b/qt/python/mantidqt/widgets/codeeditor/multifileinterpreter.py @@ -0,0 +1,49 @@ +# This file is part of the mantidqt package +# +# Copyright (C) 2017 mantidproject +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +from __future__ import (absolute_import, unicode_literals) + +# 3rd party imports +from qtpy.QtWidgets import QTabWidget, QVBoxLayout, QWidget + +# local imports +from mantidqt.widgets.codeeditor.interpreter import PythonFileInterpreter + + +class MultiPythonFileInterpreter(QWidget): + """Provides a tabbed widget for editing multiple files""" + + def __init__(self, parent=None): + super(MultiPythonFileInterpreter, self).__init__(parent) + + # layout + self.editors = QTabWidget(self) + self.editors.setMovable(True) + layout = QVBoxLayout() + layout.addWidget(self.editors) + self.setLayout(layout) + layout.setContentsMargins(0, 0, 0, 0) + + # add a single editor by default + self.append_new_editor() + + @property + def editor_count(self): + return self.editors.count() + + def append_new_editor(self): + title = "New" + self.editors.addTab(PythonFileInterpreter(self.editors), title) diff --git a/qt/python/mantidqt/widgets/codeeditor/test/test_executablecodeeditor.py b/qt/python/mantidqt/widgets/codeeditor/test/test_interpreter.py similarity index 87% rename from qt/python/mantidqt/widgets/codeeditor/test/test_executablecodeeditor.py rename to qt/python/mantidqt/widgets/codeeditor/test/test_interpreter.py index edf73068e25bfece73a09b8f99c5f14d35a70e7b..ef51a941907299b7a412c56c4ebaa67afb355bb9 100644 --- a/qt/python/mantidqt/widgets/codeeditor/test/test_executablecodeeditor.py +++ b/qt/python/mantidqt/widgets/codeeditor/test/test_interpreter.py @@ -23,7 +23,7 @@ import unittest import six # local imports -from mantidqt.widgets.codeeditor.editor import ExecutableCodeEditor +from mantidqt.widgets.codeeditor.interpreter import PythonFileInterpreter from mantidqt.utils.qt.testing import requires_qapp if six.PY2: @@ -33,14 +33,14 @@ else: @requires_qapp -class ExecutableCodeEditorTest(unittest.TestCase): +class PythonFileInterpreterTest(unittest.TestCase): def test_construction(self): - w = ExecutableCodeEditor() + w = PythonFileInterpreter() self.assertTrue("Status: Idle", w.status.currentMessage()) def test_empty_code_does_nothing_on_exec(self): - w = ExecutableCodeEditor() + w = PythonFileInterpreter() w._presenter.model.execute_async = mock.MagicMock() w.execute_all_async() @@ -49,7 +49,7 @@ class ExecutableCodeEditorTest(unittest.TestCase): self.assertTrue("Status: Idle", w.status.currentMessage()) def test_successful_execution(self): - w = ExecutableCodeEditor() + w = PythonFileInterpreter() w.editor.setText("x = 1 + 2") w.execute_all_async() self.assertTrue("Status: Idle", w.status.currentMessage()) diff --git a/qt/python/mantidqt/widgets/codeeditor/test/test_multifileeditor.py b/qt/python/mantidqt/widgets/codeeditor/test/test_multifileinterpreter.py similarity index 85% rename from qt/python/mantidqt/widgets/codeeditor/test/test_multifileeditor.py rename to qt/python/mantidqt/widgets/codeeditor/test/test_multifileinterpreter.py index b0834dbb536b92bd08cc340585aaa545522c8f79..9b04709cb15e2479390bc13ee923509bfc6a173c 100644 --- a/qt/python/mantidqt/widgets/codeeditor/test/test_multifileeditor.py +++ b/qt/python/mantidqt/widgets/codeeditor/test/test_multifileinterpreter.py @@ -22,17 +22,17 @@ import unittest # third-party library imports # local imports -from mantidqt.widgets.codeeditor.editor import MultiFileCodeEditor from mantidqt.utils.qt.testing import requires_qapp +from mantidqt.widgets.codeeditor.multifileinterpreter import MultiPythonFileInterpreter @requires_qapp -class MultiFileCodeEditorTest(unittest.TestCase): +class MultiPythonFileInterpreterTest(unittest.TestCase): # Success tests def test_default_contains_single_tab(self): - widget = MultiFileCodeEditor() + widget = MultiPythonFileInterpreter() self.assertEqual(1, widget.editor_count)