# HG changeset patch # User John W. Eaton # Date 1558562857 14400 # Node ID 54edd85237abb215453ce97c36970b33a71ab6de # Parent 0e154787183dd6a8b4b134cf23f84158e3d32f9d use signal to send input to qt interpreter object diff -r 0e154787183d -r 54edd85237ab gui-main.cpp --- 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; } diff -r 0e154787183d -r 54edd85237ab gui-main.h --- a/gui-main.h Wed May 22 17:30:46 2019 -0400 +++ b/gui-main.h Wed May 22 18:07:37 2019 -0400 @@ -28,7 +28,7 @@ // Accept an input line, parse and possibly execute it. - void accept (const std::string& line); + void accept_line (const std::string& line); // Redisplay current command line. @@ -46,6 +46,8 @@ void result_available (double value); + void accept_line_signal (const QString& line); + public slots: void handle_input_char (int key); @@ -90,9 +92,6 @@ extern void emit_error (const std::string& msg); extern void emit_result (double value); - - extern int available_char; - extern command_window *calc_interaction_window; } #endif diff -r 0e154787183d -r 54edd85237ab qt-interpreter.cpp --- a/qt-interpreter.cpp Wed May 22 17:30:46 2019 -0400 +++ b/qt-interpreter.cpp Wed May 22 18:07:37 2019 -0400 @@ -1,5 +1,7 @@ #include +#include + #include "gui-main.h" #include "interpreter.h" #include "qt-interpreter.h" @@ -9,70 +11,6 @@ namespace calc { - static int getc (FILE *) - { - int tmp = gui::available_char; - gui::available_char = 0; - return tmp; - } - - static void redisplay (void) - { - if (gui::calc_interaction_window) - gui::calc_interaction_window->redisplay (); - } - - static void prep_term (int) - { - } - - static void deprep_term (void) - { - } - - static void accept_line (char *line) - { - if (gui::calc_interaction_window) - gui::calc_interaction_window->accept (line ? line : ""); - } - - static void display_completion_matches (char **matches, int num_matches, - int /* max_length */) - { - if (gui::calc_interaction_window) - { - std::ostringstream buf; - - if (num_matches > 1) - buf << "\n"; - - for (int i = 1; i < num_matches; i++) - buf << matches[i] << "\n"; - - gui::calc_interaction_window->insert_at_end (buf.str ()); - - gui::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 (); - } - qt_interpreter::qt_interpreter (void) { interpreter::init (); @@ -81,12 +19,14 @@ qt_interpreter::~qt_interpreter (void) { interpreter::fini (); - - readline_fini (); } void qt_interpreter::execute (void) { - readline_init (); + } + + void qt_interpreter::accept_input_line (const QString& line) + { + interpreter::parse_and_execute (line.toStdString ()); } } diff -r 0e154787183d -r 54edd85237ab qt-interpreter.h --- a/qt-interpreter.h Wed May 22 17:30:46 2019 -0400 +++ b/qt-interpreter.h Wed May 22 18:07:37 2019 -0400 @@ -2,6 +2,7 @@ #define calc_qt_interpreter_h 1 #include +#include namespace calc { @@ -22,6 +23,8 @@ public slots: void execute (void); + + void accept_input_line (const QString& line); }; }