changeset 19807:034bcac0b61c

use of C++ API for running a file (bug #42307) * octave-cmd.cc: New file providing the new command class octave_cmd; (prepare_command_editor): common action that all derived command classes use for method execute; (octave_cmd_exec::execute): execute method of derived class for executing ordinary commands in the terminal; (octave_cmd_eval::execute): execute method of derived class for running a file (as function if it is a valid identifier or as script) * octave-cmd.h: providing base and derived classes * main-window.cc (main_window): _cmd_queue is not a pointer anymore; (~main_window): _cmd_queue is not a pointer anymore; (execute_command_in_terminal): use new command class for queuing a command; (run_file_in_terminal): check for valid identifier moved from here to the execute method of the related command class; (run_file_callback): use new class for queuing command, path check moved from here into the execute method of the command class; (queue_command): gets new command class instead of a string as input; (closeEvent): queue command class instead of string; (execute_command_callback): command queue contains command class instances; * main-window.h: use new octave-cmd class for queue_command, the command queue is not a pointer anymore * module.mk: new files octave-cmd.cc and octave-cmd.h
author Torsten <ttl@justmail.de>
date Thu, 19 Feb 2015 22:05:12 +0100
parents 32d9c8b904bc
children 904912f18357
files libgui/src/main-window.cc libgui/src/main-window.h libgui/src/module.mk libgui/src/octave-cmd.cc libgui/src/octave-cmd.h
diffstat 5 files changed, 186 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/main-window.cc	Thu Feb 19 22:03:53 2015 +0100
+++ b/libgui/src/main-window.cc	Thu Feb 19 22:05:12 2015 +0100
@@ -91,7 +91,7 @@
     community_news_window (0),
     _octave_qt_link (0),
     _clipboard (QApplication::clipboard ()),
-    _cmd_queue (new QStringList ()),  // no command pending
+    _cmd_queue (QList<octave_cmd *> ()),  // no command pending
     _cmd_processing (1),
     _cmd_queue_mutex (),
     _dbg_queue (new QStringList ()),  // no debug pending
@@ -162,7 +162,6 @@
       community_news_window = 0;
     }
   delete _octave_qt_link;
-  delete _cmd_queue;
 }
 
 // catch focus changes and determine the active dock widget
@@ -315,7 +314,8 @@
 void
 main_window::execute_command_in_terminal (const QString& command)
 {
-  queue_command (command);
+  octave_cmd_exec *cmd = new octave_cmd_exec (command);
+  queue_command (cmd);
   if (focus_console_after_command ())
     focus_command_window ();
 }
@@ -323,29 +323,6 @@
 void
 main_window::run_file_in_terminal (const QFileInfo& info)
 {
-  QString file_name = info.canonicalFilePath ();
-  QString command = "run \"" + file_name + "\"";
-
-  QString function_name = info.fileName ();
-  function_name.chop (info.suffix ().length () + 1);
-
-  if (! valid_identifier (function_name.toStdString ()))
-    {
-      int ans = QMessageBox::question (0, tr ("Octave"),
-         tr ("The file %1\n"
-             "can not be executed because its name\n"
-             "is not a valid identifier.\n\n"
-             "Do you want to execute\n%2\n"
-             "instead?").
-          arg (file_name).arg (command),
-          QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
-
-      if (ans == QMessageBox::Yes)
-        execute_command_in_terminal (command);
-
-      return;
-    }
-
   octave_link::post_event (this, &main_window::run_file_callback, info);
   if (focus_console_after_command ())
     focus_command_window ();
@@ -354,19 +331,15 @@
 void
 main_window::run_file_callback (const QFileInfo& info)
 {
-  QString dir = info.absolutePath ();
-  QString function_name = info.fileName ();
-  function_name.chop (info.suffix ().length () + 1);
-  if (octave_qt_link::file_in_path (info.absoluteFilePath ().toStdString (),
-                                    dir.toStdString ()))
-    queue_command (function_name);
+  octave_cmd_eval *cmd = new octave_cmd_eval (info);
+  queue_command (cmd);
 }
 
 void
-main_window::queue_command (QString command)
+main_window::queue_command (octave_cmd* cmd)
 {
   _cmd_queue_mutex.lock ();
-  _cmd_queue->append (command);   // queue command
+  _cmd_queue.append (cmd);     // queue command and type
   _cmd_queue_mutex.unlock ();
 
   if (_cmd_processing.tryAcquire ())  // if callback not processing, post event
@@ -1012,7 +985,8 @@
 main_window::closeEvent (QCloseEvent *e)
 {
   e->ignore ();
-  queue_command ("exit");
+  octave_cmd_exec *cmd = new octave_cmd_exec ("exit");
+  queue_command (cmd);
 }
 
 void
@@ -2067,26 +2041,21 @@
 {
   bool repost = false;          // flag for reposting event for this callback
 
-  if (!_cmd_queue->isEmpty ())  // list can not be empty here, just to make sure
+  if (! _cmd_queue.isEmpty ())  // list can not be empty here, just to make sure
     {
-      std::string pending_input = command_editor::get_current_line ();
-      command_editor::set_initial_input (pending_input);
-
       _cmd_queue_mutex.lock (); // critical path
-      std::string command = _cmd_queue->takeFirst ().toStdString ();
-      if (_cmd_queue->isEmpty ())
-        _cmd_processing.release ();  // cmd queue empty, processing will stop
+
+      octave_cmd *cmd = _cmd_queue.takeFirst ();
+
+      if (_cmd_queue.isEmpty ())
+          _cmd_processing.release ();  // cmd queue empty, processing will stop
       else
         repost = true;          // not empty, repost at end
       _cmd_queue_mutex.unlock ();
 
-      command_editor::replace_line (command);
-
-      command_editor::redisplay ();
-      // We are executing inside the command editor event loop.  Force
-      // the current line to be returned for processing.
-      Fdb_next_breakpoint_quiet (ovl (_suppress_dbg_location));
-      command_editor::accept_line ();
+      cmd->execute ();
+
+      delete cmd;
     }
 
   if (repost)  // queue not empty, so repost event for further processing
--- a/libgui/src/main-window.h	Thu Feb 19 22:03:53 2015 +0100
+++ b/libgui/src/main-window.h	Thu Feb 19 22:05:12 2015 +0100
@@ -57,6 +57,7 @@
 #include "octave-qt-link.h"
 #include "octave-dock-widget.h"
 #include "find-files-dialog.h"
+#include "octave-cmd.h"
 
 class settings_dialog;
 
@@ -276,7 +277,7 @@
 
   void change_directory_callback (const std::string& directory);
 
-  void queue_command (QString command);
+  void queue_command (octave_cmd *cmd);
 
   void queue_debug (QString command);
 
@@ -396,8 +397,12 @@
   // Flag for closing whole application.
   bool _closing;
 
-  // semaphore to synchronize execution signals and related callback
-  QStringList *_cmd_queue;
+  // command queue and semaphore to synchronize execution signals
+  // and related callback
+
+  // the queue for the command structures
+  QList<octave_cmd *> _cmd_queue;
+  // semaphores used for handling the queue
   QSemaphore   _cmd_processing;
   QMutex       _cmd_queue_mutex;
 
--- a/libgui/src/module.mk	Thu Feb 19 22:03:53 2015 +0100
+++ b/libgui/src/module.mk	Thu Feb 19 22:05:12 2015 +0100
@@ -130,6 +130,7 @@
   src/m-editor/octave-txt-lexer.h \
   src/main-window.h \
   src/octave-gui.h \
+  src/octave-cmd.h \
   src/octave-interpreter.h \
   src/octave-qt-link.h \
   src/qtinfo/parser.h \
@@ -157,6 +158,7 @@
   src/m-editor/octave-qscintilla.cc \
   src/m-editor/octave-txt-lexer.cc \
   src/main-window.cc \
+  src/octave-cmd.cc \
   src/octave-dock-widget.cc \
   src/octave-gui.cc \
   src/octave-interpreter.cc \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/octave-cmd.cc	Thu Feb 19 22:05:12 2015 +0100
@@ -0,0 +1,85 @@
+/*
+
+Copyright (C) 2014 Torsten
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+// Author: Torsten <ttl@justmail.de>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "octave-cmd.h"
+
+#include "octave-qt-link.h"
+#include "cmd-edit.h"
+#include "builtin-defun-decls.h"
+#include "utils.h"
+
+
+void
+octave_cmd::prepare_command_editor (const QString& cmd)
+{
+  std::string pending_input = command_editor::get_current_line ();
+
+  command_editor::set_initial_input (pending_input);
+  command_editor::replace_line (cmd.toStdString ());
+  command_editor::redisplay ();
+  // We are executing inside the command editor event loop.  Force
+  // the current line to be returned for processing.
+}
+
+
+// ---------------------------------------------------------------------
+//  class octave_cmd_exec: executing a command
+
+void
+octave_cmd_exec::execute ()
+{
+  prepare_command_editor (_cmd);
+  command_editor::accept_line ();
+}
+
+
+// ---------------------------------------------------------------------
+//  class octave_cmd_eval: running a file
+
+void
+octave_cmd_eval::execute ()
+{
+  QString function_name = _info.fileName ();
+  function_name.chop (_info.suffix ().length () + 1);
+  std::string file_path = _info.absoluteFilePath ().toStdString ();
+
+  prepare_command_editor ("");
+
+  if (valid_identifier (function_name.toStdString ()))
+    { // valid identifier: call as function with possibility to debug
+      std::string path = _info.absolutePath ().toStdString ();
+      if (octave_qt_link::file_in_path (file_path, path))
+          Feval (ovl (function_name.toStdString ()));
+    }
+  else
+    { // no valid identifier: use Fsource (), no debug possible
+      Fsource (ovl (file_path));
+    }
+
+  command_editor::accept_line ();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/octave-cmd.h	Thu Feb 19 22:05:12 2015 +0100
@@ -0,0 +1,73 @@
+/*
+
+Copyright (C) 2014 Torsten
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+// Author: Torsten <ttl@justmail.de>
+
+#if !defined (octave_cmd_h)
+#define octave_cmd_h 1
+
+#include <QString>
+#include <QFileInfo>
+
+class octave_cmd
+{
+public:
+
+  octave_cmd () { };
+  virtual ~octave_cmd () { };
+
+  virtual void execute () { };
+  void prepare_command_editor (const QString& cmd);
+};
+
+
+// ---------------------------------------------------------------------
+//  class octave_cmd_exec
+
+class octave_cmd_exec : public octave_cmd
+{
+public:
+
+  octave_cmd_exec (const QString& cmd) : octave_cmd () { _cmd = cmd; };
+  void execute ();
+
+private:
+
+  QString _cmd;
+};
+
+
+// ---------------------------------------------------------------------
+//  class octave_cmd_eval
+
+class octave_cmd_eval : public octave_cmd
+{
+public:
+
+  octave_cmd_eval (const QFileInfo& info) : octave_cmd () { _info = info; };
+  void execute ();
+
+private:
+
+  QFileInfo _info;
+};
+#endif