31 #include <QApplication>
37 #include <QHeaderView>
39 #include <QSizePolicy>
41 #include <QInputDialog>
42 #include <QMessageBox>
43 #include <QToolButton>
45 #include <QDesktopServices>
46 #include <QFileDialog>
59 if (e->button () != Qt::RightButton)
60 QTreeView::mousePressEvent (e);
67 setObjectName (
"FilesDockWidget");
68 setWindowIcon (QIcon (
":/actions/icons/logo.png"));
70 setToolTip (tr (
"Browse your files."));
74 setWidget (container);
76 connect (
this, SIGNAL (
open_file (
const QString&)),
80 main_win (), SLOT (set_current_working_directory (
const QString&)));
93 QComboBox::AdjustToMinimumContentsLengthWithIcon);
94 QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
99 directory_up_action->setToolTip (tr (
"One directory up"));
105 tr (
"Go to current Octave directory"));
112 tr (
"Set Octave directory to current browser directory"));
115 QToolButton * popdown_button =
new QToolButton ();
116 popdown_button->setToolTip (tr (
"Actions on current directory"));
117 QMenu * popdown_menu =
new QMenu ();
119 tr (
"Show Home Directory"),
123 popdown_button->setMenu (popdown_menu);
124 popdown_button->setPopupMode (QToolButton::InstantPopup);
125 popdown_button->setDefaultAction (
new QAction (
129 popdown_menu->addSeparator ();
131 tr (
"Set Browser Directory..."),
133 popdown_menu->addSeparator ();
135 tr (
"Find Files..."),
137 popdown_menu->addSeparator ();
142 tr (
"New Directory..."),
149 connect (directory_up_action, SIGNAL (triggered ()),
this,
162 if (settings->value (
"filesdockwidget/restore_last_dir",
false).toBool ())
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));
170 else if (! settings->value (
"filesdockwidget/startup_dir").toString ().isEmpty ())
173 startup_dir = QDir (settings->value (
"filesdockwidget/startup_dir").toString ());
176 if (! startup_dir.exists ())
179 startup_dir = QDir ();
183 if (settings->value (
"filesdockwidget/showHiddenFiles",
false).toBool ())
193 startup_dir.absolutePath ());
197 _file_tree_view->setSelectionMode (QAbstractItemView::ExtendedSelection);
204 tr (
"Activate to open in editor, right click for alternatives"));
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 ())
215 settings->value (
"filesdockwidget/column_state").toByteArray ());
217 QStringList mru_dirs =
218 settings->value (
"filesdockwidget/mru_dir_list").toStringList ();
230 SIGNAL (customContextMenuRequested (
const QPoint &)),
233 _file_tree_view->header()->setContextMenuPolicy (Qt::CustomContextMenu);
235 SIGNAL (customContextMenuRequested (
const QPoint &)),
239 QVBoxLayout *vbox_layout =
new QVBoxLayout ();
240 vbox_layout->setSpacing (0);
243 vbox_layout->setMargin (1);
245 container->setLayout (vbox_layout);
257 main_win (), SLOT (run_file_in_terminal (
const QFileInfo&)));
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",
283 settings->setValue (
"filesdockwidget/mru_dir_list", dirs);
344 QFileInfo fileInfo (dir);
345 if (fileInfo.exists ())
347 if (fileInfo.isDir ())
350 index (fileInfo.absoluteFilePath ()));
369 QString abs_fname = fileInfo.absoluteFilePath ();
371 if (QFile::exists (abs_fname))
376 emit
open_file (fileInfo.absoluteFilePath ());
388 QString file = fileInfo.absoluteFilePath ();
390 QDesktopServices::openUrl (QUrl::fromLocalFile (file));
397 (
"filesdockwidget/showFileSize",
398 ! settings->value (
"filesdockwidget/showFileSize",
false).toBool ());
407 (
"filesdockwidget/showFileType",
408 ! settings->value (
"filesdockwidget/showFileType",
false).toBool ());
417 (
"filesdockwidget/showLastModified",
418 ! settings->value (
"filesdockwidget/showLastModified",
false).toBool ());
427 (
"filesdockwidget/showHiddenFiles",
428 ! settings->value (
"filesdockwidget/showHiddenFiles",
false).toBool ());
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 ()),
446 menu.addAction (&fileSizeAction);
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 ()),
454 menu.addAction (&fileTypeAction);
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 ()),
462 menu.addAction (&dateModifiedAction);
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 ()),
470 menu.addAction (&showHiddenAction);
483 if (index.isValid ())
488 QModelIndexList sel = m->selectedRows ();
491 if (! sel.contains (index))
493 m->setCurrentIndex(index,
494 QItemSelectionModel::Clear | QItemSelectionModel::Select |
495 QItemSelectionModel::Rows);
502 menu.addAction (tr (
"Open in Default Application"),
505 menu.addAction (tr (
"Copy Selection to Clipboard"),
508 if (info.isFile () && info.suffix () ==
"m")
517 menu.addSeparator ();
519 tr (
"Set Current Directory"),
521 menu.addSeparator ();
523 tr (
"Find Files..."),
this,
527 menu.addSeparator ();
534 menu.addSeparator ();
539 tr (
"New Directory..."),
554 QModelIndexList rows = m->selectedRows ();
556 for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
562 emit
open_file (file.absoluteFilePath ());
573 QModelIndexList rows = m->selectedRows ();
575 for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
583 QModelIndexList rows = m->selectedRows ();
585 QStringList selection;
587 for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
591 selection << info.fileName ();
594 QClipboard *clipboard = QApplication::clipboard ();
596 clipboard->setText (selection.join (
"\n"));
603 QModelIndexList rows = m->selectedRows ();
605 if (rows.size () > 0)
607 QModelIndex index = rows[0];
619 QModelIndexList rows = m->selectedRows ();
621 if (rows.size () > 0)
623 QModelIndex index = rows[0];
634 QModelIndexList rows = m->selectedRows ();
635 if (rows.size () > 0)
637 QModelIndex index = rows[0];
640 QDir path = info.absoluteDir ();
641 QString old_name = info.fileName ();
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)
651 new_name = path.absolutePath () +
"/" + new_name;
652 old_name = path.absolutePath () +
"/" + old_name;
653 path.rename (old_name, new_name);
664 QModelIndexList rows = m->selectedRows ();
666 for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
668 QModelIndex index = *it;
672 if (QMessageBox::question (
this, tr (
"Delete file/directory"),
673 tr (
"Are you sure you want to delete\n")
675 QMessageBox::Yes|QMessageBox::No)
681 QDir path (info.absoluteFilePath ());
683 QDir::NoDotAndDotDot);
685 if (fileLst.count () != 0)
687 tr (
"Can not delete a directory that is not empty"));
706 QModelIndexList rows = m->selectedRows ();
708 if (rows.size () > 0)
710 QModelIndex index = rows[0];
713 QString parent_dir = info.filePath ();
723 QModelIndexList rows = m->selectedRows ();
725 if (rows.size () > 0)
727 QModelIndex index = rows[0];
730 QString parent_dir = info.filePath ();
740 QModelIndexList rows = m->selectedRows ();
742 if (rows.size () > 0)
744 QModelIndex index = rows[0];
759 QModelIndexList rows = m->selectedRows ();
761 if (rows.size () > 0)
763 QModelIndex index = rows[0];
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);
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);
793 ! settings->value (
"filesdockwidget/showFileSize",
false).toBool ());
795 ! settings->value (
"filesdockwidget/showFileType",
false).toBool ());
797 ! settings->value (
"filesdockwidget/showLastModified",
false).toBool ());
799 settings->value (
"filesdockwidget/useAlternatingRowColors",
true).toBool ());
800 if (settings->value (
"filesdockwidget/showHiddenFiles",
false).toBool ())
814 = settings->value (
"filesdockwidget/sync_octave_directory",
false).toBool ();
829 dir = QDir::homePath ();
837 QString dir = QFileDialog::getExistingDirectory
838 (
this, tr (
"Set directory of file browser"),
840 QFileDialog::ShowDirsOnly | QFileDialog::DontUseNativeDialog);
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)
872 name = parent_dir +
"/" + name;
875 file.open (QIODevice::WriteOnly);
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)
890 QDir dir (parent_dir);
913 QClipboard *clipboard = QApplication::clipboard ();
916 if (edit && edit->hasSelectedText ())
918 clipboard->setText (edit->selectedText ());
928 QClipboard *clipboard = QApplication::clipboard ();
929 QString str = clipboard->text ();
931 if (edit && str.length () > 0)
void mousePressEvent(QMouseEvent *e)
bool is_octave_data_file(const std::string &fname)
FileTreeViewer(QWidget *p)
QString fromStdString(const std::string &s)
static QSettings * get_settings(void)
void warning(const char *fmt,...)
static QIcon icon(const QString &icon_name, bool fallback=true)
static std::string get_home_directory(void)