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