Forráskód Böngészése

ctkConsole/ctkPythonConsole - ps1 and ps2 are property

Note also that if sys.ps1 or sys.ps2 are set from the python
console we will be updated accordingly.
Jean-Christophe Fillion-Robin 14 éve
szülő
commit
b2ea64ae2e

+ 53 - 42
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -157,16 +157,14 @@ public:
 
   bool push(const QString& code);
 
-  void resetBuffer();
+  /// Reset the input buffer of the interactive console
+//  void resetInputBuffer();
 
-  void promptForInput(const QString& indent = QString());
+  void printWelcomeMessage();
 
   ctkAbstractPythonManager* PythonManager;
 
-  /// Indicates if the last statement processes was incomplete.
-  bool MultilineStatement;
-
-  PyObject* InteractiveConsole;
+  PyObject*                 InteractiveConsole;
 };
 
 //----------------------------------------------------------------------------
@@ -176,7 +174,7 @@ public:
 ctkPythonConsolePrivate::ctkPythonConsolePrivate(
   ctkPythonConsole& object, ctkAbstractPythonManager* pythonManager)
   : ctkConsolePrivate(object), PythonManager(pythonManager),
-    MultilineStatement(false), InteractiveConsole(0)
+    InteractiveConsole(0)
 {
 }
 
@@ -191,7 +189,7 @@ void ctkPythonConsolePrivate::initializeInteractiveConsole()
   // set up the code.InteractiveConsole instance that we'll use.
   const char* code =
     "import code\n"
-    "__ctkConsole=code.InteractiveConsole(locals())\n";
+    "__ctkConsole = code.InteractiveConsole(locals())\n";
   PyRun_SimpleString(code);
 
   // Now get the reference to __ctkConsole and save the pointer.
@@ -231,42 +229,26 @@ bool ctkPythonConsolePrivate::push(const QString& code)
   return ret_value;
 }
 
-//----------------------------------------------------------------------------
-void ctkPythonConsolePrivate::resetBuffer()
-{
-  if (this->InteractiveConsole)
-  {
-    //this->MakeCurrent();
-    const char* code = "__ctkConsole.resetbuffer()\n";
-    PyRun_SimpleString(code);
-    //this->ReleaseControl();
-  }
-}
+////----------------------------------------------------------------------------
+//void ctkPythonConsolePrivate::resetInputBuffer()
+//{
+//  if (this->InteractiveConsole)
+//    {
+//    //this->MakeCurrent();
+//    const char* code = "__ctkConsole.resetbuffer()\n";
+//    PyRun_SimpleString(code);
+//    //this->ReleaseControl();
+//    }
+//}
 
 //----------------------------------------------------------------------------
-void ctkPythonConsolePrivate::promptForInput(const QString& indent)
+void ctkPythonConsolePrivate::printWelcomeMessage()
 {
   Q_Q(ctkPythonConsole);
 
-  QTextCharFormat format = q->getFormat();
-  format.setForeground(q->promptColor());
-  q->setFormat(format);
-
-//  this->Interpreter->MakeCurrent();
-  if(!this->MultilineStatement)
-    {
-    this->prompt(">>> ");
-    //q->prompt(
-    //  PyString_AsString(PySys_GetObject(const_cast<char*>("ps1"))));
-    }
-  else
-    {
-    this->prompt("... ");
-    //q->prompt(
-    //  PyString_AsString(PySys_GetObject(const_cast<char*>("ps2"))));
-    }
-  this->printCommand(indent);
-//  this->Interpreter->ReleaseControl();
+  q->printMessage(
+    QString("Python %1 on %2\n").arg(Py_GetVersion()).arg(Py_GetPlatform()),
+    q->welcomeTextColor());
 }
 
 //----------------------------------------------------------------------------
@@ -287,10 +269,11 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   d->PythonManager->mainContext();
   d->initializeInteractiveConsole();
 
-  this->printMessage(
-        QString("Python %1 on %2\n").arg(Py_GetVersion()).arg(Py_GetPlatform()),
-        this->welcomeTextColor());
+  // Set primary and secondary prompt
+  this->setPs1(this->Superclass::ps1());
+  this->setPs2(this->Superclass::ps2());
 
+  d->printWelcomeMessage();
   d->promptForInput();
 
   Q_ASSERT(PythonQt::self()); // PythonQt should be initialized
@@ -391,6 +374,34 @@ QStringList ctkPythonConsole::pythonAttributes(const QString& pythonVariableName
 }
 
 //----------------------------------------------------------------------------
+QString ctkPythonConsole::ps1() const
+{
+  PyObject * ps1 = PySys_GetObject(const_cast<char*>("ps1"));
+  const char * ps1_str = PyString_AsString(ps1);
+  return QLatin1String(ps1_str);
+}
+
+//----------------------------------------------------------------------------
+void ctkPythonConsole::setPs1(const QString& newPs1)
+{
+  PySys_SetObject(const_cast<char*>("ps1"), PyString_FromString(newPs1.toAscii().data()));
+}
+
+//----------------------------------------------------------------------------
+QString ctkPythonConsole::ps2() const
+{
+  PyObject * ps2 = PySys_GetObject(const_cast<char*>("ps2"));
+  const char * ps2_str = PyString_AsString(ps2);
+  return QLatin1String(ps2_str);
+}
+
+//----------------------------------------------------------------------------
+void ctkPythonConsole::setPs2(const QString& newPs2)
+{
+  PySys_SetObject(const_cast<char*>("ps2"), PyString_FromString(newPs2.toAscii().data()));
+}
+
+//----------------------------------------------------------------------------
 void ctkPythonConsole::executeCommand(const QString& command)
 {
   Q_D(ctkPythonConsole);

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

@@ -79,6 +79,18 @@ public:
   /// Given a python variable name, lookup its attributes and return them in a string list.
   QStringList pythonAttributes(const QString& pythonVariableName) const;
 
+  /// Returns the string used as primary prompt
+  virtual QString ps1() const;
+
+  /// Set the string used as primary prompt
+  virtual void setPs1(const QString& newPs1);
+
+  /// Returns the string used as secondary prompt
+  virtual QString ps2() const;
+
+  /// Set the string used as secondary prompt
+  virtual void setPs2(const QString& newPs2);
+
 public slots:
   virtual void clear();
   void executeScript(const QString&);

+ 30 - 1
Libs/Widgets/ctkConsole.cpp

@@ -73,7 +73,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
   QTextEdit(0),
   q_ptr(&object),
-  InteractivePosition(documentEnd())
+  InteractivePosition(documentEnd()),
+  MultilineStatement(false), Ps1(">>> "), Ps2("... ")
 {
 }
 
@@ -438,6 +439,26 @@ void ctkConsolePrivate::printCommand(const QString& cmd)
   this->updateCommandBuffer();
 }
 
+//----------------------------------------------------------------------------
+void ctkConsolePrivate::promptForInput(const QString& indent)
+{
+  Q_Q(ctkConsole);
+
+  QTextCharFormat format = q->getFormat();
+  format.setForeground(q->promptColor());
+  q->setFormat(format);
+
+  if(!this->MultilineStatement)
+    {
+    this->prompt(q->ps1());
+    }
+  else
+    {
+    this->prompt(q->ps2());
+    }
+  this->printCommand(indent);
+}
+
 //-----------------------------------------------------------------------------
 void ctkConsolePrivate::prompt(const QString& text)
 {
@@ -551,6 +572,14 @@ CTK_GET_CPP(ctkConsole, QColor, welcomeTextColor, WelcomeTextColor);
 CTK_SET_CPP(ctkConsole, const QColor&, setWelcomeTextColor, WelcomeTextColor);
 
 //-----------------------------------------------------------------------------
+CTK_GET_CPP(ctkConsole, QString, ps1, Ps1);
+CTK_SET_CPP(ctkConsole, const QString&, setPs1, Ps1);
+
+//-----------------------------------------------------------------------------
+CTK_GET_CPP(ctkConsole, QString, ps2, Ps2);
+CTK_SET_CPP(ctkConsole, const QString&, setPs2, Ps2);
+
+//-----------------------------------------------------------------------------
 void ctkConsole::executeCommand(const QString& command)
 {
   qWarning() << "ctkConsole::executeCommand not implemented !";

+ 14 - 0
Libs/Widgets/ctkConsole.h

@@ -74,6 +74,8 @@ class CTK_WIDGETS_EXPORT ctkConsole : public QWidget
   Q_PROPERTY(QColor errorTextColor READ errorTextColor WRITE setErrorTextColor)
   Q_PROPERTY(QColor commandTextColor READ commandTextColor WRITE setCommandTextColor)
   Q_PROPERTY(QColor welcomeTextColor READ welcomeTextColor WRITE setWelcomeTextColor)
+  Q_PROPERTY(QString ps1 READ ps1 WRITE setPs1)
+  Q_PROPERTY(QString ps2 READ ps2 WRITE setPs2)
   
 public:
   ctkConsole(QWidget* parentObject = 0);
@@ -116,6 +118,18 @@ public:
   /// Prints text on the console
   void printMessage(const QString& message, const QColor& color);
 
+  /// Returns the string used as primary prompt
+  virtual QString ps1() const;
+
+  /// Set the string used as primary prompt
+  virtual void setPs1(const QString& newPs1);
+
+  /// Returns the string used as secondary prompt
+  virtual QString ps2() const;
+
+  /// Set the string used as secondary prompt
+  virtual void setPs2(const QString& newPs2);
+
 signals:
 
   /// This signal emitted before and after a command is executed

+ 13 - 1
Libs/Widgets/ctkConsole_p.h

@@ -77,8 +77,10 @@ public:
   /// Unlike printMessage(), this will affect the current command being typed.
   void printCommand(const QString& cmd);
 
+  void promptForInput(const QString& indent = QString());
+
   /// Puts out an input accepting prompt.
-  /// It is recommended that one uses prompt instead of printString() to print
+  /// It is recommended that one uses prompt instead of printMessage() to print
   /// an input prompt since this call ensures that the prompt is shown on a new
   /// line.
   void prompt(const QString& text);
@@ -103,6 +105,9 @@ public:
   /// changes can't be made to the text edit contents
   int InteractivePosition;
 
+  /// Indicates if the last statement processes was incomplete.
+  bool MultilineStatement;
+
   /// Stores command-history, plus the current command buffer
   QStringList CommandHistory;
 
@@ -123,6 +128,13 @@ public:
 
   /// Welcome text color
   QColor WelcomeTextColor;
+
+  /// Primary prompt
+  QString Ps1;
+
+  /// Secondary prompt
+  QString Ps2;
+
 };