changeset 27269:6a6b5ae3d58d

add file browser context menu for adding dirs to the path (bug #55623) * files-dock-widget.cc (contextmenu_requested): add menu with submenus for adding dirs and dirs with subdirs; (get_selected_items_info): new provate mehtod for generating a file info list with all selected directories or files; (contextmenu_setcurrentdir): use the new method get_selected_items_info; (contextmenu_add_to_path): new method for calling Faddpath with the currently selected directories and subdirs if desired; (contextmenu_add_to_path_subdirs): new method calling contextmenu_add_to_path with falgs for subdirs set * files-dock-widget.h: new methods contextmenu_add_to_path, contextmenu_add_to_path_subdirs and get_selected_items_info
author Torsten Lilge <ttl-octave@mailbox.org>
date Sat, 20 Jul 2019 17:08:11 +0200
parents ab32fd297202
children 47f42ad90b0b
files libgui/src/files-dock-widget.cc libgui/src/files-dock-widget.h
diffstat 2 files changed, 89 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/files-dock-widget.cc	Sat Jul 20 10:03:31 2019 +0200
+++ b/libgui/src/files-dock-widget.cc	Sat Jul 20 17:08:11 2019 +0200
@@ -1,5 +1,4 @@
 /*
-
 Copyright (C) 2013-2019 John P. Swensen
 Copyright (C) 2011-2019 Jacob Dawid
 
@@ -48,6 +47,10 @@
 
 #include "load-save.h"
 #include "oct-env.h"
+#include "interpreter-private.h"
+#include "interpreter.h"
+#include "qt-interpreter-events.h"
+#include "builtin-defun-decls.h"
 
 namespace octave
 {
@@ -533,7 +536,16 @@
             menu.addAction (resource_manager::icon ("go-first"),
                             tr ("Set Current Directory"),
                             this, SLOT (contextmenu_setcurrentdir (bool)));
+
+            QMenu *add_path_menu = menu.addMenu (tr ("Add to Path"));
+
+            add_path_menu->addAction (tr ("Selected Directories"),
+                            this, SLOT (contextmenu_add_to_path (bool)));
+            add_path_menu->addAction (tr ("Selected Directories and Subdirectories"),
+                            this, SLOT (contextmenu_add_to_path_subdirs (bool)));
+
             menu.addSeparator ();
+
             menu.addAction (resource_manager::icon ("edit-find"),
                             tr ("Find Files..."), this,
                             SLOT (contextmenu_findfiles (bool)));
@@ -729,6 +741,28 @@
       }
   }
 
+  // Get the currently selectd files/dirs and return their file infos in a list
+  QList<QFileInfo> files_dock_widget::get_selected_items_info (bool dir)
+  {
+    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
+    QModelIndexList rows = m->selectedRows ();
+
+    QList<QFileInfo> infos;
+
+    for (auto it = rows.begin (); it != rows.end (); it++)
+      {
+        QModelIndex index = *it;
+
+        QFileInfo info = m_file_system_model->fileInfo (index);
+
+        if (info.exists () &&
+              ((dir & info.isDir ()) || (! dir && info.isFile ())))
+          infos.append (info);
+      }
+
+    return infos;
+  }
+
   void files_dock_widget::contextmenu_newfile (bool)
   {
     QItemSelectionModel *m = m_file_tree_view->selectionModel ();
@@ -763,22 +797,55 @@
 
   void files_dock_widget::contextmenu_setcurrentdir (bool)
   {
-    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
-    QModelIndexList rows = m->selectedRows ();
+    QList<QFileInfo> infos = get_selected_items_info (true);
+
+    if (infos.length () > 0 && infos.first ().isDir ())
+      process_set_current_dir (infos.first ().absoluteFilePath ());
+  }
+
+  void files_dock_widget::contextmenu_add_to_path (bool, bool subdirs)
+  {
+    QList<QFileInfo> infos = get_selected_items_info (true);
 
-    if (rows.size () > 0)
+    octave_value_list dir_list = ovl ();
+
+    for (int i = 0; i < infos.length (); i++)
+      dir_list.append (infos.at (i).absoluteFilePath ().toStdString ());
+
+    if (infos.length () > 0)
       {
-        QModelIndex index = rows[0];
+        event_manager& evmgr
+          = __get_event_manager__ ("files_dock_widget::contextmenu_add_to_path");
 
-        QFileInfo info = m_file_system_model->fileInfo (index);
-
-        if (info.isDir ())
+        evmgr.post_event
+          ([dir_list, subdirs] (void)
           {
-            process_set_current_dir (info.absoluteFilePath ());
-          }
+            // INTERPRETER THREAD
+
+            interpreter& interp
+              = __get_interpreter__ ("files_dock_widget::contextmenu_add_to_path");
+
+            octave_value_list paths = ovl ();
+
+            if (subdirs)
+              {
+                // Loop over all directories in order to get all subdirs
+                for (octave_idx_type i = 0; i < dir_list.length (); i++)
+                  paths.append (Fgenpath (dir_list(i)));
+              }
+            else
+              paths = dir_list;
+
+            Faddpath (interp, paths);
+          });
       }
   }
 
+  void files_dock_widget::contextmenu_add_to_path_subdirs (bool)
+  {
+    contextmenu_add_to_path (true, true);
+  }
+
   void files_dock_widget::contextmenu_findfiles (bool)
   {
     QItemSelectionModel *m = m_file_tree_view->selectionModel ();
--- a/libgui/src/files-dock-widget.h	Sat Jul 20 10:03:31 2019 +0200
+++ b/libgui/src/files-dock-widget.h	Sat Jul 20 17:08:11 2019 +0200
@@ -27,6 +27,7 @@
 #include <QDate>
 #include <QObject>
 #include <QWidget>
+#include <QList>
 #include <QListWidget>
 #include <QFileSystemModel>
 #include <QToolBar>
@@ -112,6 +113,8 @@
     void contextmenu_newfile (bool);
     void contextmenu_newdir (bool);
     void contextmenu_setcurrentdir (bool);
+    void contextmenu_add_to_path (bool, bool subdirs=false);
+    void contextmenu_add_to_path_subdirs (bool);
     void contextmenu_findfiles (bool);
     //!@}
 
@@ -167,8 +170,17 @@
 
   private:
 
+    //! Get currently selected QFileInfo object.
+
+    QList<QFileInfo> get_selected_items_info (bool);
+
+    //! Process new file/directory actions
+
     void process_new_file (const QString& parent_name);
     void process_new_dir (const QString& parent_name);
+
+    //! Process setting current dir or find in files
+
     void process_set_current_dir (const QString& parent_name);
     void process_find_files (const QString& dir_name);