Back to index

salome-gui  6.5.0
SALOME_PYQT_Module.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 // File   : SALOME_PYQT_Module.cxx
00023 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00024 
00025 #include <PyInterp_Dispatcher.h>
00026 
00027 #include "SALOME_PYQT_Module.h"
00028 #include "SalomeApp_Application.h"
00029 #include "SALOME_PYQT_ModuleLight.h"
00030 
00031 #include <SALOME_LifeCycleCORBA.hxx>
00032 #include <Container_init_python.hxx>
00033 #include <CORBA.h>
00034 
00035 
00036 //
00037 // NB: Library initialization
00038 // Since the SalomePyQtGUILight library is not imported in Python it's initialization function
00039 // should be called manually (and only once) in order to initialize global sip data
00040 // and to get C API from sip : sipBuildResult for example
00041 //
00042 
00043 #define INIT_FUNCTION initSalomePyQtGUILight
00044 #if defined(SIP_STATIC_MODULE)
00045 extern "C" void INIT_FUNCTION();
00046 #else
00047 PyMODINIT_FUNC INIT_FUNCTION();
00048 #endif
00049 
00061 extern "C" {
00062   SALOME_PYQT_EXPORT CAM_Module* createModule() {
00063 
00064     static bool alreadyInitialized = false;
00065     if ( !alreadyInitialized ) {
00066       // call only once (see comment above) !
00067 
00068       PyEval_RestoreThread( KERNEL_PYTHON::_gtstate);
00069       INIT_FUNCTION();
00070       PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate);
00071       alreadyInitialized = !alreadyInitialized;
00072     }
00073     return new SALOME_PYQT_Module();
00074   }
00075 }
00076 
00077 
00081 const char* __DEFAULT_NAME__  = "SALOME_PYQT_Module";
00082 
00086 SALOME_PYQT_Module::SALOME_PYQT_Module()
00087   : SalomeApp_Module(__DEFAULT_NAME__),
00088     LightApp_Module(__DEFAULT_NAME__),
00089     SALOME_PYQT_ModuleLight()
00090 {
00091 }
00092 
00096 SALOME_PYQT_Module::~SALOME_PYQT_Module()
00097 {
00098 }
00099 
00103 Engines::EngineComponent_var SALOME_PYQT_Module::getEngine() const
00104 {
00105   Engines::EngineComponent_var comp;
00106   // temporary solution
00107   try {
00108     comp = getApp()->lcc()->FindOrLoad_Component( "FactoryServerPy", name().toLatin1() );
00109   }
00110   catch (CORBA::Exception&) {
00111   }
00112   return comp;
00113 }
00114 
00118 QString SALOME_PYQT_Module::engineIOR() const
00119 {
00120   class EngineIORReq : public PyInterp_LockRequest
00121   {
00122   public:
00123     EngineIORReq( PyInterp_Interp*    _py_interp,
00124                   SALOME_PYQT_Module* _obj )
00125       : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
00126         myObj( _obj ) {}
00127 
00128   protected:
00129     virtual void execute()
00130     {
00131       myObj->getEngineIOR();
00132     }
00133 
00134   private:
00135     SALOME_PYQT_Module* myObj;
00136   };
00137 
00138   // post request
00139   PyInterp_Dispatcher::Get()->Exec( new EngineIORReq( myInterp, const_cast<SALOME_PYQT_Module*>( this ) ) );
00140 
00141   return myIOR;
00142 }
00143 
00144 
00148 bool SALOME_PYQT_Module::activateModule( SUIT_Study* theStudy )
00149 {
00150   // call base implementation
00151   bool res = SalomeApp_Module::activateModule( theStudy );
00152 
00153   if ( !res )
00154     return res;
00155 
00156   // internal activation
00157   return activateModuleInternal( theStudy );
00158 }
00159 
00166 void SALOME_PYQT_Module::getEngineIOR()
00167 {
00168   myIOR = "";
00169 
00170   // Python interpreter should be initialized and Python module should be
00171   // import first
00172   if ( !myInterp || !myModule )
00173     return;
00174 
00175   if ( PyObject_HasAttrString( myModule , "engineIOR" ) ) {
00176     PyObjWrapper res( PyObject_CallMethod( myModule, (char*)"engineIOR", (char*)"" ) );
00177     if ( !res ) {
00178       PyErr_Print();
00179     }
00180     else {
00181       // parse the return value, result chould be string
00182       if ( PyString_Check( res ) ) {
00183         myIOR = PyString_AsString( res );
00184       }
00185     }
00186   }
00187   else if ( !CORBA::is_nil( getEngine() ) )
00188     myIOR = QString( getApp()->orb()->object_to_string( getEngine() ) );
00189 }
00190 
00191 CAM_DataModel* SALOME_PYQT_Module::createDataModel()
00192 {
00193   MESSAGE( "SALOME_PYQT_Module::createDataModel()" );
00194   CAM_DataModel * dm = SalomeApp_Module::createDataModel();
00195   return dm;
00196 }
00197 
00202 void SALOME_PYQT_Module::onGUIEvent(){
00203   SALOME_PYQT_ModuleLight::onGUIEvent();
00204 }
00205 
00210 void SALOME_PYQT_Module::onViewClosed( SUIT_ViewWindow* pview )
00211 {
00212   SALOME_PYQT_ModuleLight::onViewClosed( pview );
00213 }
00214 
00219 void SALOME_PYQT_Module::onViewTryClose( SUIT_ViewWindow* pview )
00220 {
00221   SALOME_PYQT_ModuleLight::onViewTryClose( pview );
00222 }
00223 
00233 void SALOME_PYQT_Module::preferenceChanged( const QString& module, 
00234                                             const QString& section, 
00235                                             const QString& setting )
00236 {
00237   SALOME_PYQT_ModuleLight::preferenceChanged(module,section,setting);
00238 }
00239 
00244 void SALOME_PYQT_Module::onActiveViewChanged( SUIT_ViewWindow* pview )
00245 {
00246   SALOME_PYQT_ModuleLight::onActiveViewChanged(pview);
00247 }
00248 
00253 void SALOME_PYQT_Module::onViewCloned( SUIT_ViewWindow* pview )
00254 {
00255   SALOME_PYQT_ModuleLight::onViewCloned(pview);
00256 }