changeset 14803:625be3eb27c5 gui

Current directories now synchronize on top, in the file browser and the cwd in octave. * files-dockwidget: Refactored method names and redundand code. Added new signal that get emitted whenever the displayed directory changes. * main-window: Added new slot to react on directory changes. Informing the file browser of a directory change now.
author Jacob Dawid <jacob.dawid@googlemail.com>
date Tue, 26 Jun 2012 10:54:39 +0200
parents 67c6835e51b6
children a565c560e654
files gui/src/files-dockwidget.cc gui/src/files-dockwidget.h gui/src/main-window.cc gui/src/main-window.h
diffstat 4 files changed, 62 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/gui/src/files-dockwidget.cc	Thu Jun 21 15:07:05 2012 -0700
+++ b/gui/src/files-dockwidget.cc	Tue Jun 26 10:54:39 2012 +0200
@@ -38,11 +38,11 @@
   _navigation_tool_bar->setMovable (false);
   _navigation_tool_bar->setIconSize (QSize (20, 20));
 
-  // Add a button to the toolbar with the QT standard icon for up-directory
   _directory_icon = QIcon(":/actions/icons/up.png");
   _directory_up_action = new QAction (_directory_icon, "", _navigation_tool_bar);
   _directory_up_action->setStatusTip (tr ("Move up one directory."));
 
+  _last_current_directory = "";
   _current_directory = new QLineEdit (_navigation_tool_bar);
   _current_directory->setStatusTip (tr ("Enter the path or filename."));
 
@@ -54,14 +54,11 @@
   // TODO: Add other buttons for creating directories
 
   // Create the QFileSystemModel starting in the home directory
-  QString
-    homePath = QDir::homePath ();
-  // TODO: This should occur after Octave has been initialized and the startup directory of Octave is established
+  QString homePath = QDir::homePath ();
 
   _file_system_model = new QFileSystemModel (this);
   _file_system_model->setFilter (QDir::NoDotAndDotDot | QDir::AllEntries);
-  QModelIndex
-    rootPathIndex = _file_system_model->setRootPath (homePath);
+  QModelIndex rootPathIndex = _file_system_model->setRootPath (homePath);
 
   // Attach the model to the QTreeView and set the root index
   _file_tree_view = new QTreeView (widget ());
@@ -75,7 +72,7 @@
   _file_tree_view->setColumnHidden (3, true);
   _file_tree_view->setStatusTip (tr ("Doubleclick a file to open it."));
 
-  set_current_directory (_file_system_model->fileInfo (rootPathIndex).
+  _current_directory->setText(_file_system_model->fileInfo (rootPathIndex).
                        absoluteFilePath ());
 
   connect (_file_tree_view, SIGNAL (doubleClicked (const QModelIndex &)), this,
@@ -91,13 +88,15 @@
   widget ()->setLayout (layout);
   // TODO: Add right-click contextual menus for copying, pasting, deleting files (and others)
 
-  connect (_current_directory, SIGNAL (returnPressed ()), this,
-           SLOT (current_directory_entered ()));
+  connect (_current_directory, SIGNAL (returnPressed ()),
+           this, SLOT (handle_directory_entered ()));
+
   QCompleter *
     completer = new QCompleter (_file_system_model, this);
   _current_directory->setCompleter (completer);
 
-  connect (this, SIGNAL (visibilityChanged(bool)), this, SLOT(handle_visibility_changed(bool)));
+  connect (this, SIGNAL (visibilityChanged (bool)),
+           this, SLOT (handle_visibility_changed (bool)));
 }
 
 void
@@ -105,67 +104,54 @@
 {
   // Retrieve the file info associated with the model index.
   QFileInfo fileInfo = _file_system_model->fileInfo (index);
-
-  // If it is a directory, cd into it.
-  if (fileInfo.isDir ())
-    {
-      _file_system_model->setRootPath (fileInfo.absolutePath ());
-      _file_tree_view->setRootIndex (index);
-      set_current_directory (_file_system_model->fileInfo (index).
-                           absoluteFilePath ());
-    }
-  // Otherwise attempt to open it.
-  else
-    {
-      // Check if the user wants to use a custom file editor.
-      QSettings *settings = resource_manager::instance ()->get_settings ();
-      if (settings->value ("useCustomFileEditor").toBool ())
-        {
-          QString editor = settings->value ("customFileEditor").toString ();
-          QStringList arguments;
-          arguments << fileInfo.filePath ();
-          QProcess::startDetached (editor, arguments);
-        }
-      else
-        {
-          emit open_file (fileInfo.filePath ());
-        }
-    }
+  display_directory (fileInfo.absoluteFilePath ());
 }
 
 void
 files_dock_widget::set_current_directory (QString currentDirectory)
 {
-  _current_directory->setText (currentDirectory);
+  display_directory (currentDirectory);
+}
+
+void
+files_dock_widget::handle_directory_entered ()
+{
+  display_directory (_current_directory->text ());
+}
+
+void
+files_dock_widget::do_up_directory ()
+{
+  QDir dir = QDir (_file_system_model->filePath (_file_tree_view->rootIndex ()));
+  dir.cdUp ();
+  display_directory (dir.absolutePath ());
 }
 
 void
-files_dock_widget::do_up_directory (void)
+files_dock_widget::display_directory (QString directory)
 {
-  QDir dir =
-    QDir (_file_system_model->filePath (_file_tree_view->rootIndex ()));
-  dir.cdUp ();
-  _file_system_model->setRootPath (dir.absolutePath ());
-  _file_tree_view->setRootIndex (_file_system_model->
-                                index (dir.absolutePath ()));
-  set_current_directory (dir.absolutePath ());
-}
+  QFileInfo fileInfo (directory);
+  if (fileInfo.exists ())
+    {
+      if (fileInfo.isDir ())
+        {
+          _file_tree_view->setRootIndex (_file_system_model->
+                                        index (fileInfo.absoluteFilePath ()));
+          _file_system_model->setRootPath (fileInfo.absoluteFilePath ());
+          _current_directory->setText (fileInfo.absoluteFilePath ());
 
-void
-files_dock_widget::current_directory_entered ()
-{
-  QFileInfo fileInfo (_current_directory->text ());
-  if (fileInfo.isDir ())
-    {
-      _file_tree_view->setRootIndex (_file_system_model->
-                                    index (fileInfo.absolutePath ()));
-      _file_system_model->setRootPath (fileInfo.absolutePath ());
-      set_current_directory (fileInfo.absoluteFilePath ());
-    }
-  else
-    {
-      if (QFile::exists (fileInfo.absoluteFilePath ()))
-        emit open_file (fileInfo.absoluteFilePath ());
+          if (_last_current_directory != fileInfo.absoluteFilePath ())
+            {
+              emit displayed_directory_changed (fileInfo.absoluteFilePath ());
+            }
+
+          _last_current_directory = fileInfo.absoluteFilePath ();
+        }
+      else
+        {
+          if (QFile::exists (fileInfo.absoluteFilePath ()))
+            emit open_file (fileInfo.absoluteFilePath ());
+        }
     }
 }
 
--- a/gui/src/files-dockwidget.h	Thu Jun 21 15:07:05 2012 -0700
+++ b/gui/src/files-dockwidget.h	Tue Jun 26 10:54:39 2012 +0200
@@ -46,7 +46,8 @@
   /** Slot for handling the up-directory button in the toolbar. */
   void do_up_directory ();
   void set_current_directory (QString currentDirectory);
-  void current_directory_entered ();
+  void handle_directory_entered ();
+  void display_directory (QString directory);
 
   /** Tells the widget to notice settings that are probably new. */
   void notice_settings ();
@@ -54,6 +55,7 @@
 
 signals:
   void open_file (QString fileName);
+  void displayed_directory_changed (QString directory);
 
   /** Custom signal that tells if a user has clicke away that dock widget. */
   void active_changed (bool active);
@@ -64,6 +66,8 @@
 private:
   // TODO: Add toolbar with buttons for navigating the path, creating dirs, etc
 
+  QString           _last_current_directory;
+
   /** Toolbar for file and directory manipulation. */
   QToolBar *        _navigation_tool_bar;
 
--- a/gui/src/main-window.cc	Thu Jun 21 15:07:05 2012 -0700
+++ b/gui/src/main-window.cc	Tue Jun 26 10:54:39 2012 +0200
@@ -162,7 +162,7 @@
 }
 
 void
-main_window::update_current_working_directory (QString directory)
+main_window::current_working_directory_has_changed (QString directory)
 {
   if (_current_directory_combo_box->count () > 31)
     {
@@ -171,6 +171,8 @@
   _current_directory_combo_box->addItem (directory);
   int index = _current_directory_combo_box->findText (directory);
   _current_directory_combo_box->setCurrentIndex (index);
+
+  _files_dock_widget->set_current_directory (directory);
 }
 
 void
@@ -188,7 +190,7 @@
 }
 
 void
-main_window::change_current_working_directory (QString directory)
+main_window::set_current_working_directory (QString directory)
 {
   octave_link::instance ()
       ->post_event (new octave_change_directory_event (*this,
@@ -524,6 +526,8 @@
            this,                        SLOT   (notice_settings ()));
   connect (_files_dock_widget,          SIGNAL (open_file (QString)),
            _file_editor,                SLOT   (request_open_file (QString)));
+  connect (_files_dock_widget,          SIGNAL (displayed_directory_changed(QString)),
+           this,                        SLOT   (set_current_working_directory(QString)));
   connect (_history_dock_widget,        SIGNAL (information (QString)),
            this,                        SLOT   (report_status_message (QString)));
   connect (_history_dock_widget,        SIGNAL (command_double_clicked (QString)),
@@ -543,7 +547,7 @@
   connect (paste_action,                SIGNAL (triggered()),
            _terminal,                   SLOT   (pasteClipboard ()));
   connect (_current_directory_combo_box, SIGNAL (activated (QString)),
-           this,                        SLOT (change_current_working_directory (QString)));
+           this,                        SLOT (set_current_working_directory (QString)));
   connect (_debug_continue,             SIGNAL (triggered ()),
            this,                        SLOT (debug_continue ()));
   connect (_debug_step_into,            SIGNAL (triggered ()),
@@ -571,7 +575,7 @@
   connect (_octave_qt_event_listener,
            SIGNAL (current_directory_has_changed_signal (QString)),
            this,
-           SLOT (update_current_working_directory (QString)));
+           SLOT (current_working_directory_has_changed (QString)));
 
   connect (_octave_qt_event_listener,
            SIGNAL (entered_debug_mode_signal ()),
--- a/gui/src/main-window.h	Thu Jun 21 15:07:05 2012 -0700
+++ b/gui/src/main-window.h	Tue Jun 26 10:54:39 2012 +0200
@@ -87,9 +87,9 @@
   void notice_settings ();
   void prepare_for_quit ();
   void reset_windows ();
-  void update_current_working_directory (QString directory);
+  void current_working_directory_has_changed (QString directory);
   void change_current_working_directory ();
-  void change_current_working_directory (QString directory);
+  void set_current_working_directory (QString directory);
   void current_working_directory_up ();
 
   void handle_entered_debug_mode ();