Salome HOME
Fix for Bug IPAL8945
[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 #include "VVTK_ViewModel.h"
31 #include "VisuGUI_Tools.h"
32 #include "VISU_Actor.h"
33
34 #include <SVTK_ViewModel.h>
35 #include <SVTK_ViewWindow.h>
36
37 #include <SPlot2d_ViewModel.h>
38 #include <SPlot2d_Prs.h>
39 #include <Plot2d_ViewWindow.h>
40
41 #include <SalomeApp_Application.h>
42 #include <SalomeApp_Study.h>
43
44 #include "VISU_ViewManager_i.hh"
45
46 VisuGUI_Displayer::VisuGUI_Displayer( SalomeApp_Application* app )
47 : LightApp_Displayer(),
48   myApp( app )
49 {
50 }
51
52 VisuGUI_Displayer::~VisuGUI_Displayer()
53 {
54 }
55
56 SALOME_Prs* VisuGUI_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
57 {
58   SALOME_Prs* prs = 0;
59
60   SALOME_View* aViewFrame = theViewFrame ? theViewFrame : GetActiveView();
61   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
62
63   if ( study && aViewFrame )
64   {
65     _PTR(SObject) obj = study->studyDS()->FindObjectID( entry.latin1() );
66     CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
67     if( CORBA::is_nil( anObj ) )
68       return 0;
69
70     SVTK_Viewer* vtk_viewer = dynamic_cast<SVTK_Viewer*>( aViewFrame );
71     if( vtk_viewer )
72     {
73       SVTK_ViewWindow* wnd =
74         dynamic_cast<SVTK_ViewWindow*>( vtk_viewer->getViewManager()->getActiveView() );
75       if( wnd )
76       {
77         VISU::Prs3d_i* thePrs = dynamic_cast<VISU::Prs3d_i*>( VISU::GetServant( anObj ).in() );
78         if( thePrs )
79         {
80           buildPrs3d( wnd, thePrs );
81           prs = LightApp_Displayer::buildPresentation( entry, aViewFrame );
82         }
83       }
84     }
85
86     SPlot2d_Viewer* plot_viewer = dynamic_cast<SPlot2d_Viewer*>( aViewFrame );
87     if( plot_viewer )
88     {
89       Plot2d_ViewWindow* wnd =
90         dynamic_cast<Plot2d_ViewWindow*>( plot_viewer->getViewManager()->getActiveView() );
91       if( !wnd )
92         return 0;
93
94       VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in() );
95       SPlot2d_Prs* iprs = 0;
96       if( aCurve )
97         iprs = buildCurve( wnd, aCurve );
98
99       VISU::Container_i* aCont = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in() );
100       if( aCont )
101         iprs = buildContainer( wnd, aCont );
102
103       VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant( anObj ).in() );
104       if( aTable )
105         iprs = buildTable( wnd, aTable );
106
107       if( iprs )
108         prs = new SPlot2d_Prs( iprs );
109
110       if( prs )
111         UpdatePrs( prs );
112     }
113   }
114   return prs;
115 }
116
117 void VisuGUI_Displayer::buildPrs3d( SVTK_ViewWindow* wnd, VISU::Prs3d_i* thePrs ) const
118 {
119   VISU_Actor* newAct = VISU::FindActor( wnd, thePrs );
120   if( !newAct )
121   {
122     newAct = thePrs->CreateActor();
123   }
124   if( newAct )
125   {
126     wnd->AddActor( newAct );
127     wnd->Repaint();
128   }
129 }
130
131 bool VisuGUI_Displayer::addCurve( SPlot2d_Prs* prs, Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
132 {
133   if( !prs || !wnd || !c )
134     return false;
135
136   QString entry = c->GetSObject()->GetID();
137   SPlot2d_Viewer* vv = dynamic_cast<SPlot2d_Viewer*>( wnd->getModel() );
138   if( !vv )
139     return false;
140
141   SPlot2d_Curve* curve = vv->getCurveByIO( vv->FindIObject( entry.latin1() ) );
142   if( !curve )
143   {
144     curve = c->CreatePresentation();
145     VISU::UpdateCurve( c, 0, curve, VISU::eDisplay );
146   }
147   if( curve )
148     prs->AddObject( curve );
149
150   return curve!=0;
151 }
152
153 SPlot2d_Prs* VisuGUI_Displayer::buildCurve( Plot2d_ViewWindow* wnd, VISU::Curve_i* c ) const
154 {
155   SPlot2d_Prs* prs = new SPlot2d_Prs();
156   if( !addCurve( prs, wnd, c ) )
157   {
158     delete prs;
159     prs = 0;
160   }
161   return prs;
162 }
163
164 SPlot2d_Prs* VisuGUI_Displayer::buildContainer( Plot2d_ViewWindow* wnd, VISU::Container_i* c ) const
165 {
166   SPlot2d_Prs* prs = new SPlot2d_Prs();
167
168   int nbCurves = c ? c->GetNbCurves() : 0;
169   for( int k=1; k<=nbCurves; k++ )
170   {
171     VISU::Curve_i* theCurve = c->GetCurve( k );
172     if( theCurve && theCurve->IsValid() )
173       addCurve( prs, wnd, theCurve );
174   }
175   if( prs->getCurves().count()==0 )
176   {
177     delete prs;
178     prs = 0;
179   }
180   return prs;
181 }
182
183 SPlot2d_Prs* VisuGUI_Displayer::buildTable( Plot2d_ViewWindow* wnd, VISU::Table_i* t ) const
184 {
185   SPlot2d_Prs* prs = new SPlot2d_Prs();
186   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
187   if( !study )
188     return prs;
189
190   _PTR(SObject) TableSO = study->studyDS()->FindObjectID( t->GetEntry().latin1() );
191
192   if( !TableSO )
193     return prs;
194
195   _PTR(ChildIterator) Iter = study->studyDS()->NewChildIterator( TableSO );
196   for( ; Iter->More(); Iter->Next() )
197   {
198     CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
199     if( !CORBA::is_nil( childObject ) )
200     {
201       CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
202       if( !CORBA::is_nil( aCurve ) )
203       {
204         VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
205         addCurve( prs, wnd, theCurve );
206       }
207     }
208   }
209   if( prs->getCurves().count()==0 )
210   {
211     delete prs;
212     prs = 0;
213   }
214   return prs;
215 }
216
217 bool VisuGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
218 {
219   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
220   if( !study )
221     return false;
222
223   _PTR(SObject) obj = study->studyDS()->FindObjectID( entry.latin1() );
224   CORBA::Object_var anObj = VISU::ClientSObjectToObject( obj );
225   if( CORBA::is_nil( anObj ) )
226     return false;
227
228   if( study->isComponent( entry ) )
229     return true;
230
231   if( viewer_type==SVTK_Viewer::Type() || viewer_type==VVTK_Viewer::Type())
232   {
233     VISU::Prs3d_i* thePrs = dynamic_cast<VISU::Prs3d_i*>( VISU::GetServant( anObj ).in() );
234     return thePrs;
235   }
236   else if( viewer_type==SPlot2d_Viewer::Type() )
237   {
238     VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>( VISU::GetServant( anObj ).in() );
239     VISU::Container_i* aCont = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in() );
240     VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant( anObj ).in() );
241     return aCurve || aCont || aTable;
242   }
243   else 
244     return false;
245 }