# HG changeset patch # User John W. Eaton # Date 1607007728 18000 # Node ID 02ee34a30351c6a8ccd058f6a6e1e9996cf4995f # Parent 556f20454064405279a847a0a96a4d429dcdaaec eliminate separate cli_qobject and gui_qobject classes * octave-qobject.h, octave-qobject.cc (base_qobject::m_main_window): New data member. (base_qobject::base_qobject): New arg, GUI_APP. If true, set up GUI app main main_window object, signals, and set gui_running flag to true in application context. Otherwise, set up for CLI app. Start main thread after all other objects are constructed. (base_qobject::~base_qobject): Delete m_main_window. (base_qobject::confirm_shutdown): Ask for confirmation from main_window if it exists. (class cli_qobject, class gui_qobject): Delete. * qt-application.cc (qt_application::execute): Pass result of calling start_gui_p to base_qobject constructor instead of using separate objects for gui and cli interfaces. diff -r 556f20454064 -r 02ee34a30351 libgui/src/octave-qobject.cc --- a/libgui/src/octave-qobject.cc Thu Dec 03 12:28:59 2020 -0500 +++ b/libgui/src/octave-qobject.cc Thu Dec 03 10:02:08 2020 -0500 @@ -154,7 +154,7 @@ // we can handle forward Octave interpreter exceptions from the GUI // thread to the interpreter thread. - base_qobject::base_qobject (qt_application& app_context) + base_qobject::base_qobject (qt_application& app_context, bool gui_app) : QObject (), m_app_context (app_context), m_argc (m_app_context.sys_argc ()), m_argv (m_app_context.sys_argv ()), @@ -165,7 +165,8 @@ m_qsci_tr (new QTranslator ()), m_translators_installed (false), m_qt_interpreter_events (new qt_interpreter_events (*this)), m_interpreter_qobj (new interpreter_qobject (*this)), - m_main_thread (new QThread ()) + m_main_thread (new QThread ()), m_gui_app (gui_app), + m_main_window (nullptr) { std::string show_gui_msgs = sys::env::getenv ("OCTAVE_SHOW_GUI_MESSAGES"); @@ -225,6 +226,32 @@ connect (qt_link (), SIGNAL (copy_image_to_clipboard_signal (const QString&, bool)), this, SLOT (copy_image_to_clipboard (const QString&, bool))); + + if (gui_app) + { + m_main_window = new main_window (*this); + + connect (m_interpreter_qobj, SIGNAL (ready (void)), + m_main_window, SLOT (handle_octave_ready (void))); + + connect (qt_link (), + SIGNAL (focus_window_signal (const QString&)), + m_main_window, SLOT (focus_window (const QString&))); + + m_app_context.gui_running (true); + } + else + { + // Get settings file. + m_resource_manager.reload_settings (); + + // After settings. + config_translators (); + + m_qapplication->setQuitOnLastWindowClosed (false); + } + + start_main_thread (); } base_qobject::~base_qobject (void) @@ -233,6 +260,8 @@ // deleteLater slot that is called when the m_main_thread issues a // finished signal. + delete m_main_window; + delete m_interpreter_qobj; delete m_qsci_tr; delete m_gui_tr; @@ -275,7 +304,11 @@ bool base_qobject::confirm_shutdown (void) { - return true; + // Currently, we forward to main_window::confirm_shutdown instead of + // just displaying a dialog box here because the main_window also + // knows about and is responsible for notifying the editor. + + return m_main_window ? m_main_window->confirm_shutdown () : true; } void base_qobject::handle_interpreter_execution_finished (int exit_status) @@ -337,47 +370,4 @@ if (remove_file) QFile::remove (file); } - - cli_qobject::cli_qobject (qt_application& app_context) - : base_qobject (app_context) - { - // Get settings file. - m_resource_manager.reload_settings (); - - // After settings. - config_translators (); - - m_qapplication->setQuitOnLastWindowClosed (false); - - start_main_thread (); - } - - gui_qobject::gui_qobject (qt_application& app_context) - : base_qobject (app_context), m_main_window (new main_window (*this)) - { - connect (m_interpreter_qobj, SIGNAL (ready (void)), - m_main_window, SLOT (handle_octave_ready (void))); - - connect (qt_link (), - SIGNAL (focus_window_signal (const QString&)), - m_main_window, SLOT (focus_window (const QString&))); - - m_app_context.gui_running (true); - - start_main_thread (); - } - - gui_qobject::~gui_qobject (void) - { - delete m_main_window; - } - - bool gui_qobject::confirm_shutdown (void) - { - // Currently, we forward to main_window::confirm_shutdown instead of - // just displaying a dialog box here because the main_window also - // knows about and is responsible for notifying the editor. - - return m_main_window ? m_main_window->confirm_shutdown () : true; - } } diff -r 556f20454064 -r 02ee34a30351 libgui/src/octave-qobject.h --- a/libgui/src/octave-qobject.h Thu Dec 03 12:28:59 2020 -0500 +++ b/libgui/src/octave-qobject.h Thu Dec 03 10:02:08 2020 -0500 @@ -83,7 +83,7 @@ public: - base_qobject (qt_application& app_context); + base_qobject (qt_application& app_context, bool gui_app = false); ~base_qobject (void); @@ -131,6 +131,9 @@ QThread *main_thread (void) { return m_main_thread; } + // Declared virtual so that a derived class may redefine this + // method. + virtual bool confirm_shutdown (void); signals: @@ -178,38 +181,8 @@ interpreter_qobject *m_interpreter_qobj; QThread *m_main_thread; - }; - //! This object provides a command-line interface to Octave that may - //! use Qt graphics. - - class cli_qobject : public base_qobject - { - Q_OBJECT - - public: - - cli_qobject (qt_application& app_context); - - ~cli_qobject (void) = default; - }; - - //! This object provides a full GUI interface to Octave that is - //! implemented Qt. - - class gui_qobject : public base_qobject - { - Q_OBJECT - - public: - - gui_qobject (qt_application& app_context); - - ~gui_qobject (void); - - bool confirm_shutdown (void); - - private: + bool m_gui_app; main_window *m_main_window; }; diff -r 556f20454064 -r 02ee34a30351 libgui/src/qt-application.cc --- a/libgui/src/qt-application.cc Thu Dec 03 12:28:59 2020 -0500 +++ b/libgui/src/qt-application.cc Thu Dec 03 10:02:08 2020 -0500 @@ -62,15 +62,8 @@ // Create and show main window. - if (start_gui_p ()) - { - gui_qobject gui_interface (*this); - return gui_interface.exec (); - } - else - { - cli_qobject cli_interface (*this); - return cli_interface.exec (); - } + base_qobject qt_interface (*this, start_gui_p ()); + + return qt_interface.exec (); } }