Quellcode durchsuchen

ctkConsole - Commands entered by user consider CommandTextColor property

Jean-Christophe Fillion-Robin vor 14 Jahren
Ursprung
Commit
e9e8dee209
3 geänderte Dateien mit 22 neuen und 7 gelöschten Zeilen
  1. 18 3
      Libs/Widgets/ctkConsole.cpp
  2. 1 3
      Libs/Widgets/ctkConsole.h
  3. 3 1
      Libs/Widgets/ctkConsole_p.h

+ 18 - 3
Libs/Widgets/ctkConsole.cpp

@@ -190,6 +190,7 @@ void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
       {
       case Qt::Key_Up:
         e->accept();
+
         if (this->CommandPosition > 0)
           {
           this->replaceCommandBuffer(this->CommandHistory[--this->CommandPosition]);
@@ -198,6 +199,7 @@ void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
 
       case Qt::Key_Down:
         e->accept();
+
         if (this->CommandPosition < this->CommandHistory.size() - 2)
           {
           this->replaceCommandBuffer(this->CommandHistory[++this->CommandPosition]);
@@ -259,6 +261,8 @@ void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
 
       default:
         e->accept();
+        this->switchToUserInputTextColor();
+
         QTextEdit::keyPressEvent(e);
         this->updateCommandBuffer();
         this->updateCompleterIfVisible();
@@ -267,6 +271,14 @@ void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
 }
 
 //-----------------------------------------------------------------------------
+void ctkConsolePrivate::switchToUserInputTextColor()
+{
+  QTextCharFormat format = this->currentCharFormat();
+  format.setForeground(this->CommandTextColor);
+  this->setCurrentCharFormat(format);
+}
+
+//-----------------------------------------------------------------------------
 int ctkConsolePrivate::documentEnd() const
 {
   QTextCursor c(this->document());
@@ -363,15 +375,17 @@ void ctkConsolePrivate::updateCommandBuffer()
 }
 
 //-----------------------------------------------------------------------------
-void ctkConsolePrivate::replaceCommandBuffer(const QString& Text)
+void ctkConsolePrivate::replaceCommandBuffer(const QString& text)
 {
-  this->commandBuffer() = Text;
+  this->commandBuffer() = text;
 
   QTextCursor c(this->document());
   c.setPosition(this->InteractivePosition);
   c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
   c.removeSelectedText();
-  c.insertText(Text);
+  this->switchToUserInputTextColor();
+  c.setCharFormat(this->currentCharFormat());
+  c.insertText(text);
 }
 
 //-----------------------------------------------------------------------------
@@ -394,6 +408,7 @@ void ctkConsolePrivate::internalExecuteCommand()
     this->CommandHistory.push_back("");
     this->CommandPosition = this->CommandHistory.size() - 1;
     }
+
   QTextCursor c(this->document());
   c.movePosition(QTextCursor::End);
   c.insertText("\n");

+ 1 - 3
Libs/Widgets/ctkConsole.h

@@ -63,9 +63,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 class ctkConsolePrivate;
 class ctkConsoleCompleter;
 
-/// QWidget that provides an interactive console - you can send text to the
-/// console by calling printString() and receive user input by connecting to the
-/// executeCommand() slot.
+/// QWidget that provides an interactive console
 class CTK_WIDGETS_EXPORT ctkConsole : public QWidget
 {
   Q_OBJECT

+ 3 - 1
Libs/Widgets/ctkConsole_p.h

@@ -42,6 +42,8 @@ public:
   void init();
 
   void keyPressEvent(QKeyEvent* e);
+
+  void switchToUserInputTextColor();
   
   /// Returns the end of the document
   int documentEnd() const;
@@ -62,7 +64,7 @@ public:
   void updateCommandBuffer();
   
   /// Replace the contents of the command buffer, updating the display
-  void replaceCommandBuffer(const QString& Text);
+  void replaceCommandBuffer(const QString& text);
   
   /// References the buffer where the current un-executed command is stored
   QString& commandBuffer();