changeset 15860:feba9ff6e6a8

editor: add list of recently used files to the file menu * file_editor_tab.cc (set_file_name): emit signal mru_add_file () for a new file * file_editor_tab.h: new signal mru_add_file () * file_editor.cc (request_mru_open_file): new handler for opening a file from the mru-list, (handle_mru_add_file): slot for signal emitted from file_editor_tab; adds the new file to the mru list, (mru_menu_update): private function for updating the mru-list, (mru_menu_update): private function for updating the mru-list, (construct): implement mru-list menu and the related actions, (add_file_editor_tab): connect signal mru_add_file () * file-editor.h: definition of new slots, methods, string list for mru-list, and array for the related actions
author Torsten <ttl@justmail.de>
date Fri, 28 Dec 2012 15:01:08 +0100
parents 0e393e744e5e
children f425e680925e
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h
diffstat 4 files changed, 82 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Dec 27 23:04:46 2012 -0800
+++ b/libgui/src/m-editor/file-editor-tab.cc	Fri Dec 28 15:01:08 2012 +0100
@@ -167,6 +167,8 @@
 
   // update the file editor with current editing directory
   emit editor_state_changed (_copy_available, QDir::cleanPath (_file_name));
+  // add the new file to the mru list
+  emit mru_add_file (QDir::cleanPath (_file_name));
 }
 
 void
--- a/libgui/src/m-editor/file-editor-tab.h	Thu Dec 27 23:04:46 2012 -0800
+++ b/libgui/src/m-editor/file-editor-tab.h	Fri Dec 28 15:01:08 2012 +0100
@@ -89,6 +89,7 @@
   void editor_state_changed (bool copy_available, const QString& fileName);
   void tab_remove_request ();
   void add_filename_to_list (const QString& fileName);
+  void mru_add_file (const QString& file_name);
   void editor_check_conflict_save (const QString& saveFileName, bool remove_on_success);
   void process_octave_code (const QString& command);
 
--- a/libgui/src/m-editor/file-editor.cc	Thu Dec 27 23:04:46 2012 -0800
+++ b/libgui/src/m-editor/file-editor.cc	Fri Dec 28 15:01:08 2012 +0100
@@ -168,6 +168,8 @@
               // with full or short name.
               add_file_editor_tab (fileEditorTab, "");
               fileEditorTab->update_window_title (false);
+              // file already loaded, add file to mru list here
+              handle_mru_add_file(QDir::cleanPath (openFileName));
             }
           else
             {
@@ -186,6 +188,18 @@
     }
 }
 
+// open a file from the mru list
+void
+file_editor::request_mru_open_file ()
+{
+  QAction *action = qobject_cast<QAction *>(sender ());
+  if (action)
+    {
+      request_open_file (action->data ().toString ());
+    }
+}
+
+
 void
 file_editor::check_conflict_save (const QString& saveFileName, bool remove_on_success)
 {
@@ -358,6 +372,39 @@
 }
 
 void
+file_editor::handle_mru_add_file (const QString& file_name)
+{
+  _mru_files.removeAll (file_name);
+  _mru_files.prepend (file_name);
+  mru_menu_update ();
+}
+
+void
+file_editor::mru_menu_update ()
+{
+  int num_files = qMin (_mru_files.size(), int (MaxMRUFiles));
+  // configure and show active actions of mru-menu
+  for (int i = 0; i < num_files; ++i)
+    {
+      QString text = tr("&%1 %2").
+          arg ((i+1) % int (MaxMRUFiles)).arg (_mru_files.at (i));
+      _mru_file_actions[i]->setText (text);
+      _mru_file_actions[i]->setData (_mru_files.at (i));
+      _mru_file_actions[i]->setVisible (true);
+    }
+    // hide unused mru-menu entries
+    for (int j = num_files; j < MaxMRUFiles; ++j)
+      _mru_file_actions[j]->setVisible (false);
+    // delete entries in string-list beyond MaxMRUFiles
+    while (_mru_files.size () > MaxMRUFiles)
+      _mru_files.removeLast ();
+    // save actual mru-list in settings
+    QSettings *settings = resource_manager::get_settings ();
+    // FIXME -- what should happen if settings is 0?
+    settings->setValue ("editor/mru_file_list",_mru_files);
+}
+
+void
 file_editor::handle_file_name_changed (const QString& fileName)
 {
   QObject *fileEditorTab = sender();
@@ -521,6 +568,16 @@
   _run_action = new QAction (QIcon(":/actions/icons/artsbuilderexecute.png"),
                              tr("Save File And Run"), _tool_bar);
 
+  // the mru-list and an empty array of actions
+  QSettings *settings = resource_manager::get_settings ();
+  // FIXME -- what should happen if settings is 0?
+  _mru_files = settings->value ("editor/mru_file_list").toStringList ();
+  for (int i = 0; i < MaxMRUFiles; ++i)
+    {
+       _mru_file_actions[i] = new QAction (this);
+       _mru_file_actions[i]->setVisible (false);
+    }
+
   // some actions are disabled from the beginning
   _copy_action->setEnabled(false);
   _cut_action->setEnabled(false);
@@ -566,6 +623,12 @@
   fileMenu->addAction (save_action);
   fileMenu->addAction (save_as_action);
   fileMenu->addSeparator ();
+  QMenu *mru_file_menu = new QMenu (tr ("Open &Recent"), fileMenu);
+  for (int i = 0; i < MaxMRUFiles; ++i)
+    {
+      mru_file_menu->addAction (_mru_file_actions[i]);
+    }
+  fileMenu->addMenu (mru_file_menu);
   _menu_bar->addMenu (fileMenu);
 
   QMenu *editMenu = new QMenu (tr ("&Edit"), _menu_bar);
@@ -650,6 +713,12 @@
            SIGNAL (triggered ()), this, SLOT (request_uncomment_selected_text ()));
   connect (find_action,
            SIGNAL (triggered ()), this, SLOT (request_find ()));
+  // The actions of the mru file menu
+  for (int i = 0; i < MaxMRUFiles; ++i)
+    {
+      connect(_mru_file_actions[i], SIGNAL (triggered ()), this, SLOT (request_mru_open_file ()));
+    }
+  mru_menu_update ();
   connect (_tab_widget,
            SIGNAL (tabCloseRequested (int)), this, SLOT (handle_tab_close_request (int)));
   connect (_tab_widget,
@@ -662,7 +731,6 @@
   setWindowTitle ("Editor");
 
   //restore previous session
-  QSettings *settings = resource_manager::get_settings ();
   if (settings->value ("editor/restoreSession",true).toBool ())
     {
       QStringList sessionFileNames = settings->value("editor/savedSessionTabs", QStringList()).toStringList ();
@@ -688,6 +756,8 @@
            this, SLOT (handle_add_filename_to_list (const QString&)));
   connect (f, SIGNAL (editor_check_conflict_save (const QString&, bool)),
            this, SLOT (check_conflict_save (const QString&, bool)));
+  connect (f, SIGNAL (mru_add_file (const QString&)),
+           this, SLOT (handle_mru_add_file (const QString&)));
   connect (f, SIGNAL (process_octave_code (const QString&)),
            parent (), SLOT (handle_command_double_clicked (const QString&)));
   
--- a/libgui/src/m-editor/file-editor.h	Thu Dec 27 23:04:46 2012 -0800
+++ b/libgui/src/m-editor/file-editor.h	Fri Dec 28 15:01:08 2012 +0100
@@ -89,6 +89,7 @@
 public slots:
   void request_new_file ();
   void request_open_file ();
+  void request_mru_open_file ();
 
   void request_undo ();
   void request_redo ();
@@ -118,6 +119,7 @@
   void handle_add_filename_to_list (const QString& fileName);
   void active_tab_changed (int index);
   void handle_editor_state_changed (bool enableCopy, const QString& fileName);
+  void handle_mru_add_file (const QString& file_name);
   void check_conflict_save (const QString& fileName, bool remove_on_success);
 
   /** Slot when floating property changes */
@@ -133,6 +135,7 @@
   void construct ();
   void add_file_editor_tab(file_editor_tab *f, const QString &fn);
   void save_file_as (QWidget *fetabID = 0);
+  void mru_menu_update ();
 
   QStringList fetFileNames;
   QString ced;
@@ -145,6 +148,11 @@
   QAction *         _run_action;
   QTabWidget *      _tab_widget;
   int               _marker_breakpoint;
+
+  enum { MaxMRUFiles = 10 };
+  QAction *_mru_file_actions[MaxMRUFiles];
+  QStringList _mru_files;
+
 };
 
 #endif // FILEEDITORMDISUBWINDOW_H