changeset 15353:842ab161c10a

GUI: new setting to restore tabs from previous session; allow silent load file * settings-dialog.ui: new settings check box for previous session restore * settings-dialog.cc (settings_dialog::settings_dialog): load/write new settings * file-editor.h (file-editor::request_open_file): allow silent (no error msg) open file * file_editor_interface.h (file_editor_interface::request_open_file) allow silent (no error msg) open file * file-editor.cc (file_editor::~file_editor): store a list of open tabs in settings (file_editor::request_open_file): allow a silent open file with no error message if file doesn't exist; remove added tab and restore focus, if load fails. (file_editor::contruct): read list of previous tabs from settings (if enabled) * file-editor-tab.h (file-editor-tab:load_file): allow silent load file, return success * file-editor-tab.cc (file-editor-tab:load_file): allow silent load file, return success
author Thorsten Liebig <Thorsten.Liebig@gmx.de>
date Tue, 11 Sep 2012 15:24:22 +0200
parents a9fd6821eedf
children b6b261c3eab3
files libgui/src/m-editor/file-editor-interface.h libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/settings-dialog.cc libgui/src/settings-dialog.ui
diffstat 7 files changed, 55 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-interface.h
+++ b/libgui/src/m-editor/file-editor-interface.h
@@ -57,7 +57,7 @@
   public slots:
     virtual void request_new_file () = 0;
     virtual void request_open_file () = 0;
-    virtual void request_open_file (const QString& fileName) = 0;
+    virtual void request_open_file (const QString& fileName, bool silent = false) = 0;
 
   signals:
       void active_changed (bool active);
--- a/libgui/src/m-editor/file-editor-tab.cc
+++ b/libgui/src/m-editor/file-editor-tab.cc
@@ -627,8 +627,8 @@
     }
 }
 
-void
-file_editor_tab::load_file (const QString& fileName)
+bool
+file_editor_tab::load_file(const QString& fileName, bool silent)
 {
   if (!_file_editor->isVisible ())
     {
@@ -638,10 +638,11 @@
   QFile file (fileName);
   if (!file.open (QFile::ReadOnly))
     {
-      QMessageBox::warning (this, tr ("Octave Editor"),
-                            tr ("Could not open file %1 for read:\n%2.").arg (fileName).
-                            arg (file.errorString ()));
-      return;
+      if (silent==false)
+        QMessageBox::warning (this, tr ("Octave Editor"),
+                              tr ("Could not open file %1 for read:\n%2.").arg (fileName).
+                              arg (file.errorString ()));
+      return false;
     }
 
   QTextStream in (&file);
@@ -655,6 +656,8 @@
 
   update_window_title (false); // window title (no modification)
   _edit_area->setModified (false); // loaded file is not modified yet
+
+  return true;
 }
 
 void
--- a/libgui/src/m-editor/file-editor-tab.h
+++ b/libgui/src/m-editor/file-editor-tab.h
@@ -65,7 +65,7 @@
   void set_modified (bool modified = true);
 
   bool open_file (const QString& dir = QString ());
-  void load_file (const QString& fileName);
+  bool load_file (const QString& fileName, bool silent = false);
   void new_file ();
   bool save_file ();
   bool save_file (const QString& saveFileName);
--- a/libgui/src/m-editor/file-editor.cc
+++ b/libgui/src/m-editor/file-editor.cc
@@ -25,6 +25,7 @@
 #endif
 
 #include "file-editor.h"
+#include "resource-manager.h"
 #include <QVBoxLayout>
 #include <QApplication>
 #include <QFile>
@@ -46,6 +47,20 @@
 
 file_editor::~file_editor ()
 {
+  QSettings *settings = resource_manager::get_settings ();
+  QStringList sessionFileNames;
+  if (settings->value ("editor/restoreSession",true).toBool ())
+  {
+    for (int n=0;n<_tab_widget->count();++n)
+      {
+        file_editor_tab* tab = dynamic_cast<file_editor_tab*> (_tab_widget->widget (n));
+        if (!tab)
+          continue;
+        sessionFileNames.append (tab->get_file_name ());
+      }
+  }
+  settings->setValue ("editor/savedSessionTabs", sessionFileNames);
+  settings->sync ();
 }
 
 QTerminal *
@@ -120,7 +135,7 @@
 }
 
 void
-file_editor::request_open_file (const QString& fileName)
+file_editor::request_open_file (const QString& fileName, bool silent)
 {
   if (!isVisible ())
     {
@@ -128,10 +143,17 @@
     }
 
   file_editor_tab *fileEditorTab = new file_editor_tab (this);
+  int curr_tab_index = _tab_widget->currentIndex ();
   if (fileEditorTab)
     {
       add_file_editor_tab (fileEditorTab);
-      fileEditorTab->load_file (fileName);
+      if (!fileEditorTab->load_file (fileName, silent))
+        {
+          // If no file was loaded, remove the tab again.
+          _tab_widget->removeTab (_tab_widget->indexOf (fileEditorTab));
+          // restore focus to previous tab
+          _tab_widget->setCurrentIndex (curr_tab_index);
+        }
     }
 }
 
@@ -554,6 +576,16 @@
   setWindowIcon (QIcon::fromTheme ("accessories-text-editor",
                                    style->standardIcon (QStyle::SP_FileIcon)));
   setWindowTitle ("Octave Editor");
+
+  //restore previous session
+  QSettings *settings = resource_manager::get_settings ();
+  if (settings->value ("editor/restoreSession",true).toBool ())
+  {
+    QStringList sessionFileNames = settings->value("editor/savedSessionTabs", QStringList()).toStringList ();
+
+    for (int n=0;n<sessionFileNames.count();++n)
+      request_open_file(sessionFileNames.at(n), true);
+  }
 }
 
 void
--- a/libgui/src/m-editor/file-editor.h
+++ b/libgui/src/m-editor/file-editor.h
@@ -64,7 +64,7 @@
 public slots:
   void request_new_file ();
   void request_open_file ();
-  void request_open_file (const QString& fileName);
+  void request_open_file (const QString& fileName, bool silent = false);
 
   void request_undo ();
   void request_redo ();
--- a/libgui/src/settings-dialog.cc
+++ b/libgui/src/settings-dialog.cc
@@ -46,6 +46,7 @@
   ui->editor_fontName->setCurrentFont (QFont (settings->value ("editor/fontName","Courier").toString()) );
   ui->editor_fontSize->setValue (settings->value ("editor/fontSize",10).toInt ());
   ui->editor_longWindowTitle->setChecked (settings->value ("editor/longWindowTitle",false).toBool ());
+  ui->editor_restoreSession->setChecked (settings->value ("editor/restoreSession",true).toBool ());
   ui->terminal_fontName->setCurrentFont (QFont (settings->value ("terminal/fontName","Courier").toString()) );
   ui->terminal_fontSize->setValue (settings->value ("terminal/fontSize",10).toInt ());
   ui->showFilenames->setChecked (settings->value ("showFilenames").toBool());
@@ -107,6 +108,7 @@
   settings->setValue ("editor/fontName", ui->editor_fontName->currentFont().family());
   settings->setValue ("editor/fontSize", ui->editor_fontSize->value());
   settings->setValue ("editor/longWindowTitle", ui->editor_longWindowTitle->isChecked());
+  settings->setValue ("editor/restoreSession", ui->editor_restoreSession->isChecked ());
   settings->setValue ("terminal/fontSize", ui->terminal_fontSize->value());
   settings->setValue ("terminal/fontName", ui->terminal_fontName->currentFont().family());
   settings->setValue ("showFilenames", ui->showFilenames->isChecked ());
--- a/libgui/src/settings-dialog.ui
+++ b/libgui/src/settings-dialog.ui
@@ -141,6 +141,13 @@
            </property>
           </widget>
          </item>
+         <item>
+          <widget class="QCheckBox" name="editor_restoreSession">
+           <property name="text">
+            <string>Restore tabs from previous session on startup</string>
+           </property>
+          </widget>
+         </item>
         </layout>
        </item>
        <item>