Explorar o código

Merge pull request #667 from lassoan/wrap-pythonmanager-executestring

ENH: Make executeString and executeFile invokable from Python
Jean-Christophe Fillion-Robin %!s(int64=8) %!d(string=hai) anos
pai
achega
665d647cb5

+ 10 - 0
Applications/ctkSimplePythonShell/Testing/Python/ctkWidgetsTest.py

@@ -2,9 +2,19 @@
 from ctk import *
 from qt import QTimer
 
+# Test display of a CTK widget
 w = ctkMatrixWidget()
 w.show()
 
+# Test command execution using Python manager
+pythonManager = _ctkPythonManagerInstance
+pythonManager.executeString("variableInPythonConsole=523")
+try:
+  print("variableInPythonConsole was successfully set to {0}".format(variableInPythonConsole))
+except:
+  print "PythonManager.executeString failed"
+  qt.QApplication.exit(1)
+
 if not _ctkPythonConsoleInstance.isInteractive:
   #QTimer().singleShot(0, app(), SLOT('quit()'))
   t = QTimer()

+ 1 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonShellMain.cpp

@@ -117,6 +117,7 @@ int main(int argc, char** argv)
     console.completer()->setAutocompletePreferenceList(list);
 
     pythonManager.addObjectToPythonMain("_ctkPythonConsoleInstance", &console);
+    pythonManager.addObjectToPythonMain("_ctkPythonManagerInstance", &pythonManager);
 
     ctkTestWrappedQProperty testWrappedQProperty;
     pythonManager.addObjectToPythonMain("_testWrappedQPropertyInstance", &testWrappedQProperty);

+ 3 - 2
Libs/Scripting/Python/Core/ctkAbstractPythonManager.h

@@ -25,6 +25,7 @@
 #include <QObject>
 #include <QList>
 #include <QStringList>
+#include <QVariant>
 
 // PythonQt includes
 #include <PythonQtPythonInclude.h> // For PyObject
@@ -90,14 +91,14 @@ public:
 
   /// Execute a python of python code (can be multiple lines separated with newline)
   /// and return the result as a QVariant.
-  QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
+  Q_INVOKABLE QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
 
   /// Gets the value of the variable looking in the __main__ module.
   /// If the variable is not found returns a default initialized QVariant.
   QVariant getVariable(const QString& varName);
 
   /// Execute a python script with the given filename.
-  void executeFile(const QString& filename);
+  Q_INVOKABLE void executeFile(const QString& filename);
 
   /// Set function that is initialized after preInitialization and before executeInitializationScripts
   /// \sa preInitialization executeInitializationScripts