Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMEGUI / SALOMEGUI_Swig.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOMEGUI_Swig.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SALOMEGUI_Swig.hxx"
30
31 #include "QAD_Application.h"
32 #include "QAD_Desktop.h"
33 #include "QAD_Study.h"
34 #include "QAD_RightFrame.h"
35 #include "QAD_StudyFrame.h"
36
37 #include "SALOME_Event.hxx"
38
39 #include "SALOME_Selection.h"
40 #include "SALOME_InteractiveObject.hxx"
41 #include "SALOME_ListIteratorOfListIO.hxx"
42
43 #include "utilities.h"
44
45 using namespace std;
46
47 // asv : 3.12.04 : added checking for NULL GUI objects in almost all methods.
48 // In the scope of fixing bug PAL6869.
49 // (PR : modify comments)
50 // Instance of this class is created every time "import salome" line is typed 
51 // - in IAPP embedded Python interpretor  (SALOME_Session_Server executable),
52 // - in inline Python nodes in Supervisor (in SALOME_Container executable),
53 // - in stand-alone Python console outside any executable.
54 // SALOME GUI(desktop and other objects) is only available in SALOME_Session_Server
55
56 namespace SALOME{
57   QAD_ViewFrame* GetViewFrame( QAD_Study* theStudy ) {
58     if ( theStudy )
59       if ( QAD_StudyFrame* aSF = theStudy->getActiveStudyFrame() )
60         if ( QAD_RightFrame* aRF = aSF->getRightFrame() )
61           return aRF->getViewFrame();
62     return 0;
63   }
64
65   Handle(SALOME_InteractiveObject) FindIObject( QAD_Study* theStudy, const char *theEntry )
66   {
67     if ( QAD_ViewFrame* aVF = GetViewFrame( theStudy ) )
68       return aVF->FindIObject( theEntry );
69     return Handle(SALOME_InteractiveObject)(); // NULL Handle 
70   }
71 }
72
73
74 /*!
75   Constructor
76 */
77 SALOMEGUI_Swig::SALOMEGUI_Swig()
78 {
79   //  MESSAGE("Constructeur");
80 }
81
82 /*!
83   Destructor
84 */
85 SALOMEGUI_Swig::~SALOMEGUI_Swig()
86 {
87   //  MESSAGE("Destructeur");
88 }
89
90 /*!
91   Check if GUI is available (if we are linked to IAPP)
92 */
93 bool SALOMEGUI_Swig::hasDesktop()
94 {
95   if ( QAD_Desktop* aDT = QAD_Application::getDesktop() )
96     return true;
97   else
98     return false;
99 }
100
101 /*!
102   Gets active study or 0 if no study opened
103 */
104 QAD_Study* SALOMEGUI_Swig::getActiveStudy()
105 {
106   if ( QAD_Desktop* aDT = QAD_Application::getDesktop() )       
107     if ( QAD_Application* anApp = aDT->getActiveApp() )
108       return anApp->getActiveStudy();
109   return 0;
110 }
111
112 /*!
113   Call when updated object browser.
114 */
115 void SALOMEGUI_Swig::updateObjBrowser( bool updateSelection )
116 {
117   if ( QAD_Study* myActiveStudy = getActiveStudy() )
118     ProcessVoidEvent( new TVoidMemFun1ArgEvent<QAD_Study,bool>
119                      ( myActiveStudy, &QAD_Study::updateObjBrowser,updateSelection) );
120 }
121
122 /*!
123   Returns active Study Id.
124 */
125 int SALOMEGUI_Swig::getActiveStudyId()
126 {
127   if ( QAD_Study* myActiveStudy = getActiveStudy() )
128     return myActiveStudy->getStudyId();
129   return 0;
130 }
131
132 /*!
133   Returns active Study Name.
134 */
135 const char *SALOMEGUI_Swig::getActiveStudyName()
136 {
137   if ( QAD_Study* myActiveStudy = getActiveStudy() )
138     return myActiveStudy->getTitle().latin1();
139   return NULL;
140 }
141
142 /*!
143   Returns the name of component.
144 */
145 const char* SALOMEGUI_Swig::getComponentName( const char* ComponentUserName )
146 {
147   if ( QAD_Desktop* aDT = QAD_Application::getDesktop() )
148     return aDT->getComponentName( ComponentUserName );
149   return NULL;
150 }
151 /*!
152   Returns the user name of component.
153 */
154 const char* SALOMEGUI_Swig::getComponentUserName( const char* ComponentName )
155 {
156   if ( QAD_Desktop* aDT = QAD_Application::getDesktop() )
157     return aDT->getComponentUserName( ComponentName );
158   return NULL;
159 }
160
161 /*!
162   Returns the number of selected objects.
163 */
164 int SALOMEGUI_Swig::SelectedCount()
165 {
166   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
167     SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
168     return Sel->IObjectCount();
169   }
170   return 0;
171 }
172
173 /*!
174   Returns the selected object at index i.
175 */
176 const char* SALOMEGUI_Swig::getSelected( int i )
177 {
178   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
179     SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
180     SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
181     int index = 0;
182     for( ;It.More();It.Next() ) {
183         Handle(SALOME_InteractiveObject) IObject = It.Value();
184         if( i == index++ ){
185           if ( IObject->hasEntry() )
186             return IObject->getEntry();
187         }
188     }
189   }
190   return "";
191 }
192
193 /*!
194   Add object with Entry into selection.
195 */
196 void SALOMEGUI_Swig::AddIObject( const char *theEntry )
197 {
198   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
199     SALOME_Selection* aSel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
200     if ( IsInCurrentView( theEntry ) ) {
201       Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject( myActiveStudy,theEntry );
202       if ( anIO.IsNull() )  return;
203       ProcessEvent( new TMemFun2ArgEvent<SALOME_Selection,int,
204                     const Handle(SALOME_InteractiveObject)&,bool,
205                     Handle(SALOME_InteractiveObject)>
206                     ( aSel, &SALOME_Selection::AddIObject, anIO, true ) );
207     }
208   }
209 }
210
211
212 /*!
213   Removes object with Entry into selection.
214 */
215 void SALOMEGUI_Swig::RemoveIObject( const char *theEntry )
216 {
217   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
218     SALOME_Selection* aSel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
219     if ( IsInCurrentView( theEntry ) ) {
220       Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject( myActiveStudy,theEntry );
221       if ( anIO.IsNull() ) return;
222       ProcessEvent( new TMemFun2ArgEvent<SALOME_Selection,int,
223                     const Handle(SALOME_InteractiveObject)&, bool,
224                     Handle(SALOME_InteractiveObject)>
225                     ( aSel, &SALOME_Selection::RemoveIObject, anIO, true ) );
226     }
227   }
228 }
229
230
231 /*!
232   Removes all objects into selection.
233 */
234 void SALOMEGUI_Swig::ClearIObjects()
235 {
236   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
237     SALOME_Selection* aSel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
238     ProcessVoidEvent( new TVoidMemFunEvent<SALOME_Selection>
239                      ( aSel, &SALOME_Selection::ClearIObjects ) );
240   }
241 }
242
243 /*!
244   Display
245 */              
246 void SALOMEGUI_Swig::Display( const char *theEntry )
247 {
248   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
249     if ( IsInCurrentView( theEntry ) ) {
250       Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject( myActiveStudy, theEntry );
251       if ( anIO.IsNull() )  return;
252       QAD_ViewFrame* aViewFrame = SALOME::GetViewFrame( myActiveStudy );
253       ProcessVoidEvent( new TVoidMemFun2ArgEvent<QAD_ViewFrame,
254                         const Handle(SALOME_InteractiveObject)&, bool,
255                         Handle(SALOME_InteractiveObject)>
256                         ( aViewFrame, &QAD_ViewFrame::Display, anIO, true ) );
257     }
258   }
259 }
260
261 /*!
262   Display only
263 */
264 void SALOMEGUI_Swig::DisplayOnly( const char *theEntry )
265 {
266   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
267     if ( IsInCurrentView( theEntry ) ) {
268       Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject( myActiveStudy, theEntry );
269       if ( anIO.IsNull() )  return;
270       QAD_ViewFrame* aViewFrame = SALOME::GetViewFrame( myActiveStudy );
271       ProcessVoidEvent( new TVoidMemFun1ArgEvent<QAD_ViewFrame,
272                         const Handle(SALOME_InteractiveObject)&,
273                         Handle(SALOME_InteractiveObject)>
274                         ( aViewFrame, &QAD_ViewFrame::DisplayOnly, anIO ) );
275     }
276   }
277 }
278
279 /*!
280   Erase
281 */
282 void SALOMEGUI_Swig::Erase( const char *theEntry )
283 {
284   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
285     if ( IsInCurrentView( theEntry ) ) {
286       Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject( myActiveStudy, theEntry );
287       if ( anIO.IsNull() )  return;
288       QAD_ViewFrame* aViewFrame = SALOME::GetViewFrame( myActiveStudy );
289       ProcessVoidEvent( new TVoidMemFun2ArgEvent<QAD_ViewFrame,
290                         const Handle(SALOME_InteractiveObject)&, bool,
291                         Handle(SALOME_InteractiveObject)>
292                         ( aViewFrame, &QAD_ViewFrame::Erase, anIO, true ) );
293     }
294   }
295 }
296
297 /*!
298   Display all
299 */
300 void SALOMEGUI_Swig::DisplayAll()
301 {
302   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
303     QAD_ViewFrame* aViewFrame = SALOME::GetViewFrame( myActiveStudy );
304     ProcessVoidEvent( new TVoidMemFunEvent<QAD_ViewFrame>
305                       ( aViewFrame, &QAD_ViewFrame::DisplayAll ) );
306   }
307 }
308
309 /*!
310   Erase only
311 */
312 void SALOMEGUI_Swig::EraseAll()
313 {
314   ClearIObjects();
315   if ( QAD_Study* myActiveStudy = getActiveStudy() ) {
316     QAD_ViewFrame* aViewFrame = SALOME::GetViewFrame( myActiveStudy );
317     ProcessVoidEvent ( new TVoidMemFunEvent<QAD_ViewFrame>
318                       ( aViewFrame, &QAD_ViewFrame::EraseAll ) );
319   }
320 }
321
322 /*!
323   Checks if object is displayed in current viewer
324 */
325 bool SALOMEGUI_Swig::IsInCurrentView( const char *theEntry )
326 {
327   if ( QAD_Study* myActiveStudy = getActiveStudy() )
328     return myActiveStudy->isInViewer( theEntry, myActiveStudy->getActiveStudyFrame()->entry() );
329   return false;
330 }