Mercurial > jwe > qt-gui-with-push-parser
comparison gui-main.cpp @ 5:54edd85237ab
use signal to send input to qt interpreter object
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 May 2019 18:07:37 -0400 |
parents | 0e154787183d |
children | 04867eba6428 |
comparison
equal
deleted
inserted
replaced
4:0e154787183d | 5:54edd85237ab |
---|---|
21 #include <readline/readline.h> | 21 #include <readline/readline.h> |
22 #include <readline/history.h> | 22 #include <readline/history.h> |
23 | 23 |
24 namespace gui | 24 namespace gui |
25 { | 25 { |
26 int available_char = 0; | 26 static int available_char = 0; |
27 | 27 |
28 command_window *calc_interaction_window = 0; | 28 static command_window *calc_interaction_window = 0; |
29 | 29 |
30 static inline int ctrl (int c) | 30 static inline int ctrl (int c) |
31 { | 31 { |
32 return c & 0x1f; | 32 return c & 0x1f; |
33 } | |
34 | |
35 static int getc (FILE *) | |
36 { | |
37 int tmp = available_char; | |
38 available_char = 0; | |
39 return tmp; | |
40 } | |
41 | |
42 static void redisplay (void) | |
43 { | |
44 if (calc_interaction_window) | |
45 calc_interaction_window->redisplay (); | |
46 } | |
47 | |
48 static void prep_term (int) | |
49 { | |
50 } | |
51 | |
52 static void deprep_term (void) | |
53 { | |
54 } | |
55 | |
56 static void accept_line (char *line) | |
57 { | |
58 if (calc_interaction_window) | |
59 calc_interaction_window->accept_line (line ? line : ""); | |
60 } | |
61 | |
62 static void display_completion_matches (char **matches, int num_matches, | |
63 int /* max_length */) | |
64 { | |
65 if (calc_interaction_window) | |
66 { | |
67 std::ostringstream buf; | |
68 | |
69 if (num_matches > 1) | |
70 buf << "\n"; | |
71 | |
72 for (int i = 1; i < num_matches; i++) | |
73 buf << matches[i] << "\n"; | |
74 | |
75 calc_interaction_window->insert_at_end (buf.str ()); | |
76 | |
77 calc_interaction_window->redisplay (); | |
78 } | |
79 } | |
80 | |
81 static void readline_init (void) | |
82 { | |
83 rl_initialize (); | |
84 | |
85 rl_getc_function = getc; | |
86 rl_redisplay_function = redisplay; | |
87 rl_prep_term_function = prep_term; | |
88 rl_deprep_term_function = deprep_term; | |
89 rl_completion_display_matches_hook = display_completion_matches; | |
90 | |
91 rl_callback_handler_install (">> ", accept_line); | |
92 } | |
93 | |
94 static void readline_fini (void) | |
95 { | |
96 rl_callback_handler_remove (); | |
33 } | 97 } |
34 | 98 |
35 command_window::command_window (QWidget *p) | 99 command_window::command_window (QWidget *p) |
36 : QTextEdit (p), | 100 : QTextEdit (p), |
37 m_buffer (new QTextDocument ()), | 101 m_buffer (new QTextDocument ()), |
47 connect (this, SIGNAL (result_available (double)), | 111 connect (this, SIGNAL (result_available (double)), |
48 this, SLOT (handle_result (double))); | 112 this, SLOT (handle_result (double))); |
49 | 113 |
50 connect (this, SIGNAL (input_char_available (int)), | 114 connect (this, SIGNAL (input_char_available (int)), |
51 this, SLOT (handle_input_char (int))); | 115 this, SLOT (handle_input_char (int))); |
116 | |
117 connect (this, SIGNAL (accept_line_signal (const QString&)), | |
118 m_interpreter, SLOT (accept_input_line (const QString&))); | |
52 | 119 |
53 insert_at_end | 120 insert_at_end |
54 ("Qt Example Calculator.\n" | 121 ("Qt Example Calculator.\n" |
55 "Available operations: + - * / ^ ()\n" | 122 "Available operations: + - * / ^ ()\n" |
56 "Semicolon terminates statement.\n" | 123 "Semicolon terminates statement.\n" |
64 QTimer::singleShot (0, m_interpreter, SLOT (execute (void))); | 131 QTimer::singleShot (0, m_interpreter, SLOT (execute (void))); |
65 } | 132 } |
66 | 133 |
67 // Accept an input line, parse and possibly execute it. | 134 // Accept an input line, parse and possibly execute it. |
68 | 135 |
69 void command_window::accept (const std::string& line) | 136 void command_window::accept_line (const std::string& line) |
70 { | 137 { |
71 if (calc::debug_mode) | 138 if (calc::debug_mode) |
72 std::cerr << "accept: " << line << std::endl; | 139 std::cerr << "accept: " << line << std::endl; |
73 | 140 |
74 insert_at_end ("\n"); | 141 insert_at_end ("\n"); |
76 if (! line.empty ()) | 143 if (! line.empty ()) |
77 { | 144 { |
78 add_history (line.c_str ()); | 145 add_history (line.c_str ()); |
79 using_history (); | 146 using_history (); |
80 | 147 |
81 // FIXME: what is the right thing to do with status returned | 148 emit accept_line_signal (QString::fromStdString (line)); |
82 // by parser. | |
83 interpreter::parse_and_execute (line); | |
84 } | 149 } |
85 } | 150 } |
86 | 151 |
87 // Redisplay current command line. | 152 // Redisplay current command line. |
88 | 153 |
316 | 381 |
317 calc_interaction_window = new command_window (); | 382 calc_interaction_window = new command_window (); |
318 | 383 |
319 calc_interaction_window->show (); | 384 calc_interaction_window->show (); |
320 | 385 |
386 readline_init (); | |
387 | |
321 int status = app.exec (); | 388 int status = app.exec (); |
389 | |
390 readline_fini (); | |
322 | 391 |
323 return status; | 392 return status; |
324 } | 393 } |
325 | 394 |
326 void emit_error (const std::string& msg) | 395 void emit_error (const std::string& msg) |