changeset 19445:ed0df431631b gui-release

allow to cancel exiting if editor tabs are modified and exit is requested in gui * file-editor-interface.h: new function check_closing * file-editor-tab.cc (ctor): set default for app_closing to 0 (not closing); (check_file_modified): dialog boxes for modified editor tabs depending on app closing or not; (conditional_close): no slot, but ordinary function returning close result; * file-editor-tab.h: conditional_close is function, not a slot; _app_closing is now int for closing from gui, from octave, not closing * file-editor.cc (dtor): calling new function check_closing if there are open tabs (exit requested octave), do not save session here since the tabs might already have been closed; (check_closing): new function saving session info and closing all tabs, stop closing tabs and returning false if user cancels exiting if there is a modified tab; (request_close_file, request_close_all_files, request_close_other_files, handle_tab_close_request): use conditional_close instead of signal; (construct): do not connect old signal for closing the tabs * file-editor.h: remove signal fetab_close_request, new function check_closing * main-window.cc (confirm_exit_octave): call file_editor::check_closing before really closing the app
author Torsten <ttl@justmail.de>
date Sat, 20 Dec 2014 17:26:53 +0100
parents 9887a930465f
children 476032040df9
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/main-window.cc
diffstat 6 files changed, 82 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-interface.h	Mon Dec 15 20:59:21 2014 -0800
+++ b/libgui/src/m-editor/file-editor-interface.h	Sat Dec 20 17:26:53 2014 +0100
@@ -60,6 +60,8 @@
 
   virtual void handle_edit_file_request (const QString& file) = 0;
 
+  virtual bool check_closing (int closing_state) = 0;
+
   virtual void set_focus () = 0;
 
   virtual void empty_script (bool, bool) = 0;
--- a/libgui/src/m-editor/file-editor-tab.cc	Mon Dec 15 20:59:21 2014 -0800
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sat Dec 20 17:26:53 2014 +0100
@@ -69,7 +69,7 @@
 {
   QString directory = directory_arg;
   _lexer_apis = 0;
-  _app_closing = false;
+  _app_closing = 0;   // app is not closing
   _is_octave_file = true;
   _modal_dialog = false;
 
@@ -1243,13 +1243,24 @@
                                              QMessageBox::Discard;
       QString available_actions;
 
-      if (_app_closing)
-        available_actions = tr ("Do you want to save or discard the changes?");
-      else
+      switch (_app_closing)
         {
-          buttons = buttons | QMessageBox::Cancel;  // cancel is allowed
-          available_actions
-            = tr ("Do you want to cancel closing, save or discard the changes?");
+          case -1:  // octave is exiting and so does the gui
+            available_actions =
+              tr ("Do you want to save or discard the changes?");
+            break;
+
+          case 1:   // gui is exiting
+            available_actions =
+              tr ("Do you want to cancel exiting octave, save or discard the changes?");
+            buttons = buttons | QMessageBox::Cancel;
+            break;
+
+          case 0:   // tab is closing
+            available_actions =
+              tr ("Do you want to cancel closing, save or discard the changes?");
+            buttons = buttons | QMessageBox::Cancel;
+            break;
         }
 
       QString file;
@@ -1917,14 +1928,16 @@
   _edit_area->setMarginWidth (2, "1"+QString::number (_edit_area->lines ()));
 }
 
-void
-file_editor_tab::conditional_close (const QWidget *ID, bool app_closing)
+// the following close request was changed from a signal slot into a
+// normal function because we need the return value from close whether
+// the tab really was closed (for canceling exiting octave).
+// When emitting a signal, only the return value from the last slot
+// goes back to the sender
+bool
+file_editor_tab::conditional_close (int app_closing)
 {
-  if (ID != this)
-    return;
-
   _app_closing = app_closing;
-  close ();
+  return close ();
 }
 
 void
--- a/libgui/src/m-editor/file-editor-tab.h	Mon Dec 15 20:59:21 2014 -0800
+++ b/libgui/src/m-editor/file-editor-tab.h	Sat Dec 20 17:26:53 2014 +0100
@@ -51,6 +51,10 @@
 
   octave_qscintilla *qsci_edit_area () { return _edit_area; }
 
+  // Will initiate close if associated with the identifier tag.
+  bool conditional_close (int app_closing = 0);  // default 0: close tab only
+
+
 public slots:
 
   void update_window_title (bool modified);
@@ -61,9 +65,6 @@
   // Tells the editor tab to react on changed settings.
   void notice_settings (const QSettings *settings);
 
-  // Will initiate close if associated with the identifier tag.
-  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);
 
@@ -232,7 +233,7 @@
 
   bool _long_title;
   bool _copy_available;
-  bool _app_closing;
+  int _app_closing;     // -1: octave exits, 1: exit request in gui, 0: no exit
   bool _is_octave_file;
   bool _modal_dialog;
   bool _always_reload_changed_files;
--- a/libgui/src/m-editor/file-editor.cc	Mon Dec 15 20:59:21 2014 -0800
+++ b/libgui/src/m-editor/file-editor.cc	Sat Dec 20 17:26:53 2014 +0100
@@ -63,6 +63,20 @@
 
 file_editor::~file_editor (void)
 {
+  // Close open tabs, if existing. In this case app closing must be
+  // initiated by octave. All tabs will be definetly closed and the
+  // user can not cancel exiting (close state -1)
+  if (_tab_widget->count ())
+    check_closing (-1);
+
+  if (_mru_file_menu)
+    delete _mru_file_menu;
+}
+
+bool
+file_editor::check_closing (int closing_state)
+{
+  // Save open files for restoring in next session; this only is possible
   QSettings *settings = resource_manager::get_settings ();
 
   // Have all file editor tabs signal what their file names are.
@@ -82,16 +96,23 @@
   settings->setValue ("editor/savedSessionTabs", fetFileNames);
   settings->sync ();
 
+  // Close all tabs. If exit is requested by the gui (octave still running)
+  // check whether closing a tab is successful or whether user wnats to cancel
+  // exiting the program. Return false in the latter case.
+  file_editor_tab *editor_tab;
+
   for (int index = _tab_widget->count ()-1; index >= 0; index--)
     {
-      // true: app closing
-      emit fetab_close_request (_tab_widget->widget (index), true);
+      editor_tab = static_cast <file_editor_tab *> (_tab_widget->widget (index));
+      if ((! editor_tab->conditional_close (closing_state)) && closing_state == 1)
+        return false;
     }
 
-  if (_mru_file_menu)
-    delete _mru_file_menu;
+  // Here, we really want to exit and all tabs are closed
+  return true;
 }
 
+
 void
 file_editor::focus (void)
 {
@@ -919,26 +940,39 @@
 void
 file_editor::request_close_file (bool)
 {
-  emit fetab_close_request (_tab_widget->currentWidget ());
+  file_editor_tab *editor_tab =
+      static_cast <file_editor_tab *> (_tab_widget->currentWidget ());
+  editor_tab->conditional_close (0);  // 0: app is not closing, only tab
 }
 
 void
 file_editor::request_close_all_files (bool)
 {
+  file_editor_tab *editor_tab;
+
   // loop over all tabs starting from last one otherwise deletion changes index
   for (int index = _tab_widget->count ()-1; index >= 0; index--)
-    emit fetab_close_request (_tab_widget->widget (index));
+    {
+      editor_tab = static_cast <file_editor_tab *> (_tab_widget->widget (index));
+      editor_tab->conditional_close (0);  // 0: app is not closing, only tab
+    }
 }
 
 void
 file_editor::request_close_other_files (bool)
 {
+  file_editor_tab *editor_tab;
   QWidget *tabID = _tab_widget->currentWidget ();
+
   // loop over all tabs starting from last one otherwise deletion changes index
   for (int index = _tab_widget->count ()-1; index >= 0; index--)
     {
       if (tabID != _tab_widget->widget (index))
-        emit fetab_close_request (_tab_widget->widget (index));
+        {
+          editor_tab =
+              static_cast <file_editor_tab *> (_tab_widget->widget (index));
+          editor_tab->conditional_close (0);  // 0: app is not closing, only tab
+        }
     }
 }
 
@@ -946,12 +980,9 @@
 void
 file_editor::handle_tab_close_request (int index)
 {
-  // Signal to the tabs a request to close whomever matches the identifying
-  // tag (i.e., unique widget pointer).  The reason for this indirection is
-  // that it will enable a file editor widget to toss up a non-static
-  // dialog box and later signal that it wants to be removed.
-  QWidget *tabID = _tab_widget->widget (index);
-  emit fetab_close_request (tabID);
+  file_editor_tab *editor_tab =
+       static_cast <file_editor_tab *> (_tab_widget->widget (index));
+  editor_tab->conditional_close (0);  // 0: app is not closing, only tab
 }
 
 void
@@ -1596,9 +1627,6 @@
   connect (this, SIGNAL (fetab_settings_changed (const QSettings *)),
            f, SLOT (notice_settings (const QSettings *)));
 
-  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	Mon Dec 15 20:59:21 2014 -0800
+++ b/libgui/src/m-editor/file-editor.h	Sat Dec 20 17:26:53 2014 +0100
@@ -69,7 +69,6 @@
 signals:
 
   void fetab_settings_changed (const QSettings *settings);
-  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
@@ -117,6 +116,7 @@
 
 public slots:
   void focus (void);
+  bool check_closing (int closing_state);
 
   void request_new_file (const QString& commands);
   void request_new_script (const QString& commands);
--- a/libgui/src/main-window.cc	Mon Dec 15 20:59:21 2014 -0800
+++ b/libgui/src/main-window.cc	Sat Dec 20 17:26:53 2014 +0100
@@ -2461,9 +2461,12 @@
           QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
 
       if (ans !=  QMessageBox::Ok)
-        closenow = false;
+        return false;
 
     }
+
+  closenow = editor_window->check_closing (1);  // 1: exit request from gui
+
   return closenow;
 }