Sfoglia il codice sorgente

Merge remote-tracking branch 'finetjul/295-ctkconsole-ensure-lines-visible'

* finetjul/295-ctkconsole-ensure-lines-visible:
  Ensure edit line in ctkConsole is always visible
Jean-Christophe Fillion-Robin 11 anni fa
parent
commit
dc9fd8167b
2 ha cambiato i file con 28 aggiunte e 9 eliminazioni
  1. 24 9
      Libs/Widgets/ctkConsole.cpp
  2. 4 0
      Libs/Widgets/ctkConsole_p.h

+ 24 - 9
Libs/Widgets/ctkConsole.cpp

@@ -144,6 +144,7 @@ void ctkConsolePrivate::init()
 
   connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)),
           SLOT(onScrollBarValueChanged(int)));
+  connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));
 }
 
 //-----------------------------------------------------------------------------
@@ -198,7 +199,11 @@ void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
       }
 
     // Force the cursor back to the interactive area
-    if(history_area && e->key() != Qt::Key_Control)
+    if(history_area
+       && e->key() != Qt::Key_Control
+       && e->key() != Qt::Key_Meta
+       && e->key() != Qt::Key_Alt
+       )
       {
       text_cursor.setPosition(this->documentEnd());
       this->setTextCursor(text_cursor);
@@ -305,14 +310,21 @@ void ctkConsolePrivate::switchToUserInputTextColor(QTextCursor* textCursorToUpda
     color = this->StdinTextColor;
     }
   QTextCharFormat currentFormat = this->currentCharFormat();
-  currentFormat.setForeground(color);
-  this->setCurrentCharFormat(currentFormat);
+  // Do not trigger a finishEdit for no reason. onTextChanged() would be called.
+  if (currentFormat.foreground() != color)
+    {
+    currentFormat.setForeground(color);
+    this->setCurrentCharFormat(currentFormat);
+    }
 
   if (textCursorToUpdate)
     {
     QTextCharFormat textCursorFormat = textCursorToUpdate->charFormat();
-    textCursorFormat.setForeground(color);
-    textCursorToUpdate->setCharFormat(textCursorFormat);
+    if (textCursorFormat.foreground() != color)
+      {
+      textCursorFormat.setForeground(color);
+      textCursorToUpdate->setCharFormat(textCursorFormat);
+      }
     }
 }
 
@@ -519,8 +531,6 @@ void ctkConsolePrivate::printString(const QString& text)
   this->textCursor().movePosition(QTextCursor::End);
   this->textCursor().insertText(text);
   this->InteractivePosition = this->documentEnd();
-  this->ensureCursorVisible();
-  this->scrollToBottom();
 }
 
 //----------------------------------------------------------------------------
@@ -584,8 +594,6 @@ void ctkConsolePrivate::prompt(const QString& text)
 
   this->textCursor().insertText(text);
   this->InteractivePosition = this->documentEnd();
-  this->ensureCursorVisible();
-  this->scrollToBottom();
 }
 
 //----------------------------------------------------------------------------
@@ -625,6 +633,13 @@ void ctkConsolePrivate::onScrollBarValueChanged(int value)
 }
 
 //-----------------------------------------------------------------------------
+void ctkConsolePrivate::onTextChanged()
+{
+  this->scrollToBottom();
+  this->ensureCursorVisible();
+}
+
+//-----------------------------------------------------------------------------
 bool ctkConsolePrivate::isCursorInHistoryArea()const
 {
   return this->textCursor().anchor() < this->InteractivePosition

+ 4 - 0
Libs/Widgets/ctkConsole_p.h

@@ -121,6 +121,10 @@ public Q_SLOTS:
   /// Update the value of ScrollbarAtBottom given the current position of the scollbar
   void onScrollBarValueChanged(int value);
 
+  /// Ensure the interactive line is visible (even when it is split on 2 lines)
+  /// \sa onScrollBarValueChanged()
+  void onTextChanged();
+
 protected:
   /// Return true if the cursor position is in the history area
   /// false if it is after the InteractivePosition.