changeset 16640:3c2e457eeb72 default tip

ask for saving modified editor files if octave is closed (bug #38689) * files-editor-tab.cc(constrctor): init new flag indicating if app is closing, (check_file_modified): message box is modal if app is closing, no cancel, parent of box is the editor's tab widget for a correct palcement of the box, (conditional_close): new second arg: flag for closing app (default false), it is stored in the tab's class wide flag * file-editor-tab.h: second arg for conditional_close and new class wide flag * file-editor.cc(destructor): sending close requests to all editor tabs with flag indicating the application is closing (add_file_editor_tab): new arg for fetab_close_request and conditional_close * file-editor.h: new 2nd arg for fetab_close_request (closing app, def. false) * main-window.cc(destructor): delete editor window first for showing the message boxes for modified editor files in front of a complete gui
author Torsten <ttl@justmail.de>
date Sat, 11 May 2013 18:20:31 +0200
parents b3f4bdd7e5f4
children
files 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/main-window.cc
diffstat 5 files changed, 36 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc
+++ b/libgui/src/m-editor/file-editor-tab.cc
@@ -57,6 +57,8 @@
 {
   QString directory = directory_arg;
 
+  _app_closing = false;
+
   // Make sure there is a slash at the end of the directory name
   // for identification when saved later.
   if (directory.count () && directory.at (directory.count () - 1) != '/')
@@ -708,22 +710,40 @@
       // File is modified but not saved, ask user what to do.  The file
       // editor tab can't be made parent because it may be deleted depending
       // upon the response.  Instead, change the _edit_area to read only.
+      QMessageBox::StandardButtons buttons = QMessageBox::Save |
+                                             QMessageBox::Discard;
+      QString available_actions;
+
+      if (_app_closing)
+          available_actions = tr ("Do you want to save or discard the changes?");
+      else
+        {
+          buttons = buttons | QMessageBox::Cancel;  // cancel is allowed
+          available_actions
+            = tr ("Do you want to cancel closing, save or discard the changes?");
+        }
+
       QMessageBox* msgBox
         = new QMessageBox (QMessageBox::Warning, tr ("Octave Editor"),
                            tr ("The file\n"
                                "%1\n"
                                "is about to be closed but has been modified.\n"
-                               "Do you want to cancel closing, save or discard the changes?").
-                           arg (_file_name),
-                           QMessageBox::Save | QMessageBox::Cancel | QMessageBox::Discard, 0);
+                               "%2").
+                           arg (_file_name). arg (available_actions),
+                           buttons, qobject_cast<QWidget *> (parent ()));
 
       msgBox->setDefaultButton (QMessageBox::Save);
       _edit_area->setReadOnly (true);
       connect (msgBox, SIGNAL (finished (int)),
                this, SLOT (handle_file_modified_answer (int)));
-      msgBox->setWindowModality (Qt::NonModal);
       msgBox->setAttribute (Qt::WA_DeleteOnClose);
-      msgBox->show ();
+      if (_app_closing)  // app is closing, a non modal dialogs prevent
+        msgBox->exec (); // the app of being closed before an answer from user
+      else
+        {
+          msgBox->setWindowModality (Qt::NonModal);
+          msgBox->show ();
+        }
 
       return QMessageBox::Cancel;
     }
@@ -1062,11 +1082,12 @@
 }
 
 void
-file_editor_tab::conditional_close (const QWidget *ID)
+file_editor_tab::conditional_close (const QWidget *ID, bool app_closing)
 {
   if (ID != this)
     return;
 
+  _app_closing = app_closing;
   close ();
 }
 
--- a/libgui/src/m-editor/file-editor-tab.h
+++ b/libgui/src/m-editor/file-editor-tab.h
@@ -55,7 +55,7 @@
   void notice_settings (const QSettings *settings);
 
   // Will initiate close if associated with the identifier tag.
-  void conditional_close (const QWidget *ID);
+  void conditional_close (const QWidget *ID, bool app_closing = false);
 
   // Change to a different editor tab by identifier tag.
   void change_editor_state (const QWidget *ID);
@@ -182,6 +182,7 @@
 
   bool _long_title;
   bool _copy_available;
+  bool _app_closing;
 
   QFileSystemWatcher _file_system_watcher;
 
--- a/libgui/src/m-editor/file-editor.cc
+++ b/libgui/src/m-editor/file-editor.cc
@@ -72,6 +72,9 @@
   settings->setValue ("editor/savedSessionTabs", fetFileNames);
   settings->sync ();
 
+  for (int index = _tab_widget->count ()-1; index >= 0; index--)
+    emit fetab_close_request (_tab_widget->widget (index),true); // true: app closing
+
   if (_mru_file_menu)
     delete _mru_file_menu;
 }
@@ -1083,8 +1086,8 @@
   connect (this, SIGNAL (fetab_settings_changed (const QSettings *)),
            f, SLOT (notice_settings (const QSettings *)));
 
-  connect (this, SIGNAL (fetab_close_request (const QWidget*)),
-           f, SLOT (conditional_close (const QWidget*)));
+  connect (this, SIGNAL (fetab_close_request (const QWidget*,bool)),
+           f, SLOT (conditional_close (const QWidget*,bool)));
 
   connect (this, SIGNAL (fetab_change_request (const QWidget*)),
            f, SLOT (change_editor_state (const QWidget*)));
--- a/libgui/src/m-editor/file-editor.h
+++ b/libgui/src/m-editor/file-editor.h
@@ -62,7 +62,7 @@
 signals:
 
   void fetab_settings_changed (const QSettings *settings);
-  void fetab_close_request (const QWidget* ID);
+  void fetab_close_request (const QWidget* ID, bool app_closing = false);
   void fetab_change_request (const QWidget* ID);
   void fetab_file_name_query (const QWidget* ID);
   // Save is a ping-pong type of communication
--- a/libgui/src/main-window.cc
+++ b/libgui/src/main-window.cc
@@ -87,9 +87,9 @@
   // Destroy the terminal first so that STDERR stream is redirected back
   // to its original pipe to capture error messages at exit.
 
+  delete editor_window;     // first one for dialogs of modified editor-tabs
   delete command_window;
   delete workspace_window;
-  delete editor_window;
   delete doc_browser_window;
   delete file_browser_window;
   delete history_window;