changeset 33425:3e7059ae815c stable

make editor file dialogs modal * file-editor-tab.cc (save_file_as): file dialog not a pointer, make modal and show with exec(), get selected filter and files after exec returns without using signal/slot mechaninsm; (handle_save_as_filter_selected): removed slot; (handle_save_file_as_answer): removed slot; (handle_save_file_as_answer_close): removed slot; (handle_save_file_as_answer_cancel): removed slot; * file-editor-tab.h removed slots handle_save_file_as_answer, handle_save_file_as_answer_close, handle_save_file_as_answer_cancel, and handle_save_as_filter_selected * main-window.cc (request_open_file): do not use pointer for file dialog, use exec () and get selected files when exec return instead of using signal/slot mechanism; (request_open_files): remove slot * main-window.h: remove slot request_open_files
author Torsten Lilge <ttl-octave@mailbox.org>
date Sun, 07 Apr 2024 22:17:52 +0200
parents 1edbab3fae48
children 25faa5446214
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/main-window.cc libgui/src/main-window.h
diffstat 4 files changed, 95 insertions(+), 164 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Wed Apr 17 17:18:08 2024 +0200
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sun Apr 07 22:17:52 2024 +0200
@@ -2507,108 +2507,119 @@
   // reset m_new_encoding
   m_new_encoding = m_encoding;
 
-  // If the tab is removed in response to a QFileDialog signal, the tab
-  // can't be a parent.
-  QFileDialog *fileDialog;
-  if (remove_on_success)
-    {
-      // If tab is closed, "this" cannot be parent in which case modality
-      // has no effect.  Disable editing instead.
-      m_edit_area->setReadOnly (true);
-      fileDialog = new QFileDialog ();
-    }
-  else
-    fileDialog = new QFileDialog (this);
+  QFileDialog fileDialog (this);
 
   gui_settings settings;
 
   if (! settings.bool_value (global_use_native_dialogs))
     {
       // Qt file dialogs
-      fileDialog->setOption(QFileDialog::DontUseNativeDialog);
+      fileDialog.setOption(QFileDialog::DontUseNativeDialog);
     }
   else
     {
       // Native file dialogs: Test for already existing files is done manually
       // since native file dialogs might not consider the automatically
       // appended default extension when checking if the file already exists
-      fileDialog->setOption(QFileDialog::DontConfirmOverwrite);
+      fileDialog.setOption(QFileDialog::DontConfirmOverwrite);
     }
 
   // add the possible filters and the default suffix
   QStringList filters;
   filters << tr ("Octave Files (*.m)")
           << tr ("All Files (*)");
-  fileDialog->setNameFilters (filters);
-  fileDialog->setDefaultSuffix ("m");
+  fileDialog.setNameFilters (filters);
 
   if (valid_file_name ())
     {
-      fileDialog->selectFile (m_file_name);
+      fileDialog.selectFile (m_file_name);
       QFileInfo file_info (m_file_name);
       if (file_info.suffix () != "m")
-        {
-          // it is not an octave file
-          fileDialog->selectNameFilter (filters.at (1));  // "All Files"
-          fileDialog->setDefaultSuffix ("");              // no default suffix
-        }
+        fileDialog.selectNameFilter (filters.at (1));  // "All Files"
     }
   else
     {
-      fileDialog->selectFile ("");
-      fileDialog->setDirectory (m_ced);
+      fileDialog.selectFile ("");
+      fileDialog.setDirectory (m_ced);
 
       // propose a name corresponding to the function name
       // if the new file contains a function
       QString fname = get_function_name ();
       if (! fname.isEmpty ())
-        fileDialog->selectFile (fname + ".m");
+        fileDialog.selectFile (fname + ".m");
     }
 
-  fileDialog->setAcceptMode (QFileDialog::AcceptSave);
-  fileDialog->setViewMode (QFileDialog::Detail);
-  fileDialog->setOption (QFileDialog::HideNameFilterDetails, false);
-
-  // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
-
-  connect (fileDialog, &QFileDialog::filterSelected,
-           this, &file_editor_tab::handle_save_as_filter_selected);
-
-  if (remove_on_success)
-    {
-      connect (fileDialog, &QFileDialog::fileSelected,
-               this, &file_editor_tab::handle_save_file_as_answer_close);
-
-      connect (fileDialog, &QFileDialog::rejected,
-               this, &file_editor_tab::handle_save_file_as_answer_cancel);
-    }
-  else
+  fileDialog.setAcceptMode (QFileDialog::AcceptSave);
+  fileDialog.setViewMode (QFileDialog::Detail);
+  fileDialog.setOption (QFileDialog::HideNameFilterDetails, false);
+
+  if (fileDialog.exec ())     // Modal dialog
     {
-      connect (fileDialog, &QFileDialog::fileSelected,
-               this, &file_editor_tab::handle_save_file_as_answer);
+      // Accepted file dialog, no actions required if rejected
+
+      QString save_file_name = fileDialog.selectedFiles ().at (0);
+
+      if (remove_on_success)
+        {
+          // Remove the tab after save
+          if (check_valid_identifier (save_file_name))
+            save_file_as (true);
+          else
+            emit editor_check_conflict_save (save_file_name, true);
+        }
+      else
+        {
+          // Save the file under the selected name
+
+          QFileInfo file (save_file_name);
+
+          // Make sure that the file has the desire suffix
+          QString filter = fileDialog.selectedNameFilter ();
+          QRegularExpression rx {"\\*\\.([^ ^\\)]*)[ \\)]"};    // regexp for suffix in filter
+          QRegularExpressionMatch match = rx.match (filter);
+
+          bool file_name_changed = false;
+          if (match.hasMatch () && file.suffix ().isEmpty ())
+            {
+              save_file_name = save_file_name + "." + match.captured (1);
+              file_name_changed = true;
+            }
+
+          // Use final file name
+          file.setFile (save_file_name);
+
+          // Check if overwrite has to checked again
+          if ((file_name_changed || fileDialog.testOption (QFileDialog::DontConfirmOverwrite))
+              && file.exists ())
+            {
+              int ans = QMessageBox::question (this,
+                                    tr ("Octave Editor"),
+                                    tr ("%1\n already exists\n"
+                                        "Do you want to overwrite it?").arg (save_file_name),
+                                    QMessageBox::Yes | QMessageBox::No);
+              if (ans != QMessageBox::Yes)
+                {
+                  // Try again, if edit area is read only, remove on success
+                  save_file_as (remove_on_success);
+                  return;
+                }
+            }
+
+          if (save_file_name == m_file_name)
+            {
+              save_file (save_file_name);
+            }
+          else
+            {
+              // Have editor check for conflict, do not delete tab after save.
+              if (check_valid_identifier (save_file_name))
+                save_file_as (false);
+              else
+                emit editor_check_conflict_save (save_file_name, false);
+            }
+
+        }
     }
-
-  show_dialog (fileDialog, ! valid_file_name ());
-}
-
-void file_editor_tab::handle_save_as_filter_selected (const QString& filter)
-{
-  // On some systems, the filterSelected signal is emitted without user
-  // action and with  an empty filter string when the file dialog is shown.
-  // Just return in this case and do not remove the current default suffix.
-  if (filter.isEmpty ())
-    return;
-
-  QFileDialog *file_dialog = qobject_cast<QFileDialog *> (sender ());
-
-  // regexp for suffix in filter
-  QRegularExpression rx {"\\*\\.([^ ^\\)]*)[ \\)]"};
-  QRegularExpressionMatch match = rx.match (filter);
-
-  if (match.hasMatch ())
-    file_dialog->setDefaultSuffix (match.captured (1)); // found a suffix, set default
-  else
-    file_dialog->setDefaultSuffix ("");  // not found, clear default
 }
 
 bool file_editor_tab::check_valid_identifier (QString file_name)
@@ -2693,71 +2704,6 @@
   return can_encode;
 }
 
-void file_editor_tab::handle_save_file_as_answer (const QString& save_file_name)
-{
-  QString saveFileName = save_file_name;
-  QFileInfo file (saveFileName);
-  QFileDialog *file_dialog = qobject_cast<QFileDialog *> (sender ());
-
-  // Test if the file dialog should have added a default file
-  // suffix, but the selected file still has no suffix (see Qt bug
-  // https://bugreports.qt.io/browse/QTBUG-59401)
-  if ((! file_dialog->defaultSuffix ().isEmpty ()) && file.suffix ().isEmpty ())
-    {
-      saveFileName = saveFileName + "." + file_dialog->defaultSuffix ();
-    }
-
-  file.setFile (saveFileName);
-
-  // If overwrite confirmation was not done by the file dialog (in case
-  // of native file dialogs, see above), do it here
-  if (file_dialog->testOption (QFileDialog::DontConfirmOverwrite) && file.exists ())
-    {
-      int ans = QMessageBox::question (file_dialog,
-                            tr ("Octave Editor"),
-                            tr ("%1\n already exists\n"
-                                "Do you want to overwrite it?").arg (saveFileName),
-                            QMessageBox::Yes | QMessageBox::No);
-      if (ans != QMessageBox::Yes)
-        {
-          // Try again, if edit area is read only, remove on success
-          save_file_as (m_edit_area->isReadOnly ());
-          return;
-        }
-    }
-
-  if (saveFileName == m_file_name)
-    {
-      save_file (saveFileName);
-    }
-  else
-    {
-      // Have editor check for conflict, do not delete tab after save.
-      if (check_valid_identifier (saveFileName))
-        save_file_as (false);
-      else
-        emit editor_check_conflict_save (saveFileName, false);
-    }
-}
-
-void file_editor_tab::handle_save_file_as_answer_close (const QString& saveFileName)
-{
-  // saveFileName == m_file_name can not happen, because we only can get here
-  // when we close a tab and m_file_name is not a valid filename yet
-
-  // Have editor check for conflict, delete tab after save.
-  if (check_valid_identifier (saveFileName))
-    save_file_as (true);
-  else
-    emit editor_check_conflict_save (saveFileName, true);
-}
-
-void file_editor_tab::handle_save_file_as_answer_cancel ()
-{
-  // User canceled, allow editing again.
-  m_edit_area->setReadOnly (false);
-}
-
 void file_editor_tab::file_has_changed (const QString&, bool do_close)
 {
   bool file_exists = QFile::exists (m_file_name);
--- a/libgui/src/m-editor/file-editor-tab.h	Wed Apr 17 17:18:08 2024 +0200
+++ b/libgui/src/m-editor/file-editor-tab.h	Sun Apr 07 22:17:52 2024 +0200
@@ -218,12 +218,6 @@
   // When user closes message box for resave question.
   void handle_file_resave_answer (int decision);
 
-  // When user closes QFileDialog box.
-  void handle_save_file_as_answer (const QString& fileName);
-  void handle_save_file_as_answer_close (const QString& fileName);
-  void handle_save_file_as_answer_cancel ();
-  void handle_save_as_filter_selected (const QString& filter);
-
   // When user changes encoding after decoding errors were found
   void handle_current_enc_changed (const QString& enc);
 
--- a/libgui/src/main-window.cc	Wed Apr 17 17:18:08 2024 +0200
+++ b/libgui/src/main-window.cc	Sun Apr 07 22:17:52 2024 +0200
@@ -1335,25 +1335,25 @@
   if (is_internal)
     p = m_editor_window;
 
-  QFileDialog *fileDialog = new QFileDialog (p);
+  QFileDialog fileDialog (p);
 
   // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
   if (! settings.bool_value (global_use_native_dialogs))
-    fileDialog->setOption(QFileDialog::DontUseNativeDialog);
-
-  fileDialog->setNameFilter (tr ("Octave Files (*.m);;All Files (*)"));
-
-  fileDialog->setAcceptMode (QFileDialog::AcceptOpen);
-  fileDialog->setViewMode (QFileDialog::Detail);
-  fileDialog->setFileMode (QFileDialog::ExistingFiles);
-  fileDialog->setDirectory (m_current_directory_combo_box->itemText (0));
-
-  connect (fileDialog, &QFileDialog::filesSelected,
-           this, &main_window::request_open_files);
-
-  fileDialog->setWindowModality (Qt::NonModal);
-  fileDialog->setAttribute (Qt::WA_DeleteOnClose);
-  fileDialog->show ();
+    fileDialog.setOption(QFileDialog::DontUseNativeDialog);
+
+  fileDialog.setNameFilter (tr ("Octave Files (*.m);;All Files (*)"));
+
+  fileDialog.setAcceptMode (QFileDialog::AcceptOpen);
+  fileDialog.setViewMode (QFileDialog::Detail);
+  fileDialog.setFileMode (QFileDialog::ExistingFiles);
+  fileDialog.setDirectory (m_current_directory_combo_box->itemText (0));
+
+  if (fileDialog.exec ())
+    {
+      QStringList open_file_names = fileDialog.selectedFiles();
+      for (int i = 0; i < open_file_names.count (); i++)
+        emit open_file_signal (open_file_names.at (i), m_file_encoding, -1);
+    }
 }
 
 // Create a new script
@@ -1940,15 +1940,6 @@
   m_file_encoding = new_encoding;
 }
 
-// The following slot is called after files have been selected in the
-// open file dialog, possibly with a new selected encoding stored in
-// m_file_encoding
-void main_window::request_open_files (const QStringList& open_file_names)
-{
-  for (int i = 0; i < open_file_names.count (); i++)
-    emit open_file_signal (open_file_names.at (i), m_file_encoding, -1);
-}
-
 void main_window::profiler_session ()
 {
   emit interpreter_event
--- a/libgui/src/main-window.h	Wed Apr 17 17:18:08 2024 +0200
+++ b/libgui/src/main-window.h	Sun Apr 07 22:17:52 2024 +0200
@@ -247,7 +247,7 @@
   void disable_menu_shortcuts (bool disable);
   void restore_create_file_setting ();
   void set_file_encoding (const QString& new_encoding);
-  void request_open_files (const QStringList& open_file_names);
+//  void request_open_files (const QStringList& open_file_names);
 
   void warning_function_not_found (const QString& message);