comparison libgui/src/command-widget.cc @ 31648:29d734430e5f stable

maint: Re-indent code after switch to using namespace macros. * BaseControl.cc, BaseControl.h, ButtonControl.cc, ButtonControl.h, ButtonGroup.cc, ButtonGroup.h, Canvas.cc, Canvas.h, CheckBoxControl.cc, CheckBoxControl.h, Container.cc, Container.h, ContextMenu.cc, ContextMenu.h, EditControl.cc, EditControl.h, Figure.cc, Figure.h, FigureWindow.cc, FigureWindow.h, GLCanvas.cc, GLCanvas.h, GenericEventNotify.h, KeyMap.cc, KeyMap.h, ListBoxControl.cc, ListBoxControl.h, Logger.cc, Logger.h, Menu.cc, Menu.h, MenuContainer.h, Object.cc, Object.h, ObjectProxy.cc, ObjectProxy.h, Panel.cc, Panel.h, PopupMenuControl.cc, PopupMenuControl.h, PushButtonControl.cc, PushButtonControl.h, PushTool.cc, PushTool.h, QtHandlesUtils.cc, QtHandlesUtils.h, RadioButtonControl.cc, RadioButtonControl.h, SliderControl.cc, SliderControl.h, Table.cc, Table.h, TextControl.cc, TextControl.h, TextEdit.cc, TextEdit.h, ToggleButtonControl.cc, ToggleButtonControl.h, ToggleTool.cc, ToggleTool.h, ToolBar.cc, ToolBar.h, ToolBarButton.cc, ToolBarButton.h, annotation-dialog.cc, annotation-dialog.h, gl-select.cc, gl-select.h, qopengl-functions.h, qt-graphics-toolkit.cc, qt-graphics-toolkit.h, module.mk, QTerminal.h, color-picker.cc, color-picker.h, command-widget.cc, command-widget.h, community-news.cc, community-news.h, dialog.cc, dialog.h, documentation-bookmarks.cc, documentation-bookmarks.h, documentation-dock-widget.cc, documentation-dock-widget.h, documentation.cc, documentation.h, dw-main-window.cc, dw-main-window.h, external-editor-interface.cc, external-editor-interface.h, files-dock-widget.cc, files-dock-widget.h, find-files-dialog.cc, find-files-dialog.h, find-files-model.cc, find-files-model.h, graphics-init.cc, graphics-init.h, gui-settings.cc, gui-settings.h, gui-utils.cc, gui-utils.h, history-dock-widget.cc, history-dock-widget.h, interpreter-qobject.cc, interpreter-qobject.h, led-indicator.cc, led-indicator.h, file-editor-interface.h, file-editor-tab.cc, file-editor-tab.h, file-editor.cc, file-editor.h, find-dialog.cc, find-dialog.h, marker.cc, marker.h, octave-qscintilla.cc, octave-qscintilla.h, octave-txt-lexer.cc, octave-txt-lexer.h, main-window.cc, main-window.h, news-reader.cc, news-reader.h, octave-dock-widget.cc, octave-dock-widget.h, octave-qobject.cc, octave-qobject.h, qt-application.cc, qt-application.h, qt-interpreter-events.cc, qt-interpreter-events.h, qt-utils.h, release-notes.cc, release-notes.h, resource-manager.cc, resource-manager.h, set-path-dialog.cc, set-path-dialog.h, set-path-model.cc, set-path-model.h, settings-dialog.cc, settings-dialog.h, shortcut-manager.cc, shortcut-manager.h, tab-bar.cc, tab-bar.h, terminal-dock-widget.cc, terminal-dock-widget.h, variable-editor-model.cc, variable-editor-model.h, variable-editor.cc, variable-editor.h, welcome-wizard.cc, welcome-wizard.h, workspace-model.cc, workspace-model.h, workspace-view.cc, workspace-view.h: Re-indent code after switch to using namespace macros.
author John W. Eaton <jwe@octave.org>
date Tue, 06 Dec 2022 14:53:00 -0500
parents c6d54dd31a7e
children deb553ac2c54 597f3ee61a48
comparison
equal deleted inserted replaced
31646:c6d54dd31a7e 31648:29d734430e5f
48 #include "input.h" 48 #include "input.h"
49 #include "interpreter.h" 49 #include "interpreter.h"
50 50
51 OCTAVE_BEGIN_NAMESPACE(octave) 51 OCTAVE_BEGIN_NAMESPACE(octave)
52 52
53 command_widget::command_widget (base_qobject& oct_qobj, QWidget *p) 53 command_widget::command_widget (base_qobject& oct_qobj, QWidget *p)
54 : QWidget (p), m_incomplete_parse (false), 54 : QWidget (p), m_incomplete_parse (false),
55 m_prompt (QString ()), 55 m_prompt (QString ()),
56 m_console (new console (this, oct_qobj)) 56 m_console (new console (this, oct_qobj))
57 { 57 {
58 QPushButton *pause_button = new QPushButton (tr("Pause"), this); 58 QPushButton *pause_button = new QPushButton (tr("Pause"), this);
59 QPushButton *stop_button = new QPushButton (tr("Stop"), this); 59 QPushButton *stop_button = new QPushButton (tr("Stop"), this);
60 QPushButton *resume_button = new QPushButton (tr("Continue"), this); 60 QPushButton *resume_button = new QPushButton (tr("Continue"), this);
61 61
62 QGroupBox *input_group_box = new QGroupBox (); 62 QGroupBox *input_group_box = new QGroupBox ();
63 QHBoxLayout *input_layout = new QHBoxLayout; 63 QHBoxLayout *input_layout = new QHBoxLayout;
64 input_layout->addWidget (pause_button); 64 input_layout->addWidget (pause_button);
65 input_layout->addWidget (stop_button); 65 input_layout->addWidget (stop_button);
66 input_layout->addWidget (resume_button); 66 input_layout->addWidget (resume_button);
67 input_group_box->setLayout (input_layout); 67 input_group_box->setLayout (input_layout);
68 68
69 QVBoxLayout *main_layout = new QVBoxLayout (); 69 QVBoxLayout *main_layout = new QVBoxLayout ();
70 main_layout->addWidget (m_console); 70 main_layout->addWidget (m_console);
71 main_layout->addWidget (input_group_box); 71 main_layout->addWidget (input_group_box);
72 72
73 setLayout (main_layout); 73 setLayout (main_layout);
74 74
75 setFocusProxy (m_console); 75 setFocusProxy (m_console);
76 76
77 connect (pause_button, &QPushButton::clicked, 77 connect (pause_button, &QPushButton::clicked,
78 this, &command_widget::interpreter_pause); 78 this, &command_widget::interpreter_pause);
79 79
80 connect (resume_button, &QPushButton::clicked, 80 connect (resume_button, &QPushButton::clicked,
81 this, &command_widget::interpreter_resume); 81 this, &command_widget::interpreter_resume);
82 82
83 connect (stop_button, &QPushButton::clicked, 83 connect (stop_button, &QPushButton::clicked,
84 this, &command_widget::interpreter_stop); 84 this, &command_widget::interpreter_stop);
85 85
86 connect (this, &command_widget::new_command_line_signal, 86 connect (this, &command_widget::new_command_line_signal,
87 m_console, &console::new_command_line); 87 m_console, &console::new_command_line);
88 88
89 insert_interpreter_output ("\n\n Welcome to Octave\n\n"); 89 insert_interpreter_output ("\n\n Welcome to Octave\n\n");
90 90
91 } 91 }
92 92
93 void command_widget::init_command_prompt () 93 void command_widget::init_command_prompt ()
94 { 94 {
95 emit interpreter_event 95 emit interpreter_event
96 ([=] (interpreter& interp) 96 ([=] (interpreter& interp)
97 { 97 {
98 // INTERPRETER THREAD 98 // INTERPRETER THREAD
99 99
100 event_manager& evmgr = interp.get_event_manager (); 100 event_manager& evmgr = interp.get_event_manager ();
101 input_system& input_sys = interp.get_input_system (); 101 input_system& input_sys = interp.get_input_system ();
102 std::string prompt = input_sys.PS1 (); 102 std::string prompt = input_sys.PS1 ();
103 evmgr.update_prompt (command_editor::decode_prompt_string (prompt)); 103 evmgr.update_prompt (command_editor::decode_prompt_string (prompt));
104 104
105 emit new_command_line_signal (); 105 emit new_command_line_signal ();
106 }); 106 });
107 } 107 }
108 108
109 void command_widget::update_prompt (const QString& prompt) 109 void command_widget::update_prompt (const QString& prompt)
110 { 110 {
111 m_prompt = prompt; 111 m_prompt = prompt;
112 } 112 }
113 113
114 QString command_widget::prompt () 114 QString command_widget::prompt ()
115 { 115 {
116 return m_prompt; 116 return m_prompt;
117 } 117 }
118 118
119 void command_widget::insert_interpreter_output (const QString& msg) 119 void command_widget::insert_interpreter_output (const QString& msg)
120 { 120 {
121 m_console->append (msg); 121 m_console->append (msg);
122 } 122 }
123 123
124 void command_widget::process_input_line (const QString& input_line) 124 void command_widget::process_input_line (const QString& input_line)
125 { 125 {
126 emit interpreter_event 126 emit interpreter_event
127 ([=] (interpreter& interp) 127 ([=] (interpreter& interp)
128 { 128 {
129 // INTERPRETER THREAD 129 // INTERPRETER THREAD
130 130
131 interp.parse_and_execute (input_line.toStdString (), 131 interp.parse_and_execute (input_line.toStdString (),
132 m_incomplete_parse); 132 m_incomplete_parse);
133 133
134 event_manager& evmgr = interp.get_event_manager (); 134 event_manager& evmgr = interp.get_event_manager ();
135 input_system& input_sys = interp.get_input_system (); 135 input_system& input_sys = interp.get_input_system ();
136 136
137 std::string prompt 137 std::string prompt
138 = m_incomplete_parse ? input_sys.PS2 () : input_sys.PS1 (); 138 = m_incomplete_parse ? input_sys.PS2 () : input_sys.PS1 ();
139 139
140 evmgr.update_prompt (command_editor::decode_prompt_string (prompt)); 140 evmgr.update_prompt (command_editor::decode_prompt_string (prompt));
141 141
142 emit new_command_line_signal (); 142 emit new_command_line_signal ();
143 }); 143 });
144 144
145 } 145 }
146 146
147 void command_widget::notice_settings (const gui_settings *settings) 147 void command_widget::notice_settings (const gui_settings *settings)
148 { 148 {
149 // Set terminal font: 149 // Set terminal font:
150 QFont term_font = QFont (); 150 QFont term_font = QFont ();
151 term_font.setStyleHint (QFont::TypeWriter); 151 term_font.setStyleHint (QFont::TypeWriter);
152 QString default_font = settings->value (global_mono_font).toString (); 152 QString default_font = settings->value (global_mono_font).toString ();
153 term_font.setFamily 153 term_font.setFamily
154 (settings->value (cs_font.key, default_font).toString ()); 154 (settings->value (cs_font.key, default_font).toString ());
155 term_font.setPointSize 155 term_font.setPointSize
156 (settings->value (cs_font_size).toInt ()); 156 (settings->value (cs_font_size).toInt ());
157 157
158 m_console->setFont (term_font); 158 m_console->setFont (term_font);
159 159
160 // Colors 160 // Colors
161 int mode = settings->value (cs_color_mode).toInt (); 161 int mode = settings->value (cs_color_mode).toInt ();
162 QColor fgc = settings->color_value (cs_colors[0], mode); 162 QColor fgc = settings->color_value (cs_colors[0], mode);
163 QColor bgc = settings->color_value (cs_colors[1], mode); 163 QColor bgc = settings->color_value (cs_colors[1], mode);
164 164
165 m_console->setStyleSheet (QString ("color: %1; background-color:%2;") 165 m_console->setStyleSheet (QString ("color: %1; background-color:%2;")
166 .arg (fgc.name ()).arg (bgc.name ())); 166 .arg (fgc.name ()).arg (bgc.name ()));
167 } 167 }
168 168
169 // The console itself using QScintilla. 169 // The console itself using QScintilla.
170 // This implementation is partly based on the basic concept of 170 // This implementation is partly based on the basic concept of
171 // "qpconsole" as proposed by user "DerManu" in the Qt-forum thread 171 // "qpconsole" as proposed by user "DerManu" in the Qt-forum thread
172 // https://forum.qt.io/topic/28765/command-terminal-using-qtextedit 172 // https://forum.qt.io/topic/28765/command-terminal-using-qtextedit
173 173
174 console::console (command_widget *p, base_qobject&) 174 console::console (command_widget *p, base_qobject&)
175 : QsciScintilla (p), 175 : QsciScintilla (p),
176 m_command_position (-1), 176 m_command_position (-1),
177 m_cursor_position (0), 177 m_cursor_position (0),
178 m_text_changed (false), 178 m_text_changed (false),
179 m_command_widget (p), 179 m_command_widget (p),
180 m_last_key_string (QString ()) 180 m_last_key_string (QString ())
181 { 181 {
182 setMargins (0); 182 setMargins (0);
183 setWrapMode (QsciScintilla::WrapWord); 183 setWrapMode (QsciScintilla::WrapWord);
184 184
185 connect (this, SIGNAL (cursorPositionChanged (int, int)), 185 connect (this, SIGNAL (cursorPositionChanged (int, int)),
186 this, SLOT (cursor_position_changed (int, int))); 186 this, SLOT (cursor_position_changed (int, int)));
187 187
188 connect (this, SIGNAL (textChanged (void)), 188 connect (this, SIGNAL (textChanged (void)),
189 this, SLOT (text_changed (void))); 189 this, SLOT (text_changed (void)));
190 190
191 connect (this, SIGNAL (modificationAttempted (void)), 191 connect (this, SIGNAL (modificationAttempted (void)),
192 this, SLOT (move_cursor_to_end (void))); 192 this, SLOT (move_cursor_to_end (void)));
193 } 193 }
194 194
195 // Prepare a new command line with the current prompt 195 // Prepare a new command line with the current prompt
196 void console::new_command_line (const QString& command) 196 void console::new_command_line (const QString& command)
197 { 197 {
198 if (! text (lines () -1).isEmpty ()) 198 if (! text (lines () -1).isEmpty ())
199 append ("\n"); 199 append ("\n");
200 200
201 append_string (m_command_widget->prompt ()); 201 append_string (m_command_widget->prompt ());
202 202
203 int line, index; 203 int line, index;
204 getCursorPosition (&line,&index); 204 getCursorPosition (&line,&index);
205 m_command_position = positionFromLineIndex (line, index); 205 m_command_position = positionFromLineIndex (line, index);
206 206
207 append_string (command); 207 append_string (command);
208 } 208 }
209 209
210 // Accept the current command line (or block) 210 // Accept the current command line (or block)
211 void console::accept_command_line () 211 void console::accept_command_line ()
212 { 212 {
213 QString input_line = text (lines () - 1); 213 QString input_line = text (lines () - 1);
214 214
215 if (input_line.startsWith (m_command_widget->prompt ())) 215 if (input_line.startsWith (m_command_widget->prompt ()))
216 input_line.remove(0, m_command_widget->prompt ().length ()); 216 input_line.remove(0, m_command_widget->prompt ().length ());
217 217
218 input_line = input_line.trimmed (); 218 input_line = input_line.trimmed ();
219 219
220 append_string ("\n"); 220 append_string ("\n");
221 221
222 if (input_line.isEmpty ()) 222 if (input_line.isEmpty ())
223 new_command_line (); 223 new_command_line ();
224 else 224 else
225 m_command_widget->process_input_line (input_line); 225 m_command_widget->process_input_line (input_line);
226 } 226 }
227 227
228 // Execute a command 228 // Execute a command
229 void console::execute_command (const QString& command) 229 void console::execute_command (const QString& command)
230 { 230 {
231 if (command.trimmed ().isEmpty ()) 231 if (command.trimmed ().isEmpty ())
232 return; 232 return;
233 233
234 new_command_line (command); 234 new_command_line (command);
235 accept_command_line ();
236 }
237
238 // Append a string and update the curdor püosition
239 void console::append_string (const QString& string)
240 {
241 setReadOnly (false);
242 append (string);
243
244 int line, index;
245 lineIndexFromPosition (text ().length (), &line, &index);
246
247 setCursorPosition (line, index);
248 }
249
250 // Cursor position changed: Are we in the command line or not?
251 void console::cursor_position_changed (int line, int col)
252 {
253 m_cursor_position = positionFromLineIndex (line, col);
254 if (m_cursor_position < m_command_position)
255 {
256 // We are in the read only area
257 if (m_text_changed && (m_cursor_position == m_command_position - 1))
258 {
259 setReadOnly (false);
260 insert (m_command_widget->prompt ().right (1)); // And here we have tried to remove the prompt by Backspace
261 setCursorPosition (line+1, col);
262 }
263 setReadOnly (true);
264 }
265 else
266 setReadOnly (false); // Writable area
267
268 m_text_changed = false;
269 }
270
271 // User attempted to type on read only mode: move cursor at end and allow
272 // editing
273 void console::move_cursor_to_end (void)
274 {
275 if ((! m_last_key_string.isEmpty ()) && (m_last_key_string.at (0).isPrint ()))
276 {
277 append_string (m_last_key_string);
278 setReadOnly (true); // Avoid that changing read only text is done afterwards
279 }
280 }
281
282 // Text has changed: is cursor still in "writable" area?
283 // This signal seems to be emitted before cursor position changed.
284 void console::text_changed (void)
285 {
286 m_text_changed = true;
287 }
288
289 // Re-implement key event
290 void console::keyPressEvent (QKeyEvent *e)
291 {
292 if (e->key () == Qt::Key_Return)
293 // On "return", accept the current command line
235 accept_command_line (); 294 accept_command_line ();
236 } 295 else
237 296 {
238 // Append a string and update the curdor püosition 297 // Otherwise, store text process the expected event
239 void console::append_string (const QString& string) 298 m_last_key_string = e->text ();
240 { 299 QsciScintilla::keyPressEvent(e);
241 setReadOnly (false); 300 }
242 append (string); 301 }
243
244 int line, index;
245 lineIndexFromPosition (text ().length (), &line, &index);
246
247 setCursorPosition (line, index);
248 }
249
250 // Cursor position changed: Are we in the command line or not?
251 void console::cursor_position_changed (int line, int col)
252 {
253 m_cursor_position = positionFromLineIndex (line, col);
254 if (m_cursor_position < m_command_position)
255 {
256 // We are in the read only area
257 if (m_text_changed && (m_cursor_position == m_command_position - 1))
258 {
259 setReadOnly (false);
260 insert (m_command_widget->prompt ().right (1)); // And here we have tried to remove the prompt by Backspace
261 setCursorPosition (line+1, col);
262 }
263 setReadOnly (true);
264 }
265 else
266 setReadOnly (false); // Writable area
267
268 m_text_changed = false;
269 }
270
271 // User attempted to type on read only mode: move cursor at end and allow
272 // editing
273 void console::move_cursor_to_end (void)
274 {
275 if ((! m_last_key_string.isEmpty ()) && (m_last_key_string.at (0).isPrint ()))
276 {
277 append_string (m_last_key_string);
278 setReadOnly (true); // Avoid that changing read only text is done afterwards
279 }
280 }
281
282 // Text has changed: is cursor still in "writable" area?
283 // This signal seems to be emitted before cursor position changed.
284 void console::text_changed (void)
285 {
286 m_text_changed = true;
287 }
288
289 // Re-implement key event
290 void console::keyPressEvent (QKeyEvent *e)
291 {
292 if (e->key () == Qt::Key_Return)
293 // On "return", accept the current command line
294 accept_command_line ();
295 else
296 {
297 // Otherwise, store text process the expected event
298 m_last_key_string = e->text ();
299 QsciScintilla::keyPressEvent(e);
300 }
301 }
302 302
303 OCTAVE_END_NAMESPACE(octave) 303 OCTAVE_END_NAMESPACE(octave)
304 304
305 #endif 305 #endif