Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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& entry, SALOME_View* theViewFrame )
60 {
61   SALOME_Prs* prs = 0;
62
63   SALOME_View* aViewFrame = theViewFrame ? theViewFrame : GetActiveView();
64   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
65
66   if ( study && aViewFrame )
67   {
68     _PTR(SObject) obj = study->studyDS()->FindObjectID( entry.latin1() );
69     CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
70     if( CORBA::is_nil( anObj ) )
71       return 0;
72
73     SVTK_Viewer* vtk_viewer = dynamic_cast<VVTK_Viewer*>( aViewFrame );
74     if (!vtk_viewer)
75       vtk_viewer = dynamic_cast<SVTK_Viewer*>( aViewFrame );
76     if( vtk_viewer )
77     {
78       SVTK_ViewWindow* wnd =
79         dynamic_cast<SVTK_ViewWindow*>( vtk_viewer->getViewManager()->getActiveView() );
80       if( wnd )
81       {
82         VISU::Prs3d_i* thePrs = dynamic_cast<VISU::Prs3d_i*>( VISU::GetServant( anObj ).in() );
83         if( thePrs )
84         {
85           buildPrs3d( wnd, thePrs );
86           prs = LightApp_Displayer::buildPresentation( entry, aViewFrame );
87         }
88       }
89     }
90
91     SPlot2d_Viewer* plot_viewer = dynamic_cast<SPlot2d_Viewer*>( aViewFrame );
92     if( plot_viewer )
93     {
94       Plot2d_ViewWindow* wnd =
95         dynamic_cast<Plot2d_ViewWindow*>( plot_viewer->getViewManager()->getActiveView() );
96       if( !wnd )
97         return 0;
98
99       VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in() );
100       SPlot2d_Prs* iprs = 0;
101       if( aCurve )
102         iprs = buildCurve( wnd, aCurve );
103
104       VISU::Container_i* aCont = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in() );
105       if( aCont )
106         iprs = buildContainer( wnd, aCont );
107
108       VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant( anObj ).in() );
109       if( aTable )
110         iprs = buildTable( wnd, aTable );
111
112       if( iprs )
113         prs = new SPlot2d_Prs( iprs );
114
115       if( prs )
116         UpdatePrs( prs );
117     }
118   }
119   return prs;
120 }
121
122 void VisuGUI_Displayer::buildPrs3d( SVTK_ViewWindow* wnd, VISU::Prs3d_i* thePrs ) const
123 {
124   VISU_Actor* newAct = VISU::FindActor(wnd, thePrs);
125   if (!newAct) {
126     try {
127       newAct = thePrs->CreateActor();
128     } catch (std::runtime_error& exc) {
129       thePrs->RemoveActors();
130
131       INFOS(exc.what());
132       SUIT_MessageBox::warn1
133         (myApp->desktop(), QObject::tr("WRN_VISU"),
134          QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
135          QObject::tr("BUT_OK"));
136     }
137   }
138   if (newAct) {
139     wnd->AddActor(newAct);
140     wnd->Repaint();
141   }
142 }
143
144 bool VisuGUI_Displayer::addCurve( SPlot2d_Prs* prs, Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
145 {
146   if( !prs || !wnd || !c )
147     return false;
148
149   QString entry = c->GetSObject()->GetID();
150   SPlot2d_Viewer* vv = dynamic_cast<SPlot2d_Viewer*>( wnd->getModel() );
151   if( !vv )
152     return false;
153
154   SPlot2d_Curve* curve = vv->getCurveByIO( vv->FindIObject( entry.latin1() ) );
155   if( !curve )
156   {
157     curve = c->CreatePresentation();
158     VISU::UpdateCurve( c, 0, curve, VISU::eDisplay );
159   }
160   if( curve )
161     prs->AddObject( curve );
162
163   return curve!=0;
164 }
165
166 SPlot2d_Prs* VisuGUI_Displayer::buildCurve( Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
167 {
168   SPlot2d_Prs* prs = new SPlot2d_Prs();
169   if( !addCurve( prs, wnd, c ) )
170   {
171     delete prs;
172     prs = 0;
173   }
174   return prs;
175 }
176
177 SPlot2d_Prs* VisuGUI_Displayer::buildContainer( Plot2d_ViewWindow* wnd, VISU::Container_i* c ) const
178 {
179   SPlot2d_Prs* prs = new SPlot2d_Prs();
180
181   int nbCurves = c ? c->GetNbCurves() : 0;
182   for( int k=1; k<=nbCurves; k++ )
183   {
184     VISU::Curve_i* theCurve = c->GetCurve( k );
185     if( theCurve && theCurve->IsValid() )
186       addCurve( prs, wnd, theCurve );
187   }
188   if( prs->getCurves().count()==0 )
189   {
190     delete prs;
191     prs = 0;
192   }
193   return prs;
194 }
195
196 SPlot2d_Prs* VisuGUI_Displayer::buildTable( Plot2d_ViewWindow* wnd, VISU::Table_i* t ) const
197 {
198   SPlot2d_Prs* prs = new SPlot2d_Prs();
199   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
200   if( !study )
201     return prs;
202
203   _PTR(SObject) TableSO = study->studyDS()->FindObjectID( t->GetEntry().latin1() );
204
205   if( !TableSO )
206     return prs;
207
208   _PTR(ChildIterator) Iter = study->studyDS()->NewChildIterator( TableSO );
209   for( ; Iter->More(); Iter->Next() )
210   {
211     CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
212     if( !CORBA::is_nil( childObject ) )
213     {
214       CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
215       if( !CORBA::is_nil( aCurve ) )
216       {
217         VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
218         addCurve( prs, wnd, theCurve );
219       }
220     }
221   }
222   if( prs->getCurves().count()==0 )
223   {
224     delete prs;
225     prs = 0;
226   }
227   return prs;
228 }
229
230 bool VisuGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
231 {
232   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
233   if( !study )
234     return false;
235
236   _PTR(SObject) obj = study->studyDS()->FindObjectID( entry.latin1() );
237   CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
238   if( CORBA::is_nil( anObj ) )
239     return false;
240
241   if( study->isComponent( entry ) )
242     return true;
243
244   if( viewer_type==SVTK_Viewer::Type() || viewer_type==VVTK_Viewer::Type())
245   {
246     VISU::Prs3d_i* thePrs = dynamic_cast<VISU::Prs3d_i*>( VISU::GetServant( anObj ).in() );
247     return thePrs;
248   }
249   else if( viewer_type==SPlot2d_Viewer::Type() )
250   {
251     VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in() );
252     VISU::Container_i* aCont = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in() );
253     VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant( anObj ).in() );
254     return aCurve || aCont || aTable;
255   }
256   else 
257     return false;
258 }