Salome HOME
cde6ceadf9a2e25c4f0ccc312e17d371905211d1
[modules/gui.git] / src / LightApp / LightApp_Displayer.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 #include "LightApp_Displayer.h"
24 #include "LightApp_Application.h"
25 #include "LightApp_Module.h"
26 #include "LightApp_Study.h"
27
28 #include <CAM_Study.h>
29
30 #include <SUIT_Session.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_ViewManager.h>
33 #include <SUIT_ViewModel.h>
34 #include <SUIT_ViewWindow.h>
35
36 #include <QStringList>
37 #include <QString>
38 #ifndef DISABLE_SALOMEOBJECT
39   #include "SALOME_InteractiveObject.hxx"
40 #endif
41
42 /*!
43   Default constructor
44 */
45 LightApp_Displayer::LightApp_Displayer()
46 {
47 }
48
49 /*!
50   Destructor
51 */
52 LightApp_Displayer::~LightApp_Displayer()
53 {
54 }
55
56 /*!
57   Displays object in view
58   \param entry - object entry
59   \param updateViewer - is it necessary to update viewer
60   \param theViewFrame - view
61 */
62 void LightApp_Displayer::Display( const QString& entry, const bool updateViewer,
63                                   SALOME_View* theViewFrame )
64 {
65   QStringList aList;
66   aList.append( entry );
67   Display( aList, updateViewer, theViewFrame );
68 }
69
70 /*!
71   Displays object in view
72   \param list - object entries
73   \param updateViewer - is it necessary to update viewer
74   \param theViewFrame - view
75 */
76 void LightApp_Displayer::Display( const QStringList& list, const bool updateViewer,
77                                   SALOME_View* theViewFrame )
78 {
79   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
80   QStringList::const_iterator it = list.constBegin();
81   for ( ; it != list.constEnd(); ++it)
82   {
83     SALOME_Prs* prs = buildPresentation( *it, vf );
84     if ( prs )
85     {
86
87       if ( vf )
88       {
89         myLastEntry = *it;
90         vf->BeforeDisplay( this, prs );
91         vf->Display( prs );
92         vf->AfterDisplay( this, prs );
93
94         if ( updateViewer )
95           vf->Repaint();
96       }
97       delete prs;  // delete presentation because displayer is its owner
98       setVisibilityState(*it, Qtx::ShownState);
99     }
100
101   }
102 }
103
104 /*!
105   Redisplays object in view
106   \param entry - object entry
107   \param updateViewer - is it necessary to update viewer
108 */
109 void LightApp_Displayer::Redisplay( const QString& entry, const bool updateViewer )
110 {
111   // Remove the object permanently (<forced> == true)
112   SUIT_Session* ses = SUIT_Session::session();
113   SUIT_Application* app = ses->activeApplication();
114   if ( app )
115   {
116     SUIT_Desktop* desk = app->desktop();
117     QListIterator<SUIT_ViewWindow*> itWnds( desk->windows() );
118     while ( itWnds.hasNext() )
119     {
120       SUIT_ViewManager* vman = itWnds.next()->getViewManager();
121       if( !vman )
122         continue;
123
124       SUIT_ViewModel* vmodel = vman->getViewModel();
125       if( !vmodel )
126         continue;
127         
128       SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
129       if( view && ( IsDisplayed( entry, view ) || view == GetActiveView() ) )
130       {
131         Erase( entry, true, false, view );
132         Display( entry, updateViewer, view );
133       }
134     }
135   }
136 }
137
138 /*!
139   Erases object in view
140   \param entry - object entry
141   \param forced - deletes object from viewer (otherwise it will be erased, but cached)
142   \param updateViewer - is it necessary to update viewer
143   \param theViewFrame - view
144 */
145 void LightApp_Displayer::Erase( const QString& entry, const bool forced,
146                                 const bool updateViewer,
147                                 SALOME_View* theViewFrame )
148 {
149   QStringList aList;
150   aList.append( entry );
151   Erase( aList, forced, updateViewer, theViewFrame );
152 }
153
154 /*!
155   Erases object in view
156   \param list - object entries
157   \param forced - deletes object from viewer (otherwise it will be erased, but cached)
158   \param updateViewer - is it necessary to update viewer
159   \param theViewFrame - view
160 */
161 void LightApp_Displayer::Erase( const QStringList& list, const bool forced,
162                                 const bool updateViewer,
163                                 SALOME_View* theViewFrame )
164 {
165   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
166
167   if ( !vf )
168     return;
169
170   QStringList::const_iterator it = list.constBegin();
171   for ( ; it != list.constEnd(); ++it)
172   {
173     SALOME_Prs* prs = vf->CreatePrs( (*it).toLatin1().data() );
174     if ( prs ) {
175       myLastEntry = *it;
176       vf->BeforeErase( this, prs );
177       vf->Erase( prs, forced );
178       vf->AfterErase( this, prs );
179       if ( updateViewer )
180         vf->Repaint();
181       delete prs;  // delete presentation because displayer is its owner
182       setVisibilityState(*it,Qtx::HiddenState);
183     }
184   }
185 }
186
187 /*!
188   Erases all objects in view
189   \param forced - deletes objects from viewer
190   \param updateViewer - is it necessary to update viewer
191   \param theViewFrame - view
192 */
193 void LightApp_Displayer::EraseAll( const bool forced, const bool updateViewer, SALOME_View* theViewFrame ) const
194 {
195   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
196
197   if ( vf ) {
198     vf->EraseAll( forced );
199     if ( updateViewer )
200       vf->Repaint();
201   }
202
203   LightApp_Application* app = dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
204   LightApp_Study* study = app ? dynamic_cast<LightApp_Study*>( app->activeStudy() ) : 0;  
205   if(study)
206     study->setVisibilityStateForAll(Qtx::HiddenState);
207 }
208
209 /*!
210   \return true if object is displayed in viewer
211   \param entry - object entry
212   \param theViewFrame - view
213 */
214 bool LightApp_Displayer::IsDisplayed( const QString& entry, SALOME_View* theViewFrame ) const
215 {
216   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
217   bool res = false;
218   if( vf )
219   {
220 #ifndef DISABLE_SALOMEOBJECT
221     Handle( SALOME_InteractiveObject ) temp = new SALOME_InteractiveObject();
222     temp->setEntry( entry.toLatin1() );
223     res = vf->isVisible( temp );
224 #endif
225   }
226   return res;
227 }
228
229 /*!
230   Updates active view
231 */
232 void LightApp_Displayer::UpdateViewer() const
233 {
234   SALOME_View* vf = GetActiveView();
235   if ( vf )
236     vf->Repaint();
237 }
238
239 /*!
240   \return presentation of object, built with help of CreatePrs method
241   \param entry - object entry
242   \param theViewFrame - view
243   \sa CreatePrs()
244 */
245 SALOME_Prs* LightApp_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
246 {
247   SALOME_Prs* prs = 0;
248
249   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
250
251   if ( vf )
252     prs = vf->CreatePrs( entry.toLatin1() );
253
254   return prs;
255 }
256
257 /*!
258   \return active view
259 */
260 SALOME_View* LightApp_Displayer::GetActiveView()
261 {
262   SUIT_Session* session = SUIT_Session::session();
263   if (  SUIT_Application* app = session->activeApplication() ) {
264     if ( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) ) {
265       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
266         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
267           return dynamic_cast<SALOME_View*>( vmod );
268       }
269     }
270   }
271   return 0;
272 }
273
274 /*!
275   \return true, if object can be displayed in this type of viewer
276   \param entry - object entry
277   \param viewer_type - type of viewer
278 */
279 bool LightApp_Displayer::canBeDisplayed( const QString& /*entry*/, const QString& /*viewer_type*/ ) const
280 {
281   return true;
282 }
283
284 /*!
285   \return true, if object can be displayed in any type of viewer
286   \param entry - object entry
287 */
288 bool LightApp_Displayer::canBeDisplayed( const QString& entry ) const
289 {
290   QString viewerType;
291   SUIT_Session* session = SUIT_Session::session();
292   if(  SUIT_Application* app = session->activeApplication() )
293     if( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) )
294       if( SUIT_ViewManager* vman = sApp->activeViewManager() )
295         if( SUIT_ViewModel* vmod = vman->getViewModel() )
296           viewerType = vmod->getType();
297   return canBeDisplayed( entry, viewerType );
298 }
299
300 /*!
301   \return displayer, corresponding to module
302   \param mod_name - name of module
303   \param load - is module has to be forced loaded
304 */
305 LightApp_Displayer* LightApp_Displayer::FindDisplayer( const QString& mod_name, const bool load )
306 {
307   SUIT_Session* session = SUIT_Session::session();
308   SUIT_Application* sapp = session ? session->activeApplication() : 0;
309   LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
310   if( !app )
311     return 0;
312
313   LightApp_Module* m = dynamic_cast<LightApp_Module*>( app ? app->module( mod_name ) : 0 );
314   bool wasLoaded = false;
315   if( !m && load )
316   {
317     m = dynamic_cast<LightApp_Module*>( app->loadModule( mod_name, false ) );
318         if( m ) {
319       app->addModule( m );
320           wasLoaded = true;
321         }
322   }
323
324   if( m )
325   {
326     m->connectToStudy( dynamic_cast<CAM_Study*>( app->activeStudy() ) );
327         if( wasLoaded ) 
328                 m->updateModuleVisibilityState();
329   }
330   return m ? m->displayer() : 0;
331 }
332
333 /*!
334   Find the active study and set the 'visibility state' property of the object
335   \param mod_name - name of module
336   \param load - is module has to be forced loaded
337 */
338 void LightApp_Displayer::setVisibilityState( const QString& theEntry, Qtx::VisibilityState theState) const {
339   LightApp_Application* app = dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
340   LightApp_Study* study = app ? dynamic_cast<LightApp_Study*>( app->activeStudy() ) : 0;
341   
342   if(study)
343     study->setVisibilityState( theEntry, theState);
344 }