GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
interpreter-qobject.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "interpreter-qobject.h"
31 #include "octave-qobject.h"
32 #include "qt-application.h"
33 #include "qt-interpreter-events.h"
34 
35 #include "graphics-init.h"
36 #include "input.h"
37 #include "interpreter.h"
38 
40 
42 : QObject (), m_octave_qobj (oct_qobj), m_interpreter (nullptr)
43 { }
44 
46 {
47  // The Octave application context owns the interpreter.
48 
49  qt_application& app_context = m_octave_qobj.app_context ();
50 
51  interpreter& interp = app_context.create_interpreter ();
52 
53  event_manager& evmgr = interp.get_event_manager ();
54 
57  evmgr.enable ();
58 
59  int exit_status = 0;
60 
61  try
62  {
63  // Final initialization.
64 
65  interp.initialize ();
66 
67  if (app_context.start_gui_p ()
69  {
70  input_system& input_sys = interp.get_input_system ();
71 
72  input_sys.PS1 (">> ");
73  input_sys.PS2 ("");
74  }
75 
76  if (interp.initialized ())
77  {
78  // The interpreter should be completely ready at this point so let
79  // the GUI know.
80 
81  m_interpreter = &interp;
82 
83  emit ready ();
84 
85  graphics_init (interp, m_octave_qobj);
86 
87  // Start executing commands in the command window.
88 
89  exit_status = interp.execute ();
90  }
91  }
92  catch (const exit_exception& xe)
93  {
94  exit_status = xe.exit_status ();
95  }
96 
97  // FIXME: The following comment doesn't seem to make sense now.
98 
99  // Signal that the interpreter is done executing code in the
100  // main REPL, from script files, or command line eval arguments.
101  // By using a signal here, we give the GUI a chance to process
102  // any pending events, then signal that it is safe to shutdown
103  // the interpreter. Our notification here allows the GUI to
104  // insert the request to shutdown the interpreter in the event
105  // queue after any other pending signals. The application
106  // context owns the interpreter and will be responsible for
107  // deleting it later, when the application object destructor is
108  // executed.
109 
110  emit shutdown_finished (exit_status);
111 }
112 
114 {
115  if (! m_interpreter)
116  return;
117 
119 
120  evmgr.post_event (fcn);
121 }
122 
124 {
125  if (! m_interpreter)
126  return;
127 
129 
130  evmgr.post_event (meth);
131 }
132 
134 {
135  if (! m_interpreter)
136  return;
137 
138  // The following is a direct function call across threads.
139  // We need to ensure that it uses thread-safe functions.
140 
142 }
143 
145 {
146  // FIXME: Should we make this action work with the old terminal
147  // widget?
148 
150  {
151  if (! m_interpreter)
152  return;
153 
154  // The following is a direct function call across threads.
155  // We need to ensure that it uses thread-safe functions.
156 
157  m_interpreter->pause ();
158  }
159 }
160 
162 {
163  // FIXME: Should we make this action work with the old terminal
164  // widget?
165 
167  {
168  if (! m_interpreter)
169  return;
170 
171  // The following is a direct function call across threads.
172  // We need to ensure that it uses thread-safe functions.
173 
174  m_interpreter->stop ();
175  }
176 }
177 
179 {
180  // FIXME: Should we make this action work with the old terminal
181  // widget?
182 
184  {
185  // FIXME: This action should only be available when the
186  // interpreter is paused.
187 
189  ([=] (interpreter& interp)
190  {
191  // INTERPRETER THREAD
192 
193  interp.resume ();
194  });
195  }
196 }
197 
199 {
200  return m_octave_qobj.qt_link ();
201 }
202 
OCTAVE_END_NAMESPACE(octave)
virtual interpreter & create_interpreter(void)
Definition: octave.cc:345
Base class for Octave interfaces that use Qt.
bool experimental_terminal_widget(void) const
qt_interpreter_events * qt_link(void)
qt_application & app_context(void)
std::shared_ptr< qt_interpreter_events > get_qt_interpreter_events(void)
Provides threadsafe access to octave.
OCTINTERP_API void connect_link(const std::shared_ptr< interpreter_events > &obj)
void install_qt_event_handlers(const std::shared_ptr< interpreter_events > &obj)
OCTINTERP_API bool enable(void)
OCTINTERP_API void post_event(const fcn_callback &fcn)
octave_value PS2(const octave_value_list &args, int nargout)
octave_value PS1(const octave_value_list &args, int nargout)
interpreter * m_interpreter
void execute(void)
Initialize and execute the octave interpreter.
base_qobject & m_octave_qobj
void interpreter_event(const fcn_callback &fcn)
void shutdown_finished(int)
qt_interpreter_events * qt_link(void)
void resume(void)
event_manager & get_event_manager(void)
Definition: interpreter.h:328
void interrupt(void)
void pause(void)
void stop(void)
bool initialized(void) const
Definition: interpreter.h:216
void initialize(void)
Definition: interpreter.cc:739
int execute(void)
Definition: interpreter.cc:841
input_system & get_input_system(void)
Definition: interpreter.h:263
This class inherits from the pure-virtual base class application and provides an implementation of th...
bool start_gui_p(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::function< void(void)> fcn_callback
Definition: event-manager.h:43
std::function< void(interpreter &)> meth_callback
Definition: event-manager.h:48
void graphics_init(interpreter &interp, base_qobject &oct_qobj)