diff libgui/src/m-editor/file-editor.cc @ 18399:2ea741d22554 gui-release

create empty script when editor becomes visible without open files (bug #41347) * file-editor.cc (empty_script): new function determining whether to create an empty script or not; (handle_visibility): reimplemented from octave_dock_widget, calls empty_script each time the editor becomes visible; * file-editor.h: new slot handle_visibility, new function empty_script * file-editor-interface.h: new virtual function empty_script * main-window.cc (connect_visibility_changed): call empty_script when main window and all widgets are initialized
author Torsten <ttl@justmail.de>
date Sun, 26 Jan 2014 12:55:21 +0100
parents dfc6ef6ac455
children f01ac1bb8a5d
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Fri Jan 24 17:17:00 2014 -0800
+++ b/libgui/src/m-editor/file-editor.cc	Sun Jan 26 12:55:21 2014 +0100
@@ -36,6 +36,7 @@
 #include <QMessageBox>
 #include <QStyle>
 #include <QTextStream>
+#include <QTabBar>
 #include <QProcess>
 #include <QInputDialog>
 
@@ -1547,5 +1548,67 @@
   _redo_action->setEnabled (have_tabs);
 }
 
+// empty_script determines whether we have to create an empty script
+// 1. At startup, when the editor has to be (really) visible
+//    (Here we can not use the visibility changed signal)
+// 2. When the editor becomes visible when octave is running
+void
+file_editor::empty_script (bool startup, bool visible)
+{
+  bool real_visible;
+
+  if (startup)
+    real_visible = isVisible ();
+  else
+    real_visible = visible;
+
+  if (! real_visible || _tab_widget->count () > 0)
+    return;
+
+  if (startup && ! isFloating ())
+    {
+      // check is editor is really visible or hidden between tabbed widgets
+      QList<QTabBar *> tab_list = main_win ()->findChildren<QTabBar *>();
+
+      bool in_tab = false;
+      int i = 0;
+      while ((i < tab_list.count ()) && (! in_tab))
+        {
+          QTabBar *tab = tab_list.at (i);
+          i++;
+
+          int j = 0;
+          while ((j < tab->count ()) && (! in_tab))
+            {
+              // check all tabs for the editor
+              if (tab->tabText (j) == windowTitle ())
+                {
+                  // editor is in this tab widget
+                  in_tab = true;
+                  int top = tab->currentIndex ();
+                  if (top > -1 && tab->tabText (top) == windowTitle ())
+                    real_visible = true;  // and is the current tab
+                  else
+                    return; // not current tab -> not visible
+                }
+              j++;
+            }
+        }
+    }
+
+  request_new_file ("");
+}
+
+// This slot is a reimplementation of the virtual slot in octave_dock_widget.
+// We need this for creating an empty script when the editor has no open files
+// and is made visible
+void
+file_editor::handle_visibility (bool visible)
+  {
+    empty_script (false, visible);
+
+    if (visible && ! isFloating ())
+      focus ();
+  }
 
 #endif