diff libgui/src/m-editor/file-editor.cc @ 16377:8430ea8c1594

open editor tab and insert marker for debugging with gui * file-editor-interface.h (file_editor_interface::request_open_file): New public slot. (file_editor_interface::handle_dbstop_request): New virtual function. * file-editor-tab.h, file-editor-tab.cc (file_editor_tab::goto_line): New arg, line, with default value. Don't prompt if line is greater than zero. (file_editor_tab::set_debugger_position): New arg, widget id. (file_editor_tab::add_filename_to_list): New arg, widget id. * file-editor.h, file-editor.cc (file_eidtor::editor_tab_map): New data member. (file_editor::fetFileNames) Delete. (file_editor::~file_editor, file_editor::check_conflict_save, file_editor::handle_add_filename_to_list): Use editor_tab_map instead of fetFileNames. (file_editor::request_open_file): New args, line and set_marker. Optionally position cursor at line and with debug marker. If file is already open in tab, switch to it instead of giving error. (file_editor::construct): Connect fetab_set_debugger_position signal to set_debugger_position slot. * main-window.h, main-window.cc (main_window::dbstop_signal): New signal. (main_window::handle_dbstop_request): New function. * octave-event-listener.h (octave_event_listener::dbstop): New virtual function. * octave-qt-event-listener.h, octave-qt-event-listener.cc (octave_qt_event_listener::dbstop): New function. (octave_qt_event_listener::dbstop_signal): New signal. * octave-link.h, octave-link.cc (octave_link::dbstop, octave_link::do_dbstop, octave_link::dbstop_event_hook_fcn, octave_link::do_dbstop_event_hook_fcn): New functions * octave-main-thread.h, octave-main-thread.cc (dbstop_event_hook_fcn): New function. (octave_main_thread::run): Add it to the list of dbstop event hook functions.
author John W. Eaton <jwe@octave.org>
date Wed, 27 Mar 2013 12:59:12 -0400
parents f482302d81c9
children 3cacd597464d
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Wed Mar 27 00:45:49 2013 -0400
+++ b/libgui/src/m-editor/file-editor.cc	Wed Mar 27 12:59:12 2013 -0400
@@ -54,12 +54,17 @@
 file_editor::~file_editor ()
 {
   QSettings *settings = resource_manager::get_settings ();
-  fetFileNames.clear ();
+  editor_tab_map.clear ();
   if (settings->value ("editor/restoreSession",true).toBool ())
     {
       // Have all file editor tabs signal what their file names are.
       emit fetab_file_name_query (0);
     }
+  QStringList fetFileNames;
+  for (std::map<QString, QWidget *>::const_iterator p = editor_tab_map.begin ();
+       p != editor_tab_map.end (); p++)
+    fetFileNames.append (p->first);
+
   settings->setValue ("editor/savedSessionTabs", fetFileNames);
   settings->sync ();
 
@@ -142,7 +147,8 @@
 }
 
 void
-file_editor::request_open_file (const QString& openFileName)
+file_editor::request_open_file (const QString& openFileName, int line,
+                                bool set_marker)
 {
   if (openFileName.isEmpty ())
     {
@@ -152,63 +158,66 @@
   else
     {
       // Have all file editor tabs signal what their file names are.
-      fetFileNames.clear ();
+      editor_tab_map.clear ();
       emit fetab_file_name_query (0);
 
       // Check whether this file is already open in the editor.
-      if (fetFileNames.contains (openFileName, Qt::CaseSensitive))
+      std::map<QString, QWidget *>::const_iterator p = editor_tab_map.find (openFileName);
+      if (p != editor_tab_map.end ())
+        {
+          _tab_widget->setCurrentWidget (p->second);
+
+          if (line > 0)
+            {
+              emit fetab_goto_line (p->second, line);
+
+              if (set_marker)
+                emit fetab_set_debugger_position (p->second, line-1);
+            }
+
+          emit fetab_set_focus (p->second);
+        }
+      else
         {
-          // Create a NonModal message so nothing is blocked and
-          // bring the existing file forward.
-          QMessageBox* msgBox = new QMessageBox (
-                  QMessageBox::Critical, tr ("Octave Editor"),
-                  tr ("File %1 is already open in the editor.").
-                  arg (openFileName), QMessageBox::Ok, 0);
-          msgBox->setWindowModality (Qt::NonModal);
-          msgBox->setAttribute (Qt::WA_DeleteOnClose);
-          msgBox->show ();
-          QFileInfo file(openFileName);
-          QString short_openFileName = file.fileName();  // get file name only
-          for(int i = 0; i < _tab_widget->count (); i++)
-            { // check whether tab title is file name (long or short)
-              if (_tab_widget->tabText (i) == openFileName ||
-                  _tab_widget->tabText (i) == short_openFileName)
+          file_editor_tab *fileEditorTab = new file_editor_tab ();
+          if (fileEditorTab)
+            {
+              QString result = fileEditorTab->load_file(openFileName);
+              if (result == "")
                 {
-                  _tab_widget->setCurrentIndex (i);
-                  break;
+                  // Supply empty title then have the file_editor_tab update
+                  // 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));
+
+                  if (line > 0)
+                    {
+                      emit fetab_goto_line (fileEditorTab, line);
+
+                      if (set_marker)
+                        emit fetab_set_debugger_position (fileEditorTab, line-1);
+                    }
+                }
+              else
+                {
+                  delete fileEditorTab;
+                  // Create a NonModal message about error.
+                  QMessageBox* msgBox = new QMessageBox (
+                                                         QMessageBox::Critical, tr ("Octave Editor"),
+                                                         tr ("Could not open file %1 for read:\n%2.").
+                                                         arg (openFileName).arg (result),
+                                                         QMessageBox::Ok, 0);
+                  msgBox->setWindowModality (Qt::NonModal);
+                  msgBox->setAttribute (Qt::WA_DeleteOnClose);
+                  msgBox->show ();
                 }
             }
-          return;
+
+          // really show editor and the current editor tab
+          set_focus ();
         }
-
-      file_editor_tab *fileEditorTab = new file_editor_tab ();
-      if (fileEditorTab)
-        {
-          QString result = fileEditorTab->load_file(openFileName);
-          if (result == "")
-            {
-              // Supply empty title then have the file_editor_tab update
-              // 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
-            {
-              delete fileEditorTab;
-              // Create a NonModal message about error.
-              QMessageBox* msgBox = new QMessageBox (
-                      QMessageBox::Critical, tr ("Octave Editor"),
-                      tr ("Could not open file %1 for read:\n%2.").
-                      arg (openFileName).arg (result),
-                      QMessageBox::Ok, 0);
-              msgBox->setWindowModality (Qt::NonModal);
-              msgBox->setAttribute (Qt::WA_DeleteOnClose);
-              msgBox->show ();
-            }
-        }
-      set_focus ();  // really show editor and the current editor tab
     }
 }
 
@@ -228,11 +237,11 @@
 file_editor::check_conflict_save (const QString& saveFileName, bool remove_on_success)
 {
   // Have all file editor tabs signal what their file names are.
-  fetFileNames.clear ();
+  editor_tab_map.clear ();
   emit fetab_file_name_query (0);
 
-  // If one of those names matches the desired name, that's a conflict.
-  if (fetFileNames.contains (saveFileName, Qt::CaseSensitive))
+  std::map<QString, QWidget *>::const_iterator p = editor_tab_map.find (saveFileName);
+  if (p != editor_tab_map.end ())
     {
       // Note: to overwrite the contents of some other file editor tab
       // with the same name requires identifying which file editor tab
@@ -283,6 +292,12 @@
 }
 
 void
+file_editor::handle_dbstop_request (const QString& file, int line)
+{
+  request_open_file (file, line, true);
+}
+
+void
 file_editor::request_undo ()
 {
   emit fetab_undo (_tab_widget->currentWidget ());
@@ -484,9 +499,11 @@
 }
 
 void
-file_editor::handle_add_filename_to_list (const QString& fileName)
+file_editor::handle_add_filename_to_list (const QString& fileName, QWidget *ID)
 {
-  fetFileNames.append (fileName);
+  // Should we allow multiple tabs for a single file?
+
+  editor_tab_map[fileName] = ID;
 }
 
 void
@@ -788,8 +805,8 @@
            this, SLOT (handle_editor_state_changed (bool, const QString&)));
   connect (f, SIGNAL (tab_remove_request ()),
            this, SLOT (handle_tab_remove_request ()));
-  connect (f, SIGNAL (add_filename_to_list (const QString&)),
-           this, SLOT (handle_add_filename_to_list (const QString&)));
+  connect (f, SIGNAL (add_filename_to_list (const QString&, QWidget *)),
+           this, SLOT (handle_add_filename_to_list (const QString&, QWidget *)));
   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&)),
@@ -847,10 +864,12 @@
            f, SLOT (uncomment_selected_text (const QWidget*)));
   connect (this, SIGNAL (fetab_find (const QWidget*)),
            f, SLOT (find (const QWidget*)));
-  connect (this, SIGNAL (fetab_goto_line (const QWidget*)),
-           f, SLOT (goto_line (const QWidget*)));
+  connect (this, SIGNAL (fetab_goto_line (const QWidget *, int)),
+           f, SLOT (goto_line (const QWidget *, int)));
   connect (this, SIGNAL (fetab_set_focus (const QWidget*)),
            f, SLOT (set_focus (const QWidget*)));
+  connect (this, SIGNAL (fetab_set_debugger_position (const QWidget *, int)),
+           f, SLOT (set_debugger_position (const QWidget *, int)));
 
   _tab_widget->setCurrentWidget (f);
 }