Mercurial > jwe > qt-gui-with-push-parser
diff 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 |
line wrap: on
line diff
--- a/gui-main.cpp Wed May 22 17:30:46 2019 -0400 +++ b/gui-main.cpp Wed May 22 18:07:37 2019 -0400 @@ -23,15 +23,79 @@ namespace gui { - int available_char = 0; + static int available_char = 0; - command_window *calc_interaction_window = 0; + static command_window *calc_interaction_window = 0; static inline int ctrl (int c) { return c & 0x1f; } + static int getc (FILE *) + { + int tmp = available_char; + available_char = 0; + return tmp; + } + + static void redisplay (void) + { + if (calc_interaction_window) + calc_interaction_window->redisplay (); + } + + static void prep_term (int) + { + } + + static void deprep_term (void) + { + } + + static void accept_line (char *line) + { + if (calc_interaction_window) + calc_interaction_window->accept_line (line ? line : ""); + } + + static void display_completion_matches (char **matches, int num_matches, + int /* max_length */) + { + if (calc_interaction_window) + { + std::ostringstream buf; + + if (num_matches > 1) + buf << "\n"; + + for (int i = 1; i < num_matches; i++) + buf << matches[i] << "\n"; + + calc_interaction_window->insert_at_end (buf.str ()); + + calc_interaction_window->redisplay (); + } + } + + static void readline_init (void) + { + rl_initialize (); + + rl_getc_function = getc; + rl_redisplay_function = redisplay; + rl_prep_term_function = prep_term; + rl_deprep_term_function = deprep_term; + rl_completion_display_matches_hook = display_completion_matches; + + rl_callback_handler_install (">> ", accept_line); + } + + static void readline_fini (void) + { + rl_callback_handler_remove (); + } + command_window::command_window (QWidget *p) : QTextEdit (p), m_buffer (new QTextDocument ()), @@ -50,6 +114,9 @@ connect (this, SIGNAL (input_char_available (int)), this, SLOT (handle_input_char (int))); + connect (this, SIGNAL (accept_line_signal (const QString&)), + m_interpreter, SLOT (accept_input_line (const QString&))); + insert_at_end ("Qt Example Calculator.\n" "Available operations: + - * / ^ ()\n" @@ -66,7 +133,7 @@ // Accept an input line, parse and possibly execute it. - void command_window::accept (const std::string& line) + void command_window::accept_line (const std::string& line) { if (calc::debug_mode) std::cerr << "accept: " << line << std::endl; @@ -78,9 +145,7 @@ add_history (line.c_str ()); using_history (); - // FIXME: what is the right thing to do with status returned - // by parser. - interpreter::parse_and_execute (line); + emit accept_line_signal (QString::fromStdString (line)); } } @@ -318,8 +383,12 @@ calc_interaction_window->show (); + readline_init (); + int status = app.exec (); + readline_fini (); + return status; }