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