Ver código fonte

Add ctkConsole::SplitCopiedTextByLine property

Copied text is split by lines and each one is executed (expected the last one)
Jean-Christophe Fillion-Robin 14 anos atrás
pai
commit
b56b6c7b14
3 arquivos alterados com 35 adições e 10 exclusões
  1. 32 8
      Libs/Widgets/ctkConsole.cpp
  2. 2 1
      Libs/Widgets/ctkConsole.h
  3. 1 1
      Libs/Widgets/ctkConsole_p.h

+ 32 - 8
Libs/Widgets/ctkConsole.cpp

@@ -175,8 +175,26 @@ void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
         const QString text = clipboard->text();
         if(!text.isNull())
           {
-          text_cursor.insertText(text);
-          this->updateCommandBuffer();
+          if (this->EditorHints & ctkConsole::SplitCopiedTextByLine)
+            {
+            QStringList lines = text.split(QRegExp("(?:\r\n|\r|\n)"));
+            for(int i=0; i < lines.count(); ++i)
+              {
+              this->switchToUserInputTextColor(&text_cursor);
+              text_cursor.insertText(lines.at(i));
+              this->updateCommandBuffer();
+              if (i < lines.count() - 1)
+                {
+                this->internalExecuteCommand();
+                }
+              }
+            }
+          else
+            {
+            this->switchToUserInputTextColor(&text_cursor);
+            text_cursor.insertText(text);
+            this->updateCommandBuffer();
+            }
           }
         }
 
@@ -276,11 +294,18 @@ void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
 }
 
 //-----------------------------------------------------------------------------
-void ctkConsolePrivate::switchToUserInputTextColor()
+void ctkConsolePrivate::switchToUserInputTextColor(QTextCursor* textCursorToUpdate)
 {
-  QTextCharFormat format = this->currentCharFormat();
-  format.setForeground(this->CommandTextColor);
-  this->setCurrentCharFormat(format);
+  QTextCharFormat currentFormat = this->currentCharFormat();
+  currentFormat.setForeground(this->CommandTextColor);
+  this->setCurrentCharFormat(currentFormat);
+
+  if (textCursorToUpdate)
+    {
+    QTextCharFormat textCursorFormat = textCursorToUpdate->charFormat();
+    textCursorFormat.setForeground(this->CommandTextColor);
+    textCursorToUpdate->setCharFormat(textCursorFormat);
+    }
 }
 
 //-----------------------------------------------------------------------------
@@ -388,8 +413,7 @@ void ctkConsolePrivate::replaceCommandBuffer(const QString& text)
   c.setPosition(this->InteractivePosition);
   c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
   c.removeSelectedText();
-  this->switchToUserInputTextColor();
-  c.setCharFormat(this->currentCharFormat());
+  this->switchToUserInputTextColor(&c);
   c.insertText(text);
 }
 

+ 2 - 1
Libs/Widgets/ctkConsole.h

@@ -86,7 +86,8 @@ public:
   {
     NoHints = 0x00,
     AutomaticIndentation = 0x01, /*!< Align cursor based an indentation of the previous command */
-    RemoveTrailingSpaces = 0x02  /*!< Remove trailing spaces of the entered command */
+    RemoveTrailingSpaces = 0x02, /*!< Remove trailing spaces of the entered command */
+    SplitCopiedTextByLine = 0x4  /*!< Copied text is split by lines and each one is executed (expected the last one) */
   };
   Q_DECLARE_FLAGS(EditorHints, EditorHint)
 

+ 1 - 1
Libs/Widgets/ctkConsole_p.h

@@ -43,7 +43,7 @@ public:
 
   virtual void keyPressEvent(QKeyEvent* e);
 
-  void switchToUserInputTextColor();
+  void switchToUserInputTextColor(QTextCursor* textCursorToUpdate = 0);
   
   /// Returns the end of the document
   int documentEnd() const;