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