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