view libgui/src/octave-qobject.h @ 29613:9367cba2a6d5

allow most dock widgets to be used from command line This change is an initial attempt to allow the documentation, file, history, variable editor, and workspace widgets to be opened from the command line. While running in command line mode, some connections between those widgets and the interpreter or between two widgets may be missing. The aim is to eventually make those work but this is a good start. These dock widgets are now owned by the base_qobject object and shared with the main_window object. If they exist when the main_window is created (possible if using the new terminal widget) then a pointer to the main_window object is passed to the dock_widget so that they may be docked there. If the desktop is closed and control returns to the command line, undocked widgets remain available. * octave-qobject.h, octave-qobject.cc (base_qobject::m_documentation_widget, base_qobject::m_file_browser_widget, base_qobject::m_history_widget, base_qobject::m_workspace_widget, base_qobject::m_editor_widget, base_qobject::m_variable_editor_widget): New data members. (base_qobject::documentation_widget, base_qobject::file_browser_widget, base_qobject::history_widget, base_qobject::workspace_widget, base_qobject::editor_widget, base_qobject::variable_editor_widget): New functions to create dock widgets as needed. (base_qobject::show_documentation_window, base_qobject::show_file_browser_window, base_qobject::show_command_history_window, base_qobject::show_workspace_window, base_qobject::edit_variable, base_qobject::handle_variable_editor_update): New functions. Make signal slot connections that do not require the main window here, not in main_window::construct_octave_qt_link. * files-dock-widget.cc (files_dock_widget::files_dock_widget): If no parent, call make_window. * documentation-dock-widget.cc (documentation_dock_widget::documentation_dock_widget): Likewise. * history-dock-widget.cc (history_dock_widget::history_dock_widget): Likewise. * workspace-view.cc (workspace_view::workspace_view): Likewise. * doc.m: Always give the event manager a chance to display the documentation, not just if the GUI is running. * documentation.cc (documentation::documentation): Make sub-objects children of the documentation object, not children of the parent of the documentation object. * main-window.h, main-window.cc (main_window::m_command_window, main_window::m_history_window, main_window::m_file_browser_window, main_window::m_doc_browser_window, main_window::m_editor_window, main_window::m_workspace_window, main_window::m_variable_editor_window): Manage objects with QPointer. (main_window::main_window): Acquire m_doc_browser_window, m_history_window, m_workspace_window, and m_variable_editor_window from octave_qobj. * octave-dock-widget.cc (octave_dock_widget::octave_dock_widget): Don't reference m_parent if it is nullptr. (octave_dock_widget::handle_settings): Likewise. Return immediately if settings is nullptr. (octave_dock_widget::set_main_window): New function. * variable-editor.h, variable-editor.cc (variable_editor::~variable_editor): Explicitly delete m_tool_bar and set it to nullptr. (variable_editor::edit_variable, variable_editor::notice_settings, variable_editor::variable_destroyed): Avoid referencing m_tool_bar if it is nullptr. * qt-interpreter-events.h, qt-interpreter-events.cc (qt_interpreter_events::show_documentation): Rename from show_doc. Change all uses. (qt_interpreter_events::register_documentation): Rename from register_doc. Change all uses. (qt_interpreter_events::unregister_documentation): Rename from unregister_doc. Change all uses. (qt_interpreter_events::show_file_browser, qt_interpreter_events::show_command_history, qt_interpreter_events::show_workspace): New functions. (qt_interpreter_events::show_documentation_signal): Rename from show_doc_signal. Change all uses. qt_interpreter_events::register_documentation_signal): Rename from register_doc_signal. Change all uses. qt_interpreter_events::unregister_documentation_signal): Rename from unregister_doc_signal. Change all uses. (qt_interpreter_events::show_file_browser_signal, qt_interpreter_events::show_command_history_signal, qt_interpreter_events::show_workspace_signal): New signals. * event-manager.h, event-manager.cc (F__event_manager_show_file_browser__, F__event_manager_show_command_history__, F__event_manager_show_workspace__): New functions. (F__event_manager_show_documentation__): Rename from __event_manager_show_doc__. Change all uses. (F__event_manager_register_documentation__): Rename from F__event_manager_register_doc__. Change all uses. (F__event_manager_unregister_documentation__): Rename from F__event_manager_unregister_doc__. Change all uses. (Fopenvar): Don't require GUI to be running. (interpreter_events::show_documentation): Rename from show_doc. Change all uses. (interpreter_events::register_documentation): Rename from register_doc. Change all uses. (interpreter_events::unregister_documentation): Rename from unregister_doc. Change all uses. (interpreter_events::show_file_browser, interpreter_events::show_command_history, interpreter_events::show_workspace): New functions. (event_manager::show_documentation): Rename from show_doc. Change all uses. (event_manager::register_documentation): Rename from register_doc. Change all uses. (event_manager::register_documentation): Rename from unregister_doc. Change all uses.
author John W. Eaton <jwe@octave.org>
date Thu, 29 Apr 2021 11:12:40 -0400
parents f6ad83cbe3c4
children 4c569e386e9b
line wrap: on
line source

////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if ! defined (octave_octave_qobject_h)
#define octave_octave_qobject_h 1

#include <memory>

#include <QApplication>
#include <QList>
#include <QObject>
#include <QString>
#include <QStringList>

#include "interpreter-qobject.h"
#include "resource-manager.h"
#include "shortcut-manager.h"

namespace octave
{
  class main_window;
  class qt_application;
  class qt_interpreter_events;

  //! This class is a simple wrapper around QApplication so that we can
  //! reimplement QApplication::notify.  The octave_qapplication object
  //! should behave identically to a QApplication object except that it
  //! overrides the notify method so we can handle forward Octave
  //! execution_exception exceptions from the GUI thread to the
  //! interpreter thread.

  class octave_qapplication : public QApplication
  {
    Q_OBJECT

  public:

    octave_qapplication (int& argc, char **argv)
      : QApplication (argc, argv)
    { }

    virtual bool notify (QObject *receiver, QEvent *e) override;

    ~octave_qapplication (void) { };

  signals:

    void interpreter_event (const fcn_callback& fcn);
    void interpreter_event (const meth_callback& meth);
  };

  //! Container for windows that may be created from the command line or
  //! docked with the main GUI window.  Any of these windows that are
  //! created in command line mode will be adopted by the main window if
  //! it is opened from the command line.  Any that are undocked from
  //! the main window will remain open if control returns to the command
  //! line.

  class base_qobject;
  class documentation_dock_widget;
  class file_editor_interface;
  class files_dock_widget;
  class history_dock_widget;
  class variable_editor;
  class workspace_model;
  class workspace_view;

  //! Base class for Octave interfaces that use Qt.  There are two
  //! classes derived from this one.  One provides a command-line
  //! interface that may use Qt graphics and another provides the
  //! full GUI experience.

  class base_qobject : public QObject
  {
    Q_OBJECT

  public:

    // Note: the GUI_APP argument is not needed with the new
    // experimental terminal widget.
    base_qobject (qt_application& app_context, bool gui_app = false);

    ~base_qobject (void);

    void config_translators (void);

    void start_main_thread (void);

    int exec (void);

    // The Octave application context.
    qt_application& app_context (void) { return m_app_context; }

    // The Qt QApplication.
    QApplication * qapplication (void) { return m_qapplication; };

    // Provided for convenience.  Will be removed once we eliminate the
    // old terminal widget.
    bool experimental_terminal_widget (void) const;

    // Provided for convenience.
    bool gui_running (void) const;

    resource_manager& get_resource_manager (void)
    {
      return m_resource_manager;
    }

    shortcut_manager& get_shortcut_manager (void)
    {
      return m_shortcut_manager;
    }

    std::shared_ptr<qt_interpreter_events> get_qt_interpreter_events (void)
    {
      return m_qt_interpreter_events;
    }

    qt_interpreter_events * qt_link (void)
    {
      return m_qt_interpreter_events.get ();
    }

    interpreter_qobject * interpreter_qobj (void)
    {
      return m_interpreter_qobj;
    }

    workspace_model * get_workspace_model (void)
    {
      return m_workspace_model;
    }

    QPointer<documentation_dock_widget>
    documentation_widget (main_window *mw = nullptr);

    QPointer<files_dock_widget>
    file_browser_widget (main_window *mw = nullptr);

    QPointer<history_dock_widget>
    history_widget (main_window *mw = nullptr);

    QPointer<workspace_view>
    workspace_widget (main_window *mw = nullptr);

    // FIXME: The file_editor_interface needs to be a proper generic
    // interface for all editors (internal and external) for this to
    // work properly.
    QPointer<file_editor_interface>
    editor_widget (main_window *mw = nullptr);

    QPointer<variable_editor>
    variable_editor_widget (main_window *mw = nullptr);

    QThread *main_thread (void) { return m_main_thread; }

    // Declared virtual so that a derived class may redefine this
    // method.

    virtual bool confirm_shutdown (void);

  public slots:

    // Note: START_GUI and CLOSE_GUI don't currently perform any work
    // with the old terminal widget.
    void start_gui (bool gui_app);
    void close_gui (void);

    void show_documentation_window (const QString& file);

    void show_file_browser_window (void);

    void show_command_history_window (void);

    void show_workspace_window (void);

    void edit_variable (const QString& expr, const octave_value& val);

    void handle_variable_editor_update (void);

    void interpreter_ready (void);

    void interpreter_event (const fcn_callback& fcn);

    void interpreter_event (const meth_callback& meth);

    void interpreter_interrupt (void);

    // Note: these currently only work with the new experimental
    // terminal widget.
    void interpreter_pause (void);
    void interpreter_stop (void);
    void interpreter_resume (void);

    void copy_image_to_clipboard (const QString& file, bool remove_file);

  protected:

    qt_application& m_app_context;

    // Use these to ensure that argc and argv exist for as long as the
    // QApplication object.

    int m_argc;
    char **m_argv;

    octave_qapplication *m_qapplication;

    resource_manager m_resource_manager;

    shortcut_manager m_shortcut_manager;

    QTranslator *m_qt_tr;
    QTranslator *m_gui_tr;
    QTranslator *m_qsci_tr;

    bool m_translators_installed;

    std::shared_ptr<qt_interpreter_events> m_qt_interpreter_events;

    interpreter_qobject *m_interpreter_qobj;

    QThread *m_main_thread;

    bool m_gui_app;

    bool m_interpreter_ready;

    workspace_model *m_workspace_model;

    // Dock widgets that may be used from the command line.  They are
    // adopted by the desktop (main window) if it is also started from
    // the command line.

    QPointer<documentation_dock_widget> m_documentation_widget;

    QPointer<files_dock_widget> m_file_browser_widget;

    QPointer<history_dock_widget> m_history_widget;

    QPointer<workspace_view> m_workspace_widget;

    QPointer<file_editor_interface> m_editor_widget;

    QPointer<variable_editor> m_variable_editor_widget;

    main_window *m_main_window;
  };
}

#endif