changeset 31080:56ee6a223c51

use QScintilla instead of QTextEdit for new terminal widget * command-widget.cc (ctor): pass qobj to console; (insert_interpreter_output): just append output message to console text; (console::console): initialize QScintilla, remove obsolete document; (console::new_command_line): append new line, add prompt, remember current cursor pos for later use, append possibly given command string; (console::accept_command_line): just get last line, and append a new line; (console::append_block): removed; (console::append_string): new function appending a string and moving cursor to the end; (console::keyPressEvent): process QsciScintilla::keyPressEvent * command-widget.h: include Qsci/qsciscintilla.h, console inherits from QsciScintilla and gets oct_qobj for later use, new function append_string, new class variable m_command_position for storing the begin of the active command line
author Torsten Lilge <ttl-octave@mailbox.org>
date Tue, 07 Jun 2022 23:03:11 +0200
parents f0343b3685df
children b818d4ec035e
files libgui/src/command-widget.cc libgui/src/command-widget.h
diffstat 2 files changed, 41 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/command-widget.cc	Tue Jun 07 19:44:07 2022 +0200
+++ b/libgui/src/command-widget.cc	Tue Jun 07 23:03:11 2022 +0200
@@ -48,10 +48,10 @@
 
 namespace octave
 {
-  command_widget::command_widget (base_qobject&, QWidget *p)
+  command_widget::command_widget (base_qobject& oct_qobj, QWidget *p)
     : QWidget (p), m_incomplete_parse (false),
       m_prompt (QString ()),
-      m_console (new console (this))
+      m_console (new console (this, oct_qobj))
   {
     QPushButton *pause_button = new QPushButton (tr("Pause"), this);
     QPushButton *stop_button = new QPushButton (tr("Stop"), this);
@@ -116,11 +116,7 @@
 
   void command_widget::insert_interpreter_output (const QString& msg)
   {
-    QTextCursor cursor = m_console->textCursor ();
-
-    cursor.insertText (msg);
-
-    m_console->setTextCursor (cursor);
+    m_console->append (msg);
   }
 
   void command_widget::process_input_line (const QString& input_line)
@@ -169,46 +165,44 @@
   }
 
 
-  // The console itself using QTextEdit with QTextBlock and QTextCursor.
-  // This implementation is based on the basic concept of "qpconsole" as
-  // proposed by user "DerManu" in the Qt-forum thread
+  // The console itself using QScintilla.
+  // This implementation is partly based on the basic concept of
+  // "qpconsole" as proposed by user "DerManu" in the Qt-forum thread
   // https://forum.qt.io/topic/28765/command-terminal-using-qtextedit
 
-  console::console (command_widget *p)
-    : QTextEdit (p),
-      m_command_block_number (-1),
-      m_command_widget (p),
-      m_document (new QTextDocument (this))
+  console::console (command_widget *p, base_qobject&)
+    : QsciScintilla (p),
+      m_command_position (-1),
+      m_command_widget (p)
   {
-    setDocument (m_document);
   }
 
   // Prepare a new command line with the current prompt
   void console::new_command_line (const QString& command)
   {
-    QTextCursor cursor (m_document->lastBlock ());
+    if (! text (lines () -1).isEmpty ())
+      append ("\n");
+
+    append_string (m_command_widget->prompt ());
 
-    if (! m_document->lastBlock ().text ().isEmpty ())
-      {
-        cursor.movePosition (QTextCursor::EndOfBlock);
-        cursor.insertBlock ();
-      }
+    int line, index;
+    getCursorPosition (&line,&index);
+    m_command_position = positionFromLineIndex (line, index);
 
-    cursor.insertText (m_command_widget->prompt () + command);
-    setTextCursor (cursor);
+    append_string (command);
   }
 
   // Accept the current command line (or block)
   void console::accept_command_line ()
   {
-    QString input_line = m_document->lastBlock().text();
+    QString input_line = text (lines () - 1);
 
     if (input_line.startsWith (m_command_widget->prompt ()))
       input_line.remove(0, m_command_widget->prompt ().length ());
 
     input_line = input_line.trimmed ();
-  
-    append_block ();
+
+    append_string ("\n");
 
     if (input_line.isEmpty ())
       new_command_line ();
@@ -216,15 +210,6 @@
       m_command_widget->process_input_line (input_line);
   }
 
-  // Append a block to the document
-  void console::append_block ()
-  {
-    QTextCursor cursor (m_document->lastBlock ());
-    cursor.movePosition (QTextCursor::EndOfBlock);
-    cursor.insertBlock ();
-    setTextCursor (cursor);
-  }
-
   // Execute a command
   void console::execute_command (const QString& command)
   {
@@ -235,6 +220,17 @@
     accept_command_line ();
   }
 
+  // Append a string and update the curdor püosition
+  void console::append_string (const QString& string)
+  {
+    append (string);
+
+    int line, index;
+    lineIndexFromPosition (text ().length (), &line, &index);
+
+    setCursorPosition (line, index);
+  }
+
   // Re-implement key event
   void console::keyPressEvent (QKeyEvent *e)
   {
@@ -243,7 +239,7 @@
       accept_command_line ();
     else
       // Otherwise, process the expected event
-      QTextEdit::keyPressEvent(e);                          
+      QsciScintilla::keyPressEvent(e);
   }
 
 }
--- a/libgui/src/command-widget.h	Tue Jun 07 19:44:07 2022 +0200
+++ b/libgui/src/command-widget.h	Tue Jun 07 23:03:11 2022 +0200
@@ -27,28 +27,26 @@
 #define octave_command_widget_h 1
 
 #include <QWidget>
-#include <QTextEdit>
+
+#include <Qsci/qsciscintilla.h>
 
 #include "octave-qobject.h"
 #include "gui-settings.h"
 
-class QLabel;
-class QLineEdit;
-class QStrung;
-class QTextEdit;
+class QsciScintilla;
 
 namespace octave
 {
   class base_qobject;
   class command_widget;
 
-  class console : public QTextEdit
+  class console : public QsciScintilla
   {
     Q_OBJECT
 
   public:
 
-    console (command_widget *p);
+    console (command_widget *p, base_qobject& oct_qobj);
 
   public slots:
 
@@ -62,11 +60,11 @@
 
   private:
 
+    void append_string (const QString& string);
+
     void accept_command_line (void);
 
-    void append_block (void);
-
-    int m_command_block_number;
+    int m_command_position;
     command_widget *m_command_widget;
     QTextDocument *m_document;