Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/visu.git] / src / VISUGUI / VisuGUI_PopupTools.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2005  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   : VisuGUI_PopupTools.cxx
25 //  Author : Sergey Anikin 
26 //  Module : VISU
27
28 #include "VisuGUI_PopupTools.h"
29
30 #include "VisuGUI_ViewTools.h"
31 #include "VisuGUI_Tools.h"
32
33 #include "VISU_Result_i.hh"
34 #include "VISU_Gen_i.hh"
35
36 #include "VISU_Actor.h"
37 #include "VISU_ScalarMapAct.h"
38
39 #include "SalomeApp_Study.h"
40 #include "LightApp_Study.h"
41
42 using namespace VISU;
43
44 //////////////////////////////////////////////////
45 // Class: VisuGUI_Selection
46 //////////////////////////////////////////////////
47
48 QtxValue VisuGUI_Selection::param( const int ind, const QString& p ) const
49 {
50   QtxValue val( LightApp_Selection::param( ind, p ) );
51   if ( !val.isValid() ) {
52     if      ( p == "type"           ) val = QtxValue( type( ind ) );
53     else if ( p == "nbComponents"   ) val = QtxValue( nbComponents( ind ) );
54     else if ( p == "medEntity"   ) val = QtxValue( medEntity( ind ) );
55     else if ( p == "medSource"   ) val = QtxValue( medSource( ind ) );
56     else if ( p == "representation" ) val = QtxValue( representation( ind ) );
57     else if ( p == "nbTimeStamps"   ) val = QtxValue( nbTimeStamps( ind ) );
58     else if ( p == "nbChildren"     ) val = QtxValue( nbChildren( ind ) );
59     else if ( p == "nbNamedChildren") val = QtxValue( nbNamedChildren( ind ) );
60     else if ( p == "isVisible"      ) val = QtxValue( isVisible( ind ) );
61     else if ( p == "isShrunk"       ) val = QtxValue( isShrunk( ind ) );
62     else if ( p == "hasActor"       ) val = QtxValue( hasActor( ind ) );
63     else if ( p == "isShading"      ) val = QtxValue( isShading( ind ) );
64     else if ( p == "isScalarMapAct" ) val = QtxValue( isScalarMapAct( ind ) );
65     else if ( p == "isVisuComponent") val = QtxValue( isVisuComponent( ind ) );
66   }
67
68   return val;
69 }
70
71 // Macro for converting VISU enumeration items into corresponding strings
72 #define ENUM2STRING( x, y ) \
73   case y: \
74     x = QString( #y ); \
75     break
76
77 QString VisuGUI_Selection::type( const int ind ) const
78 {
79   QString aResStr;
80   VISU::Storable::TRestoringMap aMap;
81   SalomeApp_Study* aStudy = GetStudy();
82   if ( !aStudy )
83     return aResStr;
84   CORBA::Object_var anObject = GetSelectedObj( aStudy, entry( ind ), &aMap );  
85
86   VISU::Base_var aVisuObj = VISU::Base::_narrow( anObject );
87   if ( !CORBA::is_nil( aVisuObj ) ){
88     VISU::VISUType aType = aVisuObj->GetType();
89     switch (aType) {
90       ENUM2STRING( aResStr, VISU::TVISUGEN );
91       ENUM2STRING( aResStr, VISU::TRESULT );
92       ENUM2STRING( aResStr, VISU::TTABLE );
93       ENUM2STRING( aResStr, VISU::TCURVE );
94       ENUM2STRING( aResStr, VISU::TCONTAINER );
95       ENUM2STRING( aResStr, VISU::TMESH );
96       ENUM2STRING( aResStr, VISU::TSCALARMAP );
97       ENUM2STRING( aResStr, VISU::TISOSURFACE );
98       ENUM2STRING( aResStr, VISU::TDEFORMEDSHAPE );
99       ENUM2STRING( aResStr, VISU::TSCALARMAPONDEFORMEDSHAPE );
100       ENUM2STRING( aResStr, VISU::TCUTPLANES );
101       ENUM2STRING( aResStr, VISU::TCUTLINES );
102       ENUM2STRING( aResStr, VISU::TVECTORS );
103       ENUM2STRING( aResStr, VISU::TSTREAMLINES );
104       ENUM2STRING( aResStr, VISU::TPLOT3D );
105       ENUM2STRING( aResStr, VISU::TANIMATION );
106     }
107   }
108
109   if(aResStr.isNull()){
110     bool isExist;
111     VISU::VISUType aType = (VISU::VISUType)VISU::Storable::FindValue( aMap, "myType", &isExist).toInt();
112     if( isExist ){
113       switch (aType) {
114         ENUM2STRING( aResStr, VISU::TENTITY );
115         ENUM2STRING( aResStr, VISU::TFAMILY );
116         ENUM2STRING( aResStr, VISU::TGROUP );
117         ENUM2STRING( aResStr, VISU::TVIEW3D );
118         ENUM2STRING( aResStr, VISU::TFIELD );
119         ENUM2STRING( aResStr, VISU::TTIMESTAMP );
120       }
121     }
122   }
123
124   if(aResStr.isNull()){
125     bool isExist;
126     QString aVal = VISU::Storable::FindValue(aMap,"myComment",&isExist);
127     if ( isExist && aVal!="MESH" )
128       aResStr = "VISU::T" + aVal;
129   }
130
131   return aResStr;
132 }
133
134 QString VisuGUI_Selection::nbComponents( const int ind ) const
135 {
136   QString aResStr;
137   VISU::Storable::TRestoringMap aMap;
138   SalomeApp_Study* aStudy = GetStudy();
139   if ( !aStudy )
140     return aResStr;
141   GetSelectedObj( aStudy, entry( ind ), &aMap );  
142   bool isExist;
143   const QString& aVal = VISU::Storable::FindValue(aMap,"myNumComponent",&isExist);
144   if ( isExist )
145     aResStr = aVal;
146   return aResStr;
147 }
148
149 QString VisuGUI_Selection::medEntity( const int ind ) const
150 {
151   bool isExist;
152   VISU::Storable::TRestoringMap aMap;
153   GetSelectedObj( GetStudy(), entry( ind ), &aMap );  
154   const QString& aVal = VISU::Storable::FindValue(aMap,"myEntityId",&isExist);
155   if ( isExist ) {
156     using namespace VISU;
157     TEntity anEntityId = TEntity(aVal.toInt());
158     switch(anEntityId){
159     case NODE_ENTITY :
160       return "NODE_ENTITY";
161     case EDGE_ENTITY :
162       return "EDGE_ENTITY";
163     case FACE_ENTITY :
164       return "FACE_ENTITY";
165     case CELL_ENTITY :
166       return "CELL_ENTITY";
167     default:
168       return QString();
169     }
170   }
171   return QString();
172 }
173
174 QString VisuGUI_Selection::medSource( const int ind ) const
175 {
176   _PTR(Study) aStudyDS = GetStudy()->studyDS();
177   if(_PTR(SObject) aSObject = aStudyDS->FindObjectID(entry(ind).latin1())){
178     VISU::Result_var aRes;
179     if(VISU::Result_i* aResult = CheckResult(myModule,aSObject,aRes)){
180       using namespace VISU;
181       Result_i::ECreationId aCreationId = aResult->GetCreationId();
182       switch(aCreationId){
183       case Result_i::eImportFile :
184         return "eImportFile";
185       case Result_i::eCopyAndImportFile :
186         return "eCopyAndImportFile";
187       case Result_i::eImportMed :
188         return "eImportMed";
189       case Result_i::eImportMedField :
190         return "eImportMedField";
191       default:
192         return QString();
193       }      
194     }
195   }
196   return QString();
197 }
198
199 QString VisuGUI_Selection::nbTimeStamps( const int ind ) const
200 {
201   QString aResStr;
202   VISU::Storable::TRestoringMap aMap;
203   SalomeApp_Study* aStudy = GetStudy();
204   if ( !aStudy )
205     return aResStr;
206   GetSelectedObj( aStudy, entry( ind ), &aMap );  
207   bool isExist;
208   const QString& aVal = VISU::Storable::FindValue(aMap,"myNbTimeStamps",&isExist);
209   if ( isExist )
210     aResStr = aVal;
211   return aResStr;
212 }
213
214 QString VisuGUI_Selection::representation( const int ind ) const
215 {
216   QString aResStr;
217
218   if (SVTK_ViewWindow* aView = GetActiveViewWindow<SVTK_ViewWindow>(myModule)) {
219     if (VISU_Actor* anVISUActor = FindActor(aView, entry(ind).latin1())) {
220       int aRepresent = anVISUActor->GetRepresentation();
221       switch (aRepresent) {
222         ENUM2STRING( aResStr, VISU::POINT );
223         ENUM2STRING( aResStr, VISU::WIREFRAME );
224         ENUM2STRING( aResStr, VISU::SHADED );
225         ENUM2STRING( aResStr, VISU::INSIDEFRAME );
226         ENUM2STRING( aResStr, VISU::SURFACEFRAME );
227       }
228     }
229   }
230
231   return aResStr;
232 }
233
234 SalomeApp_Study* VisuGUI_Selection::GetStudy() const
235   
236 {
237   LightApp_Study* aLightStudy = const_cast<LightApp_Study*>( study() );
238   return dynamic_cast<SalomeApp_Study*>( aLightStudy );
239 }
240
241 int VisuGUI_Selection::nbChild( const int ind, const bool named ) const
242 {
243   int cnt = 0;
244   SalomeApp_Study* aSStudy = GetStudy();
245   if ( !aSStudy )
246     return cnt;
247   _PTR(Study) aStudy =  GetCStudy( aSStudy );
248   if ( aStudy ){
249     _PTR(SObject) SO = aStudy->FindObjectID( entry( ind ).latin1() );
250     if ( SO ){
251       for ( _PTR(ChildIterator) Iter = aStudy->NewChildIterator( SO ); Iter->More(); Iter->Next() ) {
252         _PTR(SObject) refSO;
253         if ( !Iter->Value()->ReferencedObject( refSO ) && ( !named || Iter->Value()->GetName().size() ) )
254           cnt++;
255       }
256     }
257   }
258   return cnt;
259 }
260
261 QString VisuGUI_Selection::nbChildren( const int ind ) const
262 {
263   QString aResStr;
264   aResStr.setNum( nbChild( ind, false ) );
265   return aResStr;
266 }
267
268 QString VisuGUI_Selection::nbNamedChildren( const int ind ) const
269 {
270   QString aResStr;
271   aResStr.setNum( nbChild( ind, true ) );
272   return aResStr;
273 }
274
275 namespace
276 {
277   struct TIsVisibleFunctor
278   {
279     template<class TViewer>
280     QString
281     Get(VisuGUI* theModule, 
282         const QString& theEntry)
283     {
284       typedef typename TViewer::TViewWindow TViewWindow;
285       if (TViewWindow* aViewWindow = GetActiveViewWindow<TViewWindow>(theModule))
286         if (VISU_Actor* anActor = FindActor(aViewWindow,theEntry.latin1()))
287           return anActor->GetVisibility() ? "true" : "false";
288
289       return QString();
290     }
291   };
292
293   template<class TPopupFunctor>
294   struct TPopupDispatcher
295   {
296     QString
297     operator()(VisuGUI* theModule, 
298                const QString& theEntry)
299     {
300       if(SUIT_ViewManager* aViewManager = theModule->getApp()->activeViewManager()){
301         QString aType = aViewManager->getType();
302         TPopupFunctor aFunctor;
303         if(aType == SVTK_Viewer::Type())
304           return aFunctor.template Get<SVTK_Viewer>(theModule,theEntry);
305         else if(aType == VVTK_Viewer::Type())
306           return aFunctor.template Get<VVTK_Viewer>(theModule,theEntry);
307       }
308       return QString();
309     }    
310   };
311 }
312
313 QString VisuGUI_Selection::isVisible( const int ind ) const
314 {
315   return TPopupDispatcher<TIsVisibleFunctor>()((VisuGUI*)myModule,entry(ind));
316 }
317
318 QString VisuGUI_Selection::isShrunk( const int ind ) const
319 {
320   QString aResStr;
321
322   if ( SVTK_ViewWindow* aView = GetActiveViewWindow<SVTK_ViewWindow>(myModule) )
323     if ( VISU_Actor* anVISUActor = FindActor( aView, entry( ind ).latin1() ) )
324       if ( anVISUActor->IsShrunkable() )
325         aResStr = anVISUActor->IsShrunk() ? "1" : "0";
326
327   return aResStr;
328 }
329
330 QString VisuGUI_Selection::hasActor( const int ind ) const
331 {
332   return representation( ind ).isEmpty() ? "0" : "1";
333 }
334
335 QString VisuGUI_Selection::isShading( const int ind ) const
336 {
337   QString aResStr;
338   
339   if ( SVTK_ViewWindow* aView = GetActiveViewWindow<SVTK_ViewWindow>(myModule) )
340     if ( VISU_Actor* anVISUActor = FindActor( aView, entry( ind ).latin1() ) )
341       if ( VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(anVISUActor) )
342         aResStr = anActor->IsShading() ? "1" : "0";
343   
344   return aResStr;       
345 }
346
347 QString VisuGUI_Selection::isScalarMapAct( const int ind ) const
348 {
349   QString aResStr;
350
351   if ( SVTK_ViewWindow* aView = GetActiveViewWindow<SVTK_ViewWindow>(myModule) )
352     if ( VISU_Actor* anVISUActor = FindActor( aView, entry( ind ).latin1() ) )
353     {
354       VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(anVISUActor);
355       aResStr = anActor ? "1" : "0";
356     }
357
358   return aResStr;
359 }
360
361 bool VisuGUI_Selection::isVisuComponent( const int ind ) const
362 {
363   SalomeApp_Study* study = GetStudy();
364   if ( !study )
365     return false;
366   
367   _PTR(SObject) obj = study->studyDS()->FindObjectID( entry( ind ).latin1() );
368   if ( !obj )
369     return false;
370   CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
371   if( CORBA::is_nil( anObj ) )
372     return false;
373
374   return dynamic_cast<VISU::VISU_Gen_i*>( VISU::GetServant( anObj ).in() );
375 }