# HG changeset patch # User Thorsten Liebig # Date 1347369862 -7200 # Node ID 842ab161c10a3ebd6b2d662c0c90b1edd152256b # Parent a9fd6821eedff29229365986da28a68adf07fdb3 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 diff -r a9fd6821eedf -r 842ab161c10a libgui/src/m-editor/file-editor-interface.h --- a/libgui/src/m-editor/file-editor-interface.h Tue Sep 11 10:50:43 2012 +0200 +++ b/libgui/src/m-editor/file-editor-interface.h Tue Sep 11 15:24:22 2012 +0200 @@ -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); diff -r a9fd6821eedf -r 842ab161c10a libgui/src/m-editor/file-editor-tab.cc --- a/libgui/src/m-editor/file-editor-tab.cc Tue Sep 11 10:50:43 2012 +0200 +++ b/libgui/src/m-editor/file-editor-tab.cc Tue Sep 11 15:24:22 2012 +0200 @@ -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 diff -r a9fd6821eedf -r 842ab161c10a libgui/src/m-editor/file-editor-tab.h --- a/libgui/src/m-editor/file-editor-tab.h Tue Sep 11 10:50:43 2012 +0200 +++ b/libgui/src/m-editor/file-editor-tab.h Tue Sep 11 15:24:22 2012 +0200 @@ -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); diff -r a9fd6821eedf -r 842ab161c10a libgui/src/m-editor/file-editor.cc --- a/libgui/src/m-editor/file-editor.cc Tue Sep 11 10:50:43 2012 +0200 +++ b/libgui/src/m-editor/file-editor.cc Tue Sep 11 15:24:22 2012 +0200 @@ -25,6 +25,7 @@ #endif #include "file-editor.h" +#include "resource-manager.h" #include #include #include @@ -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 (_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;neditor_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 ()); diff -r a9fd6821eedf -r 842ab161c10a libgui/src/settings-dialog.ui --- a/libgui/src/settings-dialog.ui Tue Sep 11 10:50:43 2012 +0200 +++ b/libgui/src/settings-dialog.ui Tue Sep 11 15:24:22 2012 +0200 @@ -141,6 +141,13 @@ + + + + Restore tabs from previous session on startup + + +