Salome HOME
updated copyright message
[modules/gui.git] / src / SALOME_PY / SalomePy.cxx
old mode 100755 (executable)
new mode 100644 (file)
index d099217..83c3ba1
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 {                                                    \
   PyObject *w;                                       \
   int rc;                                            \
-  if ( ( w = PyInt_FromLong( i ) ) == NULL ) return; \
+  if ( ( w = PyLong_FromLong( i ) ) == NULL ) return NULL; \
   rc = PyDict_SetItemString( aModuleDict, #i, w );   \
   Py_DECREF( w );                                    \
-  if ( rc < 0 ) return;                              \
+  if ( rc < 0 ) return NULL;                              \
 }
 
 //! View operation type
@@ -118,15 +118,7 @@ static PyTypeObject* GetPyClass( const char* theClassName )
   static PyObject* aVTKModule = 0;
   PyObject* aPyClass = 0;
   if( !aVTKModule ) {
-#if VTK_XVERSION < 30000
-    aVTKModule = PyImport_ImportModule( "libVTKGraphicsPython" ); 
-#elif VTK_XVERSION < 50700
-    aVTKModule = PyImport_ImportModule( "vtk.libvtkRenderingPython" ); 
-#elif VTK_XVERSION < 60000
-    aVTKModule = PyImport_ImportModule( "vtkRenderingPython" ); 
-#else
-    aVTKModule = PyImport_ImportModule( "vtkRenderingCorePython" ); 
-#endif
+    aVTKModule = PyImport_ImportModule( "vtk.vtkRenderingCore" ); 
     if( PyErr_Occurred() ) {
       PyErr_Print();
     }
@@ -155,7 +147,7 @@ enum {
   \param toCreate window find/create mode
   \return VTK window pointer or 0 if it could not be found/created
 */
-static SVTK_ViewWindow* GetVTKViewWindow( int toCreate = __FindOrCreate ) {
+static SVTK_ViewWindow* GetVTKViewWindow( int toCreate = __FindOrCreate, int toKeepDetached = 0 ) {
   SVTK_ViewWindow* aVW = 0;
   if ( SUIT_Session::session() ) {
     // get application
@@ -178,7 +170,9 @@ static SVTK_ViewWindow* GetVTKViewWindow( int toCreate = __FindOrCreate ) {
           }
         }
         else {
+          anApp->setProperty("keep_detached", toKeepDetached != 0 );
           SVTK_ViewManager* aVM = dynamic_cast<SVTK_ViewManager*>( anApp->getViewManager( "VTKViewer", toCreate == __FindOrCreate ) );
+          anApp->setProperty("keep_detached", QVariant());
           if ( aVM ) {
             aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getActiveView() );
             // VSR : When new view window is created it can be not active yet at this moment,
@@ -217,32 +211,30 @@ public:
   typedef PyObject* TResult;
   TResult myResult;
   int     myCreate;
-  TGetRendererEvent( bool toCreate )
-    : myResult( Py_None ), myCreate( toCreate ) {}
+  int     myKeepDetached;
+  TGetRendererEvent( bool toCreate, bool toKeepDetached )
+    : myResult( Py_None ), myCreate( toCreate ), myKeepDetached( toKeepDetached ) {}
   virtual void Execute()
   {
     PyTypeObject* aPyClass = ::GetPyClass( "vtkRenderer" );
     SVTK_ViewWindow* aVTKViewWindow = 
-      ::GetVTKViewWindow( myCreate ? __Create : __FindOrCreate );
+      ::GetVTKViewWindow( myCreate ? __Create : __FindOrCreate, myKeepDetached );
     if( aVTKViewWindow && aPyClass ) {
       vtkRenderer* aVTKObject = aVTKViewWindow->getRenderer();
-#if VTK_XVERSION < 50700
-      myResult = PyVTKObject_New( aPyClass, aVTKObject );
-#else
       myResult = PyVTKObject_FromPointer( aPyClass, NULL, aVTKObject );
-#endif
     }
   }
 };
 
-extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_getRenderer( PyObject* self, PyObject* args )
+extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_getRenderer( PyObject* /*self*/, PyObject* args )
 {
   PyObject* aResult = Py_None;
   int toCreate = 0;
-  if ( !PyArg_ParseTuple( args, "|i:getRenderer", &toCreate ) )
+  int toKeepDetached = 0;
+  if ( !PyArg_ParseTuple( args, "|ii:getRenderer", &toCreate, &toKeepDetached ) )
     PyErr_Print();
   else
-    aResult = ProcessEvent( new TGetRendererEvent( toCreate ) );
+    aResult = ProcessEvent( new TGetRendererEvent( toCreate, toKeepDetached ) );
   return aResult;
 }
 
@@ -270,32 +262,30 @@ public:
   typedef PyObject* TResult;
   TResult myResult;
   int     myCreate;
-  TGetRenderWindowEvent( bool toCreate )
-    : myResult( Py_None ), myCreate( toCreate ) {}
+  int     myKeepDetached;
+  TGetRenderWindowEvent( bool toCreate, bool toKeepDetached )
+    : myResult( Py_None ), myCreate( toCreate ), myKeepDetached( toKeepDetached ) {}
   virtual void Execute()
   {
     PyTypeObject* aPyClass = ::GetPyClass( "vtkRenderWindow" );
     SVTK_ViewWindow* aVTKViewWindow = 
-      ::GetVTKViewWindow( myCreate ? __Create : __FindOrCreate );
+      ::GetVTKViewWindow( myCreate ? __Create : __FindOrCreate, myKeepDetached );
     if( aVTKViewWindow && aPyClass ) {
       vtkRenderWindow* aVTKObject = aVTKViewWindow->getRenderWindow();
-#if VTK_XVERSION < 50700
-      myResult = PyVTKObject_New( aPyClass, aVTKObject );
-#else
       myResult = PyVTKObject_FromPointer( aPyClass, NULL, aVTKObject );
-#endif
     }
   }
 };
 
-extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_getRenderWindow( PyObject* self, PyObject* args )
+extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_getRenderWindow( PyObject* /*self*/, PyObject* args )
 {
   PyObject* aResult = Py_None;
   int toCreate = 0;
-  if ( !PyArg_ParseTuple( args, "|i:getRenderWindow", &toCreate ) )
+  int toKeepDetached = 0;
+  if ( !PyArg_ParseTuple( args, "|ii:getRenderWindow", &toCreate, &toKeepDetached ) )
     PyErr_Print();
   else
-    aResult = ProcessEvent( new TGetRenderWindowEvent( toCreate ) );
+    aResult = ProcessEvent( new TGetRenderWindowEvent( toCreate, toKeepDetached ) );
   return aResult;
 }
 
@@ -323,32 +313,30 @@ public:
   typedef PyObject* TResult;
   TResult myResult;
   int     myCreate;
-  TGetRenderWindowInteractorEvent( bool toCreate )
-    : myResult( Py_None ), myCreate( toCreate ) {}
+  int     myKeepDetached;
+  TGetRenderWindowInteractorEvent( bool toCreate, bool toKeepDetached )
+    : myResult( Py_None ), myCreate( toCreate ), myKeepDetached( toKeepDetached ) {}
   virtual void Execute()
   {
     PyTypeObject* aPyClass = ::GetPyClass( "vtkRenderWindowInteractor" );
     SVTK_ViewWindow* aVTKViewWindow = 
-      ::GetVTKViewWindow( myCreate ? __Create : __FindOrCreate );
+      ::GetVTKViewWindow( myCreate ? __Create : __FindOrCreate, myKeepDetached );
     if( aVTKViewWindow && aPyClass ) {
       vtkRenderWindowInteractor* aVTKObject = aVTKViewWindow->getInteractor();
-#if VTK_XVERSION < 50700
-      myResult = PyVTKObject_New( aPyClass, aVTKObject );
-#else
       myResult = PyVTKObject_FromPointer( aPyClass, NULL, aVTKObject );
-#endif
     }
   }
 };
 
-extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_getRenderWindowInteractor( PyObject* self, PyObject* args )
+extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_getRenderWindowInteractor( PyObject* /*self*/, PyObject* args )
 {
   PyObject* aResult = Py_None;
   int toCreate = 0;
-  if ( !PyArg_ParseTuple( args, "|i:getRenderWindowInteractor", &toCreate ) )
+  int toKeepDetached = 0;
+  if ( !PyArg_ParseTuple( args, "|ii:getRenderWindowInteractor", &toCreate, &toKeepDetached ) )
     PyErr_Print();
   else
-    aResult = ProcessEvent( new TGetRenderWindowInteractorEvent( toCreate ) );
+    aResult = ProcessEvent( new TGetRenderWindowInteractorEvent( toCreate, toKeepDetached ) );
   return aResult;
 }
 
@@ -362,7 +350,7 @@ extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_getRenderWindowInteractor( PyOb
   \return nothing (Py_None)
 */
 
-extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_showTrihedron( PyObject* self, PyObject* args )
+extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_showTrihedron( PyObject* /*self*/, PyObject* args )
 {
   class TEvent: public SALOME_Event
   {
@@ -373,7 +361,7 @@ extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_showTrihedron( PyObject* self,
     virtual void Execute()
     {
       if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow( __Find ) ) {
-        if ( aVTKViewWindow->isTrihedronDisplayed() != myShow )
+        if ( aVTKViewWindow->isTrihedronDisplayed() != (bool)myShow )
           aVTKViewWindow->onViewTrihedron(myShow);
       }
     }
@@ -397,7 +385,7 @@ extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_showTrihedron( PyObject* self,
   \return nothing (Py_None)
 */
 
-extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_fitAll( PyObject* self, PyObject* args )
+extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_fitAll( PyObject* /*self*/, PyObject* /*args*/ )
 {
   class TEvent: public SALOME_Event
   {
@@ -425,7 +413,7 @@ extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_fitAll( PyObject* self, PyObjec
   \return nothing (Py_None)
 */
 
-extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_setView( PyObject* self, PyObject* args )
+extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_setView( PyObject* /*self*/, PyObject* args )
 {
   class TEvent: public SALOME_Event
   {
@@ -476,7 +464,7 @@ extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_setView( PyObject* self, PyObje
   \return nothing (Py_None)
 */
 
-extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_resetView( PyObject* self, PyObject* args )
+extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_resetView( PyObject* /*self*/, PyObject* /*args*/ )
 {
   class TEvent: public SALOME_Event
   {
@@ -494,37 +482,63 @@ extern "C" SALOMEPY_EXPORT PyObject* libSalomePy_resetView( PyObject* self, PyOb
   return Py_None;
 }
 
-static PyMethodDef Module_Methods[] = 
+static PyMethodDef libSalomePy_methods[] = 
 {
-  { "getRenderer",               libSalomePy_getRenderer,               METH_VARARGS },
-  { "getRenderWindow",           libSalomePy_getRenderWindow,           METH_VARARGS },
-  { "getRenderWindowInteractor", libSalomePy_getRenderWindowInteractor, METH_VARARGS },
-  { "showTrihedron",             libSalomePy_showTrihedron,             METH_VARARGS },
-  { "fitAll",                    libSalomePy_fitAll,                    METH_NOARGS  },
-  { "setView",                   libSalomePy_setView,                   METH_VARARGS },
-  { "resetView",                 libSalomePy_resetView,                 METH_NOARGS  },
-  { NULL, NULL }
+  { "getRenderer",               libSalomePy_getRenderer,               METH_VARARGS, PyDoc_STR("Get renderer from current vtk view") },
+  { "getRenderWindow",           libSalomePy_getRenderWindow,           METH_VARARGS, PyDoc_STR("Get render window from current vtk view") },
+  { "getRenderWindowInteractor", libSalomePy_getRenderWindowInteractor, METH_VARARGS, PyDoc_STR("Get interactor from current vtk view") },
+  { "showTrihedron",             libSalomePy_showTrihedron,             METH_VARARGS, PyDoc_STR("Show/hide trihedron in current vtk view") },
+  { "fitAll",                    libSalomePy_fitAll,                    METH_NOARGS,  PyDoc_STR("Fit current vtk view to show all contents")  },
+  { "setView",                   libSalomePy_setView,                   METH_VARARGS, PyDoc_STR("Set side view for the current VTK viewer") },
+  { "resetView",                 libSalomePy_resetView,                 METH_NOARGS,  PyDoc_STR("Reset camera for current vtk view")  },
+  { 0, 0, 0, 0 }
+};
+
+struct module_state {
+    PyObject *error;
+};
+
+#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
+
+static int libSalomePy_traverse(PyObject *m, visitproc visit, void *arg) {
+    Py_VISIT(GETSTATE(m)->error);
+    return 0;
+}
+
+static int libSalomePy_clear(PyObject *m) {
+    Py_CLEAR(GETSTATE(m)->error);
+    return 0;
+}
+
+static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+        "libSalomePy",
+        NULL,
+        sizeof(struct module_state),
+        libSalomePy_methods,
+        NULL,
+        libSalomePy_traverse,
+        libSalomePy_clear,
+        NULL
 };
 
 /*!
   \brief Python module initialization.
   \internal
 */
-extern "C" SALOMEPY_EXPORT void initlibSalomePy()
+extern "C" SALOMEPY_EXPORT PyMODINIT_FUNC PyInit_libSalomePy(void)
 {
-  static char* modulename = (char*)"libSalomePy";
-
   // init module
-  PyObject* aModule = Py_InitModule( modulename, Module_Methods );
+  PyObject *aModule = PyModule_Create(&moduledef);
   if( PyErr_Occurred() ) {
     PyErr_Print();
-    return;
+    return NULL;
   }
 
   // get module's dictionary
   PyObject *aModuleDict = PyModule_GetDict( aModule );
   if ( aModuleDict == NULL )
-    return;
+    return NULL;
 
   // export View type enumeration
   PUBLISH_ENUM( ViewFront );
@@ -533,4 +547,6 @@ extern "C" SALOMEPY_EXPORT void initlibSalomePy()
   PUBLISH_ENUM( ViewBottom );
   PUBLISH_ENUM( ViewRight );
   PUBLISH_ENUM( ViewLeft );
+
+  return aModule;
 }