Skip to content
Snippets Groups Projects
Commit 7a7472db authored by Harry Saunders's avatar Harry Saunders
Browse files

Add functions to restore previous script editors

Workbench will now note file paths of scripts that are open when
mantid is closed (saved in QSettings). It will restore these on
startup.
parent ec605e85
No related merge requests found
......@@ -220,6 +220,8 @@ class MainWindow(QMainWindow):
# uses default configuration as necessary
self.readSettings(CONF)
self.editor.restore_session_tabs()
self.setup_layout()
self.create_actions()
self.populate_menus()
......@@ -422,10 +424,9 @@ class MainWindow(QMainWindow):
event.ignore()
return
self.writeSettings(CONF) # write current window information to global settings object
# Close editors
if self.editor.app_closing():
self.writeSettings(CONF) # write current window information to global settings object
# Close all open plots
# We don't want this at module scope here
import matplotlib.pyplot as plt # noqa
......
......@@ -97,6 +97,9 @@ class MultiFileEditor(PluginWidget):
'''This is used by MainWindow to execute a file after opening it'''
return self.editors.execute_current()
def restore_session_tabs(self):
self.editors.restore_session_tabs()
# ----------- Plugin API --------------------
def app_closing(self):
......@@ -128,11 +131,14 @@ class MultiFileEditor(PluginWidget):
def get_plugin_title(self):
return "Editor"
def readSettings(self, _):
pass
def readSettings(self, settings):
try:
self.editors.prev_session_tabs = settings.get('Editor/SessionTabs')
except KeyError:
pass
def writeSettings(self, _):
pass
def writeSettings(self, settings):
settings.set('Editor/SessionTabs', self.editors.tab_filepaths)
def register_plugin(self):
self.main.add_dockwidget(self)
......
......@@ -39,6 +39,7 @@ class MultiPythonFileInterpreter(QWidget):
# attributes
self.default_content = default_content
self.prev_session_tabs = None
self.whitespace_visible = False
# widget setup
......@@ -55,6 +56,15 @@ class MultiPythonFileInterpreter(QWidget):
def editor_count(self):
return self._tabs.count()
@property
def tab_filepaths(self):
file_paths = []
for idx in range(self.editor_count):
file_path = self._tabs.widget(idx).filename
if file_path:
file_paths.append(file_path)
return file_paths
def append_new_editor(self, content=None, filename=None):
if content is None:
content = self.default_content
......@@ -171,22 +181,31 @@ class MultiPythonFileInterpreter(QWidget):
content = code_file.read()
self.append_new_editor(content=content, filename=filepath)
def open_files_in_new_tabs(self, filepaths):
for filepath in filepaths:
self.open_file_in_new_tab(filepath)
def plus_button_clicked(self, _):
"""Add a new tab when the plus button is clicked"""
self.append_new_editor()
def restore_session_tabs(self):
if self.prev_session_tabs is not None:
self.open_files_in_new_tabs(self.prev_session_tabs)
self.close_tab(0) # close default empty script
def save_current_file(self):
"""Save the current file"""
self.current_editor().save()
def toggle_comment_current(self):
self.current_editor().toggle_comment()
def spaces_to_tabs_current(self):
self.current_editor().replace_spaces_with_tabs()
def tabs_to_spaces_current(self):
self.current_editor().replace_tabs_with_spaces()
def spaces_to_tabs_current(self):
self.current_editor().replace_spaces_with_tabs()
def toggle_comment_current(self):
self.current_editor().toggle_comment()
def toggle_whitespace_visible_all(self):
if self.whitespace_visible:
......
......@@ -10,6 +10,7 @@
from __future__ import (absolute_import, unicode_literals)
# system imports
import os
import unittest
# third-party library imports
......@@ -31,6 +32,20 @@ class MultiPythonFileInterpreterTest(GuiTest):
widget.append_new_editor()
self.assertEqual(2, widget.editor_count)
def test_tab_session_restore(self):
widget = MultiPythonFileInterpreter()
widget.prev_session_tabs = [
os.path.join(os.path.dirname(__file__), u'__init__.py'),
__file__]
widget.restore_session_tabs()
self.assertEqual(2, widget.editor_count)
def test_tab_session_restore_nothing_to_restore(self):
widget = MultiPythonFileInterpreter()
widget.prev_session_tabs = None
widget.restore_session_tabs()
self.assertEqual(1, widget.editor_count) # default empty tab should be open
if __name__ == '__main__':
unittest.main()
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