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