1 //=============================================================================
2 // File : SALOME_PYQT_GUI.cxx
3 // Created : mer jun 4 17:17:20 UTC 2003
4 // Author : Paul RASCLE, EDF
6 // Copyright : EDF 2003
8 //=============================================================================
10 #include "SALOME_PYQT_GUI.h"
12 #include "QAD_Desktop.h"
14 #include "SALOME_Selection.h"
15 #include "SALOME_InteractiveObject.hxx"
16 #include "SALOMEGUI_QtCatchCorbaException.hxx"
17 #include "utilities.h"
20 #if QT_VERSION > 0x030005
21 #include <sipAPISalomePyQt.h>
23 #include <sipSalomePyQtDeclSalomePyQt.h>
26 #include <sipqtQWorkspace.h>
27 #include <sipqtQPopupMenu.h>
34 static PyInterp_PyQt *interp = NULL;
35 static map<int,PyInterp_PyQt*> mapInterp;
37 //=============================================================================
39 * Calls module.setWorkSpace with PyQt QWorkspace object to use with
42 //=============================================================================
44 void SALOME_PYQT_GUI::setWorkSpace()
46 MESSAGE("setWorkSpace");
47 PyLockWrapper aLock = interp->GetLockWrapper();
49 // Try to import qt module. If it's not possible don't go on
50 PyObjWrapper qtmodule(PyImport_ImportModule("SalomePyQt"));
56 QWorkspaceP *sipRes=QAD_Application::getDesktop()->getMainFrame();
57 PyObjWrapper pyws(sipMapCppToSelf( sipRes, sipClass_QWorkspace));
58 PyObjWrapper res(PyObject_CallMethod(_module,"setWorkSpace","O",pyws.get()));
59 SCRUTE(pyws->ob_refcnt);
66 //=============================================================================
68 * Import Python module (from _moduleName)
70 //=============================================================================
71 void SALOME_PYQT_GUI::importModule()
73 PyLockWrapper aLock = interp->GetLockWrapper();
75 PyObjWrapper amod = PyImport_ImportModule("salome");
77 MESSAGE ( " Problem import salome... " );
82 PyObjWrapper res(PyObject_CallMethod(amod,"salome_init",""));
84 MESSAGE ( " Problem... " );
89 _module=PyImport_ImportModule((char*)_moduleName.c_str());
91 MESSAGE ( " Problem... " );
97 //=============================================================================
99 * Initialises python interpreter (only one per study), imports python module
100 * from given module name, sets static reference to module,
101 * sets module workspace.
103 //=============================================================================
105 void SALOME_PYQT_GUI::initInterp(int StudyID)
107 MESSAGE("SALOME_PYQT_GUI::initInterp");
108 if(mapInterp.find(StudyID) != mapInterp.end()){
109 MESSAGE ( "SALOME_PYQT_GUI::initInterp StudyID is found " << StudyID );
110 interp = mapInterp[StudyID];
113 MESSAGE ( "SALOME_PYQT_GUI::initInterp StudyID is not found " << StudyID );
115 * The creation of Python interpretor must be protected par a C++ Lock because of C threads
117 ThreadLock aPyLock = GetPyThreadLock("SALOME_PYQT_GUI::initInterp");
118 interp = new PyInterp_PyQt();
119 interp->initialize();
120 mapInterp[StudyID] = interp;
122 // imports Python GUI module and puts it in _module attribute
124 // calls _module.setWorkspace and passes the SIP object main workspace
129 //=============================================================================
131 * constructor : only calls SALOMEGUI constructor
133 //=============================================================================
134 SALOME_PYQT_GUI::SALOME_PYQT_GUI( const QString& theName, QObject* theParent ) :
135 SALOMEGUI( theName, theParent ),_module(0)
137 MESSAGE("SALOME_PYQT_GUI::SALOME_PYQT_GUI");
140 //=============================================================================
142 * destructor : do nothing
144 //=============================================================================
145 SALOME_PYQT_GUI::~SALOME_PYQT_GUI()
149 //=============================================================================
151 * Calls python module.OnGUIEvent(theCommandID)
153 //=============================================================================
155 bool SALOME_PYQT_GUI::OnGUIEvent (int theCommandID,
158 MESSAGE("SALOME_PYQT_GUI::OnGUIEvent");
159 ThreadLock aPyLock = GetPyThreadLock("SALOME_PYQT_GUI::OnGUIEvent");
160 PyLockWrapper aLock = interp->GetLockWrapper();
162 PyObjWrapper res(PyObject_CallMethod(_module,"OnGUIEvent","i",theCommandID));
170 //=============================================================================
172 * no call to python module.OnKeyPress()
174 //=============================================================================
176 bool SALOME_PYQT_GUI::OnKeyPress (QKeyEvent* pe,
178 QAD_StudyFrame* studyFrame)
180 MESSAGE("SALOME_PYQT_GUI::OnKeyPress");
184 //=============================================================================
186 * no call to python module.OnMousePress()
188 //=============================================================================
190 bool SALOME_PYQT_GUI::OnMousePress (QMouseEvent* pe ,
192 QAD_StudyFrame* studyFrame)
194 MESSAGE("SALOME_PYQT_GUI::OnMousePress");
198 //=============================================================================
200 * no call to python module.OnMouseMove()
202 //=============================================================================
204 bool SALOME_PYQT_GUI::OnMouseMove (QMouseEvent* pe ,
206 QAD_StudyFrame* studyFrame)
208 // Commented out to avoid multiple traces ...
209 // MESSAGE("SALOME_PYQT_GUI::OnMouseMove");
213 //=============================================================================
215 * Calls initInterp to initialise python interpreter (only one per study) and
216 * to import python module. Calls module.setSettings()
218 //=============================================================================
220 bool SALOME_PYQT_GUI::SetSettings (QAD_Desktop* parent, char* moduleName)
222 MESSAGE("SALOME_PYQT_GUI::SetSettings");
223 int StudyID = QAD_Application::getDesktop()->getActiveStudy()->getStudyId();
225 _moduleName = moduleName + string("GUI");
228 // initializes one Python interpreter by study and puts it in interp global variable
229 // imports Python GUI module and puts it in _module attribute
230 // calls _module.setWorkspace and passes the SIP object main workspace
233 PyLockWrapper aLock = interp->GetLockWrapper();
235 _module = PyImport_ImportModule((char*)_moduleName.c_str());
241 PyObjWrapper res(PyObject_CallMethod(_module,"setSettings",""));
249 //=============================================================================
251 * Calls module.customPopup with popup menu to custom, and string values of
252 * context, parent and selected object (strings defined by DefinePopup, which
255 //=============================================================================
257 bool SALOME_PYQT_GUI::CustomPopup ( QAD_Desktop* parent,
259 const QString & theContext,
260 const QString & theParent,
261 const QString & theObject )
263 MESSAGE("SALOME_PYQT_GUI::CustomPopup");
265 MESSAGE ( " theContext : " << theContext.latin1() );
266 MESSAGE ( " theParent : " << theParent.latin1() );
267 MESSAGE ( " theObject : " << theObject.latin1() );
269 PyLockWrapper aLock = interp->GetLockWrapper();
271 PyObjWrapper pypop(sipMapCppToSelf( popup, sipClass_QPopupMenu));
273 PyObjWrapper res(PyObject_CallMethod(_module,"customPopup",
277 theParent.latin1()));
286 //=============================================================================
288 * Calls module.definePopup to modify the strings that define context parent
289 * and selected object. Called before CustomPopup.
291 //=============================================================================
293 void SALOME_PYQT_GUI::DefinePopup( QString & theContext,
295 QString & theObject )
297 MESSAGE("SALOME_PYQT_GUI::DefinePopup");
302 PyLockWrapper aLock = interp->GetLockWrapper();
304 PyObjWrapper res(PyObject_CallMethod(_module,"definePopup","sss",
307 theParent.latin1()));
313 if(!PyArg_ParseTuple(res, "sss", &co, &ob, &pa))
315 // It's not a valid tuple. Do nothing.
319 MESSAGE ("parseOk ");
320 MESSAGE (" --- " << co << " " << ob << " " << pa);
326 MESSAGE ( " theContext : " << theContext.latin1() );
327 MESSAGE ( " theParent : " << theParent.latin1() );
328 MESSAGE ( " theObject : " << theObject.latin1() );
331 //=============================================================================
333 * Initialize new interpreter (if not exists) with new study ID.
334 * Calls module.activeStudyChanged with new study ID. Called twice.
336 //=============================================================================
338 bool SALOME_PYQT_GUI::ActiveStudyChanged( QAD_Desktop* parent )
341 int StudyID = parent->getActiveApp()->getActiveStudy()->getStudyId();
342 MESSAGE("SALOME_PYQT_GUI::ActiveStudyChanged"<<StudyID<<" - " <<this);
345 PyLockWrapper aLock = interp->GetLockWrapper();
347 PyObjWrapper res(PyObject_CallMethod(_module,"activeStudyChanged","i", StudyID ));
355 //=============================================================================
357 * no call to python module.BuildPresentation() (not yet ???)
359 //=============================================================================
360 void SALOME_PYQT_GUI::BuildPresentation( const Handle(SALOME_InteractiveObject)&,
365 //=============================================================================
367 * no call to python module.SupportedViewType() (not yet ???)
369 //=============================================================================
370 void SALOME_PYQT_GUI::SupportedViewType(int* /*buffer*/, int /*bufferSize*/ )
374 //=============================================================================
376 * no call to python module.Deactivate() (not yet ???)
378 //=============================================================================
379 void SALOME_PYQT_GUI::Deactivate()
386 //=============================================================================
388 * Component GUI Factory : returns a new GUI obj at each call
390 //=============================================================================
394 Standard_EXPORT SALOMEGUI* GetComponentGUI() {
395 MESSAGE("SALOME_PYQT_GUI::GetComponentGUI");
396 SALOMEGUI* aGUI = new SALOME_PYQT_GUI("");