changeset 17901:2c241092b47b

use edit.m for creating a new function with the gui (bug #40502) * file-editor-interface.h: request_new_function without string argument * file-editor.cc (request_new_function): boolean instead of string as parameter, ask for function name, set preference for not asking when creating new files, connect a signal when file is loaded, and evaluate edit in the console (restore_create_file_setting): slot that restores the create file pref when file is loaded (request_open_file): emit a signal when file is loaded (construct): new action for new function, connect execute in terminal signal * file_editor.h: new signal for executing a command in terminal, new signal when file is loaded, request_new_function with boolean instead of string parameter, new slot for restoring prompt create new file preference * main-window.cc (construct_new_menu): new function action as class variable (set_global_shortcuts): shortcut for new function * main-window.h: new function action as class variable
author Torsten <ttl@justmail.de>
date Mon, 11 Nov 2013 22:01:27 +0100
parents 8e9532632838
children 9bcf1614cd80
files libgui/src/m-editor/file-editor-interface.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/main-window.cc libgui/src/main-window.h
diffstat 5 files changed, 53 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-interface.h	Mon Nov 11 09:54:07 2013 -0500
+++ b/libgui/src/m-editor/file-editor-interface.h	Mon Nov 11 22:01:27 2013 +0100
@@ -64,7 +64,7 @@
 public slots:
   virtual void request_new_file (const QString& command = QString ()) = 0;
   virtual void request_new_script (const QString& command = QString ()) = 0;
-  virtual void request_new_function (const QString& command = QString ()) = 0;
+  virtual void request_new_function (bool) = 0;
   virtual void request_open_file () = 0;
   virtual void request_open_file (const QString& openFileName, int line = -1,
                                   bool debug_pointer = false,
--- a/libgui/src/m-editor/file-editor.cc	Mon Nov 11 09:54:07 2013 -0500
+++ b/libgui/src/m-editor/file-editor.cc	Mon Nov 11 22:01:27 2013 +0100
@@ -37,6 +37,7 @@
 #include <QStyle>
 #include <QTextStream>
 #include <QProcess>
+#include <QInputDialog>
 
 #include "octave-link.h"
 #include "utils.h"
@@ -151,22 +152,39 @@
 }
 
 void
-file_editor::request_new_function (const QString& commands)
+file_editor::request_new_function (bool)
 {
-  QString text = commands;
+  bool ok;
+  // get the name of the new function
+  QString new_name  = QInputDialog::getText (this, tr ("New Function"),
+                      tr ("New function name:\n"), QLineEdit::Normal, "", &ok);
+  if (ok && new_name.length () > 0)
+    {
+      // append suffix if it not already exists
+      if (new_name.rightRef (2) != ".m")
+        new_name.append (".m");
+      // check whether new files are created without prompt
+      QSettings *settings = resource_manager::get_settings ();
+      if (! settings->value ("editor/create_new_file",false).toBool ())
+        {
+          // no, so enable this settings and wait for end of new file loading
+          settings->setValue ("editor/create_new_file",true);
+          connect (this, SIGNAL (file_loaded_signal ()),
+                   this, SLOT (restore_create_file_setting ()));
+        }
+      // start the edit command
+      emit execute_command_in_terminal_signal ("edit " + new_name);
+    }
+}
 
-  if (text.isEmpty ())
-    text = "## Copyright (C)\n"
-      "\n"
-      "## -*- texinfo -*-\n"
-      "## @deftypefn {Function File} {[outputs] =} unamed_function (inputs)\n"
-      "## @end deftypefn\n"
-      "\n"
-      "function [outputs] = unnamed_function (inputs)\n"
-      "\n"
-      "endfunction\n";
-
-  request_new_file (text);
+void
+file_editor::restore_create_file_setting ()
+{
+  // restore the new files creation setting
+  QSettings *settings = resource_manager::get_settings ();
+  settings->setValue ("editor/create_new_file",false);
+  disconnect (this, SIGNAL (file_loaded_signal ()),
+              this, SLOT (restore_create_file_setting ()));
 }
 
 void
@@ -319,8 +337,7 @@
                       // File does not exist, should it be crated?
                       QMessageBox *msgBox;
                       int answer;
-                      if (settings->value ("editor/create_new_file",
-                                           false).toBool ())
+                      if (settings->value ("editor/create_new_file", false).toBool ())
                         {
                           answer = QMessageBox::Yes;
                         }
@@ -366,6 +383,7 @@
 
           // really show editor and the current editor tab
           set_focus ();
+          emit file_loaded_signal ();
         }
     }
 }
@@ -959,6 +977,8 @@
     _mru_file_menu->addAction (_mru_file_actions[i]);
 
   fileMenu->addAction (new_action);
+  fileMenu->addAction (QIcon (), tr ("New &Function"),
+                      this, SLOT (request_new_function (bool)));
   fileMenu->addAction (open_action);
   fileMenu->addMenu (_mru_file_menu);
   fileMenu->addSeparator ();
@@ -1150,6 +1170,9 @@
   connect (_tab_widget, SIGNAL (currentChanged (int)),
            this, SLOT (active_tab_changed (int)));
 
+  connect (this, SIGNAL (execute_command_in_terminal_signal (const QString&)),
+           main_win (), SLOT (execute_command_in_terminal (const QString&)));
+
   resize (500, 400);
   setWindowIcon (QIcon (":/actions/icons/logo.png"));
   set_title ("Editor");
--- a/libgui/src/m-editor/file-editor.h	Mon Nov 11 09:54:07 2013 -0500
+++ b/libgui/src/m-editor/file-editor.h	Mon Nov 11 22:01:27 2013 +0100
@@ -99,13 +99,15 @@
                                    int line = -1);
   void fetab_set_focus (const QWidget* ID);
   void request_settings_dialog (const QString&);
+  void execute_command_in_terminal_signal (const QString&);
+  void file_loaded_signal ();
 
 public slots:
   void focus (void);
 
   void request_new_file (const QString& commands);
   void request_new_script (const QString& commands);
-  void request_new_function (const QString& commands);
+  void request_new_function (bool triggered = true);
   void request_open_file (void);
   void request_close_file (bool);
   void request_close_all_files (bool);
@@ -176,6 +178,7 @@
                           bool breakpoint_marker = false, bool insert = true);
   void request_preferences (bool);
   void request_styles_preferences (bool);
+  void restore_create_file_setting ();
 
 private:
 
--- a/libgui/src/main-window.cc	Mon Nov 11 09:54:07 2013 -0500
+++ b/libgui/src/main-window.cc	Mon Nov 11 22:01:27 2013 +0100
@@ -1171,8 +1171,9 @@
                            tr ("Script"));
   _new_script_action->setShortcutContext (Qt::ApplicationShortcut);
 
-  QAction *new_function_action = new_menu->addAction (tr ("Function"));
-  new_function_action->setEnabled (true);
+  _new_function_action = new_menu->addAction (tr ("Function"));
+  _new_function_action->setEnabled (true);
+  _new_function_action->setShortcutContext (Qt::ApplicationShortcut);
 
   QAction *new_figure_action = new_menu->addAction (tr ("Figure"));
   new_figure_action->setEnabled (true);
@@ -1181,7 +1182,7 @@
   connect (_new_script_action, SIGNAL (triggered ()),
            editor_window, SLOT (request_new_script ()));
 
-  connect (new_function_action, SIGNAL (triggered ()),
+  connect (_new_function_action, SIGNAL (triggered ()),
            editor_window, SLOT (request_new_function ()));
 #endif
 
@@ -1809,6 +1810,9 @@
 
       _open_action->setShortcut (QKeySequence::Open);
       _new_script_action->setShortcut (QKeySequence::New);
+      _new_function_action->setShortcut (Qt::ControlModifier
+                                       + Qt::ShiftModifier
+                                       + Qt::Key_N);
 
       _exit_action->setShortcut (QKeySequence::Quit);
 
@@ -1824,6 +1828,7 @@
 
       _open_action->setShortcut (no_key);
       _new_script_action->setShortcut (no_key);
+      _new_function_action->setShortcut (no_key);
 
       _exit_action->setShortcut (no_key);
 
--- a/libgui/src/main-window.h	Mon Nov 11 09:54:07 2013 -0500
+++ b/libgui/src/main-window.h	Mon Nov 11 22:01:27 2013 +0100
@@ -303,6 +303,7 @@
   QAction *_debug_quit;
 
   QAction *_new_script_action;
+  QAction *_new_function_action;
   QAction *_open_action;
 
   QAction *_copy_action;