changeset 31081:b818d4ec035e

exp cmd widget: prevent wirting in previous ouputs * command-widget.cc (console::console): initialize new variables, connect some qscintilla signals; (console::append_string): make text writable; (console::cursor_position_changed: slot for change cursor position, prevent deleting the prompt and set read only state depending on cursor location; (console::move_cursor_to_end): slot for attempt to edit read only text, move cursor to the end and add enttered key there if ti is printable; (text_changed): slot for text changed, remember chaged state for later use in the slot for cursor position changed; (console::keyPressEvent): store entered key as text * command-widget.h (console): new slots cursor_position_changed text_changed, and move_cursor_to_end, new class variable m_cursor_position, m_text_changed, and m_last_key_string
author Torsten Lilge <ttl-octave@mailbox.org>
date Wed, 08 Jun 2022 20:31:10 +0200
parents 56ee6a223c51
children b390f662a150
files libgui/src/command-widget.cc libgui/src/command-widget.h
diffstat 2 files changed, 66 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/command-widget.cc	Tue Jun 07 23:03:11 2022 +0200
+++ b/libgui/src/command-widget.cc	Wed Jun 08 20:31:10 2022 +0200
@@ -173,8 +173,22 @@
   console::console (command_widget *p, base_qobject&)
     : QsciScintilla (p),
       m_command_position (-1),
-      m_command_widget (p)
+      m_cursor_position (0),
+      m_text_changed (false),
+      m_command_widget (p),
+      m_last_key_string (QString ())
   {
+    setMargins (0);
+    setWrapMode (QsciScintilla::WrapWord);
+
+    connect (this, SIGNAL (cursorPositionChanged (int, int)),
+             this, SLOT (cursor_position_changed (int, int)));
+
+    connect (this, SIGNAL (textChanged (void)),
+             this, SLOT (text_changed (void)));
+
+    connect (this, SIGNAL (modificationAttempted (void)),
+             this, SLOT (move_cursor_to_end (void)));
   }
 
   // Prepare a new command line with the current prompt
@@ -223,6 +237,7 @@
   // Append a string and update the curdor püosition
   void console::append_string (const QString& string)
   {
+    setReadOnly (false);
     append (string);
 
     int line, index;
@@ -231,6 +246,42 @@
     setCursorPosition (line, index);
   }
 
+  // Cursor position changed: Are we in the command line or not?
+  void console::cursor_position_changed (int line, int col)
+  {
+    m_command_position = positionFromLineIndex (line, col);
+    if (m_cursor_position < m_command_position)
+      {
+        // We are in the read only area
+        if (m_text_changed && (m_cursor_position == m_command_position - 1))
+          undo ();  // And here we have tried to remove the prompt by Backspace
+        else
+          setReadOnly (true);
+      }
+    else
+      setReadOnly (false);  // Writable area
+
+    m_text_changed = false;
+  }
+
+  // User attempted to type on read only mode: move cursor at end and allow
+  // editing
+  void console::move_cursor_to_end (void)
+  {
+    if ((! m_last_key_string.isEmpty ()) && (m_last_key_string.at (0).isPrint ()))
+      {
+        append_string (m_last_key_string);
+        setReadOnly (true); // Avoid that changing read only text is done afterwards
+      }
+  }
+
+  // Text has changed: is cursor still in "writable" area?
+  // This signal seems to be emitted before cursor position changed.
+  void console::text_changed (void)
+  {
+    m_text_changed = true;
+  }
+
   // Re-implement key event
   void console::keyPressEvent (QKeyEvent *e)
   {
@@ -238,8 +289,11 @@
       // On "return", accept the current command line
       accept_command_line ();
     else
-      // Otherwise, process the expected event
-      QsciScintilla::keyPressEvent(e);
+      {
+        // Otherwise, store text process the expected event
+        m_last_key_string = e->text ();
+        QsciScintilla::keyPressEvent(e);
+      }
   }
 
 }
--- a/libgui/src/command-widget.h	Tue Jun 07 23:03:11 2022 +0200
+++ b/libgui/src/command-widget.h	Wed Jun 08 20:31:10 2022 +0200
@@ -50,6 +50,12 @@
 
   public slots:
 
+    void cursor_position_changed (int line, int col);
+
+    void text_changed (void);
+
+    void move_cursor_to_end (void);
+
     void new_command_line (const QString& command = QString ());
 
     void execute_command (const QString& command);
@@ -65,8 +71,10 @@
     void accept_command_line (void);
 
     int m_command_position;
+    int m_cursor_position;
+    bool m_text_changed;
     command_widget *m_command_widget;
-    QTextDocument *m_document;
+    QString m_last_key_string;
 
   };