Parcourir la source

Merge branch 'ctkSimplePythonShell'

* ctkSimplePythonShell:
  ENH: Scripting/Python/Core library is now using library option setup in ctk_library_options file.
  ENH: Superbuild - Simplify the way boolean option are passed to CTK inner build
  ENH: Superbuild - CTK library option are now passed to CTK inner build
  ENH: Superbuild - Underscore are now valid character in CTK library option name
  ENH: Superbuild - Ctk library options are now marked as advanced
  ENH: PythonQt wrapping option are now exposed in ctkScriptingPythonCoreConfigure.h file
  ENH: ctkSimplePythonShell - Added simplePythonQtDecorator class draft
  ENH: Scripting/Python - ctkAbstractPythonManager - Added option to initialize additional Qt lib wrapper
  ENH: Added cmake macro allowing to compile python script into a given directory
  ENH: Added first draft of the application ctkSimplePythonShell
Jean-Christophe Fillion-Robin il y a 15 ans
Parent
commit
2b07c36f89

+ 49 - 0
Applications/ctkSimplePythonShell/CMakeLists.txt

@@ -0,0 +1,49 @@
+PROJECT(ctkSimplePythonShell)
+
+#
+# See CTK/CMake/ctkMacroBuildApp.cmake for details
+#
+
+SET(KIT_SRCS
+  ctkSimplePythonManager.cpp
+  ctkSimplePythonManager.h
+  ctkSimplePythonQtDecorators.h
+  ctkSimplePythonShellMain.cpp
+)
+
+# Headers that should run through moc
+SET(KIT_MOC_SRCS
+  ctkSimplePythonManager.h
+  ctkSimplePythonQtDecorators.h
+  )
+
+# UI files
+SET(KIT_UI_FORMS
+)
+
+# Resources
+SET(KIT_resources
+)
+
+# Target libraries - See CMake/ctkMacroGetTargetLibraries.cmake
+# The following macro will read the target libraries from the file 'target_libraries.cmake'
+ctkMacroGetTargetLibraries(KIT_target_libraries)
+
+# Additional directories to include - Not that CTK_INCLUDE_LIBRARIES is already included
+SET(KIT_include_directories
+  )
+
+ctkMacroBuildApp(
+  NAME ${PROJECT_NAME}
+  INCLUDE_DIRECTORIES ${KIT_include_directories}
+  SRCS ${KIT_SRCS}
+  MOC_SRCS ${KIT_MOC_SRCS}
+  UI_FORMS ${KIT_UI_FORMS}
+  TARGET_LIBRARIES ${KIT_target_libraries}
+  RESOURCES ${KIT_resources}
+  )
+
+# Testing
+IF(BUILD_TESTING)
+#   ADD_SUBDIRECTORY(Testing)
+ENDIF(BUILD_TESTING)

+ 38 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonManager.cpp

@@ -0,0 +1,38 @@
+
+// CTK includes
+#include "ctkSimplePythonManager.h"
+#include "ctkSimplePythonQtDecorators.h"
+
+//-----------------------------------------------------------------------------
+ctkSimplePythonManager::ctkSimplePythonManager(QObject* _parent) : Superclass(_parent)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+ctkSimplePythonManager::~ctkSimplePythonManager()
+{
+}
+
+//-----------------------------------------------------------------------------
+QStringList ctkSimplePythonManager::pythonPaths()
+{  
+  QStringList paths;  
+  
+  return paths; 
+}
+
+//-----------------------------------------------------------------------------
+void ctkSimplePythonManager::preInitialization()
+{
+  Superclass::preInitialization();
+
+  // Register decorators
+  this->registerPythonQtDecorator(new ctkSimplePythonQtDecorators(this));  
+  
+  // Add object to python interpreter context
+  //this->addObjectToPythonMain("_qSlicerCoreApplicationInstance", app);
+
+  // Evaluate application script
+  //this->executeFile(app->slicerHome() + "/bin/Python/slicer/slicerqt.py");
+}

+ 27 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonManager.h

@@ -0,0 +1,27 @@
+#ifndef __ctkSimplePythonManager_h
+#define __ctkSimplePythonManager_h
+
+// CTK includes
+# include <ctkAbstractPythonManager.h>
+
+class PythonQtObjectPtr;
+
+class ctkSimplePythonManager : public ctkAbstractPythonManager
+{
+  Q_OBJECT
+
+public:
+  typedef ctkAbstractPythonManager Superclass;
+  ctkSimplePythonManager(QObject* parent=0);
+  ~ctkSimplePythonManager();
+  
+protected:
+
+  virtual QStringList pythonPaths();
+  virtual void preInitialization();
+
+};
+
+
+#endif
+

+ 38 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonQtDecorators.h

@@ -0,0 +1,38 @@
+
+#ifndef __ctkSimplePythonQtDecorators_h
+#define __ctkSimplePythonQtDecorators_h
+
+// CTK includes
+#include <ctkAbstractPythonManager.h>
+
+// NOTE:
+//
+// For decorators it is assumed that the methods will never be called
+// with the self argument as NULL.  The self argument is the first argument
+// for non-static methods.
+//
+
+class ctkSimplePythonQtDecorators : public QObject
+{
+  Q_OBJECT
+
+public:
+
+  ctkSimplePythonQtDecorators(ctkAbstractPythonManager* pythonManager)
+    {
+    Q_ASSERT(pythonManager);
+    //pythonManager->registerClassForPythonQt(&qSlicerCoreApplication::staticMetaObject);
+    //pythonManager->registerClassForPythonQt(&qSlicerModuleManager::staticMetaObject);
+    //pythonManager->registerClassForPythonQt(&qSlicerAbstractModule::staticMetaObject);
+    //pythonManager->registerClassForPythonQt(&qSlicerAbstractModuleWidget::staticMetaObject);
+    //pythonManager->registerCPPClassForPythonQt("qSlicerModuleFactoryManager");
+    }
+
+public slots:
+
+  
+  
+};
+
+#endif
+

+ 22 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonShellMain.cpp

@@ -0,0 +1,22 @@
+
+// Qt includes
+#include <QApplication>
+
+// CTK includes
+#include <ctkPythonShell.h>
+
+#include "ctkSimplePythonManager.h"
+
+int main(int argc, char** argv)
+{
+  QApplication app(argc, argv);
+  
+  ctkSimplePythonManager pythonManager;
+  
+  ctkPythonShell shell(&pythonManager);
+  shell.setAttribute(Qt::WA_QuitOnClose, false);
+  shell.resize(600, 280);
+  shell.show();
+  
+  return app.exec();
+}

+ 9 - 0
Applications/ctkSimplePythonShell/target_libraries.cmake

@@ -0,0 +1,9 @@
+#
+# See CMake/ctkMacroGetTargetLibraries.cmake
+# 
+# This file should list the libraries required to build the current CTK application.
+# 
+
+SET(target_libraries
+  CTKScriptingPythonWidgets
+  )

+ 1 - 1
CMake/ctkFunctionExtractOptionNameAndValue.cmake

@@ -6,7 +6,7 @@
 FUNCTION(ctkFunctionExtractOptionNameAndValue my_opt var_opt_name var_opt_value)
 
  # Make sure option is correctly formated
-  IF(NOT "${my_opt}" MATCHES "^[/A-Za-z0-9.]+:(ON|OFF)")
+  IF(NOT "${my_opt}" MATCHES "^[/A-Za-z0-9._]+:(ON|OFF)")
     MESSAGE(FATAL_ERROR "Option ${my_opt} is incorrect. Options should be specified using the following format OPT1:[ON|OFF]. For example OPT1:OFF or OPT2:ON")
   ENDIF()
 

+ 2 - 0
CMake/ctkMacroAddCtkLibraryOptions.cmake

@@ -37,6 +37,8 @@ MACRO(ctkMacroAddCtkLibraryOptions lib)
     FOREACH(option ${ctk_library_options})
       ctkFunctionExtractOptionNameAndValue(${option} option_name option_value)
       OPTION(CTK_LIB_${lib}_${option_name} "Enable ${lib} Library ${option_name} option." ${option_value})
+      MARK_AS_ADVANCED(CTK_LIB_${lib}_${option_name})
+      LIST(APPEND ctk_lib_options_list CTK_LIB_${lib}_${option_name})
     ENDFOREACH()
     
   ENDIF()

+ 57 - 0
CMake/ctkMacroCompilePythonScript.cmake

@@ -0,0 +1,57 @@
+
+
+#
+# Based on ParaView/VTK/Utilities/vtkTclTest2Py/CMakeLists.txt and
+#          ParaView/VTK/Wrapping/Python/CMakeLists.txt
+#
+
+MACRO(ctkMacroCompilePythonScript kit_name python_source_files dest_dir)
+
+  FIND_PACKAGE(PythonInterp REQUIRED)
+  
+  SET(copied_python_files)
+  
+  FOREACH(file ${python_source_files})
+    GET_FILENAME_COMPONENT(file_we "${CMAKE_CURRENT_SOURCE_DIR}/${file}" NAME_WE)
+    SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file_we}.py")
+    SET(tgt "${dest_dir}/${file_we}.py")
+    SET(copied_python_files ${copied_python_files} ${tgt})
+    ADD_CUSTOM_COMMAND(DEPENDS ${src}
+                        COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
+                        OUTPUT ${tgt}
+                        COMMENT "source copy")
+  ENDFOREACH()
+  
+  ADD_CUSTOM_TARGET(Copy${kit_name}PythonFiles
+                  ALL
+                  DEPENDS ${copied_python_files})
+  
+  # Byte compile the Python files.
+  SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_all_${kit_name}.py")
+  
+  # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
+  FILE(WRITE ${compile_all_script} "
+\# Auto-generated
+import compileall
+compileall.compile_dir('${dest_dir}')
+file = open('${CMAKE_CURRENT_BINARY_DIR}/${kit_name}_python_compile_complete', 'w')
+file.write('Done')
+")
+
+  ADD_CUSTOM_COMMAND(
+    COMMAND ${PYTHON_EXECUTABLE}
+    ARGS  "${compile_all_script}"
+    DEPENDS ${copied_python_files}  ${compile_all_script}
+    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${kit_name}_python_compile_complete"
+    )
+  
+  ADD_CUSTOM_TARGET(${kit_name}_pyc ALL
+    DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${kit_name}_python_compile_complete")
+
+
+  # Install the slicer python module directory
+  #INSTALL(DIRECTORY "${Slicer3_BINARY_DIR}/bin/Python"
+  #  DESTINATION "${Slicer3_INSTALL_BIN_DIR}" COMPONENT Runtime
+  #  USE_SOURCE_PERMISSIONS)
+
+ENDMACRO()

+ 3 - 4
CMakeExternals/PythonQt.cmake

@@ -15,12 +15,11 @@ IF(${add_project})
   #   MESSAGE(STATUS "Adding project:${proj}")
     SET(PythonQt_DEPENDS ${proj})
     
-    # Expose PythonQt options
+    # Generate ep_PythonQt_args
     SET(ep_PythonQt_args)
     foreach(qtlib gui network opengl sql svg uitools webkit xml xmlpatterns)
-      OPTION(CTK_PythonQt_Wrap_Qt${qtlib} "Make all of Qt${qtlib} available in python" OFF)
-      MARK_AS_ADVANCED(CTK_PythonQt_Wrap_Qt${qtlib})
-      LIST(APPEND ep_PythonQt_args -DPythonQt_Wrap_Qt${qtlib}:BOOL=${CTK_PythonQt_Wrap_Qt${qtlib}})
+      STRING(TOUPPER ${qtlib} qtlib_uppercase)
+      LIST(APPEND ep_PythonQt_args -DPythonQt_Wrap_Qt${qtlib}:BOOL=${CTK_LIB_Scripting/Python/Core_PYTHONQT_WRAP_QT${qtlib_uppercase}})
     endforeach()
 
     # Python is required

+ 3 - 0
CMakeLists.txt

@@ -140,6 +140,7 @@ INCLUDE(CMake/ctkMacroBuildLib.cmake)
 INCLUDE(CMake/ctkMacroBuildPlugin.cmake)
 INCLUDE(CMake/ctkMacroBuildApp.cmake)
 INCLUDE(CMake/ctkMacroBuildQtDesignerPlugin.cmake)
+INCLUDE(CMake/ctkMacroCompilePythonScript.cmake)
 INCLUDE(CMake/ctkMacroSetupQt.cmake)
 INCLUDE(CMake/ctkMacroTargetLibraries.cmake) # Import multiple macros
 INCLUDE(CMake/ctkFunctionExtractOptionNameAndValue.cmake)
@@ -292,6 +293,7 @@ SET(CTK_APPLICATIONS
   ctkDICOMQuery:OFF
   ctkDICOMRetrieve:OFF
   ctkPluginBrowser:OFF
+  ctkSimplePythonShell:OFF
   )
   
 #-----------------------------------------------------------------------------
@@ -329,6 +331,7 @@ SET(CTK_APPLICATIONS_SUBDIRS )
 #  CTK_LIB_<DIR>/<LIBNAME>_OPT1  (set to OFF)
 #  CTK_LIB_<DIR>/<LIBNAME>_OPT2  (set to ON)
 #
+SET(ctk_lib_options_list) # This list will be updated in ctkFunctionExtractOptionNameAndValue
 FOREACH(lib ${CTK_LIBS})
   ctkFunctionExtractOptionNameAndValue(${lib} lib_name lib_value)
   OPTION(CTK_LIB_${lib_name} "Enable ${lib_name} Library." ${lib_value})

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

@@ -40,6 +40,22 @@ SET(KIT_UI_FORMS
 SET(KIT_resources
 )
 
+#
+# Configure file describing which Qt modules are wrapped
+#
+FOREACH(qtlib GUI NETWORK OPENGL SQL SVG UITOOLS WEBKIT XML XMLPATTERNS)
+  SET(CTK_PYTHONQT_WRAP_QT${qtlib} ${CTK_LIB_Scripting/Python/Core_PYTHONQT_WRAP_QT${qtlib}})
+  #message(CTK_LIB_Scripting/Python/Core_PYTHONQT_WRAP_QT${qtlib}:${CTK_LIB_Scripting/Python/Core_PYTHONQT_WRAP_QT${qtlib}})
+ENDFOREACH()
+
+# TODO Ideally a file named ctkScripingPythonWidgetsConfigure.h should
+# also be configured, this file should deal with Qt Module depending only on QtGui
+
+CONFIGURE_FILE(
+  ctkScriptingPythonCoreConfigure.h.in
+  ${CMAKE_CURRENT_BINARY_DIR}/ctkScriptingPythonCoreConfigure.h
+  )
+
 # Target libraries - See CMake/ctkMacroGetTargetLibraries.cmake
 # The following macro will read the target libraries from the file 'target_libraries.cmake'
 ctkMacroGetTargetLibraries(KIT_target_libraries)

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

@@ -24,6 +24,7 @@
 
 // CTK includes
 #include "ctkAbstractPythonManager.h"
+#include "ctkScriptingPythonCoreConfigure.h"
 
 // PythonQT includes
 #include <PythonQt.h>
@@ -31,6 +32,42 @@
 // STD includes
 #include <csignal>
 
+#ifdef CTK_PYTHONQT_WRAP_QTGUI
+void PythonQt_init_QtGui(PyObject*);
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTNETWORK
+void PythonQt_init_QtNetwork(PyObject*);
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTOPENGL
+void PythonQt_init_QtOpenGL(PyObject*);
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTSQL
+void PythonQt_init_QtSql(PyObject*);
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTSVG
+void PythonQt_init_QtSvg(PyObject*); 
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTUITOOLS
+void PythonQt_init_QtUITools(PyObject*);
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTWEBKIT
+void PythonQt_init_QtWebKit(PyObject*);
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTXML
+void PythonQt_init_QtXml(PyObject*);
+#endif
+
+#ifdef CTK_PYTHONQT_WRAP_QTXMLPATTERNS
+void PythonQt_init_QtXmlPatterns(PyObject*);
+#endif
+
 //-----------------------------------------------------------------------------
 ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent)
 {
@@ -74,7 +111,43 @@ void ctkAbstractPythonManager::initPythonQt()
                 SLOT(printStdout(const QString&)));
   this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
                 SLOT(printStderr(const QString&)));
+  
+  #ifdef CTK_PYTHONQT_WRAP_QTGUI
+  PythonQt_init_QtGui(0);
+  #endif
 
+  #ifdef CTK_PYTHONQT_WRAP_QTNETWORK
+  PythonQt_init_QtNetwork(0);
+  #endif
+
+  #ifdef CTK_PYTHONQT_WRAP_QTOPENGL
+  PythonQt_init_QtOpenGL(0);
+  #endif
+
+  #ifdef CTK_PYTHONQT_WRAP_QTSQL
+  PythonQt_init_QtSql(0);
+  #endif
+
+  #ifdef CTK_PYTHONQT_WRAP_QTSVG
+  PythonQt_init_QtSvg(0); 
+  #endif
+
+  #ifdef CTK_PYTHONQT_WRAP_QTUITOOLS
+  PythonQt_init_QtUITools(0);
+  #endif
+
+  #ifdef CTK_PYTHONQT_WRAP_QTWEBKIT
+  PythonQt_init_QtWebKit(0);
+  #endif
+
+  #ifdef CTK_PYTHONQT_WRAP_QTXML
+  PythonQt_init_QtXml(0);
+  #endif
+
+  #ifdef CTK_PYTHONQT_WRAP_QTXMLPATTERNS
+  PythonQt_init_QtXmlPatterns(0);
+  #endif
+  
   QStringList initCode;
   initCode << "import sys";
   foreach (QString path, this->pythonPaths())

+ 19 - 0
Libs/Scripting/Python/Core/ctkScriptingPythonCoreConfigure.h.in

@@ -0,0 +1,19 @@
+#ifndef __ctkScriptingPythonCoreConfigure_h
+#define __ctkScriptingPythonCoreConfigure_h
+
+
+///
+/// Here is where system computed values get stored
+///
+
+#cmakedefine CTK_PYTHONQT_WRAP_QTGUI
+#cmakedefine CTK_PYTHONQT_WRAP_QTNETWORK
+#cmakedefine CTK_PYTHONQT_WRAP_QTOPENGL
+#cmakedefine CTK_PYTHONQT_WRAP_QTSQL
+#cmakedefine CTK_PYTHONQT_WRAP_QTSVG
+#cmakedefine CTK_PYTHONQT_WRAP_QTUITOOLS
+#cmakedefine CTK_PYTHONQT_WRAP_QTWEBKIT
+#cmakedefine CTK_PYTHONQT_WRAP_QTXML
+#cmakedefine CTK_PYTHONQT_WRAP_QTXMLPATTERNS
+
+#endif

+ 18 - 0
Libs/Scripting/Python/Core/ctk_library_options.cmake

@@ -0,0 +1,18 @@
+#
+# See CMake/ctkMacroAddCtkLibraryOptions.cmake
+#
+# This file should list of options available for considered CTK library
+# For example: MYOPT1:OFF MYOPT2:ON
+#
+
+SET(ctk_library_options
+  PYTHONQT_WRAP_QTGUI:OFF
+  PYTHONQT_WRAP_QTNETWORK:OFF
+  PYTHONQT_WRAP_QTOPENGL:OFF
+  PYTHONQT_WRAP_QTSQL:OFF
+  PYTHONQT_WRAP_QTSVG:OFF
+  PYTHONQT_WRAP_QTUITOOLS:OFF
+  PYTHONQT_WRAP_QTWEBKIT:OFF
+  PYTHONQT_WRAP_QTXML:OFF
+  PYTHONQT_WRAP_QTXMLPATTERNS:OFF
+  )

+ 2 - 12
SuperBuild.cmake

@@ -170,16 +170,6 @@ FOREACH(app ${CTK_APPLICATIONS_SUBDIRS})
 ENDFOREACH()
 
 #-----------------------------------------------------------------------------
-# Convenient function allowing to define superbuild arg
-#
-FUNCTION(ctk_set_superbuild_boolean_arg ctk_cmake_var)
-  SET(superbuild_${ctk_cmake_var} OFF)
-  IF(DEFINED ${ctk_cmake_var})
-    SET(superbuild_${ctk_cmake_var} ${${ctk_cmake_var}} PARENT_SCOPE)
-  ENDIF()
-ENDFUNCTION()
-
-#-----------------------------------------------------------------------------
 # Set superbuild boolean args
 #
 
@@ -192,12 +182,12 @@ SET(ctk_cmake_boolean_args
   ${ctk_libs_bool_vars}
   ${ctk_plugins_bool_vars}
   ${ctk_applications_bool_vars}
+  ${ctk_lib_options_list}
   )
 
 SET(ctk_superbuild_boolean_args)
 FOREACH(ctk_cmake_arg ${ctk_cmake_boolean_args})
-  ctk_set_superbuild_boolean_arg(${ctk_cmake_arg})
-  LIST(APPEND ctk_superbuild_boolean_args -D${ctk_cmake_arg}:BOOL=${superbuild_${ctk_cmake_arg}})
+  LIST(APPEND ctk_superbuild_boolean_args -D${ctk_cmake_arg}:BOOL=${${ctk_cmake_arg}})
 ENDFOREACH()
 
 # MESSAGE("CMake args:")