Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / LightApp / LightApp_Displayer.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 #include "LightApp_Displayer.h"
23 #include "LightApp_Application.h"
24 #include "LightApp_Module.h"
25
26 #include <CAM_Study.h>
27
28 #include <SUIT_Session.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_ViewManager.h>
31 #include <SUIT_ViewModel.h>
32 #include <SUIT_ViewWindow.h>
33
34 #include <QString>
35 #ifndef DISABLE_SALOMEOBJECT
36   #include "SALOME_InteractiveObject.hxx"
37 #endif
38
39 /*!
40   Default constructor
41 */
42 LightApp_Displayer::LightApp_Displayer()
43 {
44 }
45
46 /*!
47   Destructor
48 */
49 LightApp_Displayer::~LightApp_Displayer()
50 {
51 }
52
53 /*!
54   Displays object in view
55   \param entry - object entry
56   \param updateViewer - is it necessary to update viewer
57   \param theViewFrame - view
58 */
59 void LightApp_Displayer::Display( const QString& entry, const bool updateViewer, SALOME_View* theViewFrame )
60 {
61   SALOME_Prs* prs = buildPresentation( entry, theViewFrame );
62   if ( prs )
63   {
64     SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
65     if ( vf )
66     {
67       vf->BeforeDisplay( this );
68       vf->Display( prs );
69       vf->AfterDisplay( this );
70
71       if ( updateViewer )
72         vf->Repaint();
73
74       delete prs;  // delete presentation because displayer is its owner
75     }
76   }
77 }
78
79 /*!
80   Redisplays object in view
81   \param entry - object entry
82   \param updateViewer - is it necessary to update viewer
83 */
84 void LightApp_Displayer::Redisplay( const QString& entry, const bool updateViewer )
85 {
86   // Remove the object permanently (<forced> == true)
87   SUIT_Session* ses = SUIT_Session::session();
88   SUIT_Application* app = ses->activeApplication();
89   if ( app )
90   {
91     SUIT_Desktop* desk = app->desktop();
92     QListIterator<SUIT_ViewWindow*> itWnds( desk->windows() );
93     while ( itWnds.hasNext() )
94     {
95       SUIT_ViewManager* vman = itWnds.next()->getViewManager();
96       if( !vman )
97         continue;
98
99       SUIT_ViewModel* vmodel = vman->getViewModel();
100       if( !vmodel )
101         continue;
102         
103       SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
104       if( view && ( IsDisplayed( entry, view ) || view == GetActiveView() ) )
105       {
106         Erase( entry, true, false, view );
107         Display( entry, updateViewer, view );
108       }
109     }
110   }
111 }
112
113 /*!
114   Erases object in view
115   \param entry - object entry
116   \param forced - deletes object from viewer (otherwise it will be erased, but cached)
117   \param updateViewer - is it necessary to update viewer
118   \param theViewFrame - view
119 */
120 void LightApp_Displayer::Erase( const QString& entry, const bool forced,
121                                 const bool updateViewer, SALOME_View* theViewFrame )
122 {
123   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
124
125   if ( vf ) {
126     SALOME_Prs* prs = vf->CreatePrs( entry.toLatin1() );
127     if ( prs ) {
128       vf->Erase( prs, forced );
129       if ( updateViewer )
130         vf->Repaint();
131       delete prs;  // delete presentation because displayer is its owner
132     }
133   }
134 }
135
136 /*!
137   Erases all objects in view
138   \param forced - deletes objects from viewer
139   \param updateViewer - is it necessary to update viewer
140   \param theViewFrame - view
141 */
142 void LightApp_Displayer::EraseAll( const bool forced, const bool updateViewer, SALOME_View* theViewFrame ) const
143 {
144   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
145
146   if ( vf ) {
147     vf->EraseAll( forced );
148     if ( updateViewer )
149       vf->Repaint();
150   }
151 }
152
153 /*!
154   \return true if object is displayed in viewer
155   \param entry - object entry
156   \param theViewFrame - view
157 */
158 bool LightApp_Displayer::IsDisplayed( const QString& entry, SALOME_View* theViewFrame ) const
159 {
160   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
161   bool res = false;
162   if( vf )
163   {
164 #ifndef DISABLE_SALOMEOBJECT
165     Handle( SALOME_InteractiveObject ) temp = new SALOME_InteractiveObject();
166     temp->setEntry( entry.toLatin1() );
167     res = vf->isVisible( temp );
168 #endif
169   }
170   return res;
171 }
172
173 /*!
174   Updates active view
175 */
176 void LightApp_Displayer::UpdateViewer() const
177 {
178   SALOME_View* vf = GetActiveView();
179   if ( vf )
180     vf->Repaint();
181 }
182
183 /*!
184   \return presentation of object, built with help of CreatePrs method
185   \param entry - object entry
186   \param theViewFrame - view
187   \sa CreatePrs()
188 */
189 SALOME_Prs* LightApp_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
190 {
191   SALOME_Prs* prs = 0;
192
193   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
194
195   if ( vf )
196     prs = vf->CreatePrs( entry.toLatin1() );
197
198   return prs;
199 }
200
201 /*!
202   \return active view
203 */
204 SALOME_View* LightApp_Displayer::GetActiveView()
205 {
206   SUIT_Session* session = SUIT_Session::session();
207   if (  SUIT_Application* app = session->activeApplication() ) {
208     if ( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) ) {
209       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
210         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
211           return dynamic_cast<SALOME_View*>( vmod );
212       }
213     }
214   }
215   return 0;
216 }
217
218 /*!
219   \return true, if object can be displayed in this type of viewer
220   \param entry - object entry
221   \param viewer_type - type of viewer
222 */
223 bool LightApp_Displayer::canBeDisplayed( const QString& /*entry*/, const QString& /*viewer_type*/ ) const
224 {
225   return true;
226 }
227
228 /*!
229   \return true, if object can be displayed in any type of viewer
230   \param entry - object entry
231 */
232 bool LightApp_Displayer::canBeDisplayed( const QString& entry ) const
233 {
234   QString viewerType;
235   SUIT_Session* session = SUIT_Session::session();
236   if(  SUIT_Application* app = session->activeApplication() )
237     if( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) )
238       if( SUIT_ViewManager* vman = sApp->activeViewManager() )
239         if( SUIT_ViewModel* vmod = vman->getViewModel() )
240           viewerType = vmod->getType();
241   return canBeDisplayed( entry, viewerType );
242 }
243
244 /*!
245   \return displayer, corresponding to module
246   \param mod_name - name of module
247   \param load - is module has to be forced loaded
248 */
249 LightApp_Displayer* LightApp_Displayer::FindDisplayer( const QString& mod_name, const bool load )
250 {
251   SUIT_Session* session = SUIT_Session::session();
252   SUIT_Application* sapp = session ? session->activeApplication() : 0;
253   LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
254   if( !app )
255     return 0;
256
257   LightApp_Module* m = dynamic_cast<LightApp_Module*>( app ? app->module( mod_name ) : 0 );
258   if( !m && load )
259   {
260     m = dynamic_cast<LightApp_Module*>( app->loadModule( mod_name, false ) );
261     if( m )
262       app->addModule( m );
263   }
264
265   if( m )
266   {
267     m->connectToStudy( dynamic_cast<CAM_Study*>( app->activeStudy() ) );
268     if( m!=app->activeModule() && load )
269     {
270       m->setMenuShown( false );
271       m->setToolShown( false );
272     }
273   }
274   return m ? m->displayer() : 0;
275 }