changeset 16450:3207f1d62e74

improve encapsulation of file browser window object * files-dockwidget.h, files-dockwidget.cc (files_dock_widget::files_dock_widget): Use new local variable container for clarity. Set status tip. (files_dock_widget::connect_visibility_changed, files_dock_widget::focus, files_dock_widget::handle_visibility): New functions. * main-window.h, main-window.cc (main_window::file_browser_window): Rename from _files_dock_widget. Change all uses. (main_window::main_window): Initialize it here. (main_window::focus_current_directory, main_window::handle_current_directory_visible): Delete. (main_window::connect_visibility_changed): Call file_browser_window->connect_visibility_changed. (main_window::construct): Don't create _files_dock_widget here. Connect file_browser_action::triggered to file_browser_window::focus instead of main_window::focus_current_directory.
author John W. Eaton <jwe@octave.org>
date Sat, 06 Apr 2013 19:08:14 -0400
parents c129a8b73d25
children d4c3736e1e28
files libgui/src/files-dockwidget.cc libgui/src/files-dockwidget.h libgui/src/main-window.cc libgui/src/main-window.h
diffstat 4 files changed, 65 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/files-dockwidget.cc	Sat Apr 06 17:46:12 2013 -0400
+++ b/libgui/src/files-dockwidget.cc	Sat Apr 06 19:08:14 2013 -0400
@@ -41,10 +41,14 @@
   setObjectName ("FilesDockWidget");
   setWindowIcon (QIcon(":/actions/icons/logo.png"));
   setWindowTitle (tr ("File Browser"));
-  setWidget (new QWidget (this));
+  setStatusTip (tr ("Browse your files."));
+
+  QWidget *container = new QWidget (this);
+
+  setWidget (container);
 
   // Create a toolbar
-  _navigation_tool_bar = new QToolBar ("", widget ());
+  _navigation_tool_bar = new QToolBar ("", container);
   _navigation_tool_bar->setAllowedAreas (Qt::TopToolBarArea);
   _navigation_tool_bar->setMovable (false);
   _navigation_tool_bar->setIconSize (QSize (20, 20));
@@ -71,7 +75,7 @@
   QModelIndex rootPathIndex = _file_system_model->setRootPath (homePath);
 
   // Attach the model to the QTreeView and set the root index
-  _file_tree_view = new QTreeView (widget ());
+  _file_tree_view = new QTreeView (container);
   _file_tree_view->setModel (_file_system_model);
   _file_tree_view->setRootIndex (rootPathIndex);
   _file_tree_view->setSortingEnabled (true);
@@ -91,8 +95,8 @@
   _current_directory->setText(_file_system_model->fileInfo (rootPathIndex).
                               absoluteFilePath ());
 
-  connect (_file_tree_view, SIGNAL (doubleClicked (const QModelIndex &)), this,
-           SLOT (item_double_clicked (const QModelIndex &)));
+  connect (_file_tree_view, SIGNAL (doubleClicked (const QModelIndex &)),
+           this, SLOT (item_double_clicked (const QModelIndex &)));
 
   // Layout the widgets vertically with the toolbar on top
   QVBoxLayout *vbox_layout = new QVBoxLayout ();
@@ -100,14 +104,15 @@
   vbox_layout->addWidget (_navigation_tool_bar);
   vbox_layout->addWidget (_file_tree_view);
   vbox_layout->setMargin (1);
-  widget ()->setLayout (vbox_layout);
+
+  container->setLayout (vbox_layout);
+
   // TODO: Add right-click contextual menus for copying, pasting, deleting files (and others)
 
   connect (_current_directory, SIGNAL (returnPressed ()),
            this, SLOT (accept_directory_line_edit ()));
 
-  QCompleter *
-    completer = new QCompleter (_file_system_model, this);
+  QCompleter *completer = new QCompleter (_file_system_model, this);
   _current_directory->setCompleter (completer);
 
   setFocusProxy (_current_directory);
@@ -125,6 +130,13 @@
 }
 
 void
+files_dock_widget::connect_visibility_changed (void)
+{
+  connect (this, SIGNAL (visibilityChanged (bool)),
+           this, SLOT (handle_visibility (bool)));
+}
+
+void
 files_dock_widget::item_double_clicked (const QModelIndex& index)
 {
   // Retrieve the file info associated with the model index.
@@ -193,3 +205,22 @@
       // TODO: React on option for hidden files.
     }
 }
+
+void
+files_dock_widget::focus (void)
+{
+  if (! isVisible ())
+    setVisible (true);
+
+  setFocus ();
+  activateWindow ();
+  raise ();
+}
+
+void
+files_dock_widget::handle_visibility (bool visible)
+{
+  if (visible && ! isFloating ())
+    focus ();
+}
+
--- a/libgui/src/files-dockwidget.h	Sat Apr 06 17:46:12 2013 -0400
+++ b/libgui/src/files-dockwidget.h	Sat Apr 06 19:08:14 2013 -0400
@@ -46,11 +46,15 @@
 class files_dock_widget : public octave_dock_widget
 {
   Q_OBJECT
-  public:
-  /** Constructs a new files_dock_widget. */
+
+public:
+
   files_dock_widget (QWidget *parent = 0);
+
   ~files_dock_widget ();
 
+  void connect_visibility_changed (void);
+
 public slots:
 
   /** Slot for handling a change in directory via double click. */
@@ -70,6 +74,10 @@
   /** Tells the widget to react on changed settings. */
   void notice_settings (const QSettings *settings);
 
+  void focus (void);
+
+  void handle_visibility (bool visible);
+
 signals:
   /** Emitted, whenever the user requested to open a file. */
   void open_file (const QString& fileName);
--- a/libgui/src/main-window.cc	Sat Apr 06 17:46:12 2013 -0400
+++ b/libgui/src/main-window.cc	Sat Apr 06 19:08:14 2013 -0400
@@ -57,7 +57,8 @@
 
 main_window::main_window (QWidget *p)
   : QMainWindow (p), _workspace_model (), status_bar (),
-    command_window (this), history_window (this)
+    command_window (this), history_window (this),
+    file_browser_window (new files_dock_widget (this))
 {
   // We have to set up all our windows, before we finally launch octave.
   construct ();
@@ -65,6 +66,8 @@
 
 main_window::~main_window ()
 {
+  delete file_browser_window;
+
   // Clean up all dynamically created objects to ensure they are
   // deleted before this main_window is.  Otherwise, some will be
   // attached to a non-existent parent.
@@ -83,9 +86,6 @@
   if (_documentation_dock_widget)
     delete _documentation_dock_widget;
 
-  if (_files_dock_widget)
-    delete _files_dock_widget;
-
   delete _workspace_view;
 }
 
@@ -290,7 +290,7 @@
   _current_directory_combo_box->insertItem (0, dir);
   _current_directory_combo_box->setCurrentIndex (0);
 
-  _files_dock_widget->display_directory (dir);
+  file_browser_window->display_directory (dir);
 }
 
 void
@@ -347,19 +347,6 @@
 }
 
 void
-main_window::focus_current_directory ()
-{
-  if (!_files_dock_widget->isVisible ())
-    {
-      _files_dock_widget->setVisible (true);
-    }
-
-  _files_dock_widget->setFocus ();
-  _files_dock_widget->activateWindow ();
-  _files_dock_widget->raise ();
-}
-
-void
 main_window::focus_workspace ()
 {
   if (!_workspace_view->isVisible ())
@@ -396,14 +383,6 @@
 }
 
 void
-main_window::handle_current_directory_visible (bool visible)
-{
-  // if changed to visible and widget is not floating
-  if (visible && !_files_dock_widget->isFloating ())
-    focus_current_directory ();
-}
-
-void
 main_window::handle_workspace_visible (bool visible)
 {
   // if changed to visible and widget is not floating
@@ -593,15 +572,16 @@
 {
   command_window.connect_visibility_changed ();
   history_window.connect_visibility_changed ();
+  file_browser_window->connect_visibility_changed ();
 
   connect (_workspace_view,       SIGNAL (visibilityChanged (bool)),
            this,                  SLOT (handle_workspace_visible (bool)));
-  connect (_files_dock_widget,    SIGNAL (visibilityChanged (bool)),
-           this,                  SLOT (handle_current_directory_visible (bool)));
+
 #ifdef HAVE_QSCINTILLA
   connect (_file_editor,          SIGNAL (visibilityChanged (bool)),
            this,                  SLOT (handle_editor_visible (bool)));
 #endif
+
   connect (_documentation_dock_widget,  SIGNAL (visibilityChanged (bool)),
            this,                  SLOT (handle_documentation_visible (bool)));
 }
@@ -623,8 +603,6 @@
   connect (&_workspace_model, SIGNAL (model_changed ()),
            _workspace_view, SLOT (model_changed ()));
 
-  _files_dock_widget        = new files_dock_widget (this);
-  _files_dock_widget->setStatusTip (tr ("Browse your files."));
   _documentation_dock_widget= new documentation_dock_widget (this);
   _documentation_dock_widget->setStatusTip (tr ("See the documentation for help."));
 
@@ -989,8 +967,8 @@
   connect (&history_window,             SIGNAL (active_changed (bool)),
            show_history_action,         SLOT   (setChecked (bool)));
   connect (show_file_browser_action,    SIGNAL (toggled (bool)),
-           _files_dock_widget,          SLOT   (setVisible (bool)));
-  connect (_files_dock_widget,          SIGNAL (active_changed (bool)),
+           file_browser_window,         SLOT   (setVisible (bool)));
+  connect (file_browser_window,         SIGNAL (active_changed (bool)),
            show_file_browser_action,    SLOT   (setChecked (bool)));
 #ifdef HAVE_QSCINTILLA
   connect (show_editor_action,          SIGNAL (toggled (bool)),
@@ -1011,7 +989,7 @@
   connect (history_action,              SIGNAL (triggered ()),
            &history_window,             SLOT (focus ()));
   connect (file_browser_action,         SIGNAL (triggered ()),
-           this,                        SLOT (focus_current_directory ()));
+           file_browser_window,         SLOT (focus ()));
   connect (editor_action,               SIGNAL (triggered ()),
            this,                        SLOT (focus_editor ()));
   connect (documentation_action,        SIGNAL (triggered ()),
@@ -1028,12 +1006,12 @@
   connect (this,                        SIGNAL (settings_changed (const QSettings *)),
            &command_window,             SLOT   (notice_settings (const QSettings *)));
   connect (this,                        SIGNAL (settings_changed (const QSettings *)),
-           _files_dock_widget,          SLOT   (notice_settings (const QSettings *)));
+           file_browser_window,         SLOT   (notice_settings (const QSettings *)));
   connect (this,                        SIGNAL (settings_changed (const QSettings *)),
            this,                        SLOT   (notice_settings (const QSettings *)));
-  connect (_files_dock_widget,          SIGNAL (open_file (QString)),
+  connect (file_browser_window,         SIGNAL (open_file (QString)),
            this,                        SLOT   (open_file (QString)));
-  connect (_files_dock_widget,          SIGNAL (displayed_directory_changed(QString)),
+  connect (file_browser_window,         SIGNAL (displayed_directory_changed(QString)),
            this,                        SLOT   (set_current_working_directory(QString)));
   connect (this,                        SIGNAL (relay_command_signal (const QString&)),
            &command_window,             SLOT   (relay_command (const QString&)));
@@ -1078,7 +1056,7 @@
   addDockWidget (Qt::RightDockWidgetArea, _file_editor);
   tabifyDockWidget (&command_window, _file_editor);
 #endif
-  addDockWidget (Qt::LeftDockWidgetArea, _files_dock_widget);
+  addDockWidget (Qt::LeftDockWidgetArea, file_browser_window);
   addDockWidget (Qt::LeftDockWidgetArea, _workspace_view);
   addDockWidget (Qt::LeftDockWidgetArea, &history_window);
 
--- a/libgui/src/main-window.h	Sat Apr 06 17:46:12 2013 -0400
+++ b/libgui/src/main-window.h	Sat Apr 06 19:08:14 2013 -0400
@@ -103,11 +103,9 @@
 
   void handle_command_double_clicked (const QString& command);
 
-  void focus_current_directory ();
   void focus_workspace ();
   void focus_editor ();
   void focus_documentation ();
-  void handle_current_directory_visible (bool);
   void handle_workspace_visible (bool);
   void handle_editor_visible (bool);
   void handle_documentation_visible (bool);
@@ -162,6 +160,7 @@
   // Subwindows.
   terminal_dock_widget command_window;
   history_dock_widget history_window;
+  files_dock_widget *file_browser_window;
 
 #ifdef HAVE_QSCINTILLA
   file_editor_interface *   _file_editor;
@@ -176,7 +175,6 @@
 
   // Dock widgets.
   workspace_view *          _workspace_view;
-  files_dock_widget *       _files_dock_widget;
   documentation_dock_widget*_documentation_dock_widget;
 
   // Toolbars.