changeset 17884:766ad9be2966

Prompt for non-existent new file when using edit.m from GUI (bug #40450) * libgui/src/octave-qt-link.cc(do_prompt_new_edit_file): New function to display question dialog. * libgui/src/octave-qt-link.h(do_prompt_new_edit_file): Add function to header file. * libinterp/corefcn/octave-link.cc(__octave_link_edit_file__): If 2nd argument present, display prompt dialog. * libinterp/corefcn/octave-link.h(prompt_new_edit_file): If octave-link enabled, use prompt dialog box. * scripts/miscellaneous/edit.m: Modify to use new prompt routine. CLI behavior is unchanged.
author Torsten <ttl@justmail.de>
date Fri, 08 Nov 2013 07:40:36 -0800
parents 3851e5fde76d
children 1d109119ac71
files libgui/src/octave-qt-link.cc libgui/src/octave-qt-link.h libinterp/corefcn/octave-link.cc libinterp/corefcn/octave-link.h scripts/miscellaneous/edit.m
diffstat 5 files changed, 71 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/octave-qt-link.cc	Thu Nov 07 10:39:30 2013 -0500
+++ b/libgui/src/octave-qt-link.cc	Fri Nov 08 07:40:36 2013 -0800
@@ -27,6 +27,8 @@
 #endif
 
 #include <QStringList>
+#include <QDialog>
+#include <QDir>
 
 #include "str-vec.h"
 #include "dialog.h"
@@ -39,6 +41,8 @@
 
 #include "octave-qt-link.h"
 
+#include "resource-manager.h"
+
 octave_qt_link::octave_qt_link (octave_main_thread *mt)
   : octave_link (), main_thread (mt)
 { }
@@ -67,6 +71,34 @@
   return true;
 }
 
+bool
+octave_qt_link::do_prompt_new_edit_file (const std::string& file)
+{
+  QSettings *settings = resource_manager::get_settings ();
+
+  if (settings->value ("editor/create_new_file",false).toBool ())
+    return true;
+
+  QFileInfo file_info (QString::fromStdString (file));
+  QStringList btn;
+  QStringList role;
+  role << "AcceptRole" << "AcceptRole";
+  btn << tr ("Yes") << tr ("No");
+
+  uiwidget_creator.signal_dialog (
+    tr ("File\n%1\ndoes not exist. Do you want to create it?").
+    arg (QDir::currentPath () + QDir::separator ()
+         + QString::fromStdString (file)),
+    tr ("Octave Editor"), "quest", btn, tr ("Yes"), role );
+
+  // Wait while the user is responding to message box.
+  uiwidget_creator.wait ();
+  // The GUI has sent a signal and the process has been awakened.
+  QString answer = uiwidget_creator.get_dialog_button ();
+
+  return (answer == tr ("Yes"));
+}
+
 int
 octave_qt_link::do_message_dialog (const std::string& dlg,
                                    const std::string& msg,
--- a/libgui/src/octave-qt-link.h	Thu Nov 07 10:39:30 2013 -0500
+++ b/libgui/src/octave-qt-link.h	Fri Nov 08 07:40:36 2013 -0800
@@ -61,6 +61,7 @@
   bool do_exit (int status);
 
   bool do_edit_file (const std::string& file);
+  bool do_prompt_new_edit_file (const std::string& file);
 
   int do_message_dialog (const std::string& dlg, const std::string& msg,
                          const std::string& title);
--- a/libinterp/corefcn/octave-link.cc	Thu Nov 07 10:39:30 2013 -0500
+++ b/libinterp/corefcn/octave-link.cc	Fri Nov 08 07:40:36 2013 -0800
@@ -132,6 +132,19 @@
       else
         error ("expecting file name as argument");
     }
+  else if (args.length () == 2)
+    {
+      std::string file = args(0).string_value ();
+
+      if (! error_state)
+        {
+          flush_octave_stdout ();
+
+          retval = octave_link::prompt_new_edit_file (file);
+        }
+      else
+        error ("expecting file name as first argument");
+    }
 
   return retval;
 }
--- a/libinterp/corefcn/octave-link.h	Thu Nov 07 10:39:30 2013 -0500
+++ b/libinterp/corefcn/octave-link.h	Fri Nov 08 07:40:36 2013 -0800
@@ -136,6 +136,12 @@
     return enabled () ? instance->do_edit_file (file) : false;
   }
 
+  static bool
+  prompt_new_edit_file (const std::string& file)
+  {
+    return enabled () ? instance->do_prompt_new_edit_file (file) : false;
+  }
+
   static int
   message_dialog (const std::string& dlg, const std::string& msg,
                   const std::string& title)
@@ -385,6 +391,7 @@
   virtual bool do_exit (int status) = 0;
 
   virtual bool do_edit_file (const std::string& file) = 0;
+  virtual bool do_prompt_new_edit_file (const std::string& file) = 0;
 
   virtual int
   do_message_dialog (const std::string& dlg, const std::string& msg,
--- a/scripts/miscellaneous/edit.m	Thu Nov 07 10:39:30 2013 -0500
+++ b/scripts/miscellaneous/edit.m	Fri Nov 08 07:40:36 2013 -0800
@@ -353,14 +353,30 @@
       endif
     endif
 
-    ## If editing a new file that is neither an m-file or an oct-file,
+    ## If editing a new file, prompt for creation if gui is running
+    if (isguirunning ())
+      if (! __octave_link_edit_file__ (file,"prompt"));
+        return;
+      endif
+    endif
+
+    ## If editing a new file that is neither an m-file nor an oct-file,
     ## just edit it.
+    ## If in gui-mode, create it before or editor would prompt again.
     fileandpath = file;
     idx = rindex (file, ".");
     name = file(1:idx-1);
     ext = file(idx+1:end);
     if (! any (strcmp (ext, {"cc", "m"})))
-      ## Some unknown file.  Just open it up.
+      ## Some unknown file.  Create and open it or just open it.
+      if (isguirunning ())
+        ## Write the initial file (if there is anything to write)
+        fid = fopen (fileandpath, "wt");
+        if (fid < 0)
+          error ("edit: could not create %s", fileandpath);
+        endif
+        fclose (fid);
+      endif
       do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE);
       return;
     endif