Ver código fonte

Add VTK PythonQt foreign wrapper

See #157
Jean-Christophe Fillion-Robin 12 anos atrás
pai
commit
dcc6b7e61a

+ 26 - 8
Applications/ctkSimplePythonShell/CMakeLists.txt

@@ -3,7 +3,10 @@ project(ctkSimplePythonShell)
 #
 # 3rd party dependencies
 #
-include(${VTK_USE_FILE})
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  find_package(VTK REQUIRED)
+  include(${VTK_USE_FILE})
+endif()
 
 #
 # See CTK/CMake/ctkMacroBuildApp.cmake for details
@@ -19,7 +22,7 @@ set(KIT_SRCS
   ctkTestWrappedSlot.h
 )
 
-if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
   list(APPEND KIT_SRCS
     ctkTestWrappedQListOfVTKObject.h
     ctkTestWrappedVTKObserver.h
@@ -37,7 +40,7 @@ set(KIT_MOC_SRCS
   ctkTestWrappedSlot.h
   )
 
-if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
   list(APPEND KIT_MOC_SRCS
     ctkTestWrappedQListOfVTKObject.h
     ctkTestWrappedVTKObserver.h
@@ -48,22 +51,36 @@ endif()
 
 # UI files
 set(KIT_UI_FORMS
-)
+  )
 
 # Resources
 set(KIT_resources
-)
+  )
+
+set(KIT_INCLUDE_DIRECTORIES
+  )
 
-# Set VTK_LIBRARIES variable
 set(VTK_LIBRARIES
-  vtkCommon
-  vtkFiltering
   )
 
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  list(APPEND KIT_INCLUDE_DIRECTORIES
+    ${CTKVisualizationVTKCore_BINARY_DIR}
+    ${CTKVisualizationVTKCore_SOURCE_DIR}
+    )
+endif()
+
 # Target libraries - See CMake/ctkFunctionGetTargetLibraries.cmake
 # The following macro will read the target libraries from the file 'target_libraries.cmake'
 ctkFunctionGetTargetLibraries(KIT_target_libraries)
 
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  list(APPEND KIT_target_libraries
+    CTKVisualizationVTKCore
+    vtkFiltering
+    )
+endif()
+
 # Configure file will be configured using CTK_WRAP_PYTHONQT_{LIGHT, FULL} and
 # CTK_BUILD_SHARED_LIBS variables
 configure_file(
@@ -123,6 +140,7 @@ ctkMacroBuildApp(
   SRCS ${KIT_SRCS}
   MOC_SRCS ${KIT_MOC_SRCS}
   UI_FORMS ${KIT_UI_FORMS}
+  INCLUDE_DIRECTORIES ${KIT_INCLUDE_DIRECTORIES}
   TARGET_LIBRARIES ${KIT_target_libraries}
   RESOURCES ${KIT_resources}
   )

+ 8 - 1
Applications/ctkSimplePythonShell/ctkSimplePythonManager.cpp.in

@@ -10,8 +10,11 @@
 // CTK includes
 #include "ctkSimplePythonManager.h"
 #include "ctkSimplePythonQtDecorators.h"
+#include "ctkSimplePythonShellConfigure.h" // For CTK_WRAP_PYTHONQT_{LIGHT, FULL}, CTK_WRAP_PYTHONQT_USE_VTK and CTK_BUILD_SHARED_LIBS
+#ifdef CTK_WRAP_PYTHONQT_USE_VTK
+# include "ctkVTKPythonQtWrapperFactory.h"
+#endif
 
-#include "ctkSimplePythonShellConfigure.h" // For CTK_WRAP_PYTHONQT_{LIGHT, FULL} and CTK_BUILD_SHARED_LIBS
 
 #if defined(CTK_WRAP_PYTHONQT_LIGHT) || defined(CTK_WRAP_PYTHONQT_FULL)
 # ifndef CTK_BUILD_SHARED_LIBS
@@ -97,6 +100,10 @@ void ctkSimplePythonManager::preInitialization()
 # endif
 #endif
 
+#ifdef CTK_WRAP_PYTHONQT_USE_VTK
+  PythonQt::self()->addWrapperFactory(new ctkVTKPythonQtWrapperFactory);
+#endif
+
   // Register decorators
   this->registerPythonQtDecorator(new ctkSimplePythonQtDecorators(this));
 

+ 0 - 1
Applications/ctkSimplePythonShell/target_libraries.cmake

@@ -5,6 +5,5 @@
 # 
 
 set(target_libraries
-  VTK_LIBRARIES
   CTKScriptingPythonWidgets
   )

+ 0 - 12
CMake/ctkMacroBuildLibWrapper.cmake

@@ -78,18 +78,6 @@ macro(ctkMacroBuildLibWrapper)
   # these ones to avoid complaints of MSVC
   # Note: "LINK_DIRECTORIES" has to be invoked before "ADD_LIBRARY"
   set(my_EXTRA_PYTHON_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
-  # Should we link against VTK
-  #if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-  #  list(APPEND my_EXTRA_PYTHON_LIBRARIES vtkCommon vtkPythonCore)
-  #endif()
-
-  # The current library might not be wrapped. Nevertheless, if one of its dependent library
-  # is linked using vtkCommon or vtkPythonCore, VTK_LIBRARY_DIRS should be added
-  # as a link directories.
-  #if(NOT CTK_BUILD_SHARED_LIBS
-  #   AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-  #  link_directories(${VTK_LIBRARY_DIRS})
-  #endif()
 
   # Does a header having the expected filename exists ?
   string(REGEX REPLACE "^CTK" "ctk" lib_name_lc_ctk ${lib_name})

+ 1 - 3
CMakeExternals/PythonQt.cmake

@@ -30,9 +30,7 @@ if(${add_project})
       # Should PythonQt use VTK
       if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
         list(APPEND proj_DEPENDENCIES VTK)
-        list(APPEND ep_PythonQt_args -DVTK_DIR:PATH=${VTK_DIR})
       endif()
-      list(APPEND ep_PythonQt_args -DPythonQt_USE_VTK:BOOL=${CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK})
 
       # Enable Qt libraries PythonQt wrapping if required
       foreach(qtlib core gui network opengl sql svg uitools webkit xml xmlpatterns)
@@ -46,7 +44,7 @@ if(${add_project})
         message(FATAL_ERROR "error: Python is required to build ${PROJECT_NAME}")
       endif()
 
-      set(revision_tag 5935f29978deed892a13ddef02cb14c205c6124d)
+      set(revision_tag 97df3b0845b3f5c987d3141a9e651436882f5913)
       if(${proj}_REVISION_TAG)
         set(revision_tag ${${proj}_REVISION_TAG})
       endif()

+ 8 - 5
CMakeLists.txt

@@ -672,6 +672,14 @@ if((${logical_expr}) AND NOT CTK_LIB_Scripting/Python/Core)
   message("Enabling [CTK_LIB_Scripting/Python/Core] because of [${enabling_msg}] evaluates to True")
 endif()
 
+set(logical_expr CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+if(${logical_expr} AND NOT CTK_LIB_Visualization/VTK/Core)
+  set(CTK_LIB_Visualization/VTK/Core ON CACHE BOOL "Build the VTK Core library" FORCE)
+  set(enabling_msg)
+  ctk_option_logical_expression_to_message(enabling_msg "${logical_expr}")
+  message("Enabling [CTK_LIB_Visualization/VTK/Core] because of [${enabling_msg}] evaluates to True")
+endif()
+
 # Check if dependencies are satisfied
 if(CTK_LIB_Scripting/Python/Core)
   find_package(PythonInterp)
@@ -796,11 +804,6 @@ foreach(_external_target ${EXTERNAL_TARGETS})
   endif()
 endforeach()
 
-# Since PYTHONQT_USE_VTK library option can be enabled independently of
-# Visualization/VTK/Core, let's make sure VTK has been properly discovered
-if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-  find_package(VTK REQUIRED)
-endif()
 set(CTK_WRAP_PYTHONQT_USE_VTK ${CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK})
 
 #-----------------------------------------------------------------------------

+ 0 - 13
Libs/Scripting/Python/Core/CMakeLists.txt

@@ -1,19 +1,11 @@
 project(CTKScriptingPythonCore)
 
 #
-# 3rd party dependencies
-#
-if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-  include(${VTK_USE_FILE})
-endif()
-
-#
 # See CTK/CMake/ctkMacroBuildLib.cmake for details
 #
 
 set(KIT_export_directive "CTK_SCRIPTING_PYTHON_CORE_EXPORT")
 
-
 # Source files
 set(KIT_SRCS
   ctkAbstractPythonManager.cpp
@@ -50,11 +42,6 @@ configure_file(
 # The following macro will read the target libraries from the file 'target_libraries.cmake'
 ctkFunctionGetTargetLibraries(KIT_target_libraries)
 
-# Link against vtkPython if required
-if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-  list(APPEND KIT_target_libraries vtkPythonCore)
-endif()
-
 ctkMacroBuildLib(
   NAME ${PROJECT_NAME}
   EXPORT_DIRECTIVE ${KIT_export_directive}

+ 0 - 4
Libs/Scripting/Python/Core/Python/CMakeLists.txt

@@ -3,10 +3,6 @@
 set(KIT_PYTHON_SCRIPTS
   qt/__init__
   )
-  
-#if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-#  list(APPEND KIT_PYTHON_SCRIPTS ctkvtk/__init__)
-#endif()
 
 # If it applies, the variable CTK_PYTHON_WRAPPED_LIBRARIES will contain
 # the comma separated list of package to load

+ 0 - 7
Libs/Scripting/Python/Widgets/CMakeLists.txt

@@ -1,13 +1,6 @@
 project(CTKScriptingPythonWidgets)
 
 #
-# 3rd party dependencies
-#
-if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
-  include(${VTK_USE_FILE})
-endif()
-
-#
 # See CTK/CMake/ctkMacroBuildLib.cmake for details
 #
 

+ 1 - 1
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -355,7 +355,7 @@ void ctkPythonConsole::initialize(ctkAbstractPythonManager* newPythonManager)
   this->connect(PythonQt::self(), SIGNAL(pythonStdErr(QString)),
                 d, SLOT(printErrorMessage(QString)));
 
-  PythonQt::self()->setRedirectStdInCallBack(
+  PythonQt::self()->setRedirectStdInCallback(
         ctkConsole::stdInRedirectCallBack, reinterpret_cast<void*>(this));
 
   // Set primary and secondary prompt

+ 31 - 2
Libs/Visualization/VTK/Core/CMakeLists.txt

@@ -5,6 +5,10 @@ project(CTKVisualizationVTKCore)
 #
 include(${VTK_USE_FILE})
 
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  find_package(PythonQt REQUIRED)
+endif()
+
 #
 # See CTK/CMake/ctkMacroBuildLib.cmake for details
 #
@@ -48,11 +52,26 @@ set(KIT_MOC_SRCS
 
 # UI files
 set(KIT_UI_FORMS
-)
+  )
 
 # Resources
 set(KIT_resources
-)
+  )
+
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  list(APPEND KIT_SRCS
+    ctkVTKPythonQtWrapperFactory.cpp
+    )
+endif()
+
+set(KIT_INCLUDE_DIRECTORIES )
+
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  list(APPEND KIT_INCLUDE_DIRECTORIES
+    ${PYTHON_INCLUDE_DIRS}
+    ${PYTHONQT_INCLUDE_DIR}
+    )
+endif()
 
 # Set VTK_LIBRARIES variable
 set(VTK_LIBRARIES
@@ -62,6 +81,15 @@ set(VTK_LIBRARIES
   vtkHybrid
   )
 
+if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
+  list(APPEND VTK_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
+  if(${VTK_VERSION_MAJOR}.${VTK_VERSION_MINOR} VERSION_GREATER 5.6)
+    list(APPEND VTK_LIBRARIES vtkPythonCore)
+  else()
+    list(APPEND VTK_LIBRARIES vtkCommonPythonD)
+  endif()
+endif()
+
 if(Q_WS_X11)
   # If the faster 'gold' linker is used, to avoid complaints about undefined symbol
   # 'XGetWindowAttributes', 'XCreateWindow', ..., let's link against X11 libraries.
@@ -79,6 +107,7 @@ ctkMacroBuildLib(
   SRCS ${KIT_SRCS}
   MOC_SRCS ${KIT_MOC_SRCS}
   UI_FORMS ${KIT_UI_FORMS}
+  INCLUDE_DIRECTORIES ${KIT_INCLUDE_DIRECTORIES}
   TARGET_LIBRARIES ${KIT_target_libraries}
   RESOURCES ${KIT_resources}
   LIBRARY_TYPE ${CTK_LIBRARY_MODE}

+ 45 - 0
Libs/Visualization/VTK/Core/ctkVTKPythonQtWrapperFactory.cpp

@@ -0,0 +1,45 @@
+
+/// CTK includes
+#include "ctkVTKPythonQtWrapperFactory.h"
+
+/// VTK includes
+#include <vtkPythonUtil.h>
+#include <vtkObject.h>
+
+//-----------------------------------------------------------------------------
+ctkVTKPythonQtWrapperFactory::ctkVTKPythonQtWrapperFactory():Superclass()
+{
+}
+
+//-----------------------------------------------------------------------------
+ctkVTKPythonQtWrapperFactory::~ctkVTKPythonQtWrapperFactory()
+{
+}
+
+//-----------------------------------------------------------------------------
+PyObject* ctkVTKPythonQtWrapperFactory::wrap(const QByteArray& classname, void *ptr)
+{
+  if (classname.startsWith("vtk"))
+    {
+#if (VTK_MAJOR_VERSION == 5 && VTK_MINOR_VERSION <= 6) || VTK_MAJOR_VERSION < 5
+    return vtkPythonGetObjectFromPointer(reinterpret_cast<vtkObjectBase*>(ptr));
+#else
+    return vtkPythonUtil::GetObjectFromPointer(reinterpret_cast<vtkObjectBase*>(ptr));
+#endif
+    }
+  return NULL;
+}
+
+//-----------------------------------------------------------------------------
+void* ctkVTKPythonQtWrapperFactory::unwrap(const QByteArray& classname, PyObject* object)
+{
+  if (classname.startsWith("vtk"))
+    {
+#if (VTK_MAJOR_VERSION == 5 && VTK_MINOR_VERSION <= 6) || VTK_MAJOR_VERSION < 5
+    return vtkPythonGetPointerFromObject(object, classname.data());
+#else
+    return vtkPythonUtil::GetPointerFromObject(object, classname.data());
+#endif
+    }
+  return NULL;
+}

+ 43 - 0
Libs/Visualization/VTK/Core/ctkVTKPythonQtWrapperFactory.h

@@ -0,0 +1,43 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkVTKPythonQtWrapperFactory_h
+#define __ctkVTKPythonQtWrapperFactory_h
+
+// PythonQt includes
+#include <PythonQtCppWrapperFactory.h>
+
+// CTK includes
+#include "ctkVisualizationVTKCoreExport.h"
+
+/// \ingroup Visualization_VTK_Core
+/// VTK PythonQt wrapper factory
+class CTK_VISUALIZATION_VTK_CORE_EXPORT ctkVTKPythonQtWrapperFactory : public PythonQtForeignWrapperFactory
+{
+public:
+  typedef PythonQtForeignWrapperFactory Superclass;
+  ctkVTKPythonQtWrapperFactory();
+  virtual ~ctkVTKPythonQtWrapperFactory();
+  virtual PyObject* wrap(const QByteArray& classname, void *ptr);
+  virtual void* unwrap(const QByteArray& classname, PyObject* object);
+};
+
+#endif
+