浏览代码

Move pythonAttributes method from ctkPythonConsole to ctkAbstractPythonManager

Jean-Christophe Fillion-Robin 14 年之前
父节点
当前提交
a76c6f840a

+ 57 - 0
Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp

@@ -170,6 +170,63 @@ void ctkAbstractPythonManager::setInitializationFunction(void (*initFunction)())
   this->InitFunction = initFunction;
 }
 
+//----------------------------------------------------------------------------
+QStringList ctkAbstractPythonManager::pythonAttributes(const QString& pythonVariableName) const
+{
+  Q_ASSERT(PyThreadState_GET()->interp);
+  PyObject* dict = PyImport_GetModuleDict();
+  PyObject* object = PyDict_GetItemString(dict, "__main__");
+  Py_INCREF(object);
+
+  if (!pythonVariableName.isEmpty())
+    {
+    QStringList tmpNames = pythonVariableName.split('.');
+    for (int i = 0; i < tmpNames.size() && object; ++i)
+      {
+      QByteArray tmpName = tmpNames.at(i).toLatin1();
+      PyObject* prevObj = object;
+      if (PyDict_Check(object))
+        {
+        object = PyDict_GetItemString(object, tmpName.data());
+        Py_XINCREF(object);
+        }
+      else
+        {
+        object = PyObject_GetAttrString(object, tmpName.data());
+        }
+      Py_DECREF(prevObj);
+      }
+    PyErr_Clear();
+    }
+
+  QStringList results;
+  if (object)
+    {
+    PyObject* keys = PyObject_Dir(object);
+    if (keys)
+      {
+      PyObject* key;
+      PyObject* value;
+      int nKeys = PyList_Size(keys);
+      for (int i = 0; i < nKeys; ++i)
+        {
+        key = PyList_GetItem(keys, i);
+        value = PyObject_GetAttr(object, key);
+        if (!value)
+          {
+          continue;
+          }
+
+        results << PyString_AsString(key);
+        Py_DECREF(value);
+        }
+      Py_DECREF(keys);
+      }
+    Py_DECREF(object);
+    }
+  return results;
+}
+
 //-----------------------------------------------------------------------------
 void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
 {

+ 4 - 0
Libs/Scripting/Python/Core/ctkAbstractPythonManager.h

@@ -61,6 +61,10 @@ public:
   /// \sa preInitialization executeInitializationScripts
   void setInitializationFunction(void (*initFunction)());
 
+  /// Given a python variable name, lookup its attributes and return them in a string list.
+  /// \note The variable is looked up from __main__
+  QStringList pythonAttributes(const QString& pythonVariableName) const;
+
 signals:
 
   /// This signal is emitted after python is pre-initialized. Observers can listen

+ 8 - 69
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -81,9 +81,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 class ctkPythonConsoleCompleter : public ctkConsoleCompleter
 {
 public:
-  ctkPythonConsoleCompleter(ctkPythonConsole& p) : Parent(p)
+  ctkPythonConsoleCompleter(ctkAbstractPythonManager& pythonManager)
+    : PythonManager(pythonManager)
     {
-    this->setParent(&p);
+    this->setParent(&pythonManager);
     }
 
   virtual void updateCompletionModel(const QString& completion)
@@ -126,7 +127,7 @@ public:
     QStringList attrs;
     if (!lookup.isEmpty() || !compareText.isEmpty())
       {
-      attrs = this->Parent.pythonAttributes(lookup);
+      attrs = this->PythonManager.pythonAttributes(lookup);
       }
 
     // Initialize the completion model
@@ -139,7 +140,7 @@ public:
       this->popup()->setCurrentIndex(this->completionModel()->index(0, 0));
       }
     }
-  ctkPythonConsole& Parent;
+  ctkAbstractPythonManager& PythonManager;
 };
 
 //----------------------------------------------------------------------------
@@ -261,14 +262,14 @@ ctkPythonConsole::ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWid
   Q_D(ctkPythonConsole);
   this->setObjectName("pythonConsole");
 
-  ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*this);
-  this->setCompleter(completer);
-
   // The call to mainContext() ensures that python has been initialized.
   Q_ASSERT(d->PythonManager);
   d->PythonManager->mainContext();
   d->initializeInteractiveConsole();
 
+  ctkPythonConsoleCompleter* completer = new ctkPythonConsoleCompleter(*d->PythonManager);
+  this->setCompleter(completer);
+
   // Set primary and secondary prompt
   this->setPs1(this->Superclass::ps1());
   this->setPs2(this->Superclass::ps2());
@@ -312,68 +313,6 @@ void ctkPythonConsole::executeScript(const QString& script)
 }
 
 //----------------------------------------------------------------------------
-QStringList ctkPythonConsole::pythonAttributes(const QString& pythonVariableName) const
-{
-//   this->makeCurrent();
-
-  Q_ASSERT(PyThreadState_GET()->interp);
-  PyObject* dict = PyImport_GetModuleDict();
-  PyObject* object = PyDict_GetItemString(dict, "__main__");
-  Py_INCREF(object);
-
-  if (!pythonVariableName.isEmpty())
-    {
-    QStringList tmpNames = pythonVariableName.split('.');
-    for (int i = 0; i < tmpNames.size() && object; ++i)
-      {
-      QByteArray tmpName = tmpNames.at(i).toLatin1();
-      PyObject* prevObj = object;
-      if (PyDict_Check(object))
-        {
-        object = PyDict_GetItemString(object, tmpName.data());
-        Py_XINCREF(object);
-        }
-      else
-        {
-        object = PyObject_GetAttrString(object, tmpName.data());
-        }
-      Py_DECREF(prevObj);
-      }
-    PyErr_Clear();
-    }
-
-  QStringList results;
-  if (object)
-    {
-    PyObject* keys = PyObject_Dir(object);
-    if (keys)
-      {
-      PyObject* key;
-      PyObject* value;
-      QString keystr;
-      int nKeys = PyList_Size(keys);
-      for (int i = 0; i < nKeys; ++i)
-        {
-        key = PyList_GetItem(keys, i);
-        value = PyObject_GetAttr(object, key);
-        if (!value)
-          {
-          continue;
-          }
-
-        results << PyString_AsString(key);
-        Py_DECREF(value);
-        }
-      Py_DECREF(keys);
-      }
-    Py_DECREF(object);
-    }
-
-//   this->releaseControl();
-  return results;
-}
-
-//----------------------------------------------------------------------------
 QString ctkPythonConsole::ps1() const
 {
   PyObject * ps1 = PySys_GetObject(const_cast<char*>("ps1"));

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

@@ -76,9 +76,6 @@ public:
   ctkPythonConsole(ctkAbstractPythonManager* pythonManager, QWidget* parentObject = 0);
   virtual ~ctkPythonConsole();
 
-  /// 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;