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