Browse Source

Test checking if slot, qinvokable and qproperty are correctly wrapped

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

+ 22 - 0
Applications/ctkSimplePythonShell/CMakeLists.txt

@@ -14,14 +14,36 @@ SET(KIT_SRCS
   ctkSimplePythonManager.h
   ctkSimplePythonQtDecorators.h
   ctkSimplePythonShellMain.cpp
+  ctkTestWrappedQProperty.h
+  ctkTestWrappedQInvokable.h
+  ctkTestWrappedSlot.h
 )
 
+IF(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  LIST(APPEND KIT_SRCS
+    ctkTestWrappedQListOfVTKObject.h
+    ctkTestWrappedVTKSlot.h
+    ctkTestWrappedVTKQInvokable.h
+    )
+ENDIF()
+
 # Headers that should run through moc
 SET(KIT_MOC_SRCS
   ctkSimplePythonManager.h
   ctkSimplePythonQtDecorators.h
+  ctkTestWrappedQProperty.h
+  ctkTestWrappedQInvokable.h
+  ctkTestWrappedSlot.h
   )
 
+IF(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  LIST(APPEND KIT_MOC_SRCS
+    ctkTestWrappedQListOfVTKObject.h
+    ctkTestWrappedVTKSlot.h
+    ctkTestWrappedVTKQInvokable.h
+    )
+ENDIF()
+
 # UI files
 SET(KIT_UI_FORMS
 )

+ 9 - 2
Applications/ctkSimplePythonShell/Testing/Python/CMakeLists.txt

@@ -2,7 +2,10 @@
 SET(KIT_TESTS ${CPP_TEST_PATH}/ctkSimplePythonShell)
 
 SET(SCRIPTS
-  DerivedQWidgetTest.py
+  derivedQWidgetTest.py
+  wrappedQInvokableTest.py
+  wrappedQPropertyTest.py
+  wrappedSlotTest.py
   )
 
 IF(CTK_LIB_Widgets)
@@ -10,7 +13,11 @@ IF(CTK_LIB_Widgets)
 ENDIF()
 
 IF(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-  LIST(APPEND SCRIPTS vtkPythonSmoke.py)
+  LIST(APPEND SCRIPTS
+    vtkPythonSmoke.py
+    wrappedVTKQInvokableTest.py
+    wrappedVTKSlotTest.py
+    )
 ENDIF()
 
 

Applications/ctkSimplePythonShell/Testing/Python/DerivedQWidgetTest.py → Applications/ctkSimplePythonShell/Testing/Python/derivedQWidgetTest.py


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

@@ -0,0 +1,10 @@
+import sys
+
+if (_testWrappedQInvokableInstance.value() != 0):
+  sys.exit(1)
+  
+_testWrappedQInvokableInstance.setValue(74)
+if (_testWrappedQInvokableInstance.value() != 74):
+  sys.exit(1)
+  
+sys.exit(0)

+ 11 - 0
Applications/ctkSimplePythonShell/Testing/Python/wrappedQPropertyTest.py

@@ -0,0 +1,11 @@
+
+import sys
+
+if (_testWrappedQPropertyInstance.Value != 0):
+  sys.exit(1)
+  
+_testWrappedQPropertyInstance.Value = 74
+if (_testWrappedQPropertyInstance.Value != 74):
+  sys.exit(1)
+  
+sys.exit(0)

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

@@ -0,0 +1,10 @@
+import sys
+
+if (_testWrappedSlotInstance.value() != 0):
+  sys.exit(1)
+  
+_testWrappedSlotInstance.setValue(74)
+if (_testWrappedSlotInstance.value() != 74):
+  sys.exit(1)
+  
+sys.exit(0)

+ 17 - 0
Applications/ctkSimplePythonShell/Testing/Python/wrappedVTKQInvokableTest.py

@@ -0,0 +1,17 @@
+
+import sys
+
+# Importing vtk initializes vtkPythonMap owned by vtkPythonUtil and prevent 
+# call to vtkPythonUtil::GetObjectFromPointer() from segfaulting.
+# PythonQt internally uses vtkPythonUtil to properly wrap/unwrap VTK objects
+from ctkvtk import *
+
+t = _testWrappedVTKQInvokableInstance.getTable()
+print t.GetClassName()
+
+t2 = vtkTable()
+_testWrappedVTKQInvokableInstance.setTable(t2)
+if _testWrappedVTKQInvokableInstance.getTable() != t2:
+  sys.exit(1)
+  
+sys.exit(0)

+ 17 - 0
Applications/ctkSimplePythonShell/Testing/Python/wrappedVTKSlotTest.py

@@ -0,0 +1,17 @@
+
+import sys
+
+# Importing vtk initializes vtkPythonMap owned by vtkPythonUtil and prevent 
+# call to vtkPythonUtil::GetObjectFromPointer() from segfaulting.
+# PythonQt internally uses vtkPythonUtil to properly wrap/unwrap VTK objects
+from ctkvtk import *
+
+t = _testWrappedVTKSlotInstance.getTable()
+print t.GetClassName()
+
+t2 = vtkTable()
+_testWrappedVTKSlotInstance.setTable(t2)
+if _testWrappedVTKSlotInstance.getTable() != t2:
+  sys.exit(1)
+  
+sys.exit(0)

+ 30 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonShellMain.cpp

@@ -8,6 +8,16 @@
 #include <ctkCommandLineParser.h>
 
 #include "ctkSimplePythonManager.h"
+#include "ctkTestWrappedQProperty.h"
+#include "ctkTestWrappedQInvokable.h"
+#include "ctkTestWrappedSlot.h"
+#include "ctkSimplePythonShellConfigure.h" // For CTK_WRAP_PYTHONQT_USE_VTK
+
+#ifdef CTK_WRAP_PYTHONQT_USE_VTK
+# include "ctkTestWrappedQListOfVTKObject.h"
+# include "ctkTestWrappedVTKSlot.h"
+# include "ctkTestWrappedVTKQInvokable.h"
+#endif
 
 int main(int argc, char** argv)
 {
@@ -53,6 +63,26 @@ int main(int argc, char** argv)
 
   pythonManager.addObjectToPythonMain("_ctkPythonShellInstance", &shell);
 
+  ctkTestWrappedQProperty testWrappedQProperty;
+  pythonManager.addObjectToPythonMain("_testWrappedQPropertyInstance", &testWrappedQProperty);
+
+  ctkTestWrappedQInvokable testWrappedQInvokable;
+  pythonManager.addObjectToPythonMain("_testWrappedQInvokableInstance", &testWrappedQInvokable);
+
+  ctkTestWrappedSlot testWrappedSlot;
+  pythonManager.addObjectToPythonMain("_testWrappedSlotInstance", &testWrappedSlot);
+
+#ifdef CTK_WRAP_PYTHONQT_USE_VTK
+  ctkTestWrappedVTKQInvokable testWrappedVTKQInvokable;
+  pythonManager.addObjectToPythonMain("_testWrappedVTKQInvokableInstance", &testWrappedVTKQInvokable);
+
+  ctkTestWrappedVTKSlot testWrappedVTKSlot;
+  pythonManager.addObjectToPythonMain("_testWrappedVTKSlotInstance", &testWrappedVTKSlot);
+
+  ctkTestWrappedQListOfVTKObject testWrappedQListOfVTKObject;
+  pythonManager.addObjectToPythonMain("_testWrappedQListOfVTKObjectInstance", &testWrappedQListOfVTKObject);
+#endif
+
   foreach(const QString& script, parser.unparsedArguments())
     {
     pythonManager.executeFile(script);

+ 27 - 0
Applications/ctkSimplePythonShell/ctkTestWrappedQInvokable.h

@@ -0,0 +1,27 @@
+
+
+#ifndef __ctkTestWrappedQInvokable_h
+#define __ctkTestWrappedQInvokable_h
+
+// Qt includes
+#include <QObject>
+
+class ctkTestWrappedQInvokable : public QObject
+{
+  Q_OBJECT
+public:
+
+  ctkTestWrappedQInvokable(QObject * newParent = 0) : QObject(newParent)
+    {
+    this->Value = 0;
+    }
+
+  /// Example of method wrapped using Q_INVOKABLE
+  Q_INVOKABLE int value() const { return this->Value; }
+  Q_INVOKABLE void setValue(int newValue){ this->Value = newValue; }
+
+private:
+  int        Value;
+};
+
+#endif

+ 29 - 0
Applications/ctkSimplePythonShell/ctkTestWrappedQListOfVTKObject.h

@@ -0,0 +1,29 @@
+
+
+#ifndef __ctkTestWrappedQListOfVTKObject_h
+#define __ctkTestWrappedQListOfVTKObject_h
+
+// Qt includes
+#include <QObject>
+#include <QList>
+
+// VTK includes
+#include <vtkTable.h>
+
+class ctkTestWrappedQListOfVTKObject : public QObject
+{
+  Q_OBJECT
+public:
+
+  ctkTestWrappedQListOfVTKObject(QObject * newParent = 0) : QObject(newParent)
+    {
+    }
+
+  /// Example ot slot accepting a VTK object as parameter
+  Q_INVOKABLE int numberOfElementInList(const QList<vtkTable*>& listOfTable)
+    {
+    return listOfTable.count();
+    }
+};
+
+#endif

+ 31 - 0
Applications/ctkSimplePythonShell/ctkTestWrappedQProperty.h

@@ -0,0 +1,31 @@
+
+
+#ifndef __ctkTestWrappedQProperty_h
+#define __ctkTestWrappedQProperty_h
+
+// Qt includes
+#include <QObject>
+
+class ctkTestWrappedQProperty : public QObject
+{
+  Q_OBJECT
+
+  Q_PROPERTY(int Value READ value WRITE setValue);
+
+public:
+
+  ctkTestWrappedQProperty(QObject * newParent = 0) : QObject(newParent)
+    {
+    this->Value = 0;
+    }
+
+  /// Example of property declared using Q_PROPERTY
+  /// Using Q_PROPERTY is enough to expose them, it's not required to declare them as slot
+  int value() const { return this->Value; }
+  void setValue(int newValue){ this->Value = newValue; }
+
+private:
+  int        Value;
+};
+
+#endif

+ 29 - 0
Applications/ctkSimplePythonShell/ctkTestWrappedSlot.h

@@ -0,0 +1,29 @@
+
+
+#ifndef __ctkTestWrappedSlot_h
+#define __ctkTestWrappedSlot_h
+
+// Qt includes
+#include <QObject>
+
+class ctkTestWrappedSlot : public QObject
+{
+  Q_OBJECT
+
+public:
+
+  ctkTestWrappedSlot(QObject * newParent = 0) : QObject(newParent)
+    {
+    this->Value = 0;
+    }
+
+public slots:
+
+  int value() const { return this->Value; }
+  void setValue(int newValue){ this->Value = newValue; }
+
+private:
+  int        Value;
+};
+
+#endif

+ 44 - 0
Applications/ctkSimplePythonShell/ctkTestWrappedVTKQInvokable.h

@@ -0,0 +1,44 @@
+#ifndef __ctkTestWrappedVTKQInvokable_h
+#define __ctkTestWrappedVTKQInvokable_h
+
+// Qt includes
+#include <QObject>
+
+// VTK includes
+#include <vtkTable.h>
+
+class ctkTestWrappedVTKQInvokable : public QObject
+{
+  Q_OBJECT
+public:
+
+  ctkTestWrappedVTKQInvokable(QObject * newParent = 0) : QObject(newParent)
+    {
+    this->MyTable = vtkTable::New();
+    }
+    
+  virtual ~ctkTestWrappedVTKQInvokable()
+    {
+    this->MyTable->Delete();
+    }
+
+  /// Example of 'invokable' returning a VTK object
+  /// Declaring a method as invokable allows to add it to the MetaObject system
+  /// \note When a method returns a value, we tend to use Q_INVOKABLE
+  /// instead of declaring a slot.
+  Q_INVOKABLE vtkTable * getTable() const
+    {
+    return this->MyTable;
+    }
+
+  /// Example of 'invokable' accepting a VTK object as parameter
+  Q_INVOKABLE void setTable(vtkTable * newTable)
+    {
+    this->MyTable = newTable;
+    }
+
+private:
+  vtkTable * MyTable;
+};
+
+#endif

+ 45 - 0
Applications/ctkSimplePythonShell/ctkTestWrappedVTKSlot.h

@@ -0,0 +1,45 @@
+
+
+#ifndef __ctkTestWrappedVTKSlot_h
+#define __ctkTestWrappedVTKSlot_h
+
+// Qt includes
+#include <QObject>
+
+// VTK includes
+#include <vtkTable.h>
+
+class ctkTestWrappedVTKSlot : public QObject
+{
+  Q_OBJECT
+public:
+
+  ctkTestWrappedVTKSlot(QObject * newParent = 0) : QObject(newParent)
+    {
+    this->MyTable = vtkTable::New();
+    }
+    
+  virtual ~ctkTestWrappedVTKSlot()
+    {
+    this->MyTable->Delete();
+    }
+
+public slots:
+
+  /// Example of slot returning a VTK object
+  vtkTable* getTable() const
+    {
+    return this->MyTable;
+    }
+
+  /// Example ot slot accepting a VTK object as parameter
+  void setTable(vtkTable * newTable)
+    {
+    this->MyTable = newTable;
+    }
+
+private:
+  vtkTable * MyTable;
+};
+
+#endif