changeset 27605:60cecb3fed04

fix saving modified files when closing editor tabs or octave * file-editor-tab.cc (closeEvent) also ignore evnet when file has to be saved, it is directly removed after saving now; in case closing is accepted, emit the related signal to the editor; (check_modified_file): call check_file_modified with false as new boolean parameter indicating that file should not be closed afterwards (check_file_modified): new boolean parameter for removing the file after checking for modification and possibly saving; use exec for the dialog for directly getting the result without extra finish signal/slot; if desired, call save_file from here; (handle_file_modified_answer): removed obsolete slot for dialog result (do_save_file): emit signal that tab could be closed now * file-editor-tab.h: new signal tab_ready_to_close, removed slo handle_file_modified_answer, slot check_file_modified with new boolean parameter * file-editor.cc (check_closing): if closing is not canceled connect tab signals on being ready to close instead of directly closing all tabs and store the number of tabs that have to be closed; (handle_tab_ready_to_close): slot for tab ready to close signal, decreasing number of tabs to be closed until it reaches zero and then close all tabs and the editor itself; (closeEvent): ignore event even when all tabs should be closed because editor will close itself after all tabs are closed * file-editor.h: new slot handle_tab_ready_to_close, new class variable m_number_of_tabs for the number of tabs, for which the editor has to wait for closing
author Torsten Lilge <ttl-octave@mailbox.org>
date Fri, 01 Nov 2019 09:26:22 +0100
parents 9bc81bf5d8a6
children 41a7902b99d4
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
diffstat 4 files changed, 51 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Oct 31 15:42:13 2019 -0400
+++ b/libgui/src/m-editor/file-editor-tab.cc	Fri Nov 01 09:26:22 2019 +0100
@@ -309,15 +309,19 @@
     m_cancelled = false;  // prevent unwanted interaction of previous
     // exits of octave which were canceled by the user
 
-    if (check_file_modified () == QMessageBox::Cancel)
+    int save_dialog = check_file_modified (true);
+    if ((save_dialog == QMessageBox::Cancel) ||
+        (save_dialog == QMessageBox::Save))
       {
-        // ignore close event if file is not saved and user cancels
-        // closing this window
+        // Ignore close event if file is saved or user cancels
+        // closing this window. In case of saving, tab is closed after
+        // successful saving.
         e->ignore ();
       }
     else
       {
         e->accept ();
+        emit tab_ready_to_close ();
         emit tab_remove_request ();
       }
   }
@@ -998,7 +1002,7 @@
     if (m_cancelled)
       return;
 
-    if (check_file_modified () == QMessageBox::Cancel)
+    if (check_file_modified (false) == QMessageBox::Cancel)
       m_cancelled = true;
   }
 
@@ -1827,7 +1831,7 @@
       }
   }
 
-  int file_editor_tab::check_file_modified (void)
+  int file_editor_tab::check_file_modified (bool remove)
   {
     int decision = QMessageBox::Yes;
     if (m_edit_area->isModified ())
@@ -1862,44 +1866,21 @@
 
         msgBox->setDefaultButton (QMessageBox::Save);
         m_edit_area->setReadOnly (true);
-        connect (msgBox, SIGNAL (finished (int)),
-                 this, SLOT (handle_file_modified_answer (int)));
-
-        show_dialog (msgBox, true);
-
-        if (m_cancelled)
-          return QMessageBox::Cancel;
-        else
-          return decision;
-      }
-    else
-      {
-        // Nothing was modified.  Leave tab present in case user
-        // decides to cancel some point further along.
+
+        decision = msgBox->exec (); // show_dialog (msgBox, true);
+
+        if (decision == QMessageBox::Cancel)
+          {
+            m_cancelled = true;
+            m_edit_area->setReadOnly (false);
+          }
+        else if (decision == QMessageBox::Save)
+          save_file (m_file_name, remove, false);   // Remove on success
       }
 
     return decision;
   }
 
-  void file_editor_tab::handle_file_modified_answer (int decision)
-  {
-    if (decision == QMessageBox::Save)
-      {
-        // Save file, but do not remove from editor.
-        save_file (m_file_name, false, false);
-      }
-    else if (decision == QMessageBox::Discard)
-      {
-        // User doesn't want to save, leave tab and remove subsequently.
-      }
-    else
-      {
-        // User canceled, allow editing again.
-        m_edit_area->setReadOnly (false);
-        m_cancelled = true;
-      }
-  }
-
   void file_editor_tab::set_modified (bool modified)
   {
     m_edit_area->setModified (modified);
@@ -2432,6 +2413,8 @@
     m_edit_area->setModified (false);
     m_enc_indicator->setText (m_encoding);
 
+    emit tab_ready_to_close ();
+
     if (remove_on_success)
       {
         emit tab_remove_request ();
--- a/libgui/src/m-editor/file-editor-tab.h	Thu Oct 31 15:42:13 2019 -0400
+++ b/libgui/src/m-editor/file-editor-tab.h	Fri Nov 01 09:26:22 2019 +0100
@@ -69,6 +69,7 @@
 
   signals:
 
+    void tab_ready_to_close (void);
     void file_name_changed (const QString& fileName,
                             const QString& toolTip,
                             bool modified);
@@ -223,9 +224,6 @@
     // When user closes message box for resave question.
     void handle_file_resave_answer (int decision);
 
-    // When user closes message box for modified question.
-    void handle_file_modified_answer (int decision);
-
     // When user closes find_dialog box.
     void handle_find_dialog_finished (int decision);
 
@@ -298,7 +296,7 @@
     void update_lexer_settings (void);
 
     void show_dialog (QDialog *dlg, bool modal);
-    int check_file_modified (void);
+    int check_file_modified (bool remove = false);
     void do_comment_selected_text (bool comment, bool input_str = false);
     void do_indent_selected_text (bool indent);
     void do_smart_indent_line_or_selected_text (void);
--- a/libgui/src/m-editor/file-editor.cc	Thu Oct 31 15:42:13 2019 -0400
+++ b/libgui/src/m-editor/file-editor.cc	Fri Nov 01 09:26:22 2019 +0100
@@ -401,6 +401,28 @@
         return false;
       }
 
+    // Wait for all editor tabs to have saved their files if required
+    m_number_of_tabs = m_editor_tab_map.size ();
+
+    for (auto p = m_editor_tab_map.cbegin ();
+         p != m_editor_tab_map.cend (); p++)
+      {
+        QString file_name = p->first;   // get file name of tab
+        connect (static_cast<file_editor_tab *> (m_editor_tab_map[file_name].fet_ID),
+                 SIGNAL (tab_ready_to_close (void)),
+                 this, SLOT (handle_tab_ready_to_close (void)));
+      }
+
+    return true;
+  }
+
+  void file_editor::handle_tab_ready_to_close (void)
+  {
+    m_number_of_tabs--;
+
+    if (m_number_of_tabs > 0)
+      return;
+
     // Here, the application or the editor will be closed -> store the session
 
     // Save open files for restoring in next session; this only is possible
@@ -415,7 +437,7 @@
 
     // save all open tabs before they are definitely closed
     for (auto p = m_editor_tab_map.cbegin ();
-         p != m_editor_tab_map.cend (); p++)
+        p != m_editor_tab_map.cend (); p++)
       {
         QString file_name = p->first;   // get file name of tab
         if (! file_name.isEmpty ())      // do not append unnamed files
@@ -427,7 +449,7 @@
             file_editor_tab *editor_tab
               = static_cast<file_editor_tab *> (m_editor_tab_map[file_name].fet_ID);
             fet_index.append (index.setNum
-                              (m_tab_widget->indexOf (editor_tab)));
+                             (m_tab_widget->indexOf (editor_tab)));
 
             int l, c;
             editor_tab->qsci_edit_area ()->getCursorPosition (&l, &c);
@@ -456,7 +478,7 @@
 
     setVisible (vis);
 
-    return true;
+    octave_dock_widget::close ();
   }
 
   void file_editor::request_new_file (const QString& commands)
@@ -1646,7 +1668,7 @@
             // all tabs are closed without cancelling,
             // store closing state for restoring session when shown again
             m_closed = true;
-            e->accept ();
+            e->ignore ();
           }
         else
           {
--- a/libgui/src/m-editor/file-editor.h	Thu Oct 31 15:42:13 2019 -0400
+++ b/libgui/src/m-editor/file-editor.h	Fri Nov 01 09:26:22 2019 +0100
@@ -187,6 +187,7 @@
     void set_focus (QWidget *fet);
     void enable_menu_shortcuts (bool);
     bool check_closing (void);
+    void handle_tab_ready_to_close (void);
 
     void request_new_file (const QString& commands);
     void request_close_file (bool);
@@ -342,6 +343,7 @@
 
     QMenu * add_menu (QMenuBar *p, QString text);
 
+    int m_number_of_tabs;
     std::map<QString, tab_info> m_editor_tab_map;
     QHash<QMenu*, QStringList> m_hash_menu_text;