Salome HOME
Fix for the "0051899: curves are not shown in opened study" issue.
[modules/visu.git] / src / VISUGUI / VisuGUI_Displayer.cxx
1 // Copyright (C) 2007-2013  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     SPlot2d_Viewer* view2D = dynamic_cast<SPlot2d_Viewer*>(vf);
134     if( !view2D )
135       return LightApp_Displayer::IsDisplayed(id, vf);
136     
137     _PTR(SObject) TableSO = aStudy->studyDS()->FindObjectID( (const char*)id.toLatin1() );
138     if( TableSO ) {
139       _PTR(ChildIterator) Iter = aStudy->studyDS()->NewChildIterator( TableSO );
140       for( ; Iter->More(); Iter->Next() ) {
141         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
142         if( !CORBA::is_nil( childObject ) ) {
143           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
144           if( !CORBA::is_nil( aCurve ) ) {
145             VISU::Curve_i* aCurve_i = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
146             if(aCurve && LightApp_Displayer::IsDisplayed(aCurve_i->GetEntry().c_str(), vf)) {
147               displayed = true;
148               break;
149             }       
150           }
151         }
152       }      
153     }
154     return displayed;
155   } else {
156     return LightApp_Displayer::IsDisplayed(id, vf);
157   }
158 }
159
160 SALOME_Prs* VisuGUI_Displayer::buildPresentation( const QString& theEntry, SALOME_View* theView )
161 {
162   SALOME_Prs* aPrs = 0;
163
164   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
165
166   if(aStudy){
167     _PTR(SObject) aSObject = aStudy->studyDS()->FindObjectID( (const char*)theEntry.toLatin1() );
168     CORBA::Object_var anObject = VISU::ClientSObjectToObject( aSObject );
169     if( CORBA::is_nil( anObject ) )
170       return 0;
171
172     VISU::Base_var aBase = VISU::Base::_narrow(anObject);
173     if (CORBA::is_nil(aBase)) 
174       return 0;
175
176     VISU::Base_i* aBaseServant = dynamic_cast<VISU::Base_i*>(VISU::GetServant(aBase).in());
177
178     SALOME_View* aView = theView ? theView : GetActiveView();
179     VISU::VISUType aType = aBase->GetType();
180     VISU::Prs3d_var aPrs3d = VISU::Prs3d::_narrow(aBase);
181     if(!CORBA::is_nil(aPrs3d) || aType == VISU::TCOLOREDPRS3DHOLDER){
182       SVTK_Viewer* aViewer = dynamic_cast<SVTK_Viewer*>( aView );
183       //if (!aViewer)
184       //aViewer = dynamic_cast<SVTK_Viewer*>( aView );
185       if( aViewer ){
186         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
187         SUIT_ViewWindow* aWindow = aViewManager->getActiveView();
188         if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( aWindow )){
189           VISU::Prs3d_i* aPrs3d = NULL;
190           if(aType == VISU::TCOLOREDPRS3DHOLDER){
191             VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(aBase);
192             VISU::ColoredPrs3d_var aColoredPrs3d = aHolder->GetDevice();
193             aPrs3d = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aColoredPrs3d).in());
194           }else if (aType == VISU::TPOINTMAP3D) {
195             VISU::PointMap3d_i* aTable3d = dynamic_cast<VISU::PointMap3d_i*>(aBaseServant);
196             VISU_ActorBase* anActorBase = VISU::FindActorBase(aViewWindow, aTable3d);
197             if (anActorBase) {
198               anActorBase->SetVisibility(true);
199               aViewWindow->Repaint();
200             }
201             else {
202               VISU_PointMap3dActor* aPointMap3dActor = aTable3d->CreateActor();
203               if (aPointMap3dActor) {
204                 aViewWindow->AddActor(aPointMap3dActor);
205                 aPointMap3dActor->SetVisibility(true);
206                 aViewWindow->Repaint();
207               }
208             }
209             VISU::SetVisibilityState(qPrintable(theEntry),Qtx::ShownState);
210           }
211           else
212             aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aBaseServant);
213
214           if(aPrs3d){
215             buildPrs3d( aViewWindow, aPrs3d );
216             Handle(SALOME_InteractiveObject) anIO = aPrs3d->GetIO();
217             if(anIO->hasEntry()){
218               std::string anEntry = anIO->getEntry();
219               aPrs = LightApp_Displayer::buildPresentation( anEntry.c_str(), aView );
220             }
221           }
222         }
223       }
224     }
225
226     SPlot2d_Viewer* aViewer = 0;
227     if (aView) aViewer = dynamic_cast<SPlot2d_Viewer*>(aView);
228     if(!aViewer && (aType==VISU::TCURVE || aType==VISU::TCONTAINER))
229       if(VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>(myApp->activeModule()))
230         aViewer = VISU::GetPlot2dViewer( (SalomeApp_Module*)aVisuGUI, true );
231     
232     if(aViewer){
233       SUIT_ViewManager* aViewManager = aViewer->getViewManager();
234       SUIT_ViewWindow* aWindow = aViewManager->getActiveView();
235       Plot2d_ViewWindow* aViewWindow = dynamic_cast<Plot2d_ViewWindow*>( aWindow );
236       if( !aViewWindow )
237         return 0;
238       
239       SPlot2d_Prs* aPlot2dPrs = 0;
240       switch (aType) {
241       case VISU::TCURVE: {
242         VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(aBaseServant);
243         aPlot2dPrs = buildCurve( aViewWindow, aCurve );
244         break;
245       }
246       case VISU::TCONTAINER: {
247         VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(aBaseServant);
248         aPlot2dPrs = buildContainer( aViewWindow, aContainer );
249         break;
250       }
251       case VISU::TPOINTMAP3D: {
252         VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(aBaseServant);
253         aPlot2dPrs = buildTable( aViewWindow, aTable );
254         break;
255       }
256       case VISU::TTABLE: {
257         VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(aBaseServant);
258         aPlot2dPrs = buildTable( aViewWindow, aTable );
259         break;
260       }}
261
262       if( aPlot2dPrs )
263         aPrs = new SPlot2d_Prs( aPlot2dPrs );
264
265       if( aPrs )
266         UpdatePrs( aPrs );
267     }
268   }
269   return aPrs;
270 }
271
272 void VisuGUI_Displayer::buildPrs3d( SVTK_ViewWindow* theViewWindow, VISU::Prs3d_i* thePrs3d ) const
273 {
274   VISU_Actor* anActor = VISU::FindActor(theViewWindow, thePrs3d);
275   if (!anActor) {
276     try {
277       anActor = thePrs3d->CreateActor();
278     } catch (std::runtime_error& exc) {
279       thePrs3d->RemoveActors();
280
281       INFOS(exc.what());
282       SUIT_MessageBox::warning
283         (myApp->desktop(), QObject::tr("WRN_VISU"),
284          QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
285          QObject::tr("BUT_OK"));
286     }
287     if (anActor)
288       theViewWindow->AddActor(anActor);
289   } else 
290     anActor->SetVisibility(true);
291
292   theViewWindow->Repaint();
293 }
294
295 bool VisuGUI_Displayer::addCurve( SPlot2d_Prs* prs, Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
296 {
297   if( !prs || !wnd || !c )
298     return false;
299
300   QString entry = c->GetSObject()->GetID();
301   SPlot2d_Viewer* vv = dynamic_cast<SPlot2d_Viewer*>( wnd->getModel() );
302   if( !vv )
303     return false;
304
305   SPlot2d_Curve* curve = vv->getCurveByIO( vv->FindIObject( (const char*)entry.toLatin1() ) );
306   if( !curve )
307   {
308     curve = c->CreatePresentation();
309     VISU::UpdateCurve( c, 0, curve, VISU::eDisplay );
310   }
311   if( curve )
312     prs->AddObject( curve );
313
314   return curve!=0;
315 }
316
317 SPlot2d_Prs* VisuGUI_Displayer::buildCurve( Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
318 {
319   SPlot2d_Prs* prs = new SPlot2d_Prs();
320   if( !addCurve( prs, wnd, c ) )
321   {
322     delete prs;
323     prs = 0;
324   }
325   return prs;
326 }
327
328 SPlot2d_Prs* VisuGUI_Displayer::buildContainer( Plot2d_ViewWindow* wnd, VISU::Container_i* c ) const
329 {
330   SPlot2d_Prs* prs = new SPlot2d_Prs();
331
332   int nbCurves = c ? c->GetNbCurves() : 0;
333   for( int k=1; k<=nbCurves; k++ )
334   {
335     VISU::Curve_i* theCurve = c->GetCurve( k );
336     if( theCurve && theCurve->IsValid() )
337       addCurve( prs, wnd, theCurve );
338   }
339   if( prs->getObjects().count()==0 )
340   {
341     delete prs;
342     prs = 0;
343   }
344   return prs;
345 }
346
347 SPlot2d_Prs* VisuGUI_Displayer::buildTable( Plot2d_ViewWindow* wnd, VISU::Table_i* t ) const
348 {
349   SPlot2d_Prs* prs = new SPlot2d_Prs();
350   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
351   if( !study )
352     return prs;
353
354   _PTR(SObject) TableSO = study->studyDS()->FindObjectID( t->GetEntry() );
355
356   if( !TableSO )
357     return prs;
358
359   _PTR(ChildIterator) Iter = study->studyDS()->NewChildIterator( TableSO );
360   for( ; Iter->More(); Iter->Next() )
361   {
362     CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
363     if( !CORBA::is_nil( childObject ) )
364     {
365       CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
366       if( !CORBA::is_nil( aCurve ) )
367       {
368         VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
369         addCurve( prs, wnd, theCurve );
370       }
371     }
372   }
373   if( prs->getObjects().count()==0 )
374   {
375     delete prs;
376     prs = 0;
377   }
378   return prs;
379 }
380
381 bool VisuGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
382 {
383   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
384   if( !study )
385     return false;
386
387   _PTR(SObject) obj = study->studyDS()->FindObjectID( (const char*)entry.toLatin1() );
388   CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
389   if( CORBA::is_nil( anObj ) )
390     return false;
391
392   if( study->isComponent( entry ) )
393     return true;
394
395   if( (dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in() )
396        ||
397        dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in() ))
398       && viewer_type==SPlot2d_Viewer::Type())
399     return true;
400
401   if( viewer_type==SVTK_Viewer::Type() /*|| viewer_type==VVTK_Viewer::Type()*/)
402   {
403     VISU::Base_i* aBase = dynamic_cast<VISU::Base_i*>(VISU::GetServant(anObj).in());
404
405     if ( !aBase )
406       return false;
407
408     if(aBase->GetType() == VISU::TCOLOREDPRS3DHOLDER)
409       return true;
410
411     if(aBase->GetType() == VISU::TPOINTMAP3D)
412       return dynamic_cast<VISU::PointMap3d_i*>(aBase);
413
414     return dynamic_cast<VISU::Prs3d_i*>(aBase);
415   }
416   else if( viewer_type==SPlot2d_Viewer::Type() )
417   {
418     VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant( anObj ).in() );
419     return aTable;
420   }
421   else 
422     return false;
423 }
424
425 void VisuGUI_Displayer::AfterDisplay( SALOME_View* vf, const SALOME_Prs2d* p ) {
426   changeVisibility(VISU::eDisplay, myLastEntry, dynamic_cast<SalomeApp_Study*>( myApp->activeStudy()));
427 }
428
429 void VisuGUI_Displayer::AfterErase( SALOME_View* vf, const SALOME_Prs2d* p ) {
430   changeVisibility(VISU::eErase, myLastEntry, dynamic_cast<SalomeApp_Study*>( myApp->activeStudy()));
431 }