GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
command-widget.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2021-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <QGroupBox>
31#include <QHBoxLayout>
32#include <QLabel>
33#include <QLineEdit>
34#include <QPushButton>
35#include <QTextBrowser>
36#include <QVBoxLayout>
37
38#include "command-widget.h"
39
40#include "cmd-edit.h"
41#include "event-manager.h"
42#include "gui-preferences-cs.h"
44#include "gui-utils.h"
45#include "input.h"
46#include "interpreter.h"
47
48namespace octave
49{
51 : QWidget (p), m_incomplete_parse (false),
52 m_prompt (new QLabel ("", this)),
53 m_line_edit (new QLineEdit (this)),
54 m_output_display (new QTextBrowser (this)),
55 m_input_color (QColor ())
56 {
57 QPushButton *pause_button = new QPushButton (tr("Pause"), this);
58 QPushButton *stop_button = new QPushButton (tr("Stop"), this);
59 QPushButton *resume_button = new QPushButton (tr("Continue"), this);
60
61 QGroupBox *input_group_box = new QGroupBox (tr("Command Input"));
62 QHBoxLayout *input_layout = new QHBoxLayout;
63 input_layout->addWidget (m_prompt);
64 input_layout->addWidget (m_line_edit);
65 input_layout->addWidget (pause_button);
66 input_layout->addWidget (stop_button);
67 input_layout->addWidget (resume_button);
68 input_group_box->setLayout (input_layout);
69
70 QGroupBox *output_group_box = new QGroupBox (tr("Command Output"));
71 QHBoxLayout *output_layout = new QHBoxLayout ();
72 output_layout->addWidget (m_output_display);
73 output_group_box->setLayout (output_layout);
74
75 QVBoxLayout *main_layout = new QVBoxLayout ();
76 main_layout->addWidget (output_group_box);
77 main_layout->addWidget (input_group_box);
78
79 setLayout (main_layout);
80
81 setFocusProxy (m_line_edit);
82
83 connect (m_line_edit, &QLineEdit::returnPressed,
85
86 connect (this, &command_widget::clear_line_edit,
87 m_line_edit, &QLineEdit::clear);
88
89 connect (pause_button, &QPushButton::clicked,
91
92 connect (resume_button, &QPushButton::clicked,
94
95 connect (stop_button, &QPushButton::clicked,
97 }
98
99 void command_widget::update_prompt (const QString& prompt)
100 {
101 m_prompt->setText (prompt);
102 }
103
105 {
106 QTextCursor cursor = m_output_display->textCursor ();
107
108 cursor.insertText (msg);
109
110 m_output_display->setTextCursor (cursor);
111 }
112
114 {
115 QTextCursor cursor = m_output_display->textCursor ();
116
117 QString input_line = m_line_edit->text ();
118
119 QString style;
120 if (! m_incomplete_parse)
121 {
122 style = QString ("<div style=\"color:%1; font-weight:bold;\">[in]:</div> ")
123 .arg (m_input_color.name ());
124 m_output_display->insertHtml (style);
125 }
126 style = QString ("<div style=\"color:%1\">%2</div><br>")
127 .arg (m_input_color.name ()).arg (input_line);
128 m_output_display->insertHtml (style);
129
131 ([=] (interpreter& interp)
132 {
133 // INTERPRETER THREAD
134
135 interp.parse_and_execute (input_line.toStdString (),
137
138 event_manager& evmgr = interp.get_event_manager ();
139 input_system& input_sys = interp.get_input_system ();
140
141 std::string prompt
142 = m_incomplete_parse ? input_sys.PS2 () : input_sys.PS1 ();
143
144 evmgr.update_prompt (command_editor::decode_prompt_string (prompt));
145 });
146
147 emit clear_line_edit ();
148 }
149
151 {
152 // Set terminal font:
153 QFont term_font = QFont ();
154 term_font.setStyleHint (QFont::TypeWriter);
155 QString default_font = settings->value (global_mono_font).toString ();
156 term_font.setFamily
157 (settings->value (cs_font.key, default_font).toString ());
158 term_font.setPointSize
159 (settings->value (cs_font_size).toInt ());
160
161 m_line_edit->setFont (term_font);
162 m_output_display->setFont (term_font);
163
164 // Colors
165 int mode = settings->value (cs_color_mode).toInt ();
166 QColor fgc = settings->color_value (cs_colors[0], mode);
167 QColor bgc = settings->color_value (cs_colors[1], mode);
168
169 m_output_display->setStyleSheet (QString ("color: %1; background-color:%2;")
170 .arg (fgc.name ()).arg (bgc.name ()));
171 m_line_edit->setStyleSheet (QString ("color: %1; background-color:%2;")
172 .arg (fgc.name ()).arg (bgc.name ()));
173
174 m_input_color = interpolate_color (fgc, bgc, 0.75, 0.5);
175 }
176
177}
Provides threadsafe access to octave.
event_manager & get_event_manager(void)
Definition: interpreter.h:328
void parse_and_execute(const std::string &input, bool &incomplete_parse)
Definition: interpreter.cc:824
input_system & get_input_system(void)
Definition: interpreter.h:261
Base class for Octave interfaces that use Qt.
static std::string decode_prompt_string(const std::string &s)
Definition: cmd-edit.cc:1271
void interpreter_event(const fcn_callback &fcn)
void interpreter_stop(void)
void interpreter_resume(void)
void update_prompt(const QString &prompt)
void insert_interpreter_output(const QString &msg)
void interpreter_pause(void)
QTextBrowser * m_output_display
command_widget(base_qobject &oct_qobj, QWidget *p)
void notice_settings(const gui_settings *settings)
void clear_line_edit(void)
const gui_pref cs_colors[2 *cs_colors_count]
const gui_pref cs_color_mode("terminal/color_mode", QVariant(0))
const gui_pref cs_font_size("terminal/fontSize", QVariant(10))
const gui_pref cs_font("terminal/fontName", QVariant())
const gui_pref global_mono_font("monospace_font", global_font_family)
OCTGUI_API QColor interpolate_color(const QColor &col1, const QColor &col2, double fs, double fv)
Definition: gui-utils.cc:35
const QString key