# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1446500548 -3600
#      Mon Nov 02 22:42:28 2015 +0100
# Node ID bcf4e180a04364e0064ae1f3480e76b971f4d073
# Parent  7ae6bbf0a5670476f8795292e02055424767a5f7
Make the file browser open files more consistently (bug #)

* default-qt-settings.in: new preference "filesdockwidget/txt_file_extensions" that defaults to main octave text files.
* files-dock-widget.cc (files_dock_widget::display_directory): open files in default app except for user defined extensions
* files-dock-widget.cc (files_dock_widget::contextmenu_requested): add action for opening files in the editor
* files-dock-widget.cc (files_dock_widget::contextmenu_open): make use of display_directory for concistency
* files-dock-widget.cc (files_dock_widget::contextmenu_open_in_editor): new slot to force opening a file in the editor
* files-dock-widget.h: declare new slot "contextmenu_open_in_editor"
* settings-dialog.ui: add new lineEdit and label for file extensions setting
* settings-dialog.cc (settings_dialog::settings_dialog,::~settings_dialog): read/write new setting "filesdockwidget/txt_file_extensions"

diff -r 7ae6bbf0a567 -r bcf4e180a043 libgui/default-qt-settings.in
--- a/libgui/default-qt-settings.in	Fri Oct 30 12:53:08 2015 +0000
+++ b/libgui/default-qt-settings.in	Mon Nov 02 22:42:28 2015 +0100
@@ -60,6 +60,7 @@
 column_state=@ByteArray(\0\0\0\xff\0\0\0\0\0\0\0\x1\0\0\0\x1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x2\x12\0\0\0\x5\x1\0\0\x1\0\0\0\0\0\0\0\0\0\0\0\0\x64\xff\xff\xff\xff\0\0\0\x81\0\0\0\0\0\0\0\x2\0\0\x1\x90\0\0\0\x4\0\0\0\0\0\0\0\x82\0\0\0\x1\0\0\0\0)
 
 [filesdockwidget]
+txt_file_extensions=m;c;cc;cpp;h;txt
 sort_files_by_column=0
 sort_files_by_order=0
 column_state=@ByteArray(\0\0\0\xff\0\0\0\0\0\0\0\x1\0\0\0\0\0\0\0\0\x1\0\0\0\0\0\0\0\0\0\0\0\x4\xe\0\0\0\x3\0\0\0\x3\0\0\0\x64\0\0\0\x2\0\0\0\x64\0\0\0\x1\0\0\0\x64\0\0\x2\v\0\0\0\x4\x1\x1\0\x1\0\0\0\0\0\0\0\0\0\0\0\0\x64\xff\xff\xff\xff\0\0\0\x81\0\0\0\0\0\0\0\x2\0\0\x2\v\0\0\0\x1\0\0\0\0\0\0\0\0\0\0\0\x3\0\0\0\0)
diff -r 7ae6bbf0a567 -r bcf4e180a043 libgui/src/files-dock-widget.cc
--- a/libgui/src/files-dock-widget.cc	Fri Oct 30 12:53:08 2015 +0000
+++ b/libgui/src/files-dock-widget.cc	Mon Nov 02 22:42:28 2015 +0100
@@ -377,12 +377,22 @@
         {
           QString abs_fname = fileInfo.absoluteFilePath ();
 
+          QString suffix = fileInfo.suffix ().toLower ();
+          QSettings *settings = resource_manager::get_settings ();
+          QStringList extensions = 
+            settings->value ("filesdockwidget/txt_file_extensions")
+            .toString ().split(";", QString::SkipEmptyParts);
+          
+            
           if (QFile::exists (abs_fname))
             {
               if (is_octave_data_file (abs_fname.toStdString ()))
                 emit load_file_signal (abs_fname);
+              else if (extensions.contains (suffix))
+                emit open_file (fileInfo.absoluteFilePath ());
               else
-                emit open_file (fileInfo.absoluteFilePath ());
+                open_item_in_app (_file_tree_view->selectionModel ()
+                                  ->currentIndex ());
             }
         }
     }
@@ -479,6 +489,10 @@
 
       menu.addAction (tr ("Open in Default Application"),
                       this, SLOT (contextmenu_open_in_app (bool)));
+      if (info.isFile ())
+        menu.addAction (tr ("Open in Text Editor"), 
+                        this, SLOT (contextmenu_open_in_editor (bool)));
+
 
       menu.addAction (tr ("Copy Selection to Clipboard"),
                       this, SLOT (contextmenu_copy_selection (bool)));
@@ -535,12 +549,23 @@
     {
       QFileInfo file = _file_system_model->fileInfo (*it);
       if (file.exists ())
-        {
-          if (file.isFile ())
-            emit open_file (file.absoluteFilePath ());
-          else
-            set_current_directory (file.absoluteFilePath ());
-        }
+        display_directory (file.absoluteFilePath ());
+    }
+}
+
+
+void
+files_dock_widget::contextmenu_open_in_editor (bool)
+{
+
+  QItemSelectionModel *m = _file_tree_view->selectionModel ();
+  QModelIndexList rows = m->selectedRows ();
+
+  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
+    {
+      QFileInfo file = _file_system_model->fileInfo (*it);
+      if (file.exists ())
+        emit open_file (file.absoluteFilePath ());
     }
 }
 
diff -r 7ae6bbf0a567 -r bcf4e180a043 libgui/src/files-dock-widget.h
--- a/libgui/src/files-dock-widget.h	Fri Oct 30 12:53:08 2015 +0000
+++ b/libgui/src/files-dock-widget.h	Mon Nov 02 22:42:28 2015 +0100
@@ -89,6 +89,7 @@
   void contextmenu_requested (const QPoint& pos);
   /* context menu actions */
   void contextmenu_open (bool);
+  void contextmenu_open_in_editor (bool);
   void contextmenu_open_in_app (bool);
   void contextmenu_copy_selection (bool);
   void contextmenu_run (bool);
diff -r 7ae6bbf0a567 -r bcf4e180a043 libgui/src/settings-dialog.cc
--- a/libgui/src/settings-dialog.cc	Fri Oct 30 12:53:08 2015 +0000
+++ b/libgui/src/settings-dialog.cc	Mon Nov 02 22:42:28 2015 +0100
@@ -506,6 +506,8 @@
     settings->value ("filesdockwidget/startup_dir").toString ());
   connect (ui->pb_file_browser_dir, SIGNAL (pressed ()),
            this, SLOT (get_file_browser_dir ()));
+  ui->le_file_browser_extensions->setText (
+    settings->value ("filesdockwidget/txt_file_extensions").toString ());
 
   ui->checkbox_allow_web_connect->setChecked (
     settings->value ("news/allow_web_connection",false).toBool ());
@@ -837,7 +839,8 @@
                       ui->cb_restore_file_browser_dir->isChecked ());
   settings->setValue ("filesdockwidget/startup_dir",
                       ui->le_file_browser_dir->text ());
-
+  settings->setValue ("filesdockwidget/txt_file_extensions",
+                      ui->le_file_browser_extensions->text ());
 
   settings->setValue ("news/allow_web_connection",
                       ui->checkbox_allow_web_connect->isChecked ());
diff -r 7ae6bbf0a567 -r bcf4e180a043 libgui/src/settings-dialog.ui
--- a/libgui/src/settings-dialog.ui	Fri Oct 30 12:53:08 2015 +0000
+++ b/libgui/src/settings-dialog.ui	Mon Nov 02 22:42:28 2015 +0100
@@ -9,8 +9,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>720</width>
-    <height>480</height>
+    <width>678</width>
+    <height>474</height>
    </rect>
   </property>
   <property name="minimumSize">
@@ -52,8 +52,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>570</width>
-            <height>382</height>
+            <width>657</width>
+            <height>400</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_17">
@@ -499,8 +499,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>662</width>
-            <height>751</height>
+            <width>690</width>
+            <height>783</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_16">
@@ -1461,8 +1461,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>488</width>
-            <height>236</height>
+            <width>636</width>
+            <height>370</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_7">
@@ -1749,28 +1749,74 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>678</width>
-            <height>378</height>
+            <width>636</width>
+            <height>370</height>
            </rect>
           </property>
-          <layout class="QVBoxLayout" name="verticalLayout_18">
-           <item>
+          <layout class="QGridLayout" name="gridLayout_8">
+           <item row="0" column="0">
             <widget class="QGroupBox" name="groupBox_4">
              <property name="title">
               <string>Behavior</string>
              </property>
-             <layout class="QVBoxLayout" name="verticalLayout_24">
-              <item>
-               <layout class="QGridLayout" name="gridLayout_8">
-                <item row="4" column="0">
+             <layout class="QGridLayout" name="gridLayout_2">
+              <item row="0" column="0">
+               <layout class="QVBoxLayout" name="verticalLayout_18">
+                <item>
                  <widget class="QCheckBox" name="sync_octave_directory">
                   <property name="text">
                    <string>Synchronize Octave working directory with file browser</string>
                   </property>
                  </widget>
                 </item>
-                <item row="5" column="0">
+                <item>
                  <layout class="QGridLayout" name="lo_file_browser_startup">
+                  <item row="1" column="3">
+                   <spacer name="horizontalSpacer_30">
+                    <property name="orientation">
+                     <enum>Qt::Horizontal</enum>
+                    </property>
+                    <property name="sizeHint" stdset="0">
+                     <size>
+                      <width>40</width>
+                      <height>20</height>
+                     </size>
+                    </property>
+                   </spacer>
+                  </item>
+                  <item row="0" column="1">
+                   <widget class="QCheckBox" name="cb_restore_file_browser_dir">
+                    <property name="text">
+                     <string>Restore last directory of previous session</string>
+                    </property>
+                   </widget>
+                  </item>
+                  <item row="0" column="3">
+                   <spacer name="horizontalSpacer_29">
+                    <property name="orientation">
+                     <enum>Qt::Horizontal</enum>
+                    </property>
+                    <property name="sizeHint" stdset="0">
+                     <size>
+                      <width>40</width>
+                      <height>20</height>
+                     </size>
+                    </property>
+                   </spacer>
+                  </item>
+                  <item row="1" column="2">
+                   <widget class="QPushButton" name="pb_file_browser_dir">
+                    <property name="sizePolicy">
+                     <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+                      <horstretch>0</horstretch>
+                      <verstretch>0</verstretch>
+                     </sizepolicy>
+                    </property>
+                    <property name="text">
+                     <string>Browse</string>
+                    </property>
+                   </widget>
+                  </item>
                   <item row="0" column="0">
                    <widget class="QLabel" name="lbl_file_browser_dir">
                     <property name="text">
@@ -1788,52 +1834,24 @@
                     </property>
                    </widget>
                   </item>
-                  <item row="0" column="1">
-                   <widget class="QCheckBox" name="cb_restore_file_browser_dir">
+                 </layout>
+                </item>
+                <item>
+                 <layout class="QVBoxLayout" name="verticalLayout_9">
+                  <item>
+                   <widget class="QLabel" name="lbl_file_browser_extensions">
                     <property name="text">
-                     <string>Restore last directory of previous session</string>
+                     <string>Extensions of files to be opened in the default text editor (separated by &quot;;&quot;):</string>
                     </property>
                    </widget>
                   </item>
-                  <item row="1" column="2">
-                   <widget class="QPushButton" name="pb_file_browser_dir">
-                    <property name="sizePolicy">
-                     <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
-                      <horstretch>0</horstretch>
-                      <verstretch>0</verstretch>
-                     </sizepolicy>
-                    </property>
+                  <item>
+                   <widget class="QLineEdit" name="le_file_browser_extensions">
                     <property name="text">
-                     <string>Browse</string>
+                     <string/>
                     </property>
                    </widget>
                   </item>
-                  <item row="0" column="3">
-                   <spacer name="horizontalSpacer_29">
-                    <property name="orientation">
-                     <enum>Qt::Horizontal</enum>
-                    </property>
-                    <property name="sizeHint" stdset="0">
-                     <size>
-                      <width>40</width>
-                      <height>20</height>
-                     </size>
-                    </property>
-                   </spacer>
-                  </item>
-                  <item row="1" column="3">
-                   <spacer name="horizontalSpacer_30">
-                    <property name="orientation">
-                     <enum>Qt::Horizontal</enum>
-                    </property>
-                    <property name="sizeHint" stdset="0">
-                     <size>
-                      <width>40</width>
-                      <height>20</height>
-                     </size>
-                    </property>
-                   </spacer>
-                  </item>
                  </layout>
                 </item>
                </layout>
@@ -1841,7 +1859,7 @@
              </layout>
             </widget>
            </item>
-           <item>
+           <item row="1" column="0">
             <spacer name="verticalSpacer_2">
              <property name="orientation">
               <enum>Qt::Vertical</enum>
@@ -1875,8 +1893,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>203</width>
-            <height>79</height>
+            <width>636</width>
+            <height>370</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_19">
@@ -1944,8 +1962,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>368</width>
-            <height>244</height>
+            <width>636</width>
+            <height>370</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_25">
@@ -2207,8 +2225,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>529</width>
-            <height>204</height>
+            <width>640</width>
+            <height>356</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_20">
