changeset 19256:ce9bd5ed44d2 gui-release

usage of default suffix depending on filter in save as dialog (bug #43335) * file-editor-tab.cc (save_file_as): use QStringList for adding filters, initialize filter and default suffix depending on suffix of current file name if available, connect the selection of a filter to a new slot; (handle_save_as_filter_selected): new slot setting the default suffix depending on the first suffix in selected filter; * file-editor-tab.h: new slot handle_save_as_filter_selected
author Torsten <ttl@justmail.de>
date Thu, 16 Oct 2014 20:29:37 +0200
parents 82c329619836
children a3eead909bf9
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h
diffstat 2 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Wed Oct 15 11:01:00 2014 -0400
+++ b/libgui/src/m-editor/file-editor-tab.cc	Thu Oct 16 20:29:37 2014 +0200
@@ -1577,10 +1577,22 @@
   // and add the extra grid layout to the dialog's layout
   dialog_layout->addLayout (extra,rows,0,1,dialog_layout->columnCount ());
 
+  // add the possible filters and the default suffix
+  QStringList filters;
+  filters << tr ("Octave Files (*.m)")
+          << tr ("All Files (*)");
+  fileDialog->setNameFilters (filters);
+  fileDialog->setDefaultSuffix ("m");
 
   if (valid_file_name ())
     {
       fileDialog->selectFile (_file_name);
+      QFileInfo file_info (_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
+        }
     }
   else
     {
@@ -1601,11 +1613,12 @@
         fileDialog->selectFile (fname + ".m");
     }
 
-  fileDialog->setNameFilter (tr ("Octave Files (*.m);;All Files (*)"));
-  fileDialog->setDefaultSuffix ("m");
   fileDialog->setAcceptMode (QFileDialog::AcceptSave);
   fileDialog->setViewMode (QFileDialog::Detail);
 
+  connect (fileDialog, SIGNAL (filterSelected (const QString&)),
+           this, SLOT (handle_save_as_filter_selected (const QString&)));
+
   if (remove_on_success)
     {
       connect (fileDialog, SIGNAL (fileSelected (const QString&)),
@@ -1629,6 +1642,20 @@
   _save_as_desired_eol = static_cast<QsciScintilla::EolMode> (index);
 }
 
+void
+file_editor_tab::handle_save_as_filter_selected (const QString& filter)
+{
+  QFileDialog *file_dialog = qobject_cast<QFileDialog *> (sender ());
+
+  QRegExp rx ("\\*\\.([^ ^\\)]*)[ \\)]");   // regexp for suffix in filter
+  int index = rx.indexIn (filter,0);        // get first suffix in filter
+
+  if (index > -1)
+    file_dialog->setDefaultSuffix (rx.cap (1)); // found a suffix, set default
+  else
+    file_dialog->setDefaultSuffix ("");         // not found, clear default
+}
+
 bool
 file_editor_tab::check_valid_identifier (QString file_name)
 {
--- a/libgui/src/m-editor/file-editor-tab.h	Wed Oct 15 11:01:00 2014 -0400
+++ b/libgui/src/m-editor/file-editor-tab.h	Thu Oct 16 20:29:37 2014 +0200
@@ -162,6 +162,7 @@
   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);
   void handle_combo_eol_current_index (int index);
 
   // When apis preparation has finished and is ready to save