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"
19 #include <sipSalomePyQtDeclSalomePyQt.h>
20 #include <sipqtQWorkspace.h>
21 #include <sipqtQPopupMenu.h>
28 static PyInterp_PyQt *interp = NULL;
29 static map<int,PyInterp_PyQt*> mapInterp;
31 //=============================================================================
33 * Calls module.setWorkSpace with PyQt QWorkspace object to use with
36 //=============================================================================
38 void SALOME_PYQT_GUI::setWorkSpace()
40 MESSAGE("setWorkSpace");
41 PyLockWrapper aLock = interp->GetLockWrapper();
43 // Try to import qt module. If it's not possible don't go on
44 PyObjWrapper qtmodule(PyImport_ImportModule("qt"));
50 PyObjWrapper pyws(sipMapCppToSelf( QAD_Application::getDesktop()->getMainFrame(),
51 sipClass_QWorkspace));
52 PyObjWrapper res(PyObject_CallMethod(_module,"setWorkSpace","O",pyws.get()));
53 SCRUTE(pyws->ob_refcnt);
60 //=============================================================================
62 * Import Python module (from _moduleName)
64 //=============================================================================
65 void SALOME_PYQT_GUI::importModule()
67 PyLockWrapper aLock = interp->GetLockWrapper();
69 PyObjWrapper amod = PyImport_ImportModule("salome");
71 MESSAGE ( " Problem import salome... " );
76 PyObjWrapper res(PyObject_CallMethod(amod,"salome_init",""));
78 MESSAGE ( " Problem... " );
83 _module=PyImport_ImportModule((char*)_moduleName.c_str());
85 MESSAGE ( " Problem... " );
91 //=============================================================================
93 * Initialises python interpreter (only one per study), imports python module
94 * from given module name, sets static reference to module,
95 * sets module workspace.
97 //=============================================================================
99 void SALOME_PYQT_GUI::initInterp(int StudyID)
101 MESSAGE("SALOME_PYQT_GUI::initInterp");
102 if(mapInterp.find(StudyID) != mapInterp.end()){
103 MESSAGE ( "SALOME_PYQT_GUI::initInterp StudyID is found " << StudyID );
104 interp = mapInterp[StudyID];
107 MESSAGE ( "SALOME_PYQT_GUI::initInterp StudyID is not found " << StudyID );
109 * The creation of Python interpretor must be protected par a C++ Lock because of C threads
111 ThreadLock aPyLock = GetPyThreadLock("SALOME_PYQT_GUI::initInterp");
112 interp = new PyInterp_PyQt();
113 interp->initialize();
114 mapInterp[StudyID] = interp;
116 // imports Python GUI module and puts it in _module attribute
118 // calls _module.setWorkspace and passes the SIP object main workspace
123 //=============================================================================
125 * constructor : only calls SALOMEGUI constructor
127 //=============================================================================
128 SALOME_PYQT_GUI::SALOME_PYQT_GUI( const QString& theName, QObject* theParent ) :
129 SALOMEGUI( theName, theParent ),_module(0)
131 MESSAGE("SALOME_PYQT_GUI::SALOME_PYQT_GUI");
134 //=============================================================================
136 * destructor : do nothing
138 //=============================================================================
139 SALOME_PYQT_GUI::~SALOME_PYQT_GUI()
143 //=============================================================================
145 * Calls python module.OnGUIEvent(theCommandID)
147 //=============================================================================
149 bool SALOME_PYQT_GUI::OnGUIEvent (int theCommandID,
152 MESSAGE("SALOME_PYQT_GUI::OnGUIEvent");
153 ThreadLock aPyLock = GetPyThreadLock("SALOME_PYQT_GUI::OnGUIEvent");
154 PyLockWrapper aLock = interp->GetLockWrapper();
156 PyObjWrapper res(PyObject_CallMethod(_module,"OnGUIEvent","i",theCommandID));
164 //=============================================================================
166 * no call to python module.OnKeyPress()
168 //=============================================================================
170 bool SALOME_PYQT_GUI::OnKeyPress (QKeyEvent* pe,
172 QAD_StudyFrame* studyFrame)
174 MESSAGE("SALOME_PYQT_GUI::OnKeyPress");
178 //=============================================================================
180 * no call to python module.OnMousePress()
182 //=============================================================================
184 bool SALOME_PYQT_GUI::OnMousePress (QMouseEvent* pe ,
186 QAD_StudyFrame* studyFrame)
188 MESSAGE("SALOME_PYQT_GUI::OnMousePress");
192 //=============================================================================
194 * no call to python module.OnMouseMove()
196 //=============================================================================
198 bool SALOME_PYQT_GUI::OnMouseMove (QMouseEvent* pe ,
200 QAD_StudyFrame* studyFrame)
202 // Commented out to avoid multiple traces ...
203 // MESSAGE("SALOME_PYQT_GUI::OnMouseMove");
207 //=============================================================================
209 * Calls initInterp to initialise python interpreter (only one per study) and
210 * to import python module. Calls module.setSettings()
212 //=============================================================================
214 bool SALOME_PYQT_GUI::SetSettings (QAD_Desktop* parent, char* moduleName)
216 MESSAGE("SALOME_PYQT_GUI::SetSettings");
217 int StudyID = QAD_Application::getDesktop()->getActiveStudy()->getStudyId();
219 _moduleName = moduleName + string("GUI");
222 // initializes one Python interpreter by study and puts it in interp global variable
223 // imports Python GUI module and puts it in _module attribute
224 // calls _module.setWorkspace and passes the SIP object main workspace
227 PyLockWrapper aLock = interp->GetLockWrapper();
229 _module = PyImport_ImportModule((char*)_moduleName.c_str());
235 PyObjWrapper res(PyObject_CallMethod(_module,"setSettings",""));
243 //=============================================================================
245 * Calls module.customPopup with popup menu to custom, and string values of
246 * context, parent and selected object (strings defined by DefinePopup, which
249 //=============================================================================
251 bool SALOME_PYQT_GUI::CustomPopup ( QAD_Desktop* parent,
253 const QString & theContext,
254 const QString & theParent,
255 const QString & theObject )
257 MESSAGE("SALOME_PYQT_GUI::CustomPopup");
259 MESSAGE ( " theContext : " << theContext.latin1() );
260 MESSAGE ( " theParent : " << theParent.latin1() );
261 MESSAGE ( " theObject : " << theObject.latin1() );
263 PyLockWrapper aLock = interp->GetLockWrapper();
265 PyObjWrapper pypop(sipMapCppToSelf( popup, sipClass_QPopupMenu));
267 PyObjWrapper res(PyObject_CallMethod(_module,"customPopup",
271 theParent.latin1()));
280 //=============================================================================
282 * Calls module.definePopup to modify the strings that define context parent
283 * and selected object. Called before CustomPopup.
285 //=============================================================================
287 void SALOME_PYQT_GUI::DefinePopup( QString & theContext,
289 QString & theObject )
291 MESSAGE("SALOME_PYQT_GUI::DefinePopup");
296 PyLockWrapper aLock = interp->GetLockWrapper();
298 PyObjWrapper res(PyObject_CallMethod(_module,"definePopup","sss",
301 theParent.latin1()));
307 if(!PyArg_ParseTuple(res, "sss", &co, &ob, &pa))
309 // It's not a valid tuple. Do nothing.
313 MESSAGE ("parseOk ");
314 MESSAGE (" --- " << co << " " << ob << " " << pa);
320 MESSAGE ( " theContext : " << theContext.latin1() );
321 MESSAGE ( " theParent : " << theParent.latin1() );
322 MESSAGE ( " theObject : " << theObject.latin1() );
325 //=============================================================================
327 * Initialize new interpreter (if not exists) with new study ID.
328 * Calls module.activeStudyChanged with new study ID. Called twice.
330 //=============================================================================
332 bool SALOME_PYQT_GUI::ActiveStudyChanged( QAD_Desktop* parent )
335 int StudyID = parent->getActiveApp()->getActiveStudy()->getStudyId();
336 MESSAGE("SALOME_PYQT_GUI::ActiveStudyChanged"<<StudyID<<" - " <<this);
339 PyLockWrapper aLock = interp->GetLockWrapper();
341 PyObjWrapper res(PyObject_CallMethod(_module,"activeStudyChanged","i", StudyID ));
349 //=============================================================================
351 * no call to python module.BuildPresentation() (not yet ???)
353 //=============================================================================
354 void SALOME_PYQT_GUI::BuildPresentation( const Handle(SALOME_InteractiveObject)&,
359 //=============================================================================
361 * no call to python module.SupportedViewType() (not yet ???)
363 //=============================================================================
364 void SALOME_PYQT_GUI::SupportedViewType(int* /*buffer*/, int /*bufferSize*/ )
368 //=============================================================================
370 * no call to python module.Deactivate() (not yet ???)
372 //=============================================================================
373 void SALOME_PYQT_GUI::Deactivate()
380 //=============================================================================
382 * Component GUI Factory : returns a new GUI obj at each call
384 //=============================================================================
388 Standard_EXPORT SALOMEGUI* GetComponentGUI() {
389 MESSAGE("SALOME_PYQT_GUI::GetComponentGUI");
390 SALOMEGUI* aGUI = new SALOME_PYQT_GUI("");