diff --git a/qt/applications/workbench/workbench/app/mainwindow.py b/qt/applications/workbench/workbench/app/mainwindow.py index d71698bcc9c1d2016d9f79c02786a5e86db3498b..665a5f5eb6bcc0e09cb04befc0c8646f7dd9921c 100644 --- a/qt/applications/workbench/workbench/app/mainwindow.py +++ b/qt/applications/workbench/workbench/app/mainwindow.py @@ -44,7 +44,7 @@ requirements.check_qt() from qtpy.QtCore import (QByteArray, QCoreApplication, QEventLoop, QPoint, QSize, Qt) # noqa from qtpy.QtGui import (QColor, QPixmap) # noqa -from qtpy.QtWidgets import (QApplication, QDockWidget, QMainWindow, +from qtpy.QtWidgets import (QApplication, QDockWidget, QFileDialog, QMainWindow, QSplashScreen) # noqa from mantidqt.utils.qt import plugins, widget_updates_disabled # noqa @@ -136,6 +136,8 @@ class MainWindow(QMainWindow): self.setDockOptions(self.DOCKOPTIONS) def setup(self): + # menus must be done first so they can be filled by the + # plugins in register_plugin self.create_menus() self.create_actions() self.populate_menus() @@ -157,7 +159,7 @@ class MainWindow(QMainWindow): self.set_splash("Loading code editing widget") from workbench.plugins.editor import MultiFileEditor self.editor = MultiFileEditor(self) - self.editor.register_plugin(self.editor_menu) + self.editor.register_plugin() self.widgets.append(self.editor) self.setup_layout() @@ -178,10 +180,19 @@ class MainWindow(QMainWindow): def create_actions(self): # --- general application menu options -- # file menu + action_open = create_action(self, "Open File", + on_triggered=self.open_file, + shortcut="Ctrl+O", + shortcut_context=Qt.ApplicationShortcut) + action_save = create_action(self, "Save File", + on_triggered=self.save_file, + shortcut="Ctrl+S", + shortcut_context=Qt.ApplicationShortcut) + action_quit = create_action(self, "&Quit", on_triggered=self.close, shortcut="Ctrl+Q", shortcut_context=Qt.ApplicationShortcut) - self.file_menu_actions = [action_quit] + self.file_menu_actions = [action_open, action_save, None, action_quit] def populate_menus(self): # Link to menus @@ -192,6 +203,8 @@ class MainWindow(QMainWindow): dockwidget, location = plugin.create_dockwidget() self.addDockWidget(location, dockwidget) + # ----------------------- Layout --------------------------------- + def setup_layout(self): window_settings = self.load_window_settings('window/') hexstate = window_settings[0] @@ -347,6 +360,19 @@ class MainWindow(QMainWindow): for widget in self.widgets: widget.read_user_settings(CONF.qsettings) + # ----------------------- Slots --------------------------------- + def open_file(self): + # todo: when more file types are added this should + # live in its own type + filepath, _ = QFileDialog.getOpenFileName(self, "Open File...", "", "Python (*.py)") + if not filepath: + return + self.editor.open_file_in_new_tab(filepath) + + def save_file(self): + # todo: how should this interact with project saving and workspaces when they are implemented? + self.editor.save_current_file() + def initialize(): """Perform an initialization of the application instance. Most notably @@ -373,7 +399,7 @@ def start_workbench(app): main_window = MainWindow() main_window.setup() - preloaded_packages = ('mantid', 'matplotlib') + preloaded_packages = ('mantid',) for name in preloaded_packages: main_window.set_splash('Preloading ' + name) importlib.import_module(name) diff --git a/qt/applications/workbench/workbench/plugins/editor.py b/qt/applications/workbench/workbench/plugins/editor.py index b2a0ef2a380eedcacb011a276c547255e211be95..bb544ec2bf277c148c19e89f94468464589ee445 100644 --- a/qt/applications/workbench/workbench/plugins/editor.py +++ b/qt/applications/workbench/workbench/plugins/editor.py @@ -54,7 +54,7 @@ class MultiFileEditor(PluginWidget): # attributes self.run_action = create_action(self, "Run", - on_triggered=self.execute_current, + on_triggered=self.editors.execute_current, shortcut="Ctrl+Return", shortcut_context=Qt.ApplicationShortcut) self.editor_actions = [self.run_action] @@ -67,11 +67,15 @@ class MultiFileEditor(PluginWidget): def read_user_settings(self, _): pass - def register_plugin(self, menu=None): + def register_plugin(self): self.main.add_dockwidget(self) - add_actions(menu, self.editor_actions) + # menus + add_actions(self.main.editor_menu, self.editor_actions) - # ----------- Plugin behaviour -------------- + # ----------- Plugin Behaviour -------------------- - def execute_current(self): - self.editors.execute_current() + def open_file_in_new_tab(self, filepath): + return self.editors.open_file_in_new_tab(filepath) + + def save_current_file(self): + self.editors.save_current_file() diff --git a/qt/python/mantidqt/utils/qt/__init__.py b/qt/python/mantidqt/utils/qt/__init__.py index 4b4af1fb38369ba0fbe03592e17858ff6fa3554a..fca1418fc9cc0b6c7135e5f3d70ee7d1654b8ec9 100644 --- a/qt/python/mantidqt/utils/qt/__init__.py +++ b/qt/python/mantidqt/utils/qt/__init__.py @@ -135,7 +135,9 @@ def add_actions(target, actions): :raises ValueError: If one of the actions is not an instance of QMenu/QAction """ for action in actions: - if isinstance(action, QMenu): + if action is None: + target.addSeparator() + elif isinstance(action, QMenu): target.addMenu(action) elif isinstance(action, QAction): target.addAction(action)