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