diff libgui/src/main-window.cc @ 16437:919796a440c6

use signal for changing directory * files-dockwidget.cc (files_dock_widget::files_dock_widget): Set title to "File Browser", not "Current Directory". * files-dockwidget.h, files-dockwidget.cc (files_dock_widget::change_directory_up): Rename from files_dock_widget::do_up_directory. Change all uses. (files_dock_widget::_last_current_directory): Delete variable and all uses. (files_dock_widget::accept_directory_line_edit): Rename from handle_directory_entered. Change all uses. (files_dock_widget::item_double_clicked): Call set_current_directory instead of display_directory. (files_dock_widget::set_current_directory): Also emit displayed_directory_changed signal. (files_dock_widget::display_directory): Sort list. Don't emit displayed_directory_changed. private: * file-editor-tab.cc (file_editor_tab::run_file): Delete unused local variable current_path. * main-window.h, main-window.cc (main_window::current_working_directory_has_changed): Delete function and all uses. (main_window::browse_for_directory): Rename from main_window::change_current_working_directory. Change all uses. (main_window::change_directory): Rename from main_window::set_current_working_directory. Change all uses. Update display. (main_window::change_directory_up): Rename from main_window::current_working_directory_up. Change all uses. Use absolute path for ".." directory. (main_window::accept_directory_line_edit): Rename from main_window::current_working_directory_entered. Change all uses. (main_window::construct): Set initial working directory to current directory at startup. * octave-qt-link.h, octave-qt-link.cc (octave_qt_link::do_change_directory): New function. (octave_qt_link::change_directory_signal): New signal. * octave-event-listener.h (octave_event_listener::current_directory_has_changed): Delete. * dirfns.cc (octave_change_to_directory): Also call octave_link::change_directory. * octave-link.h, octave-link.cc (octave_link::last_cwd): Delete variable and all uses. (octave_link::do_generate_events): Don't track current directory or post directory changed event. (octave_link::last_working_directory, octave_link::do_last_working_directory): Delete. (octave_link::do_change_directory): Rename from octave_link::do_last_working_directory.
author John W. Eaton <jwe@octave.org>
date Fri, 05 Apr 2013 00:40:12 -0400
parents fe4cd846c3e7
children a971d8bdaadc
line wrap: on
line diff
--- a/libgui/src/main-window.cc	Thu Apr 04 17:18:16 2013 -0400
+++ b/libgui/src/main-window.cc	Fri Apr 05 00:40:12 2013 -0400
@@ -270,65 +270,80 @@
 }
 
 void
-main_window::current_working_directory_has_changed (const QString& directory)
-{
-  int index = _current_directory_combo_box->findText (directory);
-  if ( index >= 0 )  // directory already in list -> remove it
-    { 
-      _current_directory_combo_box->removeItem (index);
-    }
-  _current_directory_combo_box->insertItem (0,directory);  // add (on top)
-  _current_directory_combo_box->setCurrentIndex (0);  // top is actual
-  _files_dock_widget->set_current_directory (directory);
-}
-
-void
 main_window::update_workspace (void)
 {
   _workspace_view->model_changed ();
 }
 
 void
-main_window::change_current_working_directory ()
+main_window::change_directory (const QString& dir)
 {
-  QString directory =
-    QFileDialog::getExistingDirectory(this, tr ("Set working direcotry"));
+  // Remove existing entry, if any, then add new directory at top and
+  // mark it as the current directory.  Finally, update the file list
+  // widget.
+
+  int index = _current_directory_combo_box->findText (dir);
+
+  if (index >= 0)
+    _current_directory_combo_box->removeItem (index);
+
+  _current_directory_combo_box->insertItem (0, dir);
+  _current_directory_combo_box->setCurrentIndex (0);
 
-  if (!directory.isEmpty ())
-    {
-      std::string dir = directory.toUtf8 ().data ();
-      octave_link::post_event (this, &main_window::change_directory_callback,dir);
-    }
+  _files_dock_widget->display_directory (dir);
+}
+
+void
+main_window::browse_for_directory (void)
+{
+  QString dir =
+    QFileDialog::getExistingDirectory (this, tr ("Set working directory"));
+
+  if (! dir.isEmpty ())
+    octave_link::post_event (this,
+                             &main_window::change_directory_callback,
+                             dir.toStdString ());
 }
 
 void
-main_window::set_current_working_directory (const QString& directory)
+main_window::set_current_working_directory (const QString& dir)
 {
-  QFileInfo fileInfo (directory);  // check whether this is an existing dir
-  if (fileInfo.exists () && fileInfo.isDir ())   // is dir and exists
-    {
-      std::string dir = directory.toUtf8 ().data ();
-      octave_link::post_event (this, &main_window::change_directory_callback,dir);
-    }
+  // Change to dir if it is an existing directory.
+
+  QString xdir = dir.isEmpty () ? "." : dir;
+    
+  QFileInfo fileInfo (xdir);
+
+  if (fileInfo.exists () && fileInfo.isDir ())
+    octave_link::post_event (this, &main_window::change_directory_callback,
+                             xdir.toStdString ());
 }
 
 void
-main_window::current_working_directory_up ()
+main_window::change_directory_up (void)
 {
-  set_current_working_directory ("..");
+  QDir dir ("..");
+
+  set_current_working_directory (dir.absolutePath ());
 }
 
-// Slot that is called if return is pressed in the line edit of the combobox
-// -> a new or a directory that is already in the drop down list was entered
+// Slot that is called if return is pressed in the line edit of the
+// combobox to change to a new directory or a directory that is already
+// in the drop down list.
+
 void
-main_window::current_working_directory_entered ()
+main_window::accept_directory_line_edit (void)
 {
-  QString dir = _current_directory_line_edit->text ();  // get new directory
-  int index = _current_directory_combo_box->findText (dir);  // already in list?
-  if ( index < 0 )  // directory not yet in list -> set directory
+  // Get new directory name, and change to it if it is new.  Otherwise,
+  // the combo box will triggers the "activated" signal to change to the
+  // directory.
+
+  QString dir = _current_directory_line_edit->text ();
+
+  int index = _current_directory_combo_box->findText (dir);
+
+  if (index < 0)
     set_current_working_directory (dir);
-  // if directory already in list, combobox triggers signal activated ()
-  // to change directory
 }
 
 void
@@ -1078,9 +1093,9 @@
   connect (clear_workspace_action,      SIGNAL (triggered ()),
            this,                        SLOT   (handle_clear_workspace_request ()));
   connect (current_directory_tool_button, SIGNAL (clicked ()),
-           this,                        SLOT   (change_current_working_directory ()));
+           this,                        SLOT   (browse_for_directory ()));
   connect (current_directory_up_tool_button, SIGNAL (clicked ()),
-           this,                        SLOT   (current_working_directory_up()));
+           this,                        SLOT   (change_directory_up ()));
   connect (copy_action,                 SIGNAL (triggered()),
            terminal,                    SLOT   (copyClipboard ()));
   connect (paste_action,                SIGNAL (triggered()),
@@ -1088,7 +1103,7 @@
   connect (_current_directory_combo_box, SIGNAL (activated (QString)),
            this,                        SLOT (set_current_working_directory (QString)));
   connect (_current_directory_line_edit, SIGNAL (returnPressed ()),
-           this,                        SLOT (current_working_directory_entered ()));
+           this,                        SLOT (accept_directory_line_edit ()));
   connect (_debug_continue,             SIGNAL (triggered ()),
            this,                        SLOT (debug_continue ()));
   connect (_debug_step_into,            SIGNAL (triggered ()),
@@ -1129,11 +1144,6 @@
   _octave_qt_event_listener = new octave_qt_event_listener ();
 
   connect (_octave_qt_event_listener,
-           SIGNAL (current_directory_has_changed_signal (QString)),
-           this,
-           SLOT (current_working_directory_has_changed (QString)));
-
-  connect (_octave_qt_event_listener,
            SIGNAL (update_workspace_signal ()),
            this,
            SLOT (update_workspace ()));
@@ -1152,6 +1162,9 @@
 
   _octave_qt_link = new octave_qt_link ();
 
+  connect (_octave_qt_link, SIGNAL (change_directory_signal (QString)),
+           this, SLOT (change_directory (QString)));
+
   connect (_octave_qt_link,
            SIGNAL (set_history_signal (const QStringList&)),
            _history_dock_widget, SLOT (set_history (const QStringList&)));
@@ -1189,6 +1202,9 @@
   octave_link::connect_link (_octave_qt_link);
 
   octave_link::register_event_listener (_octave_qt_event_listener);
+
+  QDir curr_dir;
+  set_current_working_directory (curr_dir.absolutePath ());
 }
 
 void