26 #if defined (HAVE_CONFIG_H)
30 #include <QApplication>
34 #include <QDesktopServices>
35 #include <QFileDialog>
37 #include <QHeaderView>
38 #include <QInputDialog>
41 #include <QMessageBox>
43 #include <QSizePolicy>
44 #include <QStyledItemDelegate>
46 #include <QToolButton>
70 if (e->button () != Qt::RightButton)
71 QTreeView::mousePressEvent (e);
86 bool setData (
const QModelIndex &idx,
const QVariant &value,
89 if (!idx.isValid () || idx.column () != 0 || role != Qt::EditRole
90 || (flags (idx) & Qt::ItemIsEditable) == 0)
95 QString new_name = value.toString ();
96 QString old_name = idx.data ().toString ();
97 if (new_name == old_name)
99 if (new_name.isEmpty ()
100 || QDir::toNativeSeparators (new_name).contains (QDir::separator ()))
106 auto parent_dir = QDir(filePath (parent (idx)));
110 fdw->
file_remove_signal(parent_dir.filePath(old_name), parent_dir.filePath(new_name));
112 if (!parent_dir.rename (old_name, new_name))
121 emit fileRenamed(parent_dir.absolutePath(), old_name, new_name);
129 const QString &new_name)
133 files_dock_widget::tr (
"Could not rename file \"%1\" to \"%2\".")
136 QMessageBox::information (
static_cast<QWidget *
> (parent ()),
137 QFileSystemModel::tr (
"Invalid filename"),
153 const QModelIndex &index)
const override
155 QLineEdit *line_edit = qobject_cast<QLineEdit *> (editor);
159 QStyledItemDelegate::setEditorData (editor, index);
163 QString filename = index.data (Qt::EditRole).toString ();
165 int select_len = filename.indexOf (QChar (
'.'));
166 if (select_len == -1)
167 select_len = filename.size ();
169 line_edit->setText (filename);
175 QTimer::singleShot (0, [=] () {
176 line_edit->setSelection (0, select_len);
185 setToolTip (tr (
"Browse your files"));
212 setWidget (container);
224 m_current_directory->setSizeAdjustPolicy (QComboBox::AdjustToMinimumContentsLengthWithIcon);
225 QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
230 QAction *directory_up_action
232 directory_up_action->setToolTip (tr (
"One directory up"));
235 =
new QAction (rmgr.
icon (
"go-first"), tr (
"Show Octave directory"),
241 =
new QAction (rmgr.
icon (
"go-last"), tr (
"Set Octave directory"),
247 popdown_button->setToolTip (tr (
"Actions on current directory"));
249 popdown_menu->addAction (rmgr.
icon (
"user-home"),
250 tr (
"Show Home Directory"),
this,
254 popdown_button->setMenu (popdown_menu);
255 popdown_button->setPopupMode (QToolButton::InstantPopup);
256 popdown_button->setDefaultAction (
257 new QAction (rmgr.
icon (
"folder-settings",
false,
"applications-system"),
260 popdown_menu->addSeparator ();
261 popdown_menu->addAction (rmgr.
icon (
"folder"),
262 tr (
"Set Browser Directory..."),
264 popdown_menu->addSeparator ();
265 popdown_menu->addAction (rmgr.
icon (
"edit-find"),
266 tr (
"Find Files..."),
268 popdown_menu->addSeparator ();
269 popdown_menu->addAction (rmgr.
icon (
"document-new"),
272 popdown_menu->addAction (rmgr.
icon (
"folder-new"),
273 tr (
"New Directory..."),
280 connect (directory_up_action, &QAction::triggered,
296 QStringList last_dirs
298 if (last_dirs.length () > 0)
299 startup_dir = QDir (last_dirs.at (0));
307 if (! startup_dir.exists ())
310 startup_dir = QDir ();
316 QDir::System | QDir::NoDotAndDotDot | QDir::AllEntries);
317 QModelIndex rootPathIndex
328 m_file_tree_view->setToolTip (tr (
"Double-click to open file/folder, right click for alternatives"));
365 QStringList mru_dirs =
382 &QHeaderView::customContextMenuRequested,
386 QVBoxLayout *vbox_layout =
new QVBoxLayout ();
387 vbox_layout->setSpacing (0);
390 vbox_layout->setMargin (1);
392 container->setLayout (vbox_layout);
439 Qt::SortOrder sort_order =
m_file_tree_view->header ()->sortIndicatorOrder ();
509 QFileInfo fileInfo (dir);
510 if (fileInfo.exists ())
512 if (fileInfo.isDir ())
515 index (fileInfo.absoluteFilePath ()));
533 QString abs_fname = fileInfo.absoluteFilePath ();
535 QString suffix = fileInfo.suffix ().toLower ();
539 #if defined (HAVE_QT_SPLITBEHAVIOR_ENUM)
540 QStringList extensions = ext.split (
";", Qt::SkipEmptyParts);
542 QStringList extensions = ext.split (
";", QString::SkipEmptyParts);
544 if (QFile::exists (abs_fname))
546 if (extensions.contains (suffix))
547 emit
open_file (fileInfo.absoluteFilePath ());
560 QString file = fileInfo.absoluteFilePath ();
562 QDesktopServices::openUrl (QUrl::fromLocalFile (file));
571 bool shown =
settings->value (key,
false).toBool ();
607 action->setCheckable (
true);
640 if (index.isValid ())
645 QModelIndexList sel =
m->selectedRows ();
648 if (! sel.contains (index))
651 m->setCurrentIndex (index,
652 QItemSelectionModel::Clear
653 | QItemSelectionModel::Select
654 | QItemSelectionModel::Rows);
660 menu.addAction (rmgr.
icon (
"document-open"), tr (
"Open"),
665 menu.addAction (tr (
"Open in System File Explorer"),
670 menu.addAction (tr (
"Open in Text Editor"),
673 menu.addAction (tr (
"Copy Selection to Clipboard"),
676 if (info.isFile () && info.suffix () ==
"m")
677 menu.addAction (rmgr.
icon (
"media-playback-start"), tr (
"Run"),
681 menu.addAction (tr (
"Load Data"),
686 menu.addSeparator ();
687 menu.addAction (rmgr.
icon (
"go-first"), tr (
"Set Current Directory"),
690 QMenu *add_path_menu = menu.addMenu (tr (
"Add to Path"));
692 add_path_menu->addAction (tr (
"Selected Directories"),
694 add_path_menu->addAction (tr (
"Selected Directories and Subdirectories"),
697 QMenu *rm_path_menu = menu.addMenu (tr (
"Remove from Path"));
699 rm_path_menu->addAction (tr (
"Selected Directories"),
701 rm_path_menu->addAction (tr (
"Selected Directories and Subdirectories"),
704 menu.addSeparator ();
706 menu.addAction (rmgr.
icon (
"edit-find"), tr (
"Find Files..."),
710 menu.addSeparator ();
712 menu.addAction (rmgr.
icon (
"edit-delete"), tr (
"Delete..."),
717 menu.addSeparator ();
718 menu.addAction (rmgr.
icon (
"document-new"), tr (
"New File..."),
720 menu.addAction (rmgr.
icon (
"folder-new"), tr (
"New Directory..."),
734 QModelIndexList rows =
m->selectedRows ();
736 for (
auto it = rows.begin (); it != rows.end (); it++)
748 QModelIndexList rows =
m->selectedRows ();
750 for (
auto it = rows.begin (); it != rows.end (); it++)
754 emit
open_file (file.absoluteFilePath ());
761 QModelIndexList rows =
m->selectedRows ();
763 for (
auto it = rows.begin (); it != rows.end (); it++)
770 QModelIndexList rows =
m->selectedRows ();
772 QStringList selection;
774 for (
auto it = rows.begin (); it != rows.end (); it++)
778 selection << info.fileName ();
781 QClipboard *clipboard = QApplication::clipboard ();
783 clipboard->setText (selection.join (
"\n"));
789 QModelIndexList rows =
m->selectedRows ();
791 if (rows.size () > 0)
793 QModelIndex index = rows[0];
804 QModelIndexList rows =
m->selectedRows ();
806 if (rows.size () > 0)
808 QModelIndex index = rows[0];
818 QModelIndexList rows =
m->selectedRows ();
819 if (rows.size () > 0)
821 QModelIndex index = rows[0];
829 QModelIndexList rows =
m->selectedRows ();
831 int file_cnt = rows.size ();
832 bool multiple_files = (file_cnt > 1);
834 for (
auto it = rows.begin (); it != rows.end (); it++)
836 QModelIndex index = *it;
840 QMessageBox::StandardButton dlg_answer;
842 if (it == rows.begin ())
844 dlg_answer = QMessageBox::question (
this,
845 tr (
"Delete file/directory"),
846 tr (
"Are you sure you want to delete all %1 selected files?\n").arg (file_cnt),
847 QMessageBox::Yes | QMessageBox::No);
848 if (dlg_answer != QMessageBox::Yes)
852 dlg_answer = QMessageBox::Yes;
855 dlg_answer = QMessageBox::question (
this,
856 tr (
"Delete file/directory"),
857 tr (
"Are you sure you want to delete\n")
859 QMessageBox::Yes | QMessageBox::No);
862 if (dlg_answer == QMessageBox::Yes)
867 QDir path (info.absoluteFilePath ());
869 QDir::Hidden | QDir::AllEntries |
870 QDir::NoDotAndDotDot | QDir::System);
872 if (fileLst.count () != 0)
874 tr (
"Can not delete a directory that is not empty"));
887 tr (
"Could not delete file \"%1\".").
888 arg (info.filePath ()));
905 QModelIndexList rows =
m->selectedRows ();
909 for (
auto it = rows.begin (); it != rows.end (); it++)
911 QModelIndex index = *it;
915 if (info.exists () &&
916 ((dir & info.isDir ()) || (! dir && info.isFile ())))
926 QModelIndexList rows =
m->selectedRows ();
928 if (rows.size () > 0)
930 QModelIndex index = rows[0];
933 QString parent_dir = info.filePath ();
942 QModelIndexList rows =
m->selectedRows ();
944 if (rows.size () > 0)
946 QModelIndex index = rows[0];
949 QString parent_dir = info.filePath ();
959 if (infos.length () > 0 && infos.first ().isDir ())
967 QStringList dir_list;
969 for (
int i = 0; i < infos.length (); i++)
970 dir_list.append (infos.at (i).absoluteFilePath ());
972 if (infos.length () > 0)
994 QModelIndexList rows =
m->selectedRows ();
996 if (rows.size () > 0)
998 QModelIndex index = rows[0];
1014 size_idx = (size_idx > 0) - (size_idx < 0) + 1;
1016 QStyle *st = style ();
1021 for (
int i = 0; i < 3; i++)
1057 dir = QDir::homePath ();
1065 int opts = QFileDialog::ShowDirsOnly;
1069 opts |= QFileDialog::DontUseNativeDialog;
1071 QString dir = QFileDialog::getExistingDirectory (
this,
1072 tr (
"Set directory of file browser"),
1074 QFileDialog::Option (opts));
1097 QString name = QInputDialog::getText (
this, tr (
"Create File"),
1098 tr (
"Create file in\n",
"String ends with \\n!") + parent_dir,
1100 tr (
"New File.txt"), &ok);
1101 if (ok && name.length () > 0)
1103 name = parent_dir +
'/' + name;
1106 file.open (QIODevice::WriteOnly);
1115 QString name = QInputDialog::getText (
this, tr (
"Create Directory"),
1116 tr (
"Create folder in\n",
"String ends with \\n!") + parent_dir,
1118 tr (
"New Directory"), &ok);
1119 if (ok && name.length () > 0)
1121 QDir dir (parent_dir);
1143 QClipboard *clipboard = QApplication::clipboard ();
1146 if (edit && edit->hasSelectedText ())
1148 clipboard->setText (edit->selectedText ());
1157 QClipboard *clipboard = QApplication::clipboard ();
1158 QString str = clipboard->text ();
1160 if (edit && str.length () > 0)
~FileTreeViewer(void)=default
void mousePressEvent(QMouseEvent *e)
FileTreeViewer(QWidget *p)
void setEditorData(QWidget *editor, const QModelIndex &index) const override
RenameItemDelegate(QObject *parent=nullptr)
Base class for Octave interfaces that use Qt.
resource_manager & get_resource_manager(void)
bool setData(const QModelIndex &idx, const QVariant &value, int role) override
file_system_model(files_dock_widget *p)
~file_system_model()=default
void display_rename_failed_message(const QString &old_name, const QString &new_name)
gui_settings * get_settings(void) const
QIcon icon(const QString &icon_name, bool octave_only=false, const QString &icon_alt_name=QString())
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning(const char *fmt,...)
void message(const char *name, const char *fmt,...)
const gui_pref fb_mru_list("filesdockwidget/mru_dir_list", QVariant(QStringList()))
const gui_pref fb_column_state("filesdockwidget/column_state", QVariant())
const gui_pref fb_show_date("filesdockwidget/showLastModified", QVariant(false))
const gui_pref fb_sync_octdir("filesdockwidget/sync_octave_directory", QVariant(true))
const gui_pref fb_sort_order("filesdockwidget/sort_files_by_order", QVariant(Qt::AscendingOrder))
const gui_pref fb_sort_column("filesdockwidget/sort_files_by_column", QVariant(0))
const gui_pref fb_show_type("filesdockwidget/showFileType", QVariant(false))
const gui_pref fb_show_hidden("filesdockwidget/showHiddenFiles", QVariant(false))
const gui_pref fb_startup_dir("filesdockwidget/startup_dir", QVariant(QString()))
const gui_pref fb_show_size("filesdockwidget/showFileSize", QVariant(false))
const gui_pref fb_txt_file_ext("filesdockwidget/txt_file_extensions", QVariant("m;c;cc;cpp;h;txt"))
const gui_pref fb_restore_last_dir("filesdockwidget/restore_last_dir", QVariant(false))
const gui_pref fb_show_altcol("filesdockwidget/useAlternatingRowColors", QVariant(true))
const QStyle::PixelMetric global_icon_sizes[3]
const gui_pref global_use_native_dialogs("use_native_file_dialogs", QVariant(true))
const gui_pref global_icon_size("toolbar_icon_size", QVariant(0))
QString fromStdString(const std::string &s)