view libgui/src/files-dock-widget.cc @ 31771:21f9b34eb893

maint: Eliminate "(void)" in C++ function prototypes/declarations. * mk-opts.pl, external.txi, embedded.cc, make_int.cc, standalone.cc, standalonebuiltin.cc, BaseControl.cc, BaseControl.h, ButtonControl.cc, ButtonControl.h, ButtonGroup.cc, ButtonGroup.h, Canvas.cc, Canvas.h, CheckBoxControl.cc, CheckBoxControl.h, Container.cc, Container.h, ContextMenu.cc, ContextMenu.h, EditControl.cc, EditControl.h, Figure.cc, Figure.h, FigureWindow.cc, FigureWindow.h, GLCanvas.cc, GLCanvas.h, GenericEventNotify.h, KeyMap.cc, ListBoxControl.cc, ListBoxControl.h, Logger.cc, Logger.h, Menu.cc, Menu.h, MenuContainer.h, Object.cc, Object.h, ObjectProxy.cc, ObjectProxy.h, Panel.cc, Panel.h, PopupMenuControl.cc, PopupMenuControl.h, PushButtonControl.cc, PushButtonControl.h, PushTool.cc, PushTool.h, RadioButtonControl.cc, RadioButtonControl.h, SliderControl.cc, SliderControl.h, Table.cc, Table.h, TextControl.cc, TextControl.h, TextEdit.h, ToggleButtonControl.cc, ToggleButtonControl.h, ToggleTool.cc, ToggleTool.h, ToolBar.cc, ToolBar.h, ToolBarButton.cc, ToolBarButton.h, gl-select.cc, gl-select.h, qopengl-functions.h, qt-graphics-toolkit.h, qdialog.cpp, qfontdialog.cpp, qprintdialog_win.cpp, liboctgui-build-info.h, liboctgui-build-info.in.cc, color-picker.cc, color-picker.h, command-widget.cc, command-widget.h, community-news.cc, community-news.h, dialog.cc, dialog.h, documentation-bookmarks.cc, documentation-bookmarks.h, documentation-dock-widget.cc, documentation-dock-widget.h, documentation.cc, documentation.h, dw-main-window.cc, dw-main-window.h, external-editor-interface.cc, external-editor-interface.h, files-dock-widget.cc, files-dock-widget.h, find-files-dialog.cc, find-files-dialog.h, find-files-model.cc, find-files-model.h, gui-preferences.cc, gui-preferences.h, gui-settings.cc, gui-settings.h, history-dock-widget.cc, history-dock-widget.h, interpreter-qobject.cc, interpreter-qobject.h, file-editor-interface.h, file-editor-tab.cc, file-editor-tab.h, file-editor.cc, file-editor.h, find-dialog.cc, find-dialog.h, marker.cc, marker.h, octave-qscintilla.cc, octave-qscintilla.h, octave-txt-lexer.cc, octave-txt-lexer.h, main-window.cc, main-window.h, news-reader.cc, news-reader.h, octave-dock-widget.cc, octave-dock-widget.h, octave-qobject.cc, octave-qobject.h, qt-application.cc, qt-application.h, qt-interpreter-events.cc, qt-interpreter-events.h, release-notes.cc, release-notes.h, set-path-dialog.cc, set-path-dialog.h, set-path-model.cc, set-path-model.h, settings-dialog.cc, settings-dialog.h, shortcuts-tree-widget.cc, shortcuts-tree-widget.h, tab-bar.cc, tab-bar.h, terminal-dock-widget.cc, terminal-dock-widget.h, variable-editor-model.cc, variable-editor-model.h, variable-editor.cc, variable-editor.h, welcome-wizard.cc, welcome-wizard.h, workspace-model.cc, workspace-model.h, workspace-view.cc, workspace-view.h, build-env.h, Cell.cc, Cell.h, __contourc__.cc, __magick_read__.cc, auto-shlib.cc, auto-shlib.h, base-text-renderer.h, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, debug.cc, defaults.cc, defaults.h, defun.cc, display.cc, display.h, dynamic-ld.cc, dynamic-ld.h, environment.cc, environment.h, error.cc, error.h, errwarn.cc, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, ft-text-renderer.cc, ft-text-renderer.h, genprops.awk, gh-manager.cc, gh-manager.h, gl-render.cc, gl-render.h, gl2ps-print.cc, graphics-toolkit.h, graphics.cc, graphics.in.h, gtk-manager.cc, gtk-manager.h, help.cc, help.h, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, jsondecode.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, ls-hdf5.cc, ls-hdf5.h, mxarray.h, oct-errno.h, oct-errno.in.cc, oct-fstrm.cc, oct-fstrm.h, oct-handle.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-map.cc, oct-map.h, oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, pager.cc, pager.h, pr-flt-fmt.cc, pr-flt-fmt.h, pr-output.cc, pr-output.h, procstream.cc, procstream.h, settings.cc, settings.h, sighandlers.cc, sighandlers.h, stack-frame.cc, stack-frame.h, svd.cc, syminfo.cc, syminfo.h, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, toplev.cc, url-handle-manager.cc, url-handle-manager.h, variables.cc, xpow.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, audiodevinfo.cc, gzip.cc, liboctinterp-build-info.h, liboctinterp-build-info.in.cc, mk-build-env-features.sh, mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base-diag.cc, ov-base-diag.h, ov-base-int.cc, ov-base-int.h, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base-scalar.h, ov-base-sparse.cc, ov-base-sparse.h, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-bool-mat.h, ov-bool-sparse.cc, ov-bool-sparse.h, ov-bool.cc, ov-bool.h, ov-builtin.cc, ov-builtin.h, ov-cell.cc, ov-cell.h, ov-ch-mat.cc, ov-ch-mat.h, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-colon.h, ov-complex.cc, ov-complex.h, ov-cs-list.h, ov-cx-diag.cc, ov-cx-diag.h, ov-cx-mat.cc, ov-cx-mat.h, ov-cx-sparse.cc, ov-cx-sparse.h, ov-dld-fcn.cc, ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.cc, ov-fcn.h, ov-float.cc, ov-float.h, ov-flt-complex.cc, ov-flt-complex.h, ov-flt-cx-diag.cc, ov-flt-cx-diag.h, ov-flt-cx-mat.cc, ov-flt-cx-mat.h, ov-flt-re-diag.cc, ov-flt-re-diag.h, ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-intx.h, ov-java.cc, ov-java.h, ov-lazy-idx.cc, ov-lazy-idx.h, ov-legacy-range.cc, ov-legacy-range.h, ov-magic-int.cc, ov-magic-int.h, ov-mex-fcn.cc, ov-mex-fcn.h, ov-null-mat.cc, ov-null-mat.h, ov-oncleanup.cc, ov-oncleanup.h, ov-perm.cc, ov-perm.h, ov-range.cc, ov-range.h, ov-re-diag.cc, ov-re-diag.h, ov-re-mat.cc, ov-re-mat.h, ov-re-sparse.cc, ov-re-sparse.h, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc, ov-str-mat.h, ov-struct.cc, ov-struct.h, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.cc, ovl.h, octave.cc, octave.h, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc, oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.h, pt-cbinop.h, pt-cell.h, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.h, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-walk.h, pt.cc, pt.h, token.cc, token.h, usage.h, Array-base.cc, Array.h, CColVector.cc, CColVector.h, CDiagMatrix.cc, CDiagMatrix.h, CMatrix.cc, CMatrix.h, CNDArray.cc, CNDArray.h, CRowVector.cc, CRowVector.h, CSparse.cc, CSparse.h, DiagArray2.cc, DiagArray2.h, MArray.cc, MArray.h, MDiagArray2.h, MSparse.h, MatrixType.cc, MatrixType.h, PermMatrix.cc, PermMatrix.h, Range.cc, Range.h, Sparse-b.cc, Sparse.cc, Sparse.h, boolMatrix.cc, boolMatrix.h, boolNDArray.cc, boolNDArray.h, boolSparse.cc, boolSparse.h, chMatrix.h, chNDArray.h, dColVector.cc, dColVector.h, dDiagMatrix.cc, dDiagMatrix.h, dMatrix.cc, dMatrix.h, dNDArray.cc, dNDArray.h, dRowVector.cc, dRowVector.h, dSparse.cc, dSparse.h, dim-vector.cc, dim-vector.h, fCColVector.cc, fCColVector.h, fCDiagMatrix.cc, fCDiagMatrix.h, fCMatrix.cc, fCMatrix.h, fCNDArray.cc, fCNDArray.h, fCRowVector.cc, fCRowVector.h, fColVector.cc, fColVector.h, fDiagMatrix.cc, fDiagMatrix.h, fMatrix.cc, fMatrix.h, fNDArray.cc, fNDArray.h, fRowVector.cc, fRowVector.h, idx-vector.cc, idx-vector.h, intNDArray.cc, intNDArray.h, liboctave-build-info.h, liboctave-build-info.in.cc, CollocWt.cc, CollocWt.h, DAE.h, DAEFunc.h, DAERT.h, DAERTFunc.h, DASPK.cc, DASPK.h, DASRT.cc, DASRT.h, DASSL.cc, DASSL.h, DET.h, EIG.h, LSODE.cc, LSODE.h, ODE.h, ODEFunc.h, ODES.h, ODESFunc.h, Quad.h, aepbalance.cc, aepbalance.h, base-dae.h, base-de.h, chol.cc, chol.h, eigs-base.cc, fEIG.h, gepbalance.h, gsvd.cc, gsvd.h, hess.h, lu.cc, lu.h, oct-fftw.cc, oct-fftw.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randmtzig.cc, randmtzig.h, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.h, base-list.h, caseless-str.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, data-conv.cc, file-info.h, glob-match.cc, glob-match.h, kpse.cc, kpse.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.cc, lo-ieee.h, lo-regexp.cc, lo-regexp.h, oct-inttypes.cc, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sort.cc, oct-sort.h, oct-sparse.cc, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, singleton-cleanup.cc, singleton-cleanup.h, str-vec.cc, str-vec.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h, version.cc, version.in.h, cxx-signal-helpers.cc, acinclude.m4, main-cli.cc, main-gui.cc, main.in.cc, mkoctfile.in.cc, octave-build-info.h, octave-build-info.in.cc, octave-config.in.cc, octave-svgconvert.cc, shared-fcns.h: maint: Eliminate "(void)" in C++ function prototypes/declarations.
author Rik <rik@octave.org>
date Tue, 24 Jan 2023 17:19:44 -0800
parents 2f730c070d3e
children e25bf4beb488
line wrap: on
line source

////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2023 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <QApplication>
#include <QClipboard>
#include <QCompleter>
#include <QDebug>
#include <QDesktopServices>
#include <QFileDialog>
#include <QFileInfo>
#include <QHeaderView>
#include <QInputDialog>
#include <QLineEdit>
#include <QMenu>
#include <QMessageBox>
#include <QProcess>
#include <QSizePolicy>
#include <QStyledItemDelegate>
#include <QTimer>
#include <QToolButton>
#include <QUrl>

#include "files-dock-widget.h"
#include "gui-preferences-fb.h"
#include "gui-preferences-global.h"
#include "gui-settings.h"
#include "qt-interpreter-events.h"

#include "oct-env.h"

OCTAVE_BEGIN_NAMESPACE(octave)

  class FileTreeViewer : public QTreeView
  {
  public:

    FileTreeViewer (QWidget *p) : QTreeView (p) { }

    ~FileTreeViewer () = default;

    void mousePressEvent (QMouseEvent *e)
    {
      if (e->button () != Qt::RightButton)
        QTreeView::mousePressEvent (e);
    }
  };

  // to have file renamed in the file tree, it has to be renamed in
  // QFileSystemModel::setData.
  // For the editor to behave correctly, some signals must be sent before
  // and after the rename
  class file_system_model : public QFileSystemModel
  {
  public:
    file_system_model (files_dock_widget *p) : QFileSystemModel (p) {}

    ~file_system_model () = default;

    bool setData (const QModelIndex &idx, const QVariant &value,
                  int role) override
    {
      if (!idx.isValid () || idx.column () != 0 || role != Qt::EditRole
          || (flags (idx) & Qt::ItemIsEditable) == 0)
        {
          return false;
        }

      QString new_name = value.toString ();
      QString old_name = idx.data ().toString ();
      if (new_name == old_name)
        return true;
      if (new_name.isEmpty ()
          || QDir::toNativeSeparators (new_name).contains (QDir::separator ()))
        {
          display_rename_failed_message (old_name, new_name);
          return false;
        }

      auto parent_dir = QDir(filePath (parent (idx)));

      files_dock_widget *fdw = static_cast<files_dock_widget*>(parent());

      fdw->file_remove_signal(parent_dir.filePath(old_name), parent_dir.filePath(new_name));

      if (!parent_dir.rename (old_name, new_name))
        {
          display_rename_failed_message (old_name, new_name);
          fdw->file_renamed_signal(false);
          return false;
        }

      fdw->file_renamed_signal(true);

      emit fileRenamed(parent_dir.absolutePath(), old_name, new_name);
      revert();

      return true;
    }

  private:
    void display_rename_failed_message (const QString &old_name,
                                        const QString &new_name)
    {
      const QString message =

          files_dock_widget::tr ("Could not rename file \"%1\" to \"%2\".")
              .arg (old_name)
              .arg (new_name);
      QMessageBox::information (static_cast<QWidget *> (parent ()),
                                QFileSystemModel::tr ("Invalid filename"),
                                message, QMessageBox::Ok);
    }
  };

  // Delegate to improve ergonomy of file renaming by pre-selecting the text
  // before the extension.
  class RenameItemDelegate : public QStyledItemDelegate
  {
  public:
    RenameItemDelegate (QObject *parent = nullptr)
        : QStyledItemDelegate{ parent }
    {
    }

    void setEditorData (QWidget *editor,
                        const QModelIndex &index) const override
    {
      QLineEdit *line_edit = qobject_cast<QLineEdit *> (editor);

      if (!line_edit)
        {
          QStyledItemDelegate::setEditorData (editor, index);
          return;
        }

      QString filename = index.data (Qt::EditRole).toString ();

      int select_len = filename.indexOf (QChar ('.'));
      if (select_len == -1)
        select_len = filename.size ();

      line_edit->setText (filename);

      // Qt calls QLineEdit::selectAll after this function is called, so to
      // actually restrict the selection, we have to post the modification at
      // the end of the event loop.
      // QTimer allows this easily with 0 as timeout.
      QTimer::singleShot (0, [=] () {
        line_edit->setSelection (0, select_len);
      });
    }
  };

  files_dock_widget::files_dock_widget (QWidget *p)
    : octave_dock_widget ("FilesDockWidget", p)
  {
    set_title (tr ("File Browser"));
    setToolTip (tr ("Browse your files"));

    m_sig_mapper = nullptr;

    m_columns_shown = QStringList ();
    m_columns_shown.append (tr ("File size"));
    m_columns_shown.append (tr ("File type"));
    m_columns_shown.append (tr ("Date modified"));
    m_columns_shown.append (tr ("Show hidden"));
    m_columns_shown.append (tr ("Alternating row colors"));

    m_columns_shown_keys = QStringList ();
    m_columns_shown_keys.append (fb_show_size.settings_key ());
    m_columns_shown_keys.append (fb_show_type.settings_key ());
    m_columns_shown_keys.append (fb_show_date.settings_key ());
    m_columns_shown_keys.append (fb_show_hidden.settings_key ());
    m_columns_shown_keys.append (fb_show_altcol.settings_key ());

    m_columns_shown_defs = QList <QVariant> ();
    m_columns_shown_defs.append (fb_show_size.def ());
    m_columns_shown_defs.append (fb_show_type.def ());
    m_columns_shown_defs.append (fb_show_date.def ());
    m_columns_shown_defs.append (fb_show_hidden.def ());
    m_columns_shown_defs.append (fb_show_altcol.def ());

    QWidget *container = new QWidget (this);

    setWidget (container);

    // Create a toolbar
    m_navigation_tool_bar = new QToolBar ("", container);
    m_navigation_tool_bar->setAllowedAreas (Qt::TopToolBarArea);
    m_navigation_tool_bar->setMovable (false);

    m_current_directory = new QComboBox (m_navigation_tool_bar);
    m_current_directory->setToolTip (tr ("Enter the path or filename"));
    m_current_directory->setEditable (true);
    m_current_directory->setMaxCount (MaxMRUDirs);
    m_current_directory->setInsertPolicy (QComboBox::NoInsert);
    m_current_directory->setSizeAdjustPolicy (QComboBox::AdjustToMinimumContentsLengthWithIcon);
    QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
    m_current_directory->setSizePolicy (sizePol);

    gui_settings settings;

    QAction *directory_up_action
      = new QAction (settings.icon ("folder-up", false, "go-up"), "",
                     m_navigation_tool_bar);
    directory_up_action->setToolTip (tr ("One directory up"));

    m_sync_browser_directory_action
      = new QAction (settings.icon ("go-first"), tr ("Show Octave directory"),
                     m_navigation_tool_bar);
    m_sync_browser_directory_action->setToolTip (tr ("Go to current Octave directory"));
    m_sync_browser_directory_action->setEnabled (false);

    m_sync_octave_directory_action
      = new QAction (settings.icon ("go-last"), tr ("Set Octave directory"),
                     m_navigation_tool_bar);
    m_sync_octave_directory_action->setToolTip (tr ("Set Octave directory to current browser directory"));
    m_sync_octave_directory_action->setEnabled (false);

    QToolButton *popdown_button = new QToolButton ();
    popdown_button->setToolTip (tr ("Actions on current directory"));
    QMenu *popdown_menu = new QMenu ();
    popdown_menu->addAction (settings.icon ("user-home"),
                             tr ("Show Home Directory"), this,
                             SLOT (popdownmenu_home (bool)));
    popdown_menu->addAction (m_sync_browser_directory_action);
    popdown_menu->addAction (m_sync_octave_directory_action);
    popdown_button->setMenu (popdown_menu);
    popdown_button->setPopupMode (QToolButton::InstantPopup);
    popdown_button->setDefaultAction
      (new QAction (settings.icon ("folder-settings", false,
                                   "applications-system"),
                    "", m_navigation_tool_bar));

    popdown_menu->addSeparator ();
    popdown_menu->addAction (settings.icon ("folder"),
                             tr ("Set Browser Directory..."),
                             this, &files_dock_widget::popdownmenu_search_dir);
    popdown_menu->addSeparator ();
    popdown_menu->addAction (settings.icon ("edit-find"),
                             tr ("Find Files..."),
                             this, &files_dock_widget::popdownmenu_findfiles);
    popdown_menu->addSeparator ();
    popdown_menu->addAction (settings.icon ("document-new"),
                             tr ("New File..."),
                             this, &files_dock_widget::popdownmenu_newfile);
    popdown_menu->addAction (settings.icon ("folder-new"),
                             tr ("New Directory..."),
                             this, &files_dock_widget::popdownmenu_newdir);

    m_navigation_tool_bar->addWidget (m_current_directory);
    m_navigation_tool_bar->addAction (directory_up_action);
    m_navigation_tool_bar->addWidget (popdown_button);

    connect (directory_up_action, &QAction::triggered,
             this, &files_dock_widget::change_directory_up);
    connect (m_sync_octave_directory_action, &QAction::triggered,
             this, &files_dock_widget::do_sync_octave_directory);
    connect (m_sync_browser_directory_action, &QAction::triggered,
             this, &files_dock_widget::do_sync_browser_directory);

    // Create the QFileSystemModel starting in the desired directory
    QDir startup_dir;  // take current dir

    if (settings.bool_value (fb_restore_last_dir))
      {
        // restore last dir from previous session
        QStringList last_dirs
          = settings.value (fb_mru_list.settings_key ()).toStringList ();
        if (last_dirs.length () > 0)
          startup_dir = QDir (last_dirs.at (0));  // last dir in previous session
      }
    else if (! settings.string_value (fb_startup_dir).isEmpty ())
      {
        // do not restore but there is a startup dir configured
        startup_dir = QDir (settings.value (fb_startup_dir.settings_key ()).toString ());
      }

    if (! startup_dir.exists ())
      {
        // the configured startup dir does not exist, take actual one
        startup_dir = QDir ();
      }

    m_file_system_model = new file_system_model (this);
    m_file_system_model->setResolveSymlinks (false);
    m_file_system_model->setFilter
      (QDir::System | QDir::NoDotAndDotDot | QDir::AllEntries);
    QModelIndex rootPathIndex
      = m_file_system_model->setRootPath (startup_dir.absolutePath ());

    // Attach the model to the QTreeView and set the root index
    m_file_tree_view = new FileTreeViewer (container);
    m_file_tree_view->setSelectionMode (QAbstractItemView::ExtendedSelection);
    m_file_tree_view->setModel (m_file_system_model);
    m_file_tree_view->setRootIndex (rootPathIndex);
    m_file_tree_view->setSortingEnabled (true);
    m_file_tree_view->setAlternatingRowColors (true);
    m_file_tree_view->setAnimated (true);
    m_file_tree_view->setToolTip (tr ("Double-click to open file/folder, right click for alternatives"));

    // allow renaming directly in the tree view with
    // m_file_tree_view->edit(index)
    m_file_system_model->setReadOnly (false);
    // delegate to improve rename ergonomy by pre-selecting text up to the
    // extension
    auto *rename_delegate = new RenameItemDelegate (this);
    m_file_tree_view->setItemDelegateForColumn (0, rename_delegate);
    // prevent the tree view to override Octave's double-click behavior
    m_file_tree_view->setEditTriggers (QAbstractItemView::NoEditTriggers);
    // create the rename action (that will be added to context menu)
    // and associate to F2 key shortcut
    m_rename_action = new QAction (tr ("Rename..."), this);
    m_rename_action->setShortcut (Qt::Key_F2);
    m_rename_action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
    connect (m_rename_action, &QAction::triggered, this,
             &files_dock_widget::contextmenu_rename);
    addAction(m_rename_action);

    // get sort column and order as well as column state (order and width)

    m_file_tree_view->sortByColumn
      (settings.int_value (fb_sort_column),
       static_cast<Qt::SortOrder> (settings.uint_value (fb_sort_order)));
       // FIXME: use value<Qt::SortOrder> instead of static cast after
       //        dropping support of Qt 5.4

    if (settings.contains (fb_column_state.settings_key ()))
      m_file_tree_view->header ()->restoreState
        (settings.value (fb_column_state.settings_key ()).toByteArray ());

    // Set header properties for sorting
    m_file_tree_view->header ()->setSectionsClickable (true);
    m_file_tree_view->header ()->setSectionsMovable (true);
    m_file_tree_view->header ()->setSortIndicatorShown (true);

    QStringList mru_dirs =
      settings.value (fb_mru_list.settings_key ()).toStringList ();
    m_current_directory->addItems (mru_dirs);

    m_current_directory->setEditText
      (m_file_system_model->fileInfo (rootPathIndex). absoluteFilePath ());

    connect (m_file_tree_view, &FileTreeViewer::activated,
             this, &files_dock_widget::item_double_clicked);

    // add context menu to tree_view
    m_file_tree_view->setContextMenuPolicy (Qt::CustomContextMenu);
    connect (m_file_tree_view, &FileTreeViewer::customContextMenuRequested,
             this, &files_dock_widget::contextmenu_requested);

    m_file_tree_view->header ()->setContextMenuPolicy (Qt::CustomContextMenu);
    connect (m_file_tree_view->header (),
             &QHeaderView::customContextMenuRequested,
             this, &files_dock_widget::headercontextmenu_requested);

    // Layout the widgets vertically with the toolbar on top
    QVBoxLayout *vbox_layout = new QVBoxLayout ();
    vbox_layout->setSpacing (0);
    vbox_layout->addWidget (m_navigation_tool_bar);
    vbox_layout->addWidget (m_file_tree_view);
    vbox_layout->setMargin (1);

    container->setLayout (vbox_layout);

    // FIXME: Add right-click contextual menus for copying, pasting,
    //        deleting files (and others).

    connect (m_current_directory->lineEdit (), &QLineEdit::returnPressed,
             this, &files_dock_widget::accept_directory_line_edit);

    // FIXME: We could use
    //
    //    connect (m_current_directory,
    //             QOverload<const QString&>::of (&QComboBox::activated),
    //             this, &files_dock_widget::set_current_directory);
    //
    // but referring to QComboBox::activated will generate deprecated
    // function warnings from GCC.  We could also use
    //
    //    connect (m_current_directory, &QComboBox::textActivated,
    //             this, &files_dock_widget::set_current_directory);
    //
    // but the function textActivated was not introduced until Qt 5.14
    // so we'll need a feature test.

    connect (m_current_directory, SIGNAL (activated (const QString&)),
             this, SLOT (set_current_directory (const QString&)));

    QCompleter *completer = new QCompleter (m_file_system_model, this);
    m_current_directory->setCompleter (completer);

    setFocusProxy (m_current_directory);

    m_sync_octave_dir = true;   // default, overwritten with notice_settings ()
    m_octave_dir = "";

    if (! p)
      make_window ();
  }

  void files_dock_widget::save_settings ()
  {
    gui_settings settings;

    int sort_column = m_file_tree_view->header ()->sortIndicatorSection ();
    Qt::SortOrder sort_order = m_file_tree_view->header ()->sortIndicatorOrder ();
    settings.setValue (fb_sort_column.settings_key (), sort_column);
    settings.setValue (fb_sort_order.settings_key (), sort_order);
    settings.setValue (fb_column_state.settings_key (),
                       m_file_tree_view->header ()->saveState ());

    QStringList dirs;
    for (int i=0; i< m_current_directory->count (); i++)
      {
        dirs.append (m_current_directory->itemText (i));
      }
    settings.setValue (fb_mru_list.settings_key (), dirs);

    settings.sync ();

    octave_dock_widget::save_settings ();

    if (m_sig_mapper)
      delete m_sig_mapper;
  }

  void files_dock_widget::item_double_clicked (const QModelIndex& index)
  {
    // Retrieve the file info associated with the model index.
    QFileInfo fileInfo = m_file_system_model->fileInfo (index);
    set_current_directory (fileInfo.absoluteFilePath ());
  }

  void files_dock_widget::set_current_directory (const QString& dir)
  {
    display_directory (dir);
  }

  void files_dock_widget::accept_directory_line_edit ()
  {
    display_directory (m_current_directory->currentText ());
  }

  void files_dock_widget::change_directory_up ()
  {
    QDir dir
      = QDir (m_file_system_model->filePath (m_file_tree_view->rootIndex ()));

    dir.cdUp ();
    display_directory (dir.absolutePath ());
  }

  void files_dock_widget::do_sync_octave_directory ()
  {
    QDir dir
      = QDir (m_file_system_model->filePath (m_file_tree_view->rootIndex ()));

    emit displayed_directory_changed (dir.absolutePath ());
  }

  void files_dock_widget::do_sync_browser_directory ()
  {
    display_directory (m_octave_dir, false); // false: no sync of octave dir
  }

  void files_dock_widget::update_octave_directory (const QString& dir)
  {
    m_octave_dir = dir;
    if (m_sync_octave_dir)
      display_directory (m_octave_dir, false); // false: no sync of octave dir
  }

  void files_dock_widget::display_directory (const QString& dir,
                                             bool set_octave_dir)
  {
    QFileInfo fileInfo (dir);
    if (fileInfo.exists ())
      {
        if (fileInfo.isDir ())
          {
            m_file_tree_view->setRootIndex (m_file_system_model->
                                            index (fileInfo.absoluteFilePath ()));
            m_file_system_model->setRootPath (fileInfo.absoluteFilePath ());
            if (m_sync_octave_dir && set_octave_dir)
              process_set_current_dir (fileInfo.absoluteFilePath ());

            // see if it's in the list, and if it is,
            // remove it and then put at top of the list
            int index
              = m_current_directory->findText (fileInfo.absoluteFilePath ());
            if (index != -1)
              {
                m_current_directory->removeItem (index);
              }
            m_current_directory->insertItem (0, fileInfo.absoluteFilePath ());
            m_current_directory->setCurrentIndex (0);
          }
        else
          {
            QString abs_fname = fileInfo.absoluteFilePath ();

            QString suffix = fileInfo.suffix ().toLower ();

            gui_settings settings;

            QString ext = settings.string_value (fb_txt_file_ext);
#if defined (HAVE_QT_SPLITBEHAVIOR_ENUM)
            QStringList extensions = ext.split (";", Qt::SkipEmptyParts);
#else
            QStringList extensions = ext.split (";", QString::SkipEmptyParts);
#endif
            if (QFile::exists (abs_fname))
              {
                if (extensions.contains (suffix))
                  emit open_file (fileInfo.absoluteFilePath ());
                else
                  emit open_any_signal (abs_fname);
              }
          }
      }
  }

  void files_dock_widget::open_item_in_app (const QModelIndex& index)
  {
    // Retrieve the file info associated with the model index.
    QFileInfo fileInfo = m_file_system_model->fileInfo (index);

    QString file = fileInfo.absoluteFilePath ();

    QDesktopServices::openUrl (QUrl::fromLocalFile (file));
  }

  void files_dock_widget::toggle_header (int col)
  {
    gui_settings settings;

    QString key = m_columns_shown_keys.at (col);
    bool shown = settings.value (key, false).toBool ();
    settings.setValue (key, ! shown);
    settings.sync ();

    switch (col)
      {
      case 0:
      case 1:
      case 2:
        // toggle column visibility
        m_file_tree_view->setColumnHidden (col + 1, shown);
        break;
      case 3:
      case 4:
        // other actions depending on new settings
        notice_settings ();
        break;
      }
  }

  void files_dock_widget::headercontextmenu_requested (const QPoint& mpos)
  {
    QMenu menu (this);

    if (m_sig_mapper)
      delete m_sig_mapper;
    m_sig_mapper = new QSignalMapper (this);

    gui_settings settings;

    for (int i = 0; i < m_columns_shown.size (); i++)
      {
        QAction *action = menu.addAction (m_columns_shown.at (i),
                                          m_sig_mapper, SLOT (map ()));
        m_sig_mapper->setMapping (action, i);
        action->setCheckable (true);
        action->setChecked
          (settings.value (m_columns_shown_keys.at (i),
                           m_columns_shown_defs.at (i)).toBool ());
      }

    // FIXME: We could use
    //
    //   connect (&m_sig_mapper, QOverload<int>::of (&QSignalMapper::mapped),
    //            this, &workspace_view::toggle_header);
    //
    // but referring to QSignalMapper::mapped will generate deprecated
    // function warnings from GCC.  We could also use
    //
    //   connect (&m_sig_mapper, &QSignalMapper::mappedInt,
    //            this, &workspace_view::toggle_header);
    //
    // but the function mappedInt was not introduced until Qt 5.15 so
    // we'll need a feature test.

    connect (m_sig_mapper, SIGNAL (mapped (int)),
             this, SLOT (toggle_header (int)));

    menu.exec (m_file_tree_view->mapToGlobal (mpos));
  }

  void files_dock_widget::contextmenu_requested (const QPoint& mpos)
  {

    QMenu menu (this);

    QModelIndex index = m_file_tree_view->indexAt (mpos);

    if (index.isValid ())
      {
        QFileInfo info = m_file_system_model->fileInfo (index);

        QItemSelectionModel *m = m_file_tree_view->selectionModel ();
        QModelIndexList sel = m->selectedRows ();

        // check if item at mouse position is seleccted
        if (! sel.contains (index))
          {
            // is not selected -> clear actual selection and select this item
            m->setCurrentIndex (index,
                                QItemSelectionModel::Clear
                                | QItemSelectionModel::Select
                                | QItemSelectionModel::Rows);
          }

        gui_settings settings;

        // construct the context menu depending on item
        menu.addAction (settings.icon ("document-open"), tr ("Open"),
                        this, &files_dock_widget::contextmenu_open);

        if (info.isDir ())
          {
            menu.addAction (tr ("Open in System File Explorer"),
                            this, &files_dock_widget::contextmenu_open_in_app);
          }

        if (info.isFile ())
          menu.addAction (tr ("Open in Text Editor"),
                          this, &files_dock_widget::contextmenu_open_in_editor);

        menu.addAction (tr ("Copy Selection to Clipboard"),
                        this, &files_dock_widget::contextmenu_copy_selection);

        if (info.isFile () && info.suffix () == "m")
          menu.addAction (settings.icon ("media-playback-start"), tr ("Run"),
                          this, &files_dock_widget::contextmenu_run);

        if (info.isFile ())
          menu.addAction (tr ("Load Data"),
                          this, &files_dock_widget::contextmenu_load);

        if (info.isDir ())
          {
            menu.addSeparator ();
            menu.addAction (settings.icon ("go-first"), tr ("Set Current Directory"),
                            this, &files_dock_widget::contextmenu_setcurrentdir);

            QMenu *add_path_menu = menu.addMenu (tr ("Add to Path"));

            add_path_menu->addAction (tr ("Selected Directories"),
                                      this, [=] (bool checked) { contextmenu_add_to_path (checked); });
            add_path_menu->addAction (tr ("Selected Directories and Subdirectories"),
                                      this, &files_dock_widget::contextmenu_add_to_path_subdirs);

            QMenu *rm_path_menu = menu.addMenu (tr ("Remove from Path"));

            rm_path_menu->addAction (tr ("Selected Directories"),
                                     this, &files_dock_widget::contextmenu_rm_from_path);
            rm_path_menu->addAction (tr ("Selected Directories and Subdirectories"),
                                     this, &files_dock_widget::contextmenu_rm_from_path_subdirs);

            menu.addSeparator ();

            menu.addAction (settings.icon ("edit-find"), tr ("Find Files..."),
                            this, &files_dock_widget::contextmenu_findfiles);
          }

        menu.addSeparator ();
        menu.addAction (m_rename_action);
        menu.addAction (settings.icon ("edit-delete"), tr ("Delete..."),
                        this, &files_dock_widget::contextmenu_delete);

        if (info.isDir ())
          {
            menu.addSeparator ();
            menu.addAction (settings.icon ("document-new"), tr ("New File..."),
                            this, &files_dock_widget::contextmenu_newfile);
            menu.addAction (settings.icon ("folder-new"), tr ("New Directory..."),
                            this, &files_dock_widget::contextmenu_newdir);
          }

        // show the menu
        menu.exec (m_file_tree_view->mapToGlobal (mpos));

      }
  }

  void files_dock_widget::contextmenu_open (bool)
  {

    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    for (auto it = rows.begin (); it != rows.end (); it++)
      {
        QFileInfo file = m_file_system_model->fileInfo (*it);
        if (file.exists ())
          display_directory (file.absoluteFilePath ());
      }
  }

  void files_dock_widget::contextmenu_open_in_editor (bool)
  {

    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    for (auto it = rows.begin (); it != rows.end (); it++)
      {
        QFileInfo file = m_file_system_model->fileInfo (*it);
        if (file.exists ())
          emit open_file (file.absoluteFilePath ());
      }
  }

  void files_dock_widget::contextmenu_open_in_app (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    for (auto it = rows.begin (); it != rows.end (); it++)
      open_item_in_app (*it);
  }

  void files_dock_widget::contextmenu_copy_selection (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    QStringList selection;

    for (auto it = rows.begin (); it != rows.end (); it++)
      {
        QFileInfo info = m_file_system_model->fileInfo (*it);

        selection << info.fileName ();
      }

    QClipboard *clipboard = QApplication::clipboard ();

    clipboard->setText (selection.join ("\n"));
  }

  void files_dock_widget::contextmenu_load (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    if (rows.size () > 0)
      {
        QModelIndex index = rows[0];

        QFileInfo info = m_file_system_model->fileInfo (index);

        emit load_file_signal (info.fileName ());
      }
  }

  void files_dock_widget::contextmenu_run (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    if (rows.size () > 0)
      {
        QModelIndex index = rows[0];

        QFileInfo info = m_file_system_model->fileInfo (index);
        emit run_file_signal (info);
      }
  }

  void files_dock_widget::contextmenu_rename (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();
    if (rows.size () > 0)
      {
        QModelIndex index = rows[0];
        m_file_tree_view->edit(index);
      }
  }

  void files_dock_widget::contextmenu_delete (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    int file_cnt = rows.size ();
    bool multiple_files = (file_cnt > 1);

    for (auto it = rows.begin (); it != rows.end (); it++)
      {
        QModelIndex index = *it;

        QFileInfo info = m_file_system_model->fileInfo (index);

        QMessageBox::StandardButton dlg_answer;
        if (multiple_files)
          if (it == rows.begin ())
            {
              dlg_answer = QMessageBox::question (this,
                                                  tr ("Delete file/directory"),
                                                  tr ("Are you sure you want to delete all %1 selected files?\n").arg (file_cnt),
                                                  QMessageBox::Yes | QMessageBox::No);
              if (dlg_answer != QMessageBox::Yes)
                return;
            }
          else
            dlg_answer = QMessageBox::Yes;
        else
          {
            dlg_answer = QMessageBox::question (this,
                           tr ("Delete file/directory"),
                           tr ("Are you sure you want to delete\n")
                           + info.filePath (),
                           QMessageBox::Yes | QMessageBox::No);
          }

        if (dlg_answer == QMessageBox::Yes)
          {
            if (info.isDir ())
              {
                // see if directory is empty
                QDir path (info.absoluteFilePath ());
                QList<QFileInfo> fileLst
                  = path.entryInfoList (QDir::Hidden | QDir::AllEntries
                                        | QDir::NoDotAndDotDot | QDir::System);

                if (fileLst.count () != 0)
                  QMessageBox::warning (this, tr ("Delete file/directory"),
                                        tr ("Can not delete a directory that is not empty"));
                else
                  m_file_system_model->rmdir (index);
              }
            else
              {
                // Close the file in the editor if open
                emit file_remove_signal (info.filePath (), QString ());
                // Remove the file.
                bool st = m_file_system_model->remove (index);
                if (! st)
                  {
                    QMessageBox::warning (this, tr ("Deletion error"),
                                          tr ("Could not delete file \"%1\".").
                                          arg (info.filePath ()));
                    // Reload the old file
                  }
                emit file_renamed_signal (st);
              }

            m_file_system_model->revert ();

          }
      }
  }

  // Get the currently selected files/dirs and return their file info
  // in a list.
  QList<QFileInfo> files_dock_widget::get_selected_items_info (bool dir)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    QList<QFileInfo> infos;

    for (auto it = rows.begin (); it != rows.end (); it++)
      {
        QModelIndex index = *it;

        QFileInfo info = m_file_system_model->fileInfo (index);

        if (info.exists () &&
            ((dir & info.isDir ()) || (! dir && info.isFile ())))
          infos.append (info);
      }

    return infos;
  }

  void files_dock_widget::contextmenu_newfile (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    if (rows.size () > 0)
      {
        QModelIndex index = rows[0];

        QFileInfo info = m_file_system_model->fileInfo (index);
        QString parent_dir = info.filePath ();

        process_new_file (parent_dir);
      }
  }

  void files_dock_widget::contextmenu_newdir (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    if (rows.size () > 0)
      {
        QModelIndex index = rows[0];

        QFileInfo info = m_file_system_model->fileInfo (index);
        QString parent_dir = info.filePath ();

        process_new_dir (parent_dir);
      }
  }

  void files_dock_widget::contextmenu_setcurrentdir (bool)
  {
    QList<QFileInfo> infos = get_selected_items_info (true);

    if (infos.length () > 0 && infos.first ().isDir ())
      process_set_current_dir (infos.first ().absoluteFilePath ());
  }

  void files_dock_widget::contextmenu_add_to_path (bool, bool rm, bool subdirs)
  {
    QList<QFileInfo> infos = get_selected_items_info (true);

    QStringList dir_list;

    for (int i = 0; i < infos.length (); i++)
      dir_list.append (infos.at (i).absoluteFilePath ());

    if (infos.length () > 0)
      emit modify_path_signal (dir_list, rm, subdirs);
  }

  void files_dock_widget::contextmenu_add_to_path_subdirs (bool)
  {
    contextmenu_add_to_path (true, false, true);
  }

  void files_dock_widget::contextmenu_rm_from_path (bool)
  {
    contextmenu_add_to_path (true, true, false);
  }

  void files_dock_widget::contextmenu_rm_from_path_subdirs (bool)
  {
    contextmenu_add_to_path (true, true, true);
  }

  void files_dock_widget::contextmenu_findfiles (bool)
  {
    QItemSelectionModel *m = m_file_tree_view->selectionModel ();
    QModelIndexList rows = m->selectedRows ();

    if (rows.size () > 0)
      {
        QModelIndex index = rows[0];

        QFileInfo info = m_file_system_model->fileInfo (index);

        if (info.isDir ())
          {
            process_find_files (info.absoluteFilePath ());
          }
      }
  }

  void files_dock_widget::notice_settings ()
  {
    gui_settings settings;

    // QSettings pointer is checked before emitting.

    int size_idx = settings.int_value (global_icon_size);
    size_idx = (size_idx > 0) - (size_idx < 0) + 1;  // Make valid index from 0 to 2

    QStyle *st = style ();
    int icon_size = st->pixelMetric (global_icon_sizes[size_idx]);
    m_navigation_tool_bar->setIconSize (QSize (icon_size, icon_size));

    // filenames are always shown, other columns can be hidden by settings
    for (int i = 0; i < 3; i++)
      m_file_tree_view->setColumnHidden (i + 1,
                                         ! settings.value (m_columns_shown_keys.at (i),false).toBool ());

    QDir::Filters current_filter = m_file_system_model->filter ();
    if (settings.value (m_columns_shown_keys.at (3), false).toBool ())
      m_file_system_model->setFilter (current_filter | QDir::Hidden);
    else
      m_file_system_model->setFilter (current_filter & (~QDir::Hidden));

    m_file_tree_view->setAlternatingRowColors
      (settings.value (m_columns_shown_keys.at (4),true).toBool ());
    m_file_tree_view->setModel (m_file_system_model);

    // enable the buttons to sync octave/browser dir
    // only if this is not done by default
    m_sync_octave_dir
      = settings.bool_value (fb_sync_octdir);
    m_sync_octave_directory_action->setEnabled (! m_sync_octave_dir);
    m_sync_browser_directory_action->setEnabled (! m_sync_octave_dir);

    // If m_sync_octave_dir is enabled, then we want the file browser to
    // update to match the current working directory of the
    // interpreter.  We don't want to queue any signal to change the
    // interpreter's current working directory.  In this case, we just
    // want the GUI to match the state of the interpreter.

    if (m_sync_octave_dir)
      do_sync_browser_directory ();
  }

  void files_dock_widget::popdownmenu_home (bool)
  {
    QString dir = QString::fromStdString (sys::env::get_home_directory ());

    if (dir.isEmpty ())
      dir = QDir::homePath ();

    set_current_directory (dir);
  }

  void files_dock_widget::popdownmenu_search_dir (bool)
  {
    // FIXME: Remove, if for all common KDE versions (bug #54607) is resolved.
    int opts = QFileDialog::ShowDirsOnly;

    gui_settings settings;

    if (! settings.bool_value (global_use_native_dialogs))
      opts |= QFileDialog::DontUseNativeDialog;

    QString dir = QFileDialog::getExistingDirectory (this,
                     tr ("Set directory of file browser"),
                     m_file_system_model->rootPath (),
                     QFileDialog::Option (opts));
    set_current_directory (dir);
  }

  void files_dock_widget::popdownmenu_findfiles (bool)
  {
    process_find_files (m_file_system_model->rootPath ());
  }

  void files_dock_widget::popdownmenu_newdir (bool)
  {
    process_new_dir (m_file_system_model->rootPath ());
  }

  void files_dock_widget::popdownmenu_newfile (bool)
  {
    process_new_file (m_file_system_model->rootPath ());
  }

  void files_dock_widget::process_new_file (const QString& parent_dir)
  {
    bool ok;

    QString name = QInputDialog::getText (this, tr ("Create File"),
                                          tr ("Create file in\n", "String ends with \\n!") + parent_dir,
                                          QLineEdit::Normal,
                                          tr ("New File.txt"), &ok);
    if (ok && name.length () > 0)
      {
        name = parent_dir + '/' + name;

        QFile file (name);
        file.open (QIODevice::WriteOnly);
        m_file_system_model->revert ();
      }
  }

  void files_dock_widget::process_new_dir (const QString& parent_dir)
  {
    bool ok;

    QString name = QInputDialog::getText (this, tr ("Create Directory"),
                                          tr ("Create folder in\n", "String ends with \\n!") + parent_dir,
                                          QLineEdit::Normal,
                                          tr ("New Directory"), &ok);
    if (ok && name.length () > 0)
      {
        QDir dir (parent_dir);
        dir.mkdir (name);
        m_file_system_model->revert ();
      }
  }

  void files_dock_widget::process_set_current_dir (const QString& dir)
  {
    emit displayed_directory_changed (dir);
  }

  void files_dock_widget::process_find_files (const QString& dir)
  {
    emit find_files_signal (dir);
  }

  void files_dock_widget::copyClipboard ()
  {
    if (m_file_tree_view->hasFocus ())
      contextmenu_copy_selection (true);
    if (m_current_directory->hasFocus ())
      {
        QClipboard *clipboard = QApplication::clipboard ();

        QLineEdit *edit = m_current_directory->lineEdit ();
        if (edit && edit->hasSelectedText ())
          {
            clipboard->setText (edit->selectedText ());
          }
      }
  }

  void files_dock_widget::pasteClipboard ()
  {
    if (m_current_directory->hasFocus ())
      {
        QClipboard *clipboard = QApplication::clipboard ();
        QString str = clipboard->text ();
        QLineEdit *edit = m_current_directory->lineEdit ();
        if (edit && str.length () > 0)
          edit->insert (str);
      }
  }

  void files_dock_widget::selectAll ()
  {
    if (m_file_tree_view->hasFocus ())
      m_file_tree_view->selectAll ();
    if (m_current_directory->hasFocus ())
      {
        QLineEdit *edit = m_current_directory->lineEdit ();
        if (edit)
          {
            edit->selectAll ();
          }
      }
  }

OCTAVE_END_NAMESPACE(octave)