# HG changeset patch
# User Kai T. Ohlhus <k.ohlhus@gmail.com>
# Date 1542320694 -3600
#      Thu Nov 15 23:24:54 2018 +0100
# Node ID c1347a3d125a46628a2437f35c1baf20dce7416b
# Parent  32d4ee071348b6d424eb25afba0bb4184adf842e
Make use of native file dialogs configurable (bug #54607).

New qt option "use_native_file_dialogs" to control application of
QFileDialog::DontUseNativeDialog:

* libgui/src/dialog.cc (FileDialog):
* libgui/src/files-dock-widget.cc (popdownmenu_search_dir):
* libgui/src/find-files-dialog.cc (browse_folders):
* libgui/src/m-editor/file-editor-tab.cc (save_file_as):
* libgui/src/main-window.cc (handle_save_workspace_request,
  handle_load_workspace_request, browse_for_directory, request_open_file):
* libgui/src/settings-dialog.cc (get_dir):
* libgui/src/shortcut-manager.cc (do_import_export):
* libgui/src/variable-editor.cc (save):


New checkbox "cb_use_native_file_dialogs" associated with qt option
"use_native_file_dialogs":

* libgui/src/settings-dialog.cc (settings_dialog, write_changed_settings):
* libgui/src/settings-dialog.ui:

diff -r 32d4ee071348 -r c1347a3d125a libgui/src/dialog.cc
--- a/libgui/src/dialog.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/dialog.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -26,6 +26,7 @@ along with Octave; see the file COPYING.
 #endif
 
 #include "dialog.h"
+#include "resource-manager.h"
 
 #include <QString>
 #include <QStringList>
@@ -462,6 +463,11 @@ namespace octave
 
     setWindowTitle (title.isEmpty () ? " " : title);
     setDirectory (dirname);
+    
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      setOption(QFileDialog::DontUseNativeDialog);
 
     if (multimode == "on")         // uigetfile multiselect=on
       {
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/files-dock-widget.cc
--- a/libgui/src/files-dock-widget.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/files-dock-widget.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -852,10 +852,16 @@ namespace octave
 
   void files_dock_widget::popdownmenu_search_dir (bool)
   {
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    int opts = QFileDialog::ShowDirsOnly;
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      opts |= QFileDialog::DontUseNativeDialog;
+
     QString dir = QFileDialog::getExistingDirectory (this,
                      tr ("Set directory of file browser"),
                      m_file_system_model->rootPath (),
-                     QFileDialog::ShowDirsOnly);
+                     QFileDialog::Option (opts));
     set_current_directory (dir);
   }
 
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/find-files-dialog.cc
--- a/libgui/src/find-files-dialog.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/find-files-dialog.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -313,9 +313,16 @@ namespace octave
 
   void find_files_dialog::browse_folders (void)
   {
+    int opts = 0;  // No options by default.
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      opts = QFileDialog::DontUseNativeDialog;
+
     QString dir =
       QFileDialog::getExistingDirectory (this, tr ("Set search directory"),
-                                         m_start_dir_edit->text ());
+                                         m_start_dir_edit->text (),
+                                         QFileDialog::Option (opts));
 
     if (! dir.isEmpty ())
       m_start_dir_edit->setText (dir);
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/m-editor/file-editor-tab.cc
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/m-editor/file-editor-tab.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -2228,6 +2228,11 @@ namespace octave
     fileDialog->setAcceptMode (QFileDialog::AcceptSave);
     fileDialog->setViewMode (QFileDialog::Detail);
 
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      fileDialog->setOption(QFileDialog::DontUseNativeDialog);
+
     connect (fileDialog, SIGNAL (filterSelected (const QString&)),
              this, SLOT (handle_save_as_filter_selected (const QString&)));
 
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/main-window.cc
--- a/libgui/src/main-window.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/main-window.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -383,9 +383,15 @@ namespace octave
 
   void main_window::handle_save_workspace_request (void)
   {
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    int opts = 0;  // No options by default.
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      opts = QFileDialog::DontUseNativeDialog;
+
     QString file
       = QFileDialog::getSaveFileName (this, tr ("Save Workspace As"), ".",
-                                      nullptr, nullptr);
+                                      nullptr, nullptr, QFileDialog::Option (opts));
 
     if (! file.isEmpty ())
       {
@@ -397,11 +403,17 @@ namespace octave
 
   void main_window::handle_load_workspace_request (const QString& file_arg)
   {
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    int opts = 0;  // No options by default.
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      opts = QFileDialog::DontUseNativeDialog;
+
     QString file = file_arg;
 
     if (file.isEmpty ())
       file = QFileDialog::getOpenFileName (this, tr ("Load Workspace"), ".",
-                                           nullptr, nullptr);
+                                           nullptr, nullptr, QFileDialog::Option (opts));
 
     if (! file.isEmpty ())
       {
@@ -876,9 +888,15 @@ namespace octave
 
   void main_window::browse_for_directory (void)
   {
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    int opts = QFileDialog::ShowDirsOnly;
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      opts = QFileDialog::DontUseNativeDialog;
+
     QString dir
       = QFileDialog::getExistingDirectory (this, tr ("Browse directories"), nullptr,
-                                           QFileDialog::ShowDirsOnly);
+                                           QFileDialog::Option (opts));
 
     set_current_working_directory (dir);
 
@@ -1042,6 +1060,11 @@ namespace octave
     fileDialog->setFileMode (QFileDialog::ExistingFiles);
     fileDialog->setDirectory (m_current_directory_combo_box->itemText (0));
 
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      fileDialog->setOption(QFileDialog::DontUseNativeDialog);
+
     connect (fileDialog, SIGNAL (filesSelected (const QStringList&)),
              this, SLOT (request_open_files (const QStringList&)));
 
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/settings-dialog.cc
--- a/libgui/src/settings-dialog.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/settings-dialog.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -190,8 +190,14 @@ namespace octave
     sb_3d_title->setValue (settings->value ("DockWidgets/widget_title_3d", 50).toInt ());
     cb_widget_custom_style->setChecked (settings->value ("DockWidgets/widget_title_custom_style", false).toBool ());
 
+    // Native file dialogs.
+    // FIXME: This preference can be deprecated / removed if all display
+    //       managers, especially KDE, run those dialogs without hangs or
+    //       delays from the start (bug #54607).
+    cb_use_native_file_dialogs->setChecked (settings->value ("use_native_file_dialogs", true).toBool ());
+
     // Cursor blinking: consider old terminal related setting if not yet set
-    // TODO: This pref. can be deprecated / removed if Qt adds support for
+    // FIXME: This pref. can be deprecated / removed if Qt adds support for
     //       getting the cursor blink preferences from all OS environments
     if (settings->contains ("cursor_blinking"))
       {
@@ -524,8 +530,14 @@ namespace octave
 
   void settings_dialog::get_dir (QLineEdit *line_edit, const QString& title)
   {
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    int opts = QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks;
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      opts |= QFileDialog::DontUseNativeDialog;
+
     QString dir = QFileDialog::getExistingDirectory
-      (this, title, line_edit->text (), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
+      (this, title, line_edit->text (), QFileDialog::Option (opts));
 
     line_edit->setText (dir);
   }
@@ -810,6 +822,9 @@ namespace octave
       icon_size = 1;
     settings->setValue ("toolbar_icon_size", icon_size);
 
+    // native file dialogs
+    settings->setValue ("use_native_file_dialogs", cb_use_native_file_dialogs->isChecked ());
+
     // cursor blinking
     settings->setValue ("cursor_blinking", cb_cursor_blinking->isChecked ());
 
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/settings-dialog.ui
--- a/libgui/src/settings-dialog.ui	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/settings-dialog.ui	Thu Nov 15 23:24:54 2018 +0100
@@ -32,7 +32,7 @@
       </size>
      </property>
      <property name="currentIndex">
-      <number>4</number>
+      <number>0</number>
      </property>
      <widget class="QWidget" name="tab_general">
       <property name="enabled">
@@ -52,8 +52,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>571</height>
+            <width>664</width>
+            <height>570</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_17">
@@ -140,7 +140,7 @@
                   </property>
                  </widget>
                 </item>
-                <item row="7" column="0">
+                <item row="8" column="0">
                  <widget class="QCheckBox" name="cb_prompt_to_exit">
                   <property name="text">
                    <string>Confirm before exiting</string>
@@ -374,7 +374,7 @@
                   </property>
                  </widget>
                 </item>
-                <item row="6" column="0">
+                <item row="7" column="0">
                  <widget class="QCheckBox" name="cb_status_bar">
                   <property name="text">
                    <string>Show status bar</string>
@@ -384,7 +384,7 @@
                   </property>
                  </widget>
                 </item>
-                <item row="5" column="0">
+                <item row="6" column="0">
                  <widget class="QCheckBox" name="cb_cursor_blinking">
                   <property name="text">
                    <string>Cursor blinking</string>
@@ -394,6 +394,16 @@
                   </property>
                  </widget>
                 </item>
+                <item row="5" column="0">
+                 <widget class="QCheckBox" name="cb_use_native_file_dialogs">
+                  <property name="text">
+                   <string>Use native file dialogs</string>
+                  </property>
+                  <property name="checked">
+                   <bool>true</bool>
+                  </property>
+                 </widget>
+                </item>
                </layout>
               </item>
              </layout>
@@ -509,8 +519,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>571</height>
+            <width>664</width>
+            <height>570</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_7">
@@ -790,8 +800,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>645</width>
-            <height>1213</height>
+            <width>647</width>
+            <height>1360</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_16">
@@ -2116,8 +2126,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>571</height>
+            <width>664</width>
+            <height>570</height>
            </rect>
           </property>
           <layout class="QGridLayout" name="gridLayout_8">
@@ -2260,8 +2270,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>571</height>
+            <width>664</width>
+            <height>570</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_19">
@@ -2322,8 +2332,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>669</width>
-            <height>558</height>
+            <width>780</width>
+            <height>550</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_vesc_manual">
@@ -2524,8 +2534,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>571</height>
+            <width>664</width>
+            <height>570</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_25">
@@ -2723,8 +2733,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>571</height>
+            <width>664</width>
+            <height>570</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_20">
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/shortcut-manager.cc
--- a/libgui/src/shortcut-manager.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/shortcut-manager.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -673,16 +673,22 @@ namespace octave
       {
         QString file;
 
+        // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+        int opts = 0;  // No options by default.
+        if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                        true).toBool ())
+          opts = QFileDialog::DontUseNativeDialog;
+
         if (action == OSC_IMPORT)
           file = QFileDialog::getOpenFileName (this,
                                                tr ("Import shortcuts from file..."), QString (),
                                                tr ("Octave Shortcut Files (*.osc);;All Files (*)"),
-                                               nullptr);
+                                               nullptr, QFileDialog::Option (opts));
         else if (action == OSC_EXPORT)
           file = QFileDialog::getSaveFileName (this,
                                                tr ("Export shortcuts to file..."), QString (),
                                                tr ("Octave Shortcut Files (*.osc);;All Files (*)"),
-                                               nullptr);
+                                               nullptr, QFileDialog::Option (opts));
 
         if (file.isEmpty ())
           return false;
diff -r 32d4ee071348 -r c1347a3d125a libgui/src/variable-editor.cc
--- a/libgui/src/variable-editor.cc	Thu Nov 15 21:29:57 2018 +0100
+++ b/libgui/src/variable-editor.cc	Thu Nov 15 23:24:54 2018 +0100
@@ -444,13 +444,19 @@ namespace octave
     if (! hasFocus ())
       return;
 
+    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
+    int opts = 0;  // No options by default.
+    if (! resource_manager::get_settings ()->value ("use_native_file_dialogs",
+                                                    true).toBool ())
+      opts = QFileDialog::DontUseNativeDialog;
+
     QString name = objectName ();
     QString file
       = QFileDialog::getSaveFileName (this,
                                       tr ("Save Variable %1 As").arg (name),
     // FIXME: Should determine extension from save_default_options
                                       QString ("./%1.txt").arg (name),
-                                      0, 0);
+                                      0, 0, QFileDialog::Option (opts));
 
     // FIXME: Type? binary, float-binary, ascii, text, hdf5, matlab format?
     // FIXME: Call octave_value::save_* directly?
