view libgui/src/terminal-dock-widget.cc @ 31696:8fed04d0607c

eliminate shortcut_manager class and revamp shortcut handling * settings-dialog.ui: Use a custom type for the * shortcuts-tree-widget.h, shortcuts-tree-widget.cc: New files. (shortcuts_tree_widget): New class to use for editing and displaying shortcuts in the settings dialog. Adapt constructor from shortcut_manager::fill_treewidget function. (enter_shortcut): Move here from shortcut-manager.h and shortcut-manager.cc. (tree_widget_shortcut_item): New class to use for items in the shortcuts_tree_widget class in place of QTreeWidgetItem. (shortcut_edit_dialog): New class to use for shortcut editing dialog in place of a simple QDialog. Allows for capturing edited values in the dialog object. * gui-preferences-sc.h, gui-preferences-sc.cc (sc_group): Drop trailing "/" from definition and move here from gui-preferences.h. Update code that prepends sc_group to a settings key. (get_shortcut_section): New function. * gui-preferences.h, gui-preferences.cc (sc_pref::def_value, sc_pref::def_text): New functions. (all_shortcut_preferences::value, all_shortcut_preferences::keys): New static funtions. (all_shortcut_preferences::do_value, all_shortcut_preferences::do_keys): New helper functions. * gui-settings.cc (gui_settings::sc_def_value): Simply call sc_pref::def_value. * settings-dialog.h, settings-dialog.cc (class settings_dialog): Eliminate use of base_qobject and shortcut_manager. (settings_dialog::import_shortcut_set): Get file name here. Call shortcuts_tree_widget::import_shortcuts instead of shortcut_manager::import_export. (settings_dialog::export_shortcut_set): Get file name here. Call shortcuts_tree_widget::export_shortcuts instead of shortcut_manager::import_export. (settings_dialog::default_shortcut_set): Check whether to overwrite shortcuts here. Call shortcuts_tree_widget::set_default_shortcuts instead of shortcut_manager::import_export. (settings_dialog::write_changed_settings): Eliminate CLOSING argument. Call shortcuts_tree_widget::write_settings instead of shortcut_manager::write_shortcuts. (settings_dialog::get_shortcuts_file_name): New function to prompt user for file name. (settings_dialog::overwrite_all_shortcuts): New function to ask user whether replacing shortcuts is OK. (import_export_action): Move enum decl here from shortcut-manager.h. * main-window.cc (main_window::process_settings_dialog_request): Eliminate m_octave_qobj in call to settings_dialog ctor. (main_window::main_window): Don't call shortcut_manager::init_data. * terminal-dock-widget.h, terminal-dock-widget.cc (terminal_dock_widget::init_control_d_shortcut_behavior): New function. (terminal_dock_widget::terminal_dock_widget): Use it instead of performing same action in shortcut_manager::init. * octave-qobject.h, octave-qobject.cc (base_qobject::m_shortcut_manager): Delete data member. (base_qobject::get_shortcut_manager): Delete. (base_qobject::base_qobject): Don't call shortcut_manager::init_data. * shortcut-manager.h, shortcut-manager.cc: Delete. Eliminates the now unnecessary shortcut_manager class. * libgui/src/module.mk: Update.
author John W. Eaton <jwe@octave.org>
date Mon, 26 Dec 2022 17:29:59 -0500
parents deb553ac2c54
children dd904ce6f53f
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.
#if defined (HAVE_QSCINTILLA)
#  include "command-widget.h"
#endif

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

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

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

OCTAVE_BEGIN_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 ())
  {
    init_control_d_shortcut_behavior ();

    // 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)
      {
#if defined (HAVE_QSCINTILLA)
        command_widget *widget = new command_widget (oct_qobj, this);
        console *con = widget->get_console ();

        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);

        connect (this, &terminal_dock_widget::execute_command_signal,
                con, &console::execute_command);

        connect (this, &terminal_dock_widget::new_command_line_signal,
                con, &console::new_command_line);

        m_terminal = widget;
#endif
      }
    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

    gui_settings 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));
  }

#if defined (HAVE_QSCINTILLA)
  command_widget * terminal_dock_widget::get_command_widget (void)
  {
    return (m_experimental_terminal_widget
            ? dynamic_cast<command_widget *> (m_terminal) : nullptr);
  }
#endif

  void terminal_dock_widget::notice_settings (void)
  {
    emit settings_changed ();
  }

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

  void terminal_dock_widget::init_control_d_shortcut_behavior (void)
  {
    gui_settings settings;

    // Reset use of Ctrl-D.  Do this before the call to beginGroup
    // because sc_main_ctrld.key already begins with the sc_group
    // prefix.
    settings.setValue (sc_main_ctrld.key, false);

    settings.beginGroup (sc_group);
    const QStringList shortcut_settings_keys = settings.allKeys ();
    settings.endGroup ();

    for (const auto& settings_key : shortcut_settings_keys)
      {
        // Check whether Ctrl+D is used from main window, i.e. is a
        // global shortcut.

        QString section = get_shortcut_section (settings_key);

        if (section.startsWith ("main_"))
          {
            sc_pref scpref = all_shortcut_preferences::value (settings_key);

            QKeySequence actual = QKeySequence (settings.sc_value (scpref));

            if (actual == QKeySequence (Qt::ControlModifier+Qt::Key_D))
              {
                settings.setValue (sc_main_ctrld.key, true);
                break;
              }
          }
     }
  }

OCTAVE_END_NAMESPACE(octave)