# HG changeset patch # User Torsten # Date 1413484177 -7200 # Node ID ce9bd5ed44d2a868e0202d1e7ff8453a92f2854f # Parent 82c329619836d0e7e3e491a7c52f2aeebc6679c3 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 diff -r 82c329619836 -r ce9bd5ed44d2 libgui/src/m-editor/file-editor-tab.cc --- 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 (index); } +void +file_editor_tab::handle_save_as_filter_selected (const QString& filter) +{ + QFileDialog *file_dialog = qobject_cast (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) { diff -r 82c329619836 -r ce9bd5ed44d2 libgui/src/m-editor/file-editor-tab.h --- 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