]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_Displayer.cxx
Salome HOME
SALOME::GenericObj : Destroy() -> UnRegister()
[modules/visu.git] / src / VISUGUI / VisuGUI_Displayer.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  VISU VISUGUI : Displayer for VISU module
24 //  File   : VisuGUI_Displayer.cxx
25 //  Author : Alexander SOLOVYOV
26 //  Module : VISU
27 //  $Header: /home/server/cvs/VISU/VISU_SRC/src/VISUGUI/VisuGUI_Displayer.cxx
28 //
29 #include "VisuGUI_Displayer.h"
30
31 #include "VisuGUI.h"
32 #include "VisuGUI_Tools.h"
33 #include "VISU_Tools.h"
34 #include "VISU_ViewManager_i.hh"
35 #include "VISU_Actor.h"
36
37 #include <VISU_Table_i.hh>
38
39 //#include "VVTK_ViewModel.h"
40
41 #include <SVTK_ViewModel.h>
42 #include <SVTK_ViewWindow.h>
43
44 #include <SPlot2d_ViewModel.h>
45 #include <SPlot2d_Prs.h>
46 #include <Plot2d_ViewWindow.h>
47
48 #include <SalomeApp_Application.h>
49 #include <SalomeApp_Study.h>
50
51 #include <SUIT_ViewManager.h>
52 #include <SUIT_MessageBox.h>
53 #include <SUIT_Desktop.h>
54
55
56
57 void changeVisibility(int theDisplaying, QString entry, SalomeApp_Study* study) {
58   
59   _PTR(SObject) obj = study->studyDS()->FindObjectID( (const char*)entry.toLatin1() );
60   CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
61   if( CORBA::is_nil( anObj ) )
62     return;
63
64   if(VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in()) ) {
65     VISU::CurveVisibilityChanged(aCurve,theDisplaying, false, true, true);
66   }
67
68   if(VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in()) ) {
69     int nbCurves = aContainer->GetNbCurves();
70     for( int k=1; k<=nbCurves; k++ ) {
71       VISU::Curve_i* aCurve = aContainer->GetCurve( k );
72       if(aCurve)
73         VISU::CurveVisibilityChanged(aCurve,theDisplaying, true, true, true);
74     }
75   }
76
77   if(VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>( VISU::GetServant( anObj ).in()) ) {
78     _PTR(SObject) TableSO = study->studyDS()->FindObjectID( aTable->GetEntry() );
79     if ( TableSO ) {
80       _PTR(ChildIterator) Iter = study->studyDS()->NewChildIterator( TableSO );
81       
82       for ( ; Iter->More(); Iter->Next() ) {
83         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
84         if( !CORBA::is_nil( childObject ) ) {
85           CORBA::Object_ptr aCurve_ptr = VISU::Curve::_narrow( childObject );
86           if( !CORBA::is_nil( aCurve_ptr ) ) {
87             VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve_ptr).in());
88             if(aCurve)
89               VISU::CurveVisibilityChanged(aCurve,theDisplaying, true, false, true);
90           }
91         }
92       }
93     }
94   }
95 }
96
97
98 VisuGUI_Displayer::VisuGUI_Displayer( SalomeApp_Application* app )
99 : LightApp_Displayer(),
100   myApp( app )
101 {
102 }
103
104 VisuGUI_Displayer::~VisuGUI_Displayer()
105 {
106 }
107
108
109 bool VisuGUI_Displayer::IsDisplayed( const QString& id, SALOME_View* vf ) const {
110
111   bool displayed = false;
112   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
113   if(!aStudy)
114     return displayed;
115   
116   _PTR(SObject) aSObject = aStudy->studyDS()->FindObjectID( (const char*)id.toLatin1() );
117   CORBA::Object_var anObj = VISU::ClientSObjectToObject( aSObject );
118   if( CORBA::is_nil( anObj ) )
119     return LightApp_Displayer::IsDisplayed(id, vf);
120   
121   if(VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in())) {
122     int nbCurves = aContainer->GetNbCurves();
123     for( int k=1; k<=nbCurves; k++ ) {
124       VISU::Curve_i* aCurve = aContainer->GetCurve( k );
125       if(aCurve && LightApp_Displayer::IsDisplayed(aCurve->GetEntry().c_str(), vf)) {
126         displayed = true;
127         break;
128       }
129     }
130     return displayed;
131   } else if(VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>( VISU::GetServant( anObj ).in())) {
132
133     _PTR(SObject) TableSO = aStudy->studyDS()->FindObjectID( (const char*)id.toLatin1() );
134     if( TableSO ) {
135       _PTR(ChildIterator) Iter = aStudy->studyDS()->NewChildIterator( TableSO );
136       for( ; Iter->More(); Iter->Next() ) {
137         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
138         if( !CORBA::is_nil( childObject ) ) {
139           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
140           if( !CORBA::is_nil( aCurve ) ) {
141             VISU::Curve_i* aCurve_i = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
142             if(aCurve && LightApp_Displayer::IsDisplayed(aCurve_i->GetEntry().c_str(), vf)) {
143               displayed = true;
144               break;
145             }       
146           }
147         }
148       }      
149     }
150     return displayed;
151   } else {
152     return LightApp_Displayer::IsDisplayed(id, vf);
153   }
154 }
155
156 SALOME_Prs* VisuGUI_Displayer::buildPresentation( const QString& theEntry, SALOME_View* theView )
157 {
158   SALOME_Prs* aPrs = 0;
159
160   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
161
162   if(aStudy){
163     _PTR(SObject) aSObject = aStudy->studyDS()->FindObjectID( (const char*)theEntry.toLatin1() );
164     CORBA::Object_var anObject = VISU::ClientSObjectToObject( aSObject );
165     if( CORBA::is_nil( anObject ) )
166       return 0;
167
168     VISU::Base_var aBase = VISU::Base::_narrow(anObject);
169     if (CORBA::is_nil(aBase)) 
170       return 0;
171
172     VISU::Base_i* aBaseServant = dynamic_cast<VISU::Base_i*>(VISU::GetServant(aBase).in());
173
174     SALOME_View* aView = theView ? theView : GetActiveView();
175     VISU::VISUType aType = aBase->GetType();
176     VISU::Prs3d_var aPrs3d = VISU::Prs3d::_narrow(aBase);
177     if(!CORBA::is_nil(aPrs3d) || aType == VISU::TCOLOREDPRS3DHOLDER){
178       SVTK_Viewer* aViewer = dynamic_cast<SVTK_Viewer*>( aView );
179       //if (!aViewer)
180       //aViewer = dynamic_cast<SVTK_Viewer*>( aView );
181       if( aViewer ){
182         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
183         SUIT_ViewWindow* aWindow = aViewManager->getActiveView();
184         if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( aWindow )){
185           VISU::Prs3d_i* aPrs3d = NULL;
186           if(aType == VISU::TCOLOREDPRS3DHOLDER){
187             VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(aBase);
188             VISU::ColoredPrs3d_var aColoredPrs3d = aHolder->GetDevice();
189             aPrs3d = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aColoredPrs3d).in());
190           }else if (aType == VISU::TPOINTMAP3D) {
191             VISU::PointMap3d_i* aTable3d = dynamic_cast<VISU::PointMap3d_i*>(aBaseServant);
192             VISU_ActorBase* anActorBase = VISU::FindActorBase(aViewWindow, aTable3d);
193             if (anActorBase) {
194               anActorBase->SetVisibility(true);
195               aViewWindow->Repaint();
196             }
197             else {
198               VISU_PointMap3dActor* aPointMap3dActor = aTable3d->CreateActor();
199               if (aPointMap3dActor) {
200                 aViewWindow->AddActor(aPointMap3dActor);
201                 aPointMap3dActor->SetVisibility(true);
202                 aViewWindow->Repaint();
203               }
204             }
205             VISU::SetVisibilityState(qPrintable(theEntry),Qtx::ShownState);
206           }
207           else
208             aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aBaseServant);
209
210           if(aPrs3d){
211             buildPrs3d( aViewWindow, aPrs3d );
212             Handle(SALOME_InteractiveObject) anIO = aPrs3d->GetIO();
213             if(anIO->hasEntry()){
214               std::string anEntry = anIO->getEntry();
215               aPrs = LightApp_Displayer::buildPresentation( anEntry.c_str(), aView );
216             }
217           }
218         }
219       }
220     }
221
222     SPlot2d_Viewer* aViewer = 0;
223     if (aView) aViewer = dynamic_cast<SPlot2d_Viewer*>(aView);
224     if(!aViewer && (aType==VISU::TCURVE || aType==VISU::TCONTAINER))
225       if(VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>(myApp->activeModule()))
226         aViewer = VISU::GetPlot2dViewer( (SalomeApp_Module*)aVisuGUI, true );
227     
228     if(aViewer){
229       SUIT_ViewManager* aViewManager = aViewer->getViewManager();
230       SUIT_ViewWindow* aWindow = aViewManager->getActiveView();
231       Plot2d_ViewWindow* aViewWindow = dynamic_cast<Plot2d_ViewWindow*>( aWindow );
232       if( !aViewWindow )
233         return 0;
234       
235       SPlot2d_Prs* aPlot2dPrs = 0;
236       switch (aType) {
237       case VISU::TCURVE: {
238         VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(aBaseServant);
239         aPlot2dPrs = buildCurve( aViewWindow, aCurve );
240         break;
241       }
242       case VISU::TCONTAINER: {
243         VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(aBaseServant);
244         aPlot2dPrs = buildContainer( aViewWindow, aContainer );
245         break;
246       }
247       case VISU::TPOINTMAP3D: {
248         VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(aBaseServant);
249         aPlot2dPrs = buildTable( aViewWindow, aTable );
250         break;
251       }
252       case VISU::TTABLE: {
253         VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(aBaseServant);
254         aPlot2dPrs = buildTable( aViewWindow, aTable );
255         break;
256       }}
257
258       if( aPlot2dPrs )
259         aPrs = new SPlot2d_Prs( aPlot2dPrs );
260
261       if( aPrs )
262         UpdatePrs( aPrs );
263     }
264   }
265   return aPrs;
266 }
267
268 void VisuGUI_Displayer::buildPrs3d( SVTK_ViewWindow* theViewWindow, VISU::Prs3d_i* thePrs3d ) const
269 {
270   VISU_Actor* anActor = VISU::FindActor(theViewWindow, thePrs3d);
271   if (!anActor) {
272     try {
273       anActor = thePrs3d->CreateActor();
274     } catch (std::runtime_error& exc) {
275       thePrs3d->RemoveActors();
276
277       INFOS(exc.what());
278       SUIT_MessageBox::warning
279         (myApp->desktop(), QObject::tr("WRN_VISU"),
280          QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
281          QObject::tr("BUT_OK"));
282     }
283     if (anActor)
284       theViewWindow->AddActor(anActor);
285   } else 
286     anActor->SetVisibility(true);
287
288   theViewWindow->Repaint();
289 }
290
291 bool VisuGUI_Displayer::addCurve( SPlot2d_Prs* prs, Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
292 {
293   if( !prs || !wnd || !c )
294     return false;
295
296   QString entry = c->GetSObject()->GetID();
297   SPlot2d_Viewer* vv = dynamic_cast<SPlot2d_Viewer*>( wnd->getModel() );
298   if( !vv )
299     return false;
300
301   SPlot2d_Curve* curve = vv->getCurveByIO( vv->FindIObject( (const char*)entry.toLatin1() ) );
302   if( !curve )
303   {
304     curve = c->CreatePresentation();
305     VISU::UpdateCurve( c, 0, curve, VISU::eDisplay );
306   }
307   if( curve )
308     prs->AddObject( curve );
309
310   return curve!=0;
311 }
312
313 SPlot2d_Prs* VisuGUI_Displayer::buildCurve( Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
314 {
315   SPlot2d_Prs* prs = new SPlot2d_Prs();
316   if( !addCurve( prs, wnd, c ) )
317   {
318     delete prs;
319     prs = 0;
320   }
321   return prs;
322 }
323
324 SPlot2d_Prs* VisuGUI_Displayer::buildContainer( Plot2d_ViewWindow* wnd, VISU::Container_i* c ) const
325 {
326   SPlot2d_Prs* prs = new SPlot2d_Prs();
327
328   int nbCurves = c ? c->GetNbCurves() : 0;
329   for( int k=1; k<=nbCurves; k++ )
330   {
331     VISU::Curve_i* theCurve = c->GetCurve( k );
332     if( theCurve && theCurve->IsValid() )
333       addCurve( prs, wnd, theCurve );
334   }
335   if( prs->getObjects().count()==0 )
336   {
337     delete prs;
338     prs = 0;
339   }
340   return prs;
341 }
342
343 SPlot2d_Prs* VisuGUI_Displayer::buildTable( Plot2d_ViewWindow* wnd, VISU::Table_i* t ) const
344 {
345   SPlot2d_Prs* prs = new SPlot2d_Prs();
346   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
347   if( !study )
348     return prs;
349
350   _PTR(SObject) TableSO = study->studyDS()->FindObjectID( t->GetEntry() );
351
352   if( !TableSO )
353     return prs;
354
355   _PTR(ChildIterator) Iter = study->studyDS()->NewChildIterator( TableSO );
356   for( ; Iter->More(); Iter->Next() )
357   {
358     CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
359     if( !CORBA::is_nil( childObject ) )
360     {
361       CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
362       if( !CORBA::is_nil( aCurve ) )
363       {
364         VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
365         addCurve( prs, wnd, theCurve );
366       }
367     }
368   }
369   if( prs->getObjects().count()==0 )
370   {
371     delete prs;
372     prs = 0;
373   }
374   return prs;
375 }
376
377 bool VisuGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
378 {
379   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
380   if( !study )
381     return false;
382
383   _PTR(SObject) obj = study->studyDS()->FindObjectID( (const char*)entry.toLatin1() );
384   CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
385   if( CORBA::is_nil( anObj ) )
386     return false;
387
388   if( study->isComponent( entry ) )
389     return true;
390
391   if( (dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in() )
392        ||
393        dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in() ))
394       && viewer_type==SPlot2d_Viewer::Type())
395     return true;
396
397   if( viewer_type==SVTK_Viewer::Type() /*|| viewer_type==VVTK_Viewer::Type()*/)
398   {
399     VISU::Base_i* aBase = dynamic_cast<VISU::Base_i*>(VISU::GetServant(anObj).in());
400
401     if ( !aBase )
402       return false;
403
404     if(aBase->GetType() == VISU::TCOLOREDPRS3DHOLDER)
405       return true;
406
407     if(aBase->GetType() == VISU::TPOINTMAP3D)
408       return dynamic_cast<VISU::PointMap3d_i*>(aBase);
409
410     return dynamic_cast<VISU::Prs3d_i*>(aBase);
411   }
412   else if( viewer_type==SPlot2d_Viewer::Type() )
413   {
414     VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant( anObj ).in() );
415     return aTable;
416   }
417   else 
418     return false;
419 }
420
421 void VisuGUI_Displayer::AfterDisplay( SALOME_View* vf, const SALOME_Plot2dViewType& ) {
422   changeVisibility(VISU::eDisplay, myLastEntry,dynamic_cast<SalomeApp_Study*>( myApp->activeStudy()));
423 }
424
425 void VisuGUI_Displayer::AfterErase( SALOME_View* vf, const SALOME_Plot2dViewType& ) {
426   changeVisibility(VISU::eErase, myLastEntry,dynamic_cast<SalomeApp_Study*>( myApp->activeStudy()));
427 }