GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
files-dock-widget.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2018 John P. Swensen
4 Copyright (C) 2011-2018 Jacob Dawid
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "resource-manager.h"
29 #include "files-dock-widget.h"
30 
31 #include <QApplication>
32 #include <QClipboard>
33 #include <QFileInfo>
34 #include <QCompleter>
35 #include <QProcess>
36 #include <QDebug>
37 #include <QHeaderView>
38 #include <QLineEdit>
39 #include <QSizePolicy>
40 #include <QMenu>
41 #include <QInputDialog>
42 #include <QMessageBox>
43 #include <QToolButton>
44 #include <QUrl>
45 #include <QDesktopServices>
46 #include <QFileDialog>
47 
48 #include "load-save.h"
49 #include "oct-env.h"
50 
51 namespace octave
52 {
53  class FileTreeViewer : public QTreeView
54  {
55  public:
56 
58 
59  ~FileTreeViewer (void) = default;
60 
61  void mousePressEvent (QMouseEvent *e)
62  {
63  if (e->button () != Qt::RightButton)
64  QTreeView::mousePressEvent (e);
65  }
66  };
67 
70  {
71  setObjectName ("FilesDockWidget");
72  setWindowIcon (QIcon (":/actions/icons/logo.png"));
73  set_title (tr ("File Browser"));
74  setToolTip (tr ("Browse your files"));
75 
76  m_sig_mapper = nullptr;
77 
78  m_columns_shown = QStringList ();
79  m_columns_shown.append (tr ("File size"));
80  m_columns_shown.append (tr ("File type"));
81  m_columns_shown.append (tr ("Date modified"));
82  m_columns_shown.append (tr ("Show hidden"));
83  m_columns_shown.append (tr ("Alternating row colors"));
84 
85  m_columns_shown_keys = QStringList ();
86  m_columns_shown_keys.append ("filesdockwidget/showFileSize");
87  m_columns_shown_keys.append ("filesdockwidget/showFileType");
88  m_columns_shown_keys.append ("filesdockwidget/showLastModified");
89  m_columns_shown_keys.append ("filesdockwidget/showHiddenFiles");
90  m_columns_shown_keys.append ("filesdockwidget/useAlternatingRowColors");
91 
92  QWidget *container = new QWidget (this);
93 
94  setWidget (container);
95 
96  connect (this, SIGNAL (open_file (const QString&)),
97  main_win (), SLOT (open_file (const QString&)));
98 
99  connect (this, SIGNAL (displayed_directory_changed (const QString&)),
100  main_win (),
101  SLOT (set_current_working_directory (const QString&)));
102 
103  // Create a toolbar
104  m_navigation_tool_bar = new QToolBar ("", container);
105  m_navigation_tool_bar->setAllowedAreas (Qt::TopToolBarArea);
106  m_navigation_tool_bar->setMovable (false);
107 
108  m_current_directory = new QComboBox (m_navigation_tool_bar);
109  m_current_directory->setToolTip (tr ("Enter the path or filename"));
110  m_current_directory->setEditable (true);
111  m_current_directory->setMaxCount (MaxMRUDirs);
112  m_current_directory->setInsertPolicy (QComboBox::NoInsert);
113  m_current_directory->setSizeAdjustPolicy (
114  QComboBox::AdjustToMinimumContentsLengthWithIcon);
115  QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
116  m_current_directory->setSizePolicy (sizePol);
117 
118  QAction *directory_up_action = new QAction (resource_manager::icon ("go-up"),
120  directory_up_action->setToolTip (tr ("One directory up"));
121 
123  = new QAction (resource_manager::icon ("go-first"),
124  tr ("Show Octave directory"), m_navigation_tool_bar);
125  m_sync_browser_directory_action->setToolTip (
126  tr ("Go to current Octave directory"));
127  m_sync_browser_directory_action->setEnabled ("false");
128 
130  = new QAction (resource_manager::icon ("go-last"),
131  tr ("Set Octave directory"), m_navigation_tool_bar);
132  m_sync_octave_directory_action->setToolTip (
133  tr ("Set Octave directory to current browser directory"));
134  m_sync_octave_directory_action->setEnabled ("false");
135 
136  QToolButton *popdown_button = new QToolButton ();
137  popdown_button->setToolTip (tr ("Actions on current directory"));
138  QMenu *popdown_menu = new QMenu ();
139  popdown_menu->addAction (resource_manager::icon ("user-home"),
140  tr ("Show Home Directory"),
141  this, SLOT (popdownmenu_home (bool)));
142  popdown_menu->addAction (m_sync_browser_directory_action);
143  popdown_menu->addAction (m_sync_octave_directory_action);
144  popdown_button->setMenu (popdown_menu);
145  popdown_button->setPopupMode (QToolButton::InstantPopup);
146  popdown_button->setDefaultAction (new QAction (
147  resource_manager::icon ("applications-system"), "",
149 
150  popdown_menu->addSeparator ();
151  popdown_menu->addAction (resource_manager::icon ("folder"),
152  tr ("Set Browser Directory..."),
153  this, SLOT (popdownmenu_search_dir (bool)));
154  popdown_menu->addSeparator ();
155  popdown_menu->addAction (resource_manager::icon ("edit-find"),
156  tr ("Find Files..."),
157  this, SLOT (popdownmenu_findfiles (bool)));
158  popdown_menu->addSeparator ();
159  popdown_menu->addAction (resource_manager::icon ("document-new"),
160  tr ("New File..."),
161  this, SLOT (popdownmenu_newfile (bool)));
162  popdown_menu->addAction (resource_manager::icon ("folder-new"),
163  tr ("New Directory..."),
164  this, SLOT (popdownmenu_newdir (bool)));
165 
167  m_navigation_tool_bar->addAction (directory_up_action);
168  m_navigation_tool_bar->addWidget (popdown_button);
169 
170  connect (directory_up_action, SIGNAL (triggered ()), this,
171  SLOT (change_directory_up ()));
172  connect (m_sync_octave_directory_action, SIGNAL (triggered ()), this,
173  SLOT (do_sync_octave_directory ()));
174  connect (m_sync_browser_directory_action, SIGNAL (triggered ()), this,
175  SLOT (do_sync_browser_directory ()));
176 
177  QSettings *settings = resource_manager::get_settings ();
178  // FIXME: what should happen if settings is 0?
179 
180  // Create the QFileSystemModel starting in the desired directory
181  QDir startup_dir; // take current dir
182 
183  if (settings->value ("filesdockwidget/restore_last_dir",false).toBool ())
184  {
185  // restore last dir from previous session
186  QStringList last_dirs
187  = settings->value ("filesdockwidget/mru_dir_list").toStringList ();
188  if (last_dirs.length () > 0)
189  startup_dir = QDir (last_dirs.at (0)); // last dir in previous session
190  }
191  else if (! settings->value ("filesdockwidget/startup_dir").toString ().isEmpty ())
192  {
193  // do not restore but there is a startup dir configured
194  startup_dir
195  = QDir (settings->value ("filesdockwidget/startup_dir").toString ());
196  }
197 
198  if (! startup_dir.exists ())
199  {
200  // the configured startup dir does not exist, take actual one
201  startup_dir = QDir ();
202  }
203 
204  m_file_system_model = new QFileSystemModel (this);
205  QModelIndex rootPathIndex = m_file_system_model->setRootPath (
206  startup_dir.absolutePath ());
207 
208  // Attach the model to the QTreeView and set the root index
209  m_file_tree_view = new FileTreeViewer (container);
210  m_file_tree_view->setSelectionMode (QAbstractItemView::ExtendedSelection);
212  m_file_tree_view->setRootIndex (rootPathIndex);
213  m_file_tree_view->setSortingEnabled (true);
214  m_file_tree_view->setAlternatingRowColors (true);
215  m_file_tree_view->setAnimated (true);
216  m_file_tree_view->setToolTip (
217  tr ("Activate to open in editor, right click for alternatives"));
218 
219  // get sort column and order as well as cloumn state (order and width)
220 
221  m_file_tree_view->sortByColumn (
222  settings->value ("filesdockwidget/sort_files_by_column",0).toInt (),
223  static_cast<Qt::SortOrder>
224  (settings->value ("filesdockwidget/sort_files_by_order",
225  Qt::AscendingOrder).toUInt ())
226  );
227  m_file_tree_view->header ()->restoreState (
228  settings->value ("filesdockwidget/column_state").toByteArray ());
229 
230  QStringList mru_dirs =
231  settings->value ("filesdockwidget/mru_dir_list").toStringList ();
232  m_current_directory->addItems (mru_dirs);
233 
234  m_current_directory->setEditText (
235  m_file_system_model->fileInfo (rootPathIndex). absoluteFilePath ());
236 
237  connect (m_file_tree_view, SIGNAL (activated (const QModelIndex &)),
238  this, SLOT (item_double_clicked (const QModelIndex &)));
239 
240  // add context menu to tree_view
241  m_file_tree_view->setContextMenuPolicy (Qt::CustomContextMenu);
242  connect (m_file_tree_view,
243  SIGNAL (customContextMenuRequested (const QPoint &)),
244  this, SLOT (contextmenu_requested (const QPoint &)));
245 
246  m_file_tree_view->header ()->setContextMenuPolicy (Qt::CustomContextMenu);
247  connect (m_file_tree_view->header (),
248  SIGNAL (customContextMenuRequested (const QPoint &)),
249  this, SLOT (headercontextmenu_requested (const QPoint &)));
250 
251  // Layout the widgets vertically with the toolbar on top
252  QVBoxLayout *vbox_layout = new QVBoxLayout ();
253  vbox_layout->setSpacing (0);
254  vbox_layout->addWidget (m_navigation_tool_bar);
255  vbox_layout->addWidget (m_file_tree_view);
256  vbox_layout->setMargin (1);
257 
258  container->setLayout (vbox_layout);
259 
260  // FIXME: Add right-click contextual menus for copying, pasting,
261  // deleting files (and others).
262 
263  connect (m_current_directory->lineEdit (), SIGNAL (returnPressed ()),
264  this, SLOT (accept_directory_line_edit ()));
265 
266  connect (m_current_directory, SIGNAL (activated (const QString &)),
267  this, SLOT (set_current_directory (const QString &)));
268 
269  connect (this, SIGNAL (run_file_signal (const QFileInfo&)),
270  main_win (), SLOT (run_file_in_terminal (const QFileInfo&)));
271 
272  QCompleter *completer = new QCompleter (m_file_system_model, this);
273  m_current_directory->setCompleter (completer);
274 
275  setFocusProxy (m_current_directory);
276 
277  m_sync_octave_dir = true; // default, overwirtten with notice_settings ()
278  m_octave_dir = "";
279  }
280 
282  {
283  QSettings *settings = resource_manager::get_settings ();
284 
285  if (! settings)
286  return;
287 
288  int sort_column = m_file_tree_view->header ()->sortIndicatorSection ();
289  Qt::SortOrder sort_order = m_file_tree_view->header ()->sortIndicatorOrder ();
290  settings->setValue ("filesdockwidget/sort_files_by_column", sort_column);
291  settings->setValue ("filesdockwidget/sort_files_by_order", sort_order);
292  settings->setValue ("filesdockwidget/column_state",
293  m_file_tree_view->header ()->saveState ());
294 
295  QStringList dirs;
296  for (int i=0; i< m_current_directory->count (); i++)
297  {
298  dirs.append (m_current_directory->itemText (i));
299  }
300  settings->setValue ("filesdockwidget/mru_dir_list", dirs);
301 
302  settings->sync ();
303 
305 
306  if (m_sig_mapper)
307  delete m_sig_mapper;
308  }
309 
310  void files_dock_widget::item_double_clicked (const QModelIndex& index)
311  {
312  // Retrieve the file info associated with the model index.
313  QFileInfo fileInfo = m_file_system_model->fileInfo (index);
314  set_current_directory (fileInfo.absoluteFilePath ());
315  }
316 
318  {
319  display_directory (dir);
320  }
321 
323  {
324  display_directory (m_current_directory->currentText ());
325  }
326 
328  {
329  QDir dir
330  = QDir (m_file_system_model->filePath (m_file_tree_view->rootIndex ()));
331 
332  dir.cdUp ();
333  display_directory (dir.absolutePath ());
334  }
335 
337  {
338  QDir dir
339  = QDir (m_file_system_model->filePath (m_file_tree_view->rootIndex ()));
340 
341  emit displayed_directory_changed (dir.absolutePath ());
342  }
343 
345  {
346  display_directory (m_octave_dir,false); // false: no sync of octave dir
347  }
348 
350  {
351  m_octave_dir = dir;
352  if (m_sync_octave_dir)
353  display_directory (m_octave_dir,false); // false: no sync of octave dir
354  }
355 
356  void files_dock_widget::display_directory (const QString& dir, bool set_octave_dir)
357  {
358  QFileInfo fileInfo (dir);
359  if (fileInfo.exists ())
360  {
361  if (fileInfo.isDir ())
362  {
363  m_file_tree_view->setRootIndex (m_file_system_model->
364  index (fileInfo.absoluteFilePath ()));
365  m_file_system_model->setRootPath (fileInfo.absoluteFilePath ());
366  if (m_sync_octave_dir && set_octave_dir)
367  process_set_current_dir (fileInfo.absoluteFilePath ());
368 
369  // see if its in the list, and if it is,
370  // remove it and then, put at top of the list
371  int index
372  = m_current_directory->findText (fileInfo.absoluteFilePath ());
373  if (index != -1)
374  {
375  m_current_directory->removeItem (index);
376  }
377  m_current_directory->insertItem (0, fileInfo.absoluteFilePath ());
378  m_current_directory->setCurrentIndex (0);
379  }
380  else
381  {
382  QString abs_fname = fileInfo.absoluteFilePath ();
383 
384  QString suffix = fileInfo.suffix ().toLower ();
385  QSettings *settings = resource_manager::get_settings ();
386  QString ext = settings->value ("filesdockwidget/txt_file_extensions",
387  "m;c;cc;cpp;h;txt").toString ();
388  QStringList extensions = ext.split (";", QString::SkipEmptyParts);
389 
390  if (QFile::exists (abs_fname))
391  {
392  if (is_octave_data_file (abs_fname.toStdString ()))
393  emit load_file_signal (abs_fname);
394  else if (extensions.contains (suffix))
395  emit open_file (fileInfo.absoluteFilePath ());
396  else
397  open_item_in_app (m_file_tree_view->selectionModel ()
398  ->currentIndex ());
399  }
400  }
401  }
402  }
403 
404  void files_dock_widget::open_item_in_app (const QModelIndex& index)
405  {
406  // Retrieve the file info associated with the model index.
407  QFileInfo fileInfo = m_file_system_model->fileInfo (index);
408 
409  QString file = fileInfo.absoluteFilePath ();
410 
411  QDesktopServices::openUrl (QUrl::fromLocalFile (file));
412  }
413 
415  {
416  QSettings *settings = resource_manager::get_settings ();
417 
418  QString key = m_columns_shown_keys.at (col);
419  bool shown = settings->value (key,false).toBool ();
420  settings->setValue (key, ! shown);
421  settings->sync ();
422 
423  switch (col)
424  {
425  case 0:
426  case 1:
427  case 2:
428  // toggle column visibility
429  m_file_tree_view->setColumnHidden (col + 1, shown);
430  break;
431  case 3:
432  case 4:
433  // other actions depending on new settings
434  notice_settings (settings);
435  break;
436  }
437  }
438 
440  {
441  QMenu menu (this);
442 
443  if (m_sig_mapper)
444  delete m_sig_mapper;
445  m_sig_mapper = new QSignalMapper (this);
446 
447  QSettings *settings = resource_manager::get_settings ();
448 
449  for (int i = 0; i < m_columns_shown.size (); i++)
450  {
451  QAction *action = menu.addAction (m_columns_shown.at (i),
452  m_sig_mapper, SLOT (map ()));
453  m_sig_mapper->setMapping (action, i);
454  action->setCheckable (true);
455  action->setChecked (
456  settings->value (m_columns_shown_keys.at (i),true).toBool ());
457  }
458 
459  connect (m_sig_mapper, SIGNAL (mapped (int)),
460  this, SLOT (toggle_header (int)));
461 
462  menu.exec (m_file_tree_view->mapToGlobal (mpos));
463  }
464 
466  {
467 
468  QMenu menu (this);
469 
470  QModelIndex index = m_file_tree_view->indexAt (mpos);
471 
472  if (index.isValid ())
473  {
474  QFileInfo info = m_file_system_model->fileInfo (index);
475 
476  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
477  QModelIndexList sel = m->selectedRows ();
478 
479  // check if item at mouse position is seleccted
480  if (! sel.contains (index))
481  {
482  // is not selected -> clear actual selection and select this item
483  m->setCurrentIndex (index,
484  QItemSelectionModel::Clear
485  | QItemSelectionModel::Select
486  | QItemSelectionModel::Rows);
487  }
488 
489  // construct the context menu depending on item
490  menu.addAction (resource_manager::icon ("document-open"), tr ("Open"),
491  this, SLOT (contextmenu_open (bool)));
492 
493  if (info.isDir ())
494  {
495  menu.addAction (tr ("Open in System File Explorer"),
496  this, SLOT (contextmenu_open_in_app (bool)));
497  }
498 
499  if (info.isFile ())
500  menu.addAction (tr ("Open in Text Editor"),
501  this, SLOT (contextmenu_open_in_editor (bool)));
502 
503  menu.addAction (tr ("Copy Selection to Clipboard"),
504  this, SLOT (contextmenu_copy_selection (bool)));
505 
506  if (info.isFile () && info.suffix () == "m")
507  menu.addAction (resource_manager::icon ("media-playback-start"),
508  tr ("Run"), this, SLOT (contextmenu_run (bool)));
509 
510  if (info.isFile ())
511  menu.addAction (tr ("Load Data"), this, SLOT (contextmenu_load (bool)));
512 
513  if (info.isDir ())
514  {
515  menu.addSeparator ();
516  menu.addAction (resource_manager::icon ("go-first"),
517  tr ("Set Current Directory"),
518  this, SLOT (contextmenu_setcurrentdir (bool)));
519  menu.addSeparator ();
520  menu.addAction (resource_manager::icon ("edit-find"),
521  tr ("Find Files..."), this,
522  SLOT (contextmenu_findfiles (bool)));
523  }
524 
525  menu.addSeparator ();
526  menu.addAction (tr ("Rename..."), this, SLOT (contextmenu_rename (bool)));
527  menu.addAction (resource_manager::icon ("edit-delete"),
528  tr ("Delete..."), this, SLOT (contextmenu_delete (bool)));
529 
530  if (info.isDir ())
531  {
532  menu.addSeparator ();
533  menu.addAction (resource_manager::icon ("document-new"),
534  tr ("New File..."),
535  this, SLOT (contextmenu_newfile (bool)));
536  menu.addAction (resource_manager::icon ("folder-new"),
537  tr ("New Directory..."),
538  this, SLOT (contextmenu_newdir (bool)));
539  }
540 
541  // show the menu
542  menu.exec (m_file_tree_view->mapToGlobal (mpos));
543 
544  }
545  }
546 
548  {
549 
550  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
551  QModelIndexList rows = m->selectedRows ();
552 
553  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
554  {
555  QFileInfo file = m_file_system_model->fileInfo (*it);
556  if (file.exists ())
557  display_directory (file.absoluteFilePath ());
558  }
559  }
560 
562  {
563 
564  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
565  QModelIndexList rows = m->selectedRows ();
566 
567  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
568  {
569  QFileInfo file = m_file_system_model->fileInfo (*it);
570  if (file.exists ())
571  emit open_file (file.absoluteFilePath ());
572  }
573  }
574 
576  {
577  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
578  QModelIndexList rows = m->selectedRows ();
579 
580  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
581  open_item_in_app (*it);
582  }
583 
585  {
586  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
587  QModelIndexList rows = m->selectedRows ();
588 
589  QStringList selection;
590 
591  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
592  {
593  QFileInfo info = m_file_system_model->fileInfo (*it);
594 
595  selection << info.fileName ();
596  }
597 
598  QClipboard *clipboard = QApplication::clipboard ();
599 
600  clipboard->setText (selection.join ("\n"));
601  }
602 
604  {
605  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
606  QModelIndexList rows = m->selectedRows ();
607 
608  if (rows.size () > 0)
609  {
610  QModelIndex index = rows[0];
611 
612  QFileInfo info = m_file_system_model->fileInfo (index);
613 
614  emit load_file_signal (info.fileName ());
615  }
616  }
617 
619  {
620  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
621  QModelIndexList rows = m->selectedRows ();
622 
623  if (rows.size () > 0)
624  {
625  QModelIndex index = rows[0];
626 
627  QFileInfo info = m_file_system_model->fileInfo (index);
628  emit run_file_signal (info);
629  }
630  }
631 
633  {
634  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
635  QModelIndexList rows = m->selectedRows ();
636  if (rows.size () > 0)
637  {
638  QModelIndex index = rows[0];
639 
640  QFileInfo info = m_file_system_model->fileInfo (index);
641  QDir path = info.absoluteDir ();
642  QString old_name = info.fileName ();
643  bool ok;
644 
645  QString new_name
646  = QInputDialog::getText (this, tr ("Rename file/directory"),
647  tr ("Rename file/directory:\n")
648  + old_name + tr ("\n to: "),
649  QLineEdit::Normal, old_name, &ok);
650  if (ok && new_name.length () > 0)
651  {
652  new_name = path.absolutePath () + '/' + new_name;
653  old_name = path.absolutePath () + '/' + old_name;
654  // editor: close old
655  emit file_remove_signal (old_name, new_name);
656  // Do the renaming
657  bool st = path.rename (old_name, new_name);
658  // editor: load new/old file depending on success
659  emit file_renamed_signal (st);
660  // Clear cache of file browser
661  m_file_system_model->revert ();
662  }
663  }
664 
665  }
666 
668  {
669  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
670  QModelIndexList rows = m->selectedRows ();
671 
672  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
673  {
674  QModelIndex index = *it;
675 
676  QFileInfo info = m_file_system_model->fileInfo (index);
677 
678  if (QMessageBox::question (this, tr ("Delete file/directory"),
679  tr ("Are you sure you want to delete\n")
680  + info.filePath (),
681  QMessageBox::Yes | QMessageBox::No)
682  == QMessageBox::Yes)
683  {
684  if (info.isDir ())
685  {
686  // see if direcory is empty
687  QDir path (info.absoluteFilePath ());
688  QList<QFileInfo> fileLst = path.entryInfoList (QDir::AllEntries |
689  QDir::NoDotAndDotDot);
690 
691  if (fileLst.count () != 0)
692  QMessageBox::warning (this, tr ("Delete file/directory"),
693  tr ("Can not delete a directory that is not empty"));
694  else
695  m_file_system_model->rmdir (index);
696  }
697  else
698  {
699  // Close the file in the editor if open
700  emit file_remove_signal (info.filePath (), QString ());
701  // Remove the file.
702  bool st = m_file_system_model->remove (index);
703  // reload the old file if removing was not successful
704  if (! st)
705  emit file_renamed_signal (false);
706  }
707 
708  m_file_system_model->revert ();
709 
710  }
711  }
712  }
713 
715  {
716  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
717  QModelIndexList rows = m->selectedRows ();
718 
719  if (rows.size () > 0)
720  {
721  QModelIndex index = rows[0];
722 
723  QFileInfo info = m_file_system_model->fileInfo (index);
724  QString parent_dir = info.filePath ();
725 
726  process_new_file (parent_dir);
727  }
728  }
729 
731  {
732  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
733  QModelIndexList rows = m->selectedRows ();
734 
735  if (rows.size () > 0)
736  {
737  QModelIndex index = rows[0];
738 
739  QFileInfo info = m_file_system_model->fileInfo (index);
740  QString parent_dir = info.filePath ();
741 
742  process_new_dir (parent_dir);
743  }
744  }
745 
747  {
748  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
749  QModelIndexList rows = m->selectedRows ();
750 
751  if (rows.size () > 0)
752  {
753  QModelIndex index = rows[0];
754 
755  QFileInfo info = m_file_system_model->fileInfo (index);
756 
757  if (info.isDir ())
758  {
759  process_set_current_dir (info.absoluteFilePath ());
760  }
761  }
762  }
763 
765  {
766  QItemSelectionModel *m = m_file_tree_view->selectionModel ();
767  QModelIndexList rows = m->selectedRows ();
768 
769  if (rows.size () > 0)
770  {
771  QModelIndex index = rows[0];
772 
773  QFileInfo info = m_file_system_model->fileInfo (index);
774 
775  if (info.isDir ())
776  {
777  process_find_files (info.absoluteFilePath ());
778  }
779  }
780  }
781 
782  void files_dock_widget::notice_settings (const QSettings *settings)
783  {
784  // Qsettings pointer is checked before emitting.
785 
786  int icon_size_settings = settings->value ("toolbar_icon_size",0).toInt ();
787  QStyle *st = style ();
788  int icon_size = st->pixelMetric (QStyle::PM_ToolBarIconSize);
789 
790  if (icon_size_settings == 1)
791  icon_size = st->pixelMetric (QStyle::PM_LargeIconSize);
792  else if (icon_size_settings == -1)
793  icon_size = st->pixelMetric (QStyle::PM_SmallIconSize);
794 
795  m_navigation_tool_bar->setIconSize (QSize (icon_size,icon_size));
796 
797  // filenames are always shown, other columns can be hidden by settings
798  for (int i = 0; i < 3; i++)
799  m_file_tree_view->setColumnHidden (i + 1,
800  ! settings->value (m_columns_shown_keys.at (i),false).toBool ());
801 
802  if (settings->value (m_columns_shown_keys.at (3),false).toBool ())
803  m_file_system_model->setFilter (QDir::NoDotAndDotDot | QDir::AllEntries
804  | QDir::Hidden);
805  else
806  m_file_system_model->setFilter (QDir::NoDotAndDotDot | QDir::AllEntries);
807 
808  m_file_tree_view->setAlternatingRowColors (
809  settings->value (m_columns_shown_keys.at (4),true).toBool ());
811 
812  // enable the buttons to sync octave/browser dir
813  // only if this is not done by default
815  = settings->value ("filesdockwidget/sync_octave_directory",true).toBool ();
818 
819  if (m_sync_octave_dir)
820  display_directory (m_octave_dir); // sync browser to octave dir
821 
822  }
823 
825  {
826  QString dir
828 
829  if (dir.isEmpty ())
830  dir = QDir::homePath ();
831 
832  set_current_directory (dir);
833  }
834 
836  {
837  QString dir = QFileDialog::getExistingDirectory (this,
838  tr ("Set directory of file browser"),
839  m_file_system_model->rootPath (),
840  QFileDialog::ShowDirsOnly
841  | QFileDialog::DontUseNativeDialog);
842  set_current_directory (dir);
843  }
844 
846  {
848  }
849 
851  {
852  process_new_dir (m_file_system_model->rootPath ());
853  }
854 
856  {
857  process_new_file (m_file_system_model->rootPath ());
858  }
859 
860  void files_dock_widget::process_new_file (const QString& parent_dir)
861  {
862  bool ok;
863 
864  QString name = QInputDialog::getText (this, tr ("Create File"),
865  tr ("Create file in\n","String ends with \\n!") + parent_dir,
866  QLineEdit::Normal,
867  tr ("New File.txt"), &ok);
868  if (ok && name.length () > 0)
869  {
870  name = parent_dir + '/' + name;
871 
872  QFile file (name);
873  file.open (QIODevice::WriteOnly);
874  m_file_system_model->revert ();
875  }
876  }
877 
878  void files_dock_widget::process_new_dir (const QString& parent_dir)
879  {
880  bool ok;
881 
882  QString name = QInputDialog::getText (this, tr ("Create Directory"),
883  tr ("Create folder in\n","String ends with \\n!") + parent_dir,
884  QLineEdit::Normal,
885  tr ("New Directory"), &ok);
886  if (ok && name.length () > 0)
887  {
888  QDir dir (parent_dir);
889  dir.mkdir (name);
890  m_file_system_model->revert ();
891  }
892  }
893 
895  {
896  emit displayed_directory_changed (dir);
897  }
898 
899  void files_dock_widget::process_find_files (const QString & dir)
900  {
901  emit find_files_signal (dir);
902  }
903 
905  {
906  if (m_file_tree_view->hasFocus ())
908  if (m_current_directory->hasFocus ())
909  {
910  QClipboard *clipboard = QApplication::clipboard ();
911 
912  QLineEdit *edit = m_current_directory->lineEdit ();
913  if (edit && edit->hasSelectedText ())
914  {
915  clipboard->setText (edit->selectedText ());
916  }
917  }
918  }
919 
921  {
922  if (m_current_directory->hasFocus ())
923  {
924  QClipboard *clipboard = QApplication::clipboard ();
925  QString str = clipboard->text ();
926  QLineEdit *edit = m_current_directory->lineEdit ();
927  if (edit && str.length () > 0)
928  edit->insert (str);
929  }
930  }
931 
933  {
934  if (m_file_tree_view->hasFocus ())
935  m_file_tree_view->selectAll ();
936  if (m_current_directory->hasFocus ())
937  {
938  QLineEdit *edit = m_current_directory->lineEdit ();
939  if (edit)
940  {
941  edit->selectAll ();
942  }
943  }
944  }
945 }
void open_file(const QString &fileName)
Emitted, whenever the user requested to open a file.
void contextmenu_rename(bool)
Context menu actions.
void item_double_clicked(const QModelIndex &index)
Slot for handling a change in directory via double click.
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
void contextmenu_newdir(bool)
Context menu actions.
void contextmenu_load(bool)
Context menu actions.
bool is_octave_data_file(const std::string &fname)
Definition: load-save.cc:539
void do_sync_browser_directory(void)
Slot for handling the sync browser directory button in the toolbar.
void contextmenu_open_in_editor(bool)
Context menu actions.
QString fromStdString(const std::string &s)
QComboBox * m_current_directory
The file system view.
void find_files_signal(const QString &startdir)
Emitted, whenever wants to search for a file .
void load_file_signal(const QString &fileName)
Emitted, whenever the user requested to load a file.
void popdownmenu_newfile(bool)
Popdown menu options.
void file_remove_signal(const QString &old_name, const QString &new_name)
Emitted, whenever the user removes or renames a file.
bool m_sync_octave_dir
Flag if syncing with Octave.
i e
Definition: data.cc:2591
QString m_octave_dir
The actual Octave directory.
nd deftypefn *std::string name
Definition: sysdep.cc:647
static std::string get_home_directory(void)
Definition: oct-env.cc:143
void selectAll()
Inherited from octave_doc_widget.
void contextmenu_run(bool)
Context menu actions.
void displayed_directory_changed(const QString &dir)
Emitted, whenever the currently displayed directory changed.
QFileSystemModel * m_file_system_model
The file system model.
std::string str
Definition: hash.cc:118
void update_octave_directory(const QString &dir)
Set the internal variable that holds the actual octave variable.
QMainWindow * main_win(void)
void popdownmenu_search_dir(bool)
Popdown menu options.
void contextmenu_delete(bool)
Context menu actions.
QToolBar * m_navigation_tool_bar
Variables for the actions.
void notice_settings(const QSettings *settings)
Tells the widget to react on changed settings.
void accept_directory_line_edit(void)
Accepts user input a the line edit for the current directory.
void contextmenu_copy_selection(bool)
Context menu actions.
void set_title(const QString &)
void contextmenu_open_in_app(bool)
Context menu actions.
void open_item_in_app(const QModelIndex &index)
void warning(const char *fmt,...)
Definition: error.cc:801
static QIcon icon(const QString &icon_name, bool fallback=true)
void change_directory_up(void)
Slot for handling the up-directory button in the toolbar.
void contextmenu_open(bool)
Context menu actions.
void process_new_dir(const QString &parent_name)
void do_sync_octave_directory(void)
Slot for handling the sync octave directory button in the toolbar.
void mousePressEvent(QMouseEvent *e)
void contextmenu_findfiles(bool)
Context menu actions.
void popdownmenu_newdir(bool)
Popdown menu options.
p
Definition: lu.cc:138
void process_set_current_dir(const QString &parent_name)
files_dock_widget(QWidget *parent=nullptr)
octave_map map(dims)
QTreeView * m_file_tree_view
The file system view.
void display_directory(const QString &dir, bool set_octave_dir=true)
set a new directory or open a file
void run_file_signal(const QFileInfo &info)
Emitted, whenever the user requested to run a file.
static QSettings * get_settings(void)
void set_current_directory(const QString &dir)
Sets the current directory being displayed.
~FileTreeViewer(void)=default
for i
Definition: data.cc:5264
void headercontextmenu_requested(const QPoint &pos)
void contextmenu_newfile(bool)
Context menu actions.
void contextmenu_requested(const QPoint &pos)
Context menu wanted.
void file_renamed_signal(bool)
Emitted, when a file or directory is renamed.
void process_new_file(const QString &parent_name)
nd example The result of the integration is returned in the value of it is recommended to verify this value for difficult integrands and then a warning is issued and as may be less efficient for a smooth or otherwise well behaved integrand than other methods such as Article No
Definition: quadcc.cc:1567
void process_find_files(const QString &dir_name)
void copyClipboard()
Inherited from octave_doc_widget.
void pasteClipboard()
Inherited from octave_doc_widget.
void popdownmenu_home(bool)
Popdown menu options.
void contextmenu_setcurrentdir(bool)
Context menu actions.
void popdownmenu_findfiles(bool)
Popdown menu options.