Browse Source

Merge branch 'ctkSimplePythonShell'

* ctkSimplePythonShell:
  ENH: Added multi-line support to ctkPythonShell
  STYLE: Remove commented code from ctkPythonShell
Jean-Christophe Fillion-Robin 15 years ago
parent
commit
ba92b24834

+ 69 - 19
Libs/Scripting/Python/Widgets/ctkPythonShell.cpp

@@ -147,7 +147,8 @@ public:
 struct ctkPythonShell::pqImplementation
 {
   pqImplementation(QWidget* _parent, ctkAbstractPythonManager* pythonManager)
-    : Console(_parent), PythonManager(pythonManager)
+    : Console(_parent), PythonManager(pythonManager), MultilineStatement(false),
+    InteractiveConsole(0)
   {
   }
 
@@ -200,16 +201,74 @@ struct ctkPythonShell::pqImplementation
 //     this->Interpreter = 0;
 //     }
 
+  //----------------------------------------------------------------------------
+  void initializeInteractiveConsole()
+  {
+    qDebug() << "initializeInteractiveConsole";
+    // set up the code.InteractiveConsole instance that we'll use.
+    const char* code =
+      "import code\n"
+      "__ctkConsole=code.InteractiveConsole(locals())\n";
+    PyRun_SimpleString(code);
+
+    // Now get the reference to __ctkConsole and save the pointer.
+    PyObject* main_module = PyImport_AddModule("__main__");
+    PyObject* global_dict = PyModule_GetDict(main_module);
+    this->InteractiveConsole = PyDict_GetItemString(
+      global_dict, "__ctkConsole");
+    if (!this->InteractiveConsole)
+      {
+      qCritical("Failed to locate the InteractiveConsole object.");
+      }
+  }
+
+
 //----------------------------------------------------------------------------
-  void executeCommand(const QString& command)
+  bool push(const QString& code)
   {
-//     this->MultilineStatement = 
-//       this->Interpreter->Push(Command.toAscii().data());
-    if (command.length())
+    bool ret_value = false;
+
+    QString buffer = code;
+    // The embedded python interpreter cannot handle DOS line-endings, see
+    // http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1167922
+    buffer.remove('\r');
+
+    PyObject *res = PyObject_CallMethod(this->InteractiveConsole,
+                                        const_cast<char*>("push"),
+                                        const_cast<char*>("z"),
+                                        buffer.toAscii().data());
+    if (res)
       {
-      Q_ASSERT(this->PythonManager);
-      this->PythonManager->executeString(command);
+      int status = 0;
+      if (PyArg_Parse(res, "i", &status))
+        {
+        ret_value = (status > 0);
+        }
+      Py_DECREF(res);
       }
+    return ret_value;
+  }
+//----------------------------------------------------------------------------
+void resetBuffer()
+  {
+  if (this->InteractiveConsole)
+    {
+    //this->MakeCurrent();
+    const char* code = "__ctkConsole.resetbuffer()\n";
+    PyRun_SimpleString(code);
+    //this->ReleaseControl();
+    }
+  }
+
+//----------------------------------------------------------------------------
+  void executeCommand(const QString& command)
+  {
+    this->MultilineStatement = this->push(command);
+//    if (command.length())
+//      {
+//      Q_ASSERT(this->PythonManager);
+//      this->PythonManager->executeString(command);
+//      }
   }
   
 //----------------------------------------------------------------------------
@@ -244,6 +303,8 @@ struct ctkPythonShell::pqImplementation
 
   /// Indicates if the last statement processes was incomplete.
   bool MultilineStatement;
+
+  PyObject* InteractiveConsole;
 };
 
 /////////////////////////////////////////////////////////////////////////
@@ -273,6 +334,7 @@ ctkPythonShell::ctkPythonShell(ctkAbstractPythonManager* pythonManager, QWidget*
   // The call to mainContext() ensures that python has been initialized.
   Q_ASSERT(this->Implementation->PythonManager);
   this->Implementation->PythonManager->mainContext();
+  this->Implementation->initializeInteractiveConsole();
 
   QTextCharFormat format = this->Implementation->Console.getFormat();
   format.setForeground(QColor(0, 0, 255));
@@ -302,18 +364,6 @@ void ctkPythonShell::clear()
   this->Implementation->promptForInput();
 }
 
-// //----------------------------------------------------------------------------
-// void ctkPythonShell::makeCurrent()
-// {
-//   this->Implementation->Interpreter->MakeCurrent();
-// }
-// 
-// //----------------------------------------------------------------------------
-// void ctkPythonShell::releaseControl()
-// {
-//   this->Implementation->Interpreter->ReleaseControl();
-// }
-
 //----------------------------------------------------------------------------
 void ctkPythonShell::executeScript(const QString& script)
 {

+ 1 - 14
Libs/Scripting/Python/Widgets/ctkPythonShell.h

@@ -68,8 +68,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 class ctkAbstractPythonManager;
 
-class CTK_SCRIPTING_PYTHON_WIDGETS_EXPORT ctkPythonShell :
-  public QWidget
+class CTK_SCRIPTING_PYTHON_WIDGETS_EXPORT ctkPythonShell : public QWidget
 {
   Q_OBJECT
   
@@ -78,21 +77,9 @@ public:
   ctkPythonShell(ctkAbstractPythonManager* pythonManager, QWidget* _parent = 0);
   ~ctkPythonShell();
 
-
-  /// Initializes the interpretor. If an interpretor is already setup (by an
-  /// earlier call to this method), it will be destroyed.
-//   void initializeInterpretor(int argc, char* argv[]);
-//   void initializeInterpretor();
-
   /// Prints some text on the shell.
   void printMessage(const QString&);
 
-  /// Calls MakeCurrent in the internal vtkPVPythonInteractiveInterpretor instance
-//   void makeCurrent();
-
-  /// Calls ReleaseControl in the internal vtkPVPythonInteractiveInterpretor instance
-//   void releaseControl();
-
   /// Given a python variable name, lookup its attributes and return them in a
   /// string list.
   QStringList getPythonAttributes(const QString& pythonVariableName);