view libgui/src/terminal-dock-widget.cc @ 31067:5261a81765b0

merge input and output of exp. terminal widget into one widget * command-widget.cc (command_widget): remove obsolete elements, use new class console as terminal, update initializations; (init_command_prompt): display the first prompt, this function is called when the interpreter is ready (via main_window); (update_prompt): prompt is a QString now; (insert_interpreter_output): use m_console instead of m_output_display; (accept_input_line): moved to console; (process_input_line): string of input line is passed as argument, a new prompt is provided when interpreter is ready (notice_settings): update for m_console, remove color and configuration of obsolete line edit (console::console): initializations, create and set QTextDocument; (console::new_command_line): Prepare a new command line with prompt; (console::accept_command_line): get input string and pass it to m_command_widget for processing; (console::append_block): append a text block to the document; (console::keyPressEvent): re-implement key event for filtering return, on which accept_input_line is called * command-widget.h: new class console, update some function arguments * main-window.cc (handle_octave_ready) call init_command_prompt of terminal_dock_widget * terminal-dock-widget.cc (init_command_prompt): new function calling related function of the command widget * terminal-dock-widget.h: new function init_command_prompt
author Torsten Lilge <ttl-octave@mailbox.org>
date Sat, 04 Jun 2022 21:52:20 +0200
parents 04601f6c47f4
children 0b402f523f09
line wrap: on
line source

////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2022 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 (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <QScreen>

// This header is only needed for the new terminal widget.
#include "command-widget.h"

// This header is only needed for the old terminal widget.
#include "QTerminal.h"

#include "gui-preferences-cs.h"
#include "gui-preferences-global.h"

#include "octave-qobject.h"
#include "terminal-dock-widget.h"

namespace octave
{
  terminal_dock_widget::terminal_dock_widget (QWidget *p,
                                              base_qobject& oct_qobj)
    : octave_dock_widget ("TerminalDockWidget", p, oct_qobj),
      m_experimental_terminal_widget (oct_qobj.experimental_terminal_widget ())
  {
    // FIXME: we could do this in a better way, but improving it doesn't
    // matter much if we will eventually be removing the old terminal.
    if (m_experimental_terminal_widget)
      {
        command_widget *widget = new command_widget (oct_qobj, this);

        connect (this, &terminal_dock_widget::settings_changed,
                 widget, &command_widget::notice_settings);

        connect (this, &terminal_dock_widget::update_prompt_signal,
                 widget, &command_widget::update_prompt);

        connect (this, &terminal_dock_widget::interpreter_output_signal,
                 widget, &command_widget::insert_interpreter_output);

        m_terminal = widget;
      }
    else
      {
        QTerminal *widget = QTerminal::create (oct_qobj, this);

        connect (this, &terminal_dock_widget::settings_changed,
                 widget, &QTerminal::notice_settings);

        // Connect the visibility signal to the terminal for
        // dis-/enabling timers.
        connect (this, &terminal_dock_widget::visibilityChanged,
                 widget, &QTerminal::handle_visibility_changed);

        m_terminal = widget;
      }

    m_terminal->setObjectName ("OctaveTerminal");
    m_terminal->setFocusPolicy (Qt::StrongFocus);

    set_title (tr ("Command Window"));

    setWidget (m_terminal);
    setFocusProxy (m_terminal);

    // Chose a reasonable size at startup in order to avoid truncated
    // startup messages
    resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
    gui_settings *settings = rmgr.get_settings ();

    QFont font = QFont ();
    font.setStyleHint (QFont::TypeWriter);
    QString default_font = settings->value (global_mono_font).toString ();
    font.setFamily
      (settings->value (cs_font.key, default_font).toString ());
    font.setPointSize
      (settings->value (cs_font_size).toInt ());

    QFontMetrics metrics(font);

    int win_x =  metrics.maxWidth()*80;
    int win_y =  metrics.height()*25;

    int max_x = QGuiApplication::primaryScreen ()->availableGeometry ().width ();
    int max_y = QGuiApplication::primaryScreen ()->availableGeometry ().height ();

    if (win_x > max_x)
      win_x = max_x;
    if (win_y > max_y)
      win_y = max_y;

    setGeometry (0, 0, win_x, win_y);

    if (! p)
      make_window ();
  }

  bool terminal_dock_widget::has_focus (void) const
  {
    QWidget *w = widget ();
    return w->hasFocus ();
  }

  QTerminal * terminal_dock_widget::get_qterminal (void)
  {
    return (m_experimental_terminal_widget
            ? nullptr : dynamic_cast<QTerminal *> (m_terminal));
  }

  command_widget * terminal_dock_widget::get_command_widget (void)
  {
    return (m_experimental_terminal_widget
            ? dynamic_cast<command_widget *> (m_terminal) : nullptr);
  }

  void terminal_dock_widget::notice_settings (const gui_settings *settings)
  {
    emit settings_changed (settings);
  }

  void terminal_dock_widget::interpreter_output (const QString& msg)
  {
    if (m_experimental_terminal_widget)
      emit interpreter_output_signal (msg);
  }

  void terminal_dock_widget::update_prompt (const QString& prompt)
  {
    if (m_experimental_terminal_widget)
      emit update_prompt_signal (prompt);
  }

  void terminal_dock_widget::init_command_prompt ()
  {
    if (m_experimental_terminal_widget)
      {
        command_widget *cmd = get_command_widget ();
        if (cmd)
          cmd->init_command_prompt ();
      }
  }
}