Mercurial > jwe > qt-gui-with-push-parser
changeset 7:04867eba6428
function objects and signals/slots for errors and results
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 23 May 2019 09:27:09 -0400 |
parents | 1b575145197e |
children | 7c4b04a6346d |
files | gui-main.cpp gui-main.h interpreter.cpp interpreter.h qt-interpreter.cpp qt-interpreter.h |
diffstat | 6 files changed, 76 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- a/gui-main.cpp Thu May 23 07:41:18 2019 -0400 +++ b/gui-main.cpp Thu May 23 09:27:09 2019 -0400 @@ -42,7 +42,7 @@ static void redisplay (void) { if (calc_interaction_window) - calc_interaction_window->redisplay (); + emit calc_interaction_window->redisplay_signal (); } static void prep_term (int) @@ -74,7 +74,7 @@ calc_interaction_window->insert_at_end (buf.str ()); - calc_interaction_window->redisplay (); + emit calc_interaction_window->redisplay_signal (); } } @@ -108,12 +108,18 @@ setDocument (m_buffer); - connect (this, SIGNAL (result_available (double)), + connect (m_interpreter, SIGNAL (result_ready (double)), this, SLOT (handle_result (double))); + connect (m_interpreter, SIGNAL (error_signal (const QString&)), + this, SLOT (handle_error (const QString&))); + connect (this, SIGNAL (input_char_available (int)), this, SLOT (handle_input_char (int))); + connect (this, SIGNAL (redisplay_signal (void)), + this, SLOT (redisplay (void))); + connect (this, SIGNAL (accept_line_signal (const QString&)), m_interpreter, SLOT (accept_input_line (const QString&))); @@ -149,6 +155,26 @@ } } + void command_window::insert_at_end (const std::string& text) + { + scroll_to_bottom (); + + insert_at_cursor (text); + } + + void command_window::handle_error (const QString& msg) + { + insert_at_end ("parse error: " + msg.toStdString () + "\n"); + + rl_abort (0, 0); + } + + // FIXME: do we really need this extra function? + void command_window::handle_result (double value) + { + insert_result (value); + } + // Redisplay current command line. void command_window::redisplay (void) @@ -169,31 +195,6 @@ setTextCursor (cursor); } - void command_window::insert_at_end (const std::string& text) - { - scroll_to_bottom (); - - insert_at_cursor (text); - } - - void command_window::emit_error (const std::string& msg) - { - insert_at_end ("parse error: " + msg); - - rl_abort (0, 0); - } - - void command_window::emit_result (double value) - { - emit result_available (value); - } - - // FIXME: do we really need this extra function? - void command_window::handle_result (double value) - { - insert_result (value); - } - void command_window::keyPressEvent (QKeyEvent *event) { if (! event) @@ -391,16 +392,6 @@ return status; } - - void emit_error (const std::string& msg) - { - calc_interaction_window->emit_error (msg); - } - - void emit_result (double value) - { - calc_interaction_window->emit_result (value); - } } // -- Variable: rl_getc_func_t * rl_getc_function
--- a/gui-main.h Thu May 23 07:41:18 2019 -0400 +++ b/gui-main.h Thu May 23 09:27:09 2019 -0400 @@ -32,8 +32,6 @@ // Redisplay current command line. - void redisplay (void); - void insert_at_end (const std::string& text); void emit_error (const std::string& msg); @@ -44,16 +42,20 @@ void input_char_available (int key); - void result_available (double value); + void accept_line_signal (const QString& line); - void accept_line_signal (const QString& line); + void redisplay_signal (void); public slots: void handle_input_char (int key); + void handle_error (const QString& msg); + void handle_result (double value); + void redisplay (void); + protected: void keyPressEvent (QKeyEvent *event); @@ -88,10 +90,6 @@ }; extern int main (int argc, char **argv); - - extern void emit_error (const std::string& msg); - - extern void emit_result (double value); } #endif
--- a/interpreter.cpp Thu May 23 07:41:18 2019 -0400 +++ b/interpreter.cpp Thu May 23 09:27:09 2019 -0400 @@ -15,6 +15,10 @@ interpreter *interpreter::the_interpreter = nullptr; interpreter::interpreter (void) + : m_error_handler ([] (const char *msg) + { std::cerr << "error: " << msg << std::endl; }), + m_result_handler ([] (double value) + { std::cout << value << std::endl; }) { if (the_interpreter) { @@ -42,17 +46,11 @@ // Simulate a delay in calculation. sleep (1); - if (calc::tty_mode) - tty::emit_result (value); - else - gui::emit_result (value); + m_result_handler (value); } void interpreter::emit_error (const char *msg) { - if (calc::tty_mode) - tty::emit_error (msg); - else - gui::emit_error (msg); + m_error_handler (msg); } }
--- a/interpreter.h Thu May 23 07:41:18 2019 -0400 +++ b/interpreter.h Thu May 23 09:27:09 2019 -0400 @@ -1,6 +1,8 @@ #if ! defined (calc_interpreter_h) #define calc_interpreter_h 1 +#include <functional> + namespace calc { class interpreter @@ -19,11 +21,30 @@ int parse_and_execute (const std::string& line); + std::function<void (const char *)> + set_error_handler (const std::function<void (const char *)>& error_handler) + { + std::function<void (const char *)> tmp = m_error_handler; + m_error_handler = error_handler; + return tmp; + } + + std::function<void (double)> + set_result_handler (const std::function<void (double)>& result_handler) + { + std::function<void (double)> tmp = m_result_handler; + m_result_handler = result_handler; + return tmp; + } + void emit_error (const char *msg); void emit_result (double value); private: + + std::function<void (const char *)> m_error_handler; + std::function<void (double)> m_result_handler; }; }
--- a/qt-interpreter.cpp Thu May 23 07:41:18 2019 -0400 +++ b/qt-interpreter.cpp Thu May 23 09:27:09 2019 -0400 @@ -1,3 +1,4 @@ +#include <iostream> #include <sstream> #include <QString> @@ -13,7 +14,13 @@ { qt_interpreter::qt_interpreter (void) : m_interpreter () - { } + { + m_interpreter.set_error_handler ([this] (const char *msg) + { emit this->error_signal (msg); }); + + m_interpreter.set_result_handler ([this] (double value) + { emit this->result_ready (value); }); + } void qt_interpreter::execute (void) {
--- a/qt-interpreter.h Thu May 23 07:41:18 2019 -0400 +++ b/qt-interpreter.h Thu May 23 09:27:09 2019 -0400 @@ -22,6 +22,12 @@ qt_interpreter& operator = (const qt_interpreter&) = delete; + signals: + + void result_ready (double); + + void error_signal (const QString&); + public slots: void execute (void);