Salome HOME
bfa420adcd4ee184f3bf624b24037605672ae59a
[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 "VVTK_ViewModel.h"
38
39 #include <SVTK_ViewModel.h>
40 #include <SVTK_ViewWindow.h>
41
42 #include <SPlot2d_ViewModel.h>
43 #include <SPlot2d_Prs.h>
44 #include <Plot2d_ViewWindow.h>
45
46 #include <SalomeApp_Application.h>
47 #include <SalomeApp_Study.h>
48
49 #include <SUIT_ViewManager.h>
50 #include <SUIT_MessageBox.h>
51 #include <SUIT_Desktop.h>
52
53 VisuGUI_Displayer::VisuGUI_Displayer( SalomeApp_Application* app )
54 : LightApp_Displayer(),
55   myApp( app )
56 {
57 }
58
59 VisuGUI_Displayer::~VisuGUI_Displayer()
60 {
61 }
62
63 SALOME_Prs* VisuGUI_Displayer::buildPresentation( const QString& theEntry, SALOME_View* theView )
64 {
65   SALOME_Prs* aPrs = 0;
66
67   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
68
69   if(aStudy){
70     _PTR(SObject) aSObject = aStudy->studyDS()->FindObjectID( (const char*)theEntry.toLatin1() );
71     CORBA::Object_var anObject = VISU::ClientSObjectToObject( aSObject );
72     if( CORBA::is_nil( anObject ) )
73       return 0;
74
75     VISU::Base_var aBase = VISU::Base::_narrow(anObject);
76     if (CORBA::is_nil(aBase)) 
77       return 0;
78
79     VISU::Base_i* aBaseServant = dynamic_cast<VISU::Base_i*>(VISU::GetServant(aBase).in());
80
81     SALOME_View* aView = theView ? theView : GetActiveView();
82     VISU::VISUType aType = aBase->GetType();
83     VISU::Prs3d_var aPrs3d = VISU::Prs3d::_narrow(aBase);
84     if(!CORBA::is_nil(aPrs3d) || aType == VISU::TCOLOREDPRS3DHOLDER){
85       SVTK_Viewer* aViewer = dynamic_cast<SVTK_Viewer*>( aView );
86       //if (!aViewer)
87       //aViewer = dynamic_cast<SVTK_Viewer*>( aView );
88       if( aViewer ){
89         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
90         SUIT_ViewWindow* aWindow = aViewManager->getActiveView();
91         if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( aWindow )){
92           VISU::Prs3d_i* aPrs3d = NULL;
93           if(aType == VISU::TCOLOREDPRS3DHOLDER){
94             VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(aBase);
95             VISU::ColoredPrs3d_var aColoredPrs3d = aHolder->GetDevice();
96             aPrs3d = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aColoredPrs3d).in());
97           }else if (aType == VISU::TPOINTMAP3D) {
98             VISU::PointMap3d_i* aTable3d = dynamic_cast<VISU::PointMap3d_i*>(aBaseServant);
99             VISU_ActorBase* anActorBase = VISU::FindActorBase(aViewWindow, aTable3d);
100             if (anActorBase) {
101               anActorBase->SetVisibility(true);
102               aViewWindow->Repaint();
103             }
104             else {
105               VISU_PointMap3dActor* aPointMap3dActor = aTable3d->CreateActor();
106               if (aPointMap3dActor) {
107                 aViewWindow->AddActor(aPointMap3dActor);
108                 aPointMap3dActor->SetVisibility(true);
109                 aViewWindow->Repaint();
110               }
111             }
112           }
113           else
114             aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aBaseServant);
115
116           if(aPrs3d){
117             buildPrs3d( aViewWindow, aPrs3d );
118             Handle(SALOME_InteractiveObject) anIO = aPrs3d->GetIO();
119             if(anIO->hasEntry()){
120               std::string anEntry = anIO->getEntry();
121               aPrs = LightApp_Displayer::buildPresentation( anEntry.c_str(), aView );
122             }
123           }
124         }
125       }
126     }
127
128     SPlot2d_Viewer* aViewer = 0;
129     if (aView) aViewer = dynamic_cast<SPlot2d_Viewer*>(aView);
130     if(!aViewer && (aType==VISU::TCURVE || aType==VISU::TCONTAINER))
131       if(VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>(myApp->activeModule()))
132         aViewer = VISU::GetPlot2dViewer( (SalomeApp_Module*)aVisuGUI, true );
133     
134     if(aViewer){
135       SUIT_ViewManager* aViewManager = aViewer->getViewManager();
136       SUIT_ViewWindow* aWindow = aViewManager->getActiveView();
137       Plot2d_ViewWindow* aViewWindow = dynamic_cast<Plot2d_ViewWindow*>( aWindow );
138       if( !aViewWindow )
139         return 0;
140       
141       SPlot2d_Prs* aPlot2dPrs = 0;
142       switch (aType) {
143       case VISU::TCURVE: {
144         VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(aBaseServant);
145         aPlot2dPrs = buildCurve( aViewWindow, aCurve );
146         break;
147       }
148       case VISU::TCONTAINER: {
149         VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(aBaseServant);
150         aPlot2dPrs = buildContainer( aViewWindow, aContainer );
151         break;
152       }
153       case VISU::TPOINTMAP3D: {
154         VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(aBaseServant);
155         aPlot2dPrs = buildTable( aViewWindow, aTable );
156         break;
157       }
158       case VISU::TTABLE: {
159         VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(aBaseServant);
160         aPlot2dPrs = buildTable( aViewWindow, aTable );
161         break;
162       }}
163
164       if( aPlot2dPrs )
165         aPrs = new SPlot2d_Prs( aPlot2dPrs );
166
167       if( aPrs )
168         UpdatePrs( aPrs );
169     }
170   }
171   return aPrs;
172 }
173
174 void VisuGUI_Displayer::buildPrs3d( SVTK_ViewWindow* theViewWindow, VISU::Prs3d_i* thePrs3d ) const
175 {
176   VISU_Actor* anActor = VISU::FindActor(theViewWindow, thePrs3d);
177   if (!anActor) {
178     try {
179       anActor = thePrs3d->CreateActor();
180     } catch (std::runtime_error& exc) {
181       thePrs3d->RemoveActors();
182
183       INFOS(exc.what());
184       SUIT_MessageBox::warning
185         (myApp->desktop(), QObject::tr("WRN_VISU"),
186          QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
187          QObject::tr("BUT_OK"));
188     }
189     if (anActor)
190       theViewWindow->AddActor(anActor);
191   } else 
192     anActor->SetVisibility(true);
193
194   theViewWindow->Repaint();
195 }
196
197 bool VisuGUI_Displayer::addCurve( SPlot2d_Prs* prs, Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
198 {
199   if( !prs || !wnd || !c )
200     return false;
201
202   QString entry = c->GetSObject()->GetID();
203   SPlot2d_Viewer* vv = dynamic_cast<SPlot2d_Viewer*>( wnd->getModel() );
204   if( !vv )
205     return false;
206
207   SPlot2d_Curve* curve = vv->getCurveByIO( vv->FindIObject( (const char*)entry.toLatin1() ) );
208   if( !curve )
209   {
210     curve = c->CreatePresentation();
211     VISU::UpdateCurve( c, 0, curve, VISU::eDisplay );
212   }
213   if( curve )
214     prs->AddObject( curve );
215
216   return curve!=0;
217 }
218
219 SPlot2d_Prs* VisuGUI_Displayer::buildCurve( Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
220 {
221   SPlot2d_Prs* prs = new SPlot2d_Prs();
222   if( !addCurve( prs, wnd, c ) )
223   {
224     delete prs;
225     prs = 0;
226   }
227   return prs;
228 }
229
230 SPlot2d_Prs* VisuGUI_Displayer::buildContainer( Plot2d_ViewWindow* wnd, VISU::Container_i* c ) const
231 {
232   SPlot2d_Prs* prs = new SPlot2d_Prs();
233
234   int nbCurves = c ? c->GetNbCurves() : 0;
235   for( int k=1; k<=nbCurves; k++ )
236   {
237     VISU::Curve_i* theCurve = c->GetCurve( k );
238     if( theCurve && theCurve->IsValid() )
239       addCurve( prs, wnd, theCurve );
240   }
241   if( prs->getObjects().count()==0 )
242   {
243     delete prs;
244     prs = 0;
245   }
246   return prs;
247 }
248
249 SPlot2d_Prs* VisuGUI_Displayer::buildTable( Plot2d_ViewWindow* wnd, VISU::Table_i* t ) const
250 {
251   SPlot2d_Prs* prs = new SPlot2d_Prs();
252   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
253   if( !study )
254     return prs;
255
256   _PTR(SObject) TableSO = study->studyDS()->FindObjectID( t->GetEntry() );
257
258   if( !TableSO )
259     return prs;
260
261   _PTR(ChildIterator) Iter = study->studyDS()->NewChildIterator( TableSO );
262   for( ; Iter->More(); Iter->Next() )
263   {
264     CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
265     if( !CORBA::is_nil( childObject ) )
266     {
267       CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
268       if( !CORBA::is_nil( aCurve ) )
269       {
270         VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
271         addCurve( prs, wnd, theCurve );
272       }
273     }
274   }
275   if( prs->getObjects().count()==0 )
276   {
277     delete prs;
278     prs = 0;
279   }
280   return prs;
281 }
282
283 bool VisuGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
284 {
285   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
286   if( !study )
287     return false;
288
289   _PTR(SObject) obj = study->studyDS()->FindObjectID( (const char*)entry.toLatin1() );
290   CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
291   if( CORBA::is_nil( anObj ) )
292     return false;
293
294   if( study->isComponent( entry ) )
295     return true;
296
297   if( dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in() )
298       ||
299       dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in() ) )
300     return true;
301
302   if( viewer_type==SVTK_Viewer::Type() /*|| viewer_type==VVTK_Viewer::Type()*/)
303   {
304     VISU::Base_i* aBase = dynamic_cast<VISU::Base_i*>(VISU::GetServant(anObj).in());
305
306     if ( !aBase )
307       return false;
308
309     if(aBase->GetType() == VISU::TCOLOREDPRS3DHOLDER)
310       return true;
311
312     if(aBase->GetType() == VISU::TPOINTMAP3D)
313       return dynamic_cast<VISU::PointMap3d_i*>(aBase);
314
315     return dynamic_cast<VISU::Prs3d_i*>(aBase);
316   }
317   else if( viewer_type==SPlot2d_Viewer::Type() )
318   {
319     VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant( anObj ).in() );
320     return aTable;
321   }
322   else 
323     return false;
324 }