diff libgui/src/files-dockwidget.cc @ 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 919796a440c6
children 38ed6bdd1f7f
line wrap: on
line diff
--- a/libgui/src/files-dockwidget.cc
+++ b/libgui/src/files-dockwidget.cc
@@ -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 ();
+}
+