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