Browse Source

ctkConsole - Move function to pimpl, txt color are now properties

Jean-Christophe Fillion-Robin 14 years ago
parent
commit
2ec3eb7e21

+ 14 - 48
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -249,24 +249,24 @@ void ctkPythonConsolePrivate::promptForInput(const QString& indent)
   Q_Q(ctkPythonConsole);
 
   QTextCharFormat format = q->getFormat();
-  format.setForeground(QColor(0, 0, 0));
+  format.setForeground(q->promptColor());
   q->setFormat(format);
 
-//     this->Interpreter->MakeCurrent();
+//  this->Interpreter->MakeCurrent();
   if(!this->MultilineStatement)
     {
-    q->prompt(">>> ");
+    this->prompt(">>> ");
     //q->prompt(
     //  PyString_AsString(PySys_GetObject(const_cast<char*>("ps1"))));
     }
   else
     {
-    q->prompt("... ");
+    this->prompt("... ");
     //q->prompt(
     //  PyString_AsString(PySys_GetObject(const_cast<char*>("ps2"))));
     }
-  q->printCommand(indent);
-//     this->Interpreter->ReleaseControl();
+  this->printCommand(indent);
+//  this->Interpreter->ReleaseControl();
 }
 
 //----------------------------------------------------------------------------
@@ -287,19 +287,18 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   d->PythonManager->mainContext();
   d->initializeInteractiveConsole();
 
-  QTextCharFormat format = this->getFormat();
-  format.setForeground(QColor(0, 0, 255));
-  this->setFormat(format);
-  this->printString(
-    QString("Python %1 on %2\n").arg(Py_GetVersion()).arg(Py_GetPlatform()));
+  this->printMessage(
+        QString("Python %1 on %2\n").arg(Py_GetVersion()).arg(Py_GetPlatform()),
+        this->welcomeTextColor());
+
   d->promptForInput();
 
-  Q_ASSERT(PythonQt::self());
+  Q_ASSERT(PythonQt::self()); // PythonQt should be initialized
 
   this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
-                SLOT(printStdout(const QString&)));
+                d, SLOT(printOutputMessage(const QString&)));
   this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
-                SLOT(printStderr(const QString&)));
+                d, SLOT(printErrorMessage(const QString&)));
 }
 
 //----------------------------------------------------------------------------
@@ -321,7 +320,7 @@ void ctkPythonConsole::executeScript(const QString& script)
   Q_D(ctkPythonConsole);
   Q_UNUSED(script);
 
-  this->printStdout("\n");
+  d->printOutputMessage("\n");
   emit this->executing(true);
 //   d->Interpreter->RunSimpleString(
 //     script.toAscii().data());
@@ -392,39 +391,6 @@ QStringList ctkPythonConsole::pythonAttributes(const QString& pythonVariableName
 }
 
 //----------------------------------------------------------------------------
-void ctkPythonConsole::printStdout(const QString& text)
-{
-  QTextCharFormat format = this->getFormat();
-  format.setForeground(QColor(0, 150, 0));
-  this->setFormat(format);
-
-  this->printString(text);
-
-  QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
-}
-
-//----------------------------------------------------------------------------
-void ctkPythonConsole::printMessage(const QString& text)
-{
-  QTextCharFormat format = this->getFormat();
-  format.setForeground(QColor(0, 0, 150));
-  this->setFormat(format);
-  this->printString(text);
-}
-
-//----------------------------------------------------------------------------
-void ctkPythonConsole::printStderr(const QString& text)
-{
-  QTextCharFormat format = this->getFormat();
-  format.setForeground(QColor(255, 0, 0));
-  this->setFormat(format);
-
-  this->printString(text);
-
-  QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
-}
-
-//----------------------------------------------------------------------------
 void ctkPythonConsole::executeCommand(const QString& command)
 {
   Q_D(ctkPythonConsole);

+ 0 - 7
Libs/Scripting/Python/Widgets/ctkPythonConsole.h

@@ -76,9 +76,6 @@ public:
   ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWidget* parentObject = 0);
   virtual ~ctkPythonConsole();
 
-  /// Prints some text on the shell.
-  void printMessage(const QString&);
-
   /// Given a python variable name, lookup its attributes and return them in a string list.
   QStringList pythonAttributes(const QString& pythonVariableName) const;
 
@@ -89,10 +86,6 @@ public slots:
 protected:
   virtual void executeCommand(const QString& command);
 
-protected slots:
-  void printStderr(const QString&);
-  void printStdout(const QString&);
-
 private:
   Q_DECLARE_PRIVATE(ctkPythonConsole);
   Q_DISABLE_COPY(ctkPythonConsole);

+ 1 - 0
Libs/Widgets/CMakeLists.txt

@@ -148,6 +148,7 @@ SET(KIT_MOC_SRCS
   ctkColorPickerButton.h
   ctkConfirmExitDialog.h
   ctkConsole.h
+  ctkConsole_p.h
   ctkCoordinatesWidget.h
   ctkDirectoryButton.h
   ctkDoubleRangeSlider.h

+ 111 - 63
Libs/Widgets/ctkConsole.cpp

@@ -64,6 +64,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // CTK includes
 #include "ctkConsole.h"
 #include "ctkConsole_p.h"
+#include "ctkPimpl.h"
 
 //-----------------------------------------------------------------------------
 // ctkConsolePrivate methods
@@ -86,6 +87,12 @@ void ctkConsolePrivate::init()
   this->setAcceptRichText(false);
   this->setUndoRedoEnabled(false);
 
+  this->PromptColor = QColor(0, 0, 0);    // Black
+  this->OutputTextColor = QColor(0, 150, 0);  // Green
+  this->ErrorTextColor = QColor(255, 0, 0);   // Red
+  this->CommandTextColor = QColor(0, 0, 150); // Blue
+  this->WelcomeTextColor = QColor(0, 0, 255); // Dark Blue
+
   QFont f;
   f.setFamily("Courier");
   f.setStyleHint(QFont::TypeWriter);
@@ -93,7 +100,7 @@ void ctkConsolePrivate::init()
 
   QTextCharFormat format;
   format.setFont(f);
-  format.setForeground(QColor(0, 0, 0));
+  format.setForeground(this->OutputTextColor);
   this->setCurrentCharFormat(format);
 
   this->CommandHistory.append("");
@@ -288,10 +295,9 @@ void ctkConsolePrivate::updateCompleterIfVisible()
 //-----------------------------------------------------------------------------
 void ctkConsolePrivate::selectCompletion()
 {
-  Q_Q(ctkConsole);
   if (this->Completer && this->Completer->completionCount() == 1)
     {
-    q->insertCompletion(this->Completer->currentCompletion());
+    this->insertCompletion(this->Completer->currentCompletion());
     this->Completer->popup()->hide();
     }
 }
@@ -382,22 +388,95 @@ void ctkConsolePrivate::internalExecuteCommand()
 //-----------------------------------------------------------------------------
 void ctkConsolePrivate::setCompleter(ctkConsoleCompleter* completer)
 {
-  Q_Q(ctkConsole);
-
   if (this->Completer)
     {
     this->Completer->setWidget(0);
-    QObject::disconnect(this->Completer, SIGNAL(activated(const QString&)),
-                        q, SLOT(insertCompletion(const QString&)));
+    disconnect(this->Completer, SIGNAL(activated(const QString&)),
+               this, SLOT(insertCompletion(const QString&)));
 
     }
   this->Completer = completer;
   if (this->Completer)
     {
     this->Completer->setWidget(this);
-    QObject::connect(this->Completer, SIGNAL(activated(const QString&)),
-                     q, SLOT(insertCompletion(const QString&)));
+    connect(this->Completer, SIGNAL(activated(const QString&)),
+            this, SLOT(insertCompletion(const QString&)));
+    }
+}
+
+//-----------------------------------------------------------------------------
+void ctkConsolePrivate::printString(const QString& text)
+{
+  this->textCursor().movePosition(QTextCursor::End);
+  this->textCursor().insertText(text);
+  this->InteractivePosition = this->documentEnd();
+  this->ensureCursorVisible();
+}
+
+//----------------------------------------------------------------------------
+void ctkConsolePrivate::printOutputMessage(const QString& text)
+{
+  Q_Q(ctkConsole);
+
+  q->printMessage(text, q->outputTextColor());
+  QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
+}
+
+//----------------------------------------------------------------------------
+void ctkConsolePrivate::printErrorMessage(const QString& text)
+{
+  Q_Q(ctkConsole);
+
+  q->printMessage(text, q->errorTextColor());
+  QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
+}
+
+//-----------------------------------------------------------------------------
+void ctkConsolePrivate::printCommand(const QString& cmd)
+{
+  this->textCursor().insertText(cmd);
+  this->updateCommandBuffer();
+}
+
+//-----------------------------------------------------------------------------
+void ctkConsolePrivate::prompt(const QString& text)
+{
+  QTextCursor text_cursor = this->textCursor();
+
+  // If the cursor is currently on a clean line, do nothing, otherwise we move
+  // the cursor to a new line before showing the prompt.
+  text_cursor.movePosition(QTextCursor::StartOfLine);
+  int startpos = text_cursor.position();
+  text_cursor.movePosition(QTextCursor::EndOfLine);
+  int endpos = text_cursor.position();
+  if (endpos != startpos)
+    {
+    this->textCursor().insertText("\n");
+    }
+
+  this->textCursor().insertText(text);
+  this->InteractivePosition = this->documentEnd();
+  this->ensureCursorVisible();
+}
+
+//-----------------------------------------------------------------------------
+void ctkConsolePrivate::insertCompletion(const QString& completion)
+{
+  QTextCursor tc = this->textCursor();
+  tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
+  if (tc.selectedText()==".")
+    {
+    tc.insertText(QString(".") + completion);
     }
+  else
+    {
+    tc = this->textCursor();
+    tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
+    tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+    tc.insertText(completion);
+    this->setTextCursor(tc);
+    }
+  this->updateCommandBuffer();
 }
 
 //-----------------------------------------------------------------------------
@@ -452,72 +531,41 @@ void ctkConsole::setCompleter(ctkConsoleCompleter* completer)
 }
 
 //-----------------------------------------------------------------------------
-void ctkConsole::insertCompletion(const QString& completion)
-{
-  Q_D(ctkConsole);
-  QTextCursor tc = d->textCursor();
-  tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
-  if (tc.selectedText()==".")
-    {
-    tc.insertText(QString(".") + completion);
-    }
-  else
-    {
-    tc = d->textCursor();
-    tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
-    tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
-    tc.insertText(completion);
-    d->setTextCursor(tc);
-    }
-  d->updateCommandBuffer();
-}
+CTK_GET_CPP(ctkConsole, QColor, promptColor, PromptColor);
+CTK_SET_CPP(ctkConsole, const QColor&, setPromptColor, PromptColor);
 
 //-----------------------------------------------------------------------------
-void ctkConsole::executeCommand(const QString& command)
-{
-  qWarning() << "ctkConsole::executeCommand not implemented !";
-  qWarning() << "command:" << command;
-}
+CTK_GET_CPP(ctkConsole, QColor, outputTextColor, OutputTextColor);
+CTK_SET_CPP(ctkConsole, const QColor&, setOutputTextColor, OutputTextColor);
 
 //-----------------------------------------------------------------------------
-void ctkConsole::printString(const QString& Text)
-{
-  Q_D(ctkConsole);
-  d->textCursor().movePosition(QTextCursor::End);
-  d->textCursor().insertText(Text);
-  d->InteractivePosition = d->documentEnd();
-  d->ensureCursorVisible();
-}
+CTK_GET_CPP(ctkConsole, QColor, errorTextColor, ErrorTextColor);
+CTK_SET_CPP(ctkConsole, const QColor&, setErrorTextColor, ErrorTextColor);
+
+//-----------------------------------------------------------------------------
+CTK_GET_CPP(ctkConsole, QColor, commandTextColor, CommandTextColor);
+CTK_SET_CPP(ctkConsole, const QColor&, setCommandTextColor, CommandTextColor);
 
 //-----------------------------------------------------------------------------
-void ctkConsole::printCommand(const QString& cmd)
+CTK_GET_CPP(ctkConsole, QColor, welcomeTextColor, WelcomeTextColor);
+CTK_SET_CPP(ctkConsole, const QColor&, setWelcomeTextColor, WelcomeTextColor);
+
+//-----------------------------------------------------------------------------
+void ctkConsole::executeCommand(const QString& command)
 {
-  Q_D(ctkConsole);
-  d->textCursor().insertText(cmd);
-  d->updateCommandBuffer();
+  qWarning() << "ctkConsole::executeCommand not implemented !";
+  qWarning() << "command:" << command;
 }
 
-//-----------------------------------------------------------------------------
-void ctkConsole::prompt(const QString& text)
+//----------------------------------------------------------------------------
+void ctkConsole::printMessage(const QString& message, const QColor& color)
 {
   Q_D(ctkConsole);
 
-  QTextCursor text_cursor = d->textCursor();
-
-  // If the cursor is currently on a clean line, do nothing, otherwise we move
-  // the cursor to a new line before showing the prompt.
-  text_cursor.movePosition(QTextCursor::StartOfLine);
-  int startpos = text_cursor.position();
-  text_cursor.movePosition(QTextCursor::EndOfLine);
-  int endpos = text_cursor.position();
-  if (endpos != startpos)
-    {
-    d->textCursor().insertText("\n");
-    }
-
-  d->textCursor().insertText(text);
-  d->InteractivePosition = d->documentEnd();
-  d->ensureCursorVisible();
+  QTextCharFormat format = this->getFormat();
+  format.setForeground(color);
+  this->setFormat(format);
+  d->printString(message);
 }
 
 //-----------------------------------------------------------------------------

+ 36 - 20
Libs/Widgets/ctkConsole.h

@@ -69,47 +69,63 @@ class ctkConsoleCompleter;
 class CTK_WIDGETS_EXPORT ctkConsole : public QWidget
 {
   Q_OBJECT
+  Q_PROPERTY(QColor promptColor READ promptColor WRITE setPromptColor)
+  Q_PROPERTY(QColor outputTextColor READ outputTextColor WRITE setOutputTextColor)
+  Q_PROPERTY(QColor errorTextColor READ errorTextColor WRITE setErrorTextColor)
+  Q_PROPERTY(QColor commandTextColor READ commandTextColor WRITE setCommandTextColor)
+  Q_PROPERTY(QColor welcomeTextColor READ welcomeTextColor WRITE setWelcomeTextColor)
   
 public:
   ctkConsole(QWidget* parentObject = 0);
   virtual ~ctkConsole();
 
-  /// Returns the current formatting that will be used by printString
+  /// Returns the current formatting that will be used by printMessage()
   QTextCharFormat getFormat() const;
   
-  /// Sets formatting that will be used by printString
+  /// Sets formatting that will be used by printMessage()
   void setFormat(const QTextCharFormat& Format);
 
-  /// Set a completer for this console widget
+  /// Set a completer for this console
   void setCompleter(ctkConsoleCompleter* completer);
 
+  QColor promptColor()const;
+
+  /// \sa promptColor()
+  void setPromptColor(const QColor& newColor);
+
+  QColor outputTextColor()const;
+
+  /// \sa outputTextColor()
+  void setOutputTextColor(const QColor& newColor);
+
+  QColor errorTextColor()const;
+
+  /// \sa errorTextColor()
+  void setErrorTextColor(const QColor& newColor);
+
+  QColor commandTextColor()const;
+
+  /// \sa commandTextColor()
+  void setCommandTextColor(const QColor& newColor);
+
+  QColor welcomeTextColor()const;
+
+  /// \sa welcomeTextColor()
+  void setWelcomeTextColor(const QColor& newColor);
+
+  /// Prints text on the console
+  void printMessage(const QString& message, const QColor& color);
+
 signals:
 
   /// This signal emitted before and after a command is executed
   void executing(bool);
 
 public slots:
-  /// Writes the supplied text to the console
-  void printString(const QString& Text);
-
-  /// Updates the current command. Unlike printString, this will affect the
-  /// current command being typed.
-  void printCommand(const QString& cmd);
 
   /// Clears the contents of the console
   virtual void clear();
 
-  /// Puts out an input accepting prompt.
-  /// It is recommended that one uses prompt instead of printString() to print
-  /// an input prompt since this call ensures that the prompt is shown on a new
-  /// line.
-  void prompt(const QString& text);
-
-  /// Inserts the given completion string at the cursor.  This will replace
-  /// the current word that the cursor is touching with the given text.
-  /// Determines the word using QTextCursor::StartOfWord, EndOfWord.
-  void insertCompletion(const QString& text);
-
 protected:
 
   /// Called whenever the user enters a command

+ 43 - 0
Libs/Widgets/ctkConsole_p.h

@@ -23,6 +23,7 @@
 
 // Qt includes
 #include <QTextEdit>
+#include <QPointer>
 
 // CTK includes
 #include "ctkConsole.h"
@@ -30,6 +31,7 @@
 
 class CTK_WIDGETS_EXPORT ctkConsolePrivate : public QTextEdit
 {
+  Q_OBJECT
   Q_DECLARE_PUBLIC(ctkConsole);
 protected:
   ctkConsole* const q_ptr;
@@ -68,6 +70,32 @@ public:
 
   void setCompleter(ctkConsoleCompleter* completer);
 
+  /// Writes the supplied text to the console
+  void printString(const QString& text);
+
+  /// Updates the current command.
+  /// Unlike printMessage(), this will affect the current command being typed.
+  void printCommand(const QString& cmd);
+
+  /// Puts out an input accepting prompt.
+  /// It is recommended that one uses prompt instead of printString() to print
+  /// an input prompt since this call ensures that the prompt is shown on a new
+  /// line.
+  void prompt(const QString& text);
+
+public slots:
+
+  /// Inserts the given completion string at the cursor.  This will replace
+  /// the current word that the cursor is touching with the given text.
+  /// Determines the word using QTextCursor::StartOfWord, EndOfWord.
+  void insertCompletion(const QString& text);
+
+  void printOutputMessage(const QString& text);
+
+  void printErrorMessage(const QString& text);
+
+public:
+
   /// A custom completer
   QPointer<ctkConsoleCompleter> Completer;
 
@@ -80,6 +108,21 @@ public:
 
   /// Stores the current position in the command-history
   int CommandPosition;
+
+  /// Prompt  color
+  QColor PromptColor;
+
+  /// Output text color
+  QColor OutputTextColor;
+
+  /// Error ext color
+  QColor ErrorTextColor;
+
+  /// Command text color
+  QColor CommandTextColor;
+
+  /// Welcome text color
+  QColor WelcomeTextColor;
 };