comparison command-window.cpp @ 17:2ddf3fe6fa33

initial attempt at using separate thread for interpreter
author John W. Eaton <jwe@octave.org>
date Tue, 28 May 2019 18:27:18 -0400
parents 79783f3e2017
children
comparison
equal deleted inserted replaced
16:bfa9aaa2d608 17:2ddf3fe6fa33
114 // ^^^^^ readline ^^^^^ 114 // ^^^^^ readline ^^^^^
115 115
116 command_window *command_window::the_command_window = nullptr; 116 command_window *command_window::the_command_window = nullptr;
117 117
118 command_window::command_window (QWidget *p) 118 command_window::command_window (QWidget *p)
119 : QTextEdit (p), m_buffer (new QTextDocument ()), 119 : QTextEdit (p),
120 #if defined (CALC_USE_INTERPRETER_THREAD)
121 m_interpreter_thread (),
122 #endif
123 m_buffer (new QTextDocument ()),
120 m_interpreter (new calc::qt_interpreter ()), 124 m_interpreter (new calc::qt_interpreter ()),
121 beg_mark (), prompt_mark () 125 beg_mark (), prompt_mark ()
122 { 126 {
123 if (the_command_window) 127 if (the_command_window)
124 { 128 {
131 setWindowTitle ("Qt::TextEdit example"); 135 setWindowTitle ("Qt::TextEdit example");
132 136
133 setMinimumSize (QSize (600, 400)); 137 setMinimumSize (QSize (600, 400));
134 138
135 setDocument (m_buffer); 139 setDocument (m_buffer);
140
141 #if defined (CALC_USE_INTERPRETER_THREAD)
142 m_interpreter->moveToThread (&m_interpreter_thread);
143
144 connect (&m_interpreter_thread, &QThread::finished,
145 m_interpreter, &QObject::deleteLater);
146 #endif
136 147
137 connect (m_interpreter, SIGNAL (result_ready (double)), 148 connect (m_interpreter, SIGNAL (result_ready (double)),
138 this, SLOT (handle_result (double))); 149 this, SLOT (handle_result (double)));
139 150
140 connect (m_interpreter, SIGNAL (error_signal (const QString&)), 151 connect (m_interpreter, SIGNAL (error_signal (const QString&)),
161 172
162 beg_mark = set_mark (); 173 beg_mark = set_mark ();
163 174
164 readline_init (); 175 readline_init ();
165 176
177 #if defined (CALC_USE_INTERPRETER_THREAD)
178 m_interpreter_thread.start ();
179 #endif
180
166 // Defer initializing and executing the interpreter until after the main 181 // Defer initializing and executing the interpreter until after the main
167 // window and QApplication are running to prevent race conditions 182 // window and QApplication are running to prevent race conditions
168 QTimer::singleShot (0, m_interpreter, SLOT (execute (void))); 183 QTimer::singleShot (0, m_interpreter, SLOT (execute (void)));
169 } 184 }
170 185
171 command_window::~command_window (void) 186 command_window::~command_window (void)
172 { 187 {
173 readline_fini (); 188 readline_fini ();
189
190 #if defined (CALC_USE_INTERPRETER_THREAD)
191 m_interpreter_thread.quit ();
192 m_interpreter_thread.wait ();
193 #endif
174 } 194 }
175 195
176 // Accept an input line, parse and possibly execute it. 196 // Accept an input line, parse and possibly execute it.
177 197
178 void command_window::accept_line (const std::string& line) 198 void command_window::accept_line (const std::string& line)
183 { 203 {
184 add_history (line.c_str ()); 204 add_history (line.c_str ());
185 using_history (); 205 using_history ();
186 206
187 emit accept_line_signal (QString::fromStdString (line)); 207 emit accept_line_signal (QString::fromStdString (line));
208
209 // FIXME: how to wait until interpreter is finished and
210 // results are printed before returning to readline callback
211 // that will print the next prompt?
188 } 212 }
189 } 213 }
190 214
191 void command_window::eof (void) 215 void command_window::eof (void)
192 { 216 {