Salome HOME
This commit was generated by cvs2git to create tag 'V1_3_0'.
[modules/kernel.git] / src / SALOME_PYQT / SALOME_PYQT_GUI.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : SALOME_PYQT_GUI.cxx
4 // Created   : mer jun  4 17:17:20 UTC 2003
5 // Author    : Paul RASCLE, EDF 
6 // Project   : SALOME
7 // Copyright : EDF 2003
8 // $Header$
9 //=============================================================================
10
11 #include "SALOME_PYQT_GUI.hxx"
12
13 #include "QAD_Desktop.h"
14
15 #include "QAD_MessageBox.h"
16 #include "SALOME_Selection.h"
17 #include "SALOME_InteractiveObject.hxx"
18 #include "SALOMEGUI_QtCatchCorbaException.hxx"
19 #include "utilities.h"
20 #include "PyInterp_PyQt.h"
21 //#include <cStringIO.h>
22
23 #include <sipSalomePyQtDeclSalomePyQt.h>
24 #include <sipqtQWorkspace.h>
25 #include <sipqtQPopupMenu.h>
26
27 #include <map>
28 #include <string>
29
30 static PyInterp_PyQt *interp = NULL;
31 static map<int,PyInterp_PyQt*> mapInterp;
32 static PyObject *module;
33 static string _moduleName;
34
35 //=============================================================================
36 /*!
37  *  Calls module.setWorkSpace with PyQt QWorkspace object to use with
38  *  interpreter.
39  */
40 //=============================================================================
41
42 static void setWorkSpace()
43 {
44   MESSAGE("setWorkSpace");
45   PyObject *res,*pyws;
46
47   interp->enter();
48   pyws=sipMapCppToSelf( QAD_Application::getDesktop()->getMainFrame(),
49                         sipClass_QWorkspace);
50   res=PyObject_CallMethod(module,"setWorkSpace","O",pyws);
51   SCRUTE(pyws->ob_refcnt);
52   Py_DECREF(pyws);
53   if (res == NULL)
54     {
55       PyErr_Print();
56       interp->quit();
57       return ;
58     }
59   Py_DECREF(res);
60   interp->quit();
61   return ;
62 }
63
64 //=============================================================================
65 /*!
66  *  Initialises python interpreter (only one per study), imports python module
67  *  from given module name, sets static reference to module,
68  *  sets module workspace.
69  */
70 //=============================================================================
71
72 static void initInterp(int StudyID)
73 {
74   MESSAGE("initInterp");
75   if (mapInterp.find(StudyID) != mapInterp.end())
76     {
77       MESSAGE ( " StudyID is found " << StudyID );
78       interp = mapInterp[StudyID];
79       return;
80     }
81   else 
82     {
83       MESSAGE ( " StudyID is not found " << StudyID );
84       interp=new PyInterp_PyQt();
85       mapInterp[StudyID] = interp;
86     }
87
88   interp->enter();
89
90   module=PyImport_ImportModule((char*)_moduleName.c_str());
91   if(module == NULL)
92     {
93       INFOS ( " Problem... " );
94       PyErr_Print();
95       interp->quit();
96       return;
97     }
98   // PyQt import is OK
99   interp->quit();
100   setWorkSpace();
101 }
102
103 //=============================================================================
104 /*!
105  *  Calls python module.OnGUIEvent(theCommandID)
106  */
107 //=============================================================================
108
109 bool SALOME_PYQT_GUI::OnGUIEvent (int theCommandID,
110                                   QAD_Desktop* parent)
111 {
112   MESSAGE("SALOME_PYQT_GUI::OnGUIEvent");
113   PyObject *res;
114   
115   interp->enter();
116   res=PyObject_CallMethod(module,"OnGUIEvent","i",theCommandID);
117   if (res == NULL)
118     {
119       PyErr_Print();
120       interp->quit();
121       return false;
122     }
123   Py_DECREF(res);
124   interp->quit();
125   return true;
126 }
127
128 //=============================================================================
129 /*!
130  *  no call to python module.OnKeyPress()
131  */
132 //=============================================================================
133
134 bool SALOME_PYQT_GUI::OnKeyPress (QKeyEvent* pe,
135                                   QAD_Desktop* parent,
136                                   QAD_StudyFrame* studyFrame)
137 {
138   MESSAGE("SALOME_PYQT_GUI::OnKeyPress");
139   return true;
140 }
141
142 //=============================================================================
143 /*!
144  *  no call to python module.OnMousePress()
145  */
146 //=============================================================================
147
148 bool SALOME_PYQT_GUI::OnMousePress (QMouseEvent* pe ,
149                                     QAD_Desktop* parent, 
150                                     QAD_StudyFrame* studyFrame)
151 {
152   MESSAGE("SALOME_PYQT_GUI::OnMousePress");
153   return false;
154 }
155
156 //=============================================================================
157 /*!
158  *  no call to python module.OnMouseMove()
159  */
160 //=============================================================================
161
162 bool SALOME_PYQT_GUI::OnMouseMove (QMouseEvent* pe ,
163                                    QAD_Desktop* parent, 
164                                    QAD_StudyFrame* studyFrame)
165 {
166   // La ligne suivante est commentée sinon multiple traces ...
167   // MESSAGE("SALOME_PYQT_GUI::OnMouseMouve");
168   return true;
169 }
170
171 //=============================================================================
172 /*!
173  *  Calls initInterp to initialise python interpreter (only one per study) and
174  *  to import python module. Calls module.setSettings() 
175  */
176 //=============================================================================
177
178 bool SALOME_PYQT_GUI::SetSettings (QAD_Desktop* parent, char* moduleName)
179 {
180   MESSAGE("SALOME_PYQT_GUI::SetSettings");
181   PyObject *res;
182   int StudyID = QAD_Application::getDesktop()->getActiveStudy()->getStudyId();
183   SCRUTE ( StudyID );
184   _moduleName = moduleName + string("GUI");
185   SCRUTE(_moduleName);
186   initInterp(StudyID);
187   
188   interp->enter();
189   res=PyObject_CallMethod(module,"setSettings","");
190   if (res == NULL)
191     {
192       PyErr_Print();
193       interp->quit();
194       return false;
195     }
196   Py_DECREF(res);
197   interp->quit();
198   return true;
199 }
200
201 //=============================================================================
202 /*!
203  * Calls module.customPopup with popup menu to custom, and string values of 
204  * context, parent and selected object (strings defined by DefinePopup, which
205  * is called before).
206  */
207 //=============================================================================
208
209 bool SALOME_PYQT_GUI::CustomPopup ( QAD_Desktop* parent,
210                                     QPopupMenu* popup,
211                                     const QString & theContext,
212                                     const QString & theParent,
213                                     const QString & theObject )
214 {
215   MESSAGE("SALOME_PYQT_GUI::CustomPopup");
216
217   MESSAGE ( " theContext : " << theContext.latin1() );
218   MESSAGE ( " theParent : " << theParent.latin1() );
219   MESSAGE ( " theObject : " << theObject.latin1() );
220
221   PyObject *res,*pypop;
222   interp->enter();
223   pypop=sipMapCppToSelf( popup,
224                         sipClass_QPopupMenu);
225
226   res=PyObject_CallMethod(module,"customPopup","Osss",pypop,
227                           theContext.latin1(), theObject.latin1(), theParent.latin1());
228   Py_DECREF(pypop);
229   if (res == NULL)
230     {
231       PyErr_Print();
232       interp->quit();
233       return false;
234     }
235
236   Py_DECREF(res);
237   interp->quit();
238
239
240   return true;
241 }
242
243
244 //=============================================================================
245 /*!
246  * Calls module.definePopup to modify the strings that define context parent
247  * and selected object. Called before CustomPopup.
248  */
249 //=============================================================================
250
251 void SALOME_PYQT_GUI::DefinePopup( QString & theContext,
252                                    QString & theParent,
253                                    QString & theObject )
254 {
255   MESSAGE("SALOME_PYQT_GUI::DefinePopup");
256   theContext = "";
257   theObject = "";
258   theParent = "";
259   
260   PyObject *res;
261   interp->enter();
262   res=PyObject_CallMethod(module,"definePopup","sss",
263                           theContext.latin1(), theObject.latin1(), theParent.latin1());
264   if (res == NULL)
265     {
266       PyErr_Print();
267       interp->quit();
268       return ;
269     }
270   char *co, *ob, *pa;
271   int parseOk = PyArg_ParseTuple(res, "sss", &co, &ob, &pa);
272
273   MESSAGE ("parseOk " << parseOk);
274   MESSAGE (" --- " << co << " " << ob << " " << pa);
275
276   theContext = co;
277   theObject = ob;
278   theParent = pa;
279   Py_DECREF(res);
280   interp->quit();
281
282   MESSAGE ( " theContext : " << theContext.latin1() );
283   MESSAGE ( " theParent : " << theParent.latin1() );
284   MESSAGE ( " theObject : " << theObject.latin1() );
285
286 }
287
288 //=============================================================================
289 /*!
290  * Initialize new interpreter (if not exists) with new study ID.
291  * Calls module.activeStudyChanged with new study ID. Called twice.
292  */
293 //=============================================================================
294
295 void SALOME_PYQT_GUI::ActiveStudyChanged( QAD_Desktop* parent )
296 {
297   MESSAGE("SALOME_PYQT_GUI::ActiveStudyChanged");
298   PyObject *res;
299   
300   int StudyID = parent->getActiveApp()->getActiveStudy()->getStudyId();
301   initInterp(StudyID);
302   
303   interp->enter();
304   res=PyObject_CallMethod(module,"activeStudyChanged","i", StudyID );
305   if (res == NULL)
306     {
307       PyErr_Print();
308       interp->quit();
309       return ;
310     }
311   Py_DECREF(res);
312   interp->quit();
313   return;
314 }
315
316
317 //=============================================================================
318 /*!
319  *  
320  */
321 //=============================================================================
322
323
324 extern "C"
325 {
326   bool OnGUIEvent(int theCommandID, QAD_Desktop* parent)
327   {
328     return SALOME_PYQT_GUI::OnGUIEvent(theCommandID, parent);
329   }
330   
331   bool OnKeyPress(QKeyEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
332   {
333     return SALOME_PYQT_GUI::OnKeyPress(pe, parent, studyFrame);
334   }
335   
336   bool OnMousePress(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
337   {
338     return SALOME_PYQT_GUI::OnMousePress(pe, parent, studyFrame);
339   }
340   
341   bool OnMouseMove(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
342   {
343     return SALOME_PYQT_GUI::OnMouseMove(pe, parent, studyFrame);
344   }
345   
346   bool SetSettings(QAD_Desktop* parent, char* moduleName)
347   {
348     return SALOME_PYQT_GUI::SetSettings(parent, moduleName);
349   }
350   
351   bool customPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString & theContext,
352                    const QString & theParent, const QString & theObject)
353   {
354     return SALOME_PYQT_GUI::CustomPopup( parent, popup, theContext, theParent, theObject );
355   }
356   
357   void definePopup(QString & theContext, QString & theParent, QString & theObject)
358   {
359     SALOME_PYQT_GUI::DefinePopup(theContext, theParent, theObject);
360   }
361   
362   void activeStudyChanged ( QAD_Desktop* parent )
363   {
364     SALOME_PYQT_GUI::ActiveStudyChanged(parent);
365   }
366 }
367