Mercurial > jwe > qt-gui-with-push-parser
annotate gui-main.cpp @ 1:08df60a01bc1
debug flag, handle input with signal
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 20 May 2019 13:45:58 -0400 |
parents | dff751fb985c |
children | 52c033864347 |
rev | line source |
---|---|
0 | 1 #include <iostream> |
2 #include <sstream> | |
3 #include <string> | |
4 | |
5 #include <cstdlib> | |
6 #include <cstring> | |
7 | |
8 #include <QApplication> | |
9 #include <QKeyEvent> | |
10 #include <QTextDocument> | |
11 #include <QTextEdit> | |
12 | |
13 #include "gui-main.h" | |
14 | |
15 #include "gui-main.h" | |
16 #include "parse.h" | |
17 | |
18 #include <readline/readline.h> | |
19 #include <readline/history.h> | |
20 | |
21 namespace gui | |
22 { | |
23 static int available_char = 0; | |
24 | |
25 static inline int ctrl (int c) | |
26 { | |
27 return c & 0x1f; | |
28 } | |
29 | |
30 static command_window *calc_interaction_window = 0; | |
31 | |
32 static int getc (FILE *) | |
33 { | |
34 int tmp = available_char; | |
35 available_char = 0; | |
36 return tmp; | |
37 } | |
38 | |
39 static void redisplay (void) | |
40 { | |
41 if (calc_interaction_window) | |
42 calc_interaction_window->redisplay (); | |
43 } | |
44 | |
45 static void prep_term (int) | |
46 { | |
47 } | |
48 | |
49 static void deprep_term (void) | |
50 { | |
51 } | |
52 | |
53 static void accept_line (char *line) | |
54 { | |
55 if (calc_interaction_window) | |
56 calc_interaction_window->accept (line ? line : ""); | |
57 } | |
58 | |
59 static void display_completion_matches (char **matches, int num_matches, | |
60 int /* max_length */) | |
61 { | |
62 if (calc_interaction_window) | |
63 { | |
64 std::ostringstream buf; | |
65 | |
66 if (num_matches > 1) | |
67 buf << "\n"; | |
68 | |
69 for (int i = 1; i < num_matches; i++) | |
70 buf << matches[i] << "\n"; | |
71 | |
72 calc_interaction_window->insert_at_end (buf.str ()); | |
73 | |
74 calc_interaction_window->redisplay (); | |
75 } | |
76 } | |
77 | |
78 static void readline_init (void) | |
79 { | |
80 rl_initialize (); | |
81 | |
82 rl_getc_function = getc; | |
83 rl_redisplay_function = redisplay; | |
84 rl_prep_term_function = prep_term; | |
85 rl_deprep_term_function = deprep_term; | |
86 rl_completion_display_matches_hook = display_completion_matches; | |
87 | |
88 rl_callback_handler_install (">> ", accept_line); | |
89 } | |
90 | |
91 static void readline_fini (void) | |
92 { | |
93 rl_callback_handler_remove (); | |
94 } | |
95 | |
96 command_window::command_window (QWidget *p) | |
97 : QTextEdit (p), | |
98 m_buffer (new QTextDocument ()), | |
99 beg_mark (), prompt_mark () | |
100 { | |
101 setWindowTitle ("Qt::TextEdit example"); | |
102 | |
103 setMinimumSize (QSize (600, 400)); | |
104 | |
105 setDocument (m_buffer); | |
106 | |
107 connect (this, SIGNAL (result_available (double)), | |
108 this, SLOT (handle_result (double))); | |
109 | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
110 connect (this, SIGNAL (input_char_available (int)), |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
111 this, SLOT (handle_input_char (int))); |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
112 |
0 | 113 insert_at_end |
114 ("Qt Example Calculator.\n" | |
115 "Available operations: + - * / ^ ()\n" | |
116 "Semicolon terminates statement.\n" | |
117 "Up Arrow key moves to previous line in the command history.\n" | |
118 "Down Arrow key moves to next line in the comand history.\n\n"); | |
119 | |
120 beg_mark = set_mark (); | |
121 } | |
122 | |
123 // Accept an input line, parse and possibly execute it. | |
124 | |
125 void command_window::accept (const std::string& line) | |
126 { | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
127 if (calc::debug_mode) |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
128 std::cerr << "accept: " << line << std::endl; |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
129 |
0 | 130 insert_at_end ("\n"); |
131 | |
132 if (! line.empty ()) | |
133 { | |
134 add_history (line.c_str ()); | |
135 using_history (); | |
136 | |
137 // FIXME: what is the right thing to do with status returned | |
138 // by parser. | |
139 interpreter::parse_and_execute (line); | |
140 } | |
141 } | |
142 | |
143 // Redisplay current command line. | |
144 | |
145 void command_window::redisplay (void) | |
146 { | |
147 erase_line (); | |
148 | |
149 std::string line = rl_line_buffer ? rl_line_buffer : ""; | |
150 std::string prompt = (rl_prompt && interpreter::beg_of_stmt) ? rl_prompt : ""; | |
151 | |
152 insert_line (prompt, line); | |
153 | |
154 scroll_to_bottom (); | |
155 | |
156 QTextCursor cursor = textCursor (); | |
157 | |
158 cursor.setPosition (prompt_mark + rl_point, QTextCursor::MoveAnchor); | |
159 | |
160 setTextCursor (cursor); | |
161 } | |
162 | |
163 void command_window::insert_at_end (const std::string& text) | |
164 { | |
165 scroll_to_bottom (); | |
166 | |
167 insert_at_cursor (text); | |
168 } | |
169 | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
170 void command_window::emit_error (const std::string& msg) |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
171 { |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
172 insert_at_end ("parse error: " + msg); |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
173 |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
174 rl_abort (0, 0); |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
175 } |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
176 |
0 | 177 void command_window::emit_result (double value) |
178 { | |
179 emit result_available (value); | |
180 } | |
181 | |
182 // FIXME: do we really need this extra function? | |
183 void command_window::handle_result (double value) | |
184 { | |
185 insert_result (value); | |
186 } | |
187 | |
188 void command_window::keyPressEvent (QKeyEvent *event) | |
189 { | |
190 if (! event) | |
191 return; | |
192 | |
193 if (event->type () == QEvent::KeyPress) | |
194 { | |
195 int key = event->key (); | |
196 | |
197 switch (key) | |
198 { | |
199 case Qt::Key_Return: | |
200 key = 0x0A; | |
201 break; | |
202 | |
203 case Qt::Key_Backspace: | |
204 key = 0x08; | |
205 break; | |
206 | |
207 case Qt::Key_Tab: | |
208 key = 0x09; | |
209 break; | |
210 | |
211 case Qt::Key_Escape: | |
212 key = 0x1b; | |
213 break; | |
214 | |
215 case Qt::Key_Up: | |
216 case Qt::Key_Down: | |
217 case Qt::Key_Right: | |
218 case Qt::Key_Left: | |
219 key = do_arrow_key (key); | |
220 break; | |
221 | |
222 default: | |
223 { | |
224 switch (event->modifiers ()) | |
225 { | |
226 case Qt::ControlModifier: | |
227 if (key > 0x3f && key < 0x7b) | |
228 key &= 0x1f; | |
229 else | |
230 key = -1; | |
231 break; | |
232 | |
233 default: | |
234 { | |
235 // Don't shoot me, this is just a demo... | |
236 QString text = event->text (); | |
237 QByteArray latin_text = text.toLatin1 (); | |
238 key = latin_text[0]; | |
239 } | |
240 break; | |
241 } | |
242 } | |
243 break; | |
244 } | |
245 | |
246 if (key >= 0) | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
247 emit input_char_available (key); |
0 | 248 } |
249 } | |
250 | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
251 |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
252 void command_window::handle_input_char (int key) |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
253 { |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
254 available_char = key; |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
255 rl_callback_read_char (); |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
256 } |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
257 |
0 | 258 int command_window::do_arrow_key (int arrow_key) |
259 { | |
260 int retval = 0; | |
261 | |
262 available_char = 0x1b; | |
263 rl_callback_read_char (); | |
264 | |
265 available_char = '['; | |
266 rl_callback_read_char (); | |
267 | |
268 switch (arrow_key) | |
269 { | |
270 case Qt::Key_Up: | |
271 retval = 'A'; | |
272 break; | |
273 | |
274 case Qt::Key_Down: | |
275 retval = 'B'; | |
276 break; | |
277 | |
278 case Qt::Key_Right: | |
279 retval = 'C'; | |
280 break; | |
281 | |
282 case Qt::Key_Left: | |
283 retval = 'D'; | |
284 break; | |
285 } | |
286 | |
287 return retval; | |
288 } | |
289 | |
290 // Retrieve a command from the history list and insert it on the | |
291 // current command. | |
292 | |
293 void command_window::history (bool up) | |
294 { | |
295 HIST_ENTRY *entry = up ? previous_history () : next_history (); | |
296 | |
297 if (entry) | |
298 { | |
299 erase_line (); | |
300 | |
301 std::string prompt = rl_prompt ? rl_prompt : ""; | |
302 | |
303 insert_line (prompt, entry->line); | |
304 } | |
305 else if (! up) | |
306 erase_line (); | |
307 | |
308 scroll_to_bottom (); | |
309 } | |
310 | |
311 void command_window::erase_line (void) | |
312 { | |
313 QTextCursor cursor = textCursor (); | |
314 | |
315 cursor.movePosition (QTextCursor::End); | |
316 cursor.select (QTextCursor::LineUnderCursor); | |
317 cursor.removeSelectedText (); | |
318 | |
319 setTextCursor (cursor); | |
320 } | |
321 | |
322 void command_window::insert_at_cursor (const std::string& text) | |
323 { | |
324 QTextCursor cursor = textCursor (); | |
325 | |
326 cursor.insertText (QString::fromStdString (text)); | |
327 | |
328 setTextCursor (cursor); | |
329 } | |
330 | |
331 void command_window::insert_line (const std::string& prompt, | |
332 const std::string& line) | |
333 { | |
334 beg_mark = set_mark (); | |
335 | |
336 insert_at_cursor (prompt); | |
337 | |
338 prompt_mark = set_mark (); | |
339 | |
340 insert_at_cursor (line); | |
341 } | |
342 | |
343 int command_window::set_mark (void) | |
344 { | |
345 return textCursor ().position (); | |
346 } | |
347 | |
348 void command_window::insert_result (double value) | |
349 { | |
350 std::ostringstream buf; | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
351 |
0 | 352 buf << "ans = " << value << "\n"; |
353 | |
354 insert_at_cursor (buf.str ()); | |
355 | |
356 beg_mark = set_mark (); | |
357 | |
358 scroll_to_bottom (); | |
359 } | |
360 | |
361 void command_window::scroll_to_bottom (void) | |
362 { | |
363 QTextCursor cursor = textCursor (); | |
364 | |
365 cursor.movePosition (QTextCursor::End); | |
366 | |
367 setTextCursor (cursor); | |
368 } | |
369 | |
370 int main (int argc, char *argv[]) | |
371 { | |
372 interpreter::parser_init (); | |
373 | |
374 QApplication app (argc, argv); | |
375 | |
376 calc_interaction_window = new command_window (); | |
377 | |
378 calc_interaction_window->show (); | |
379 | |
380 readline_init (); | |
381 | |
382 int status = app.exec (); | |
383 | |
384 readline_fini (); | |
385 | |
386 interpreter::parser_fini (); | |
387 | |
388 return status; | |
389 } | |
390 | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
391 void emit_error (const std::string& msg) |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
392 { |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
393 calc_interaction_window->emit_error (msg); |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
394 } |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
395 |
0 | 396 void emit_result (double value) |
397 { | |
398 calc_interaction_window->emit_result (value); | |
399 } | |
400 } | |
401 | |
402 // -- Variable: rl_getc_func_t * rl_getc_function | |
403 // If non-zero, Readline will call indirectly through this pointer to | |
404 // get a character from the input stream. By default, it is set to | |
405 // `rl_getc', the default Readline character input function (*note | |
406 // Character Input::). | |
407 | |
408 // -- Variable: rl_voidfunc_t * rl_redisplay_function | |
409 // If non-zero, Readline will call indirectly through this pointer to | |
410 // update the display with the current contents of the editing buffer. | |
411 // By default, it is set to `rl_redisplay', the default Readline | |
412 // redisplay function (*note Redisplay::). | |
413 | |
414 // -- Variable: rl_vintfunc_t * rl_prep_term_function | |
415 // If non-zero, Readline will call indirectly through this pointer to | |
416 // initialize the terminal. The function takes a single argument, an | |
417 // `int' flag that says whether or not to use eight-bit characters. | |
418 // By default, this is set to `rl_prep_terminal' (*note Terminal | |
419 // Management::). | |
420 | |
421 // -- Variable: rl_voidfunc_t * rl_deprep_term_function | |
422 // If non-zero, Readline will call indirectly through this pointer to | |
423 // reset the terminal. This function should undo the effects of | |
424 // `rl_prep_term_function'. By default, this is set to | |
425 // `rl_deprep_terminal' (*note Terminal Management::). | |
426 | |
427 // -- Function: void rl_callback_handler_install (const char *prompt, | |
428 // rl_vcpfunc_t *lhandler) | |
429 // Set up the terminal for readline I/O and display the initial | |
430 // expanded value of PROMPT. Save the value of LHANDLER to use as a | |
431 // function to call when a complete line of input has been entered. | |
432 // The function takes the text of the line as an argument. | |
433 | |
434 // -- Function: void rl_callback_read_char (void) | |
435 // Whenever an application determines that keyboard input is | |
436 // available, it should call `rl_callback_read_char()', which will | |
437 // read the next character from the current input source. If that | |
438 // character completes the line, `rl_callback_read_char' will invoke | |
439 // the LHANDLER function saved by `rl_callback_handler_install' to | |
440 // process the line. Before calling the LHANDLER function, the | |
441 // terminal settings are reset to the values they had before calling | |
442 // `rl_callback_handler_install'. If the LHANDLER function returns, | |
443 // the terminal settings are modified for Readline's use again. | |
444 // `EOF' is indicated by calling LHANDLER with a `NULL' line. | |
445 | |
446 // -- Function: void rl_callback_handler_remove (void) | |
447 // Restore the terminal to its initial state and remove the line | |
448 // handler. This may be called from within a callback as well as | |
449 // independently. If the LHANDLER installed by | |
450 // `rl_callback_handler_install' does not exit the program, either | |
451 // this function or the function referred to by the value of | |
452 // `rl_deprep_term_function' should be called before the program | |
453 // exits to reset the terminal settings. | |
454 | |
455 // -- Variable: rl_compdisp_func_t * rl_completion_display_matches_hook | |
456 // If non-zero, then this is the address of a function to call when | |
457 // completing a word would normally display the list of possible | |
458 // matches. This function is called in lieu of Readline displaying | |
459 // the list. It takes three arguments: (`char **'MATCHES, `int' | |
460 // NUM_MATCHES, `int' MAX_LENGTH) where MATCHES is the array of | |
461 // matching strings, NUM_MATCHES is the number of strings in that | |
462 // array, and MAX_LENGTH is the length of the longest string in that | |
463 // array. Readline provides a convenience function, | |
464 // `rl_display_match_list', that takes care of doing the display to | |
465 // Readline's output stream. That function may be called from this | |
466 // hook. |