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