Salome HOME
Unicode support: correct handling of unicode on GUI level
[modules/gui.git] / src / LightApp / LightApp_Displayer.cxx
1 // Copyright (C) 2007-2016  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( this, 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   Redisplays object in view
105   \param entry - object entry
106   \param updateViewer - is it necessary to update viewer
107 */
108 void LightApp_Displayer::Redisplay( const QString& entry, const bool updateViewer )
109 {
110   // Remove the object permanently (<forced> == true)
111   SUIT_Session* ses = SUIT_Session::session();
112   SUIT_Application* app = ses->activeApplication();
113   if ( app )
114   {
115     SUIT_Desktop* desk = app->desktop();
116     QListIterator<SUIT_ViewWindow*> itWnds( desk->windows() );
117     while ( itWnds.hasNext() )
118     {
119       SUIT_ViewManager* vman = itWnds.next()->getViewManager();
120       if( !vman )
121         continue;
122
123       SUIT_ViewModel* vmodel = vman->getViewModel();
124       if( !vmodel )
125         continue;
126         
127       SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
128       if( view && ( IsDisplayed( entry, view ) || view == GetActiveView() ) )
129       {
130         Erase( entry, true, false, view );
131         Display( entry, updateViewer, view );
132       }
133     }
134   }
135 }
136
137 /*!
138   Erases object in view
139   \param entry - object entry
140   \param forced - deletes object from viewer (otherwise it will be erased, but cached)
141   \param updateViewer - is it necessary to update viewer
142   \param theViewFrame - view
143 */
144 void LightApp_Displayer::Erase( const QString& entry, const bool forced,
145                                 const bool updateViewer,
146                                 SALOME_View* theViewFrame )
147 {
148   QStringList aList;
149   aList.append( entry );
150   Erase( aList, forced, updateViewer, theViewFrame );
151 }
152
153 /*!
154   Erases object in view
155   \param list - object entries
156   \param forced - deletes object from viewer (otherwise it will be erased, but cached)
157   \param updateViewer - is it necessary to update viewer
158   \param theViewFrame - view
159 */
160 void LightApp_Displayer::Erase( const QStringList& list, const bool forced,
161                                 const bool updateViewer,
162                                 SALOME_View* theViewFrame )
163 {
164   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
165
166   if ( !vf )
167     return;
168
169   QStringList::const_iterator it = list.constBegin();
170   for ( ; it != list.constEnd(); ++it)
171   {
172     SALOME_Prs* prs = vf->CreatePrs( (*it).toUtf8().data() );
173     if ( prs ) {
174       myLastEntry = *it;
175       vf->BeforeErase( this, prs );
176       vf->Erase( this, prs, forced );
177       vf->AfterErase( this, prs );
178       if ( updateViewer )
179         vf->Repaint();
180       delete prs;  // delete presentation because displayer is its owner
181       setVisibilityState(*it,Qtx::HiddenState);
182     }
183   }
184 }
185
186 /*!
187   Erases all objects in view
188   \param forced - deletes objects from viewer
189   \param updateViewer - is it necessary to update viewer
190   \param theViewFrame - view
191 */
192 void LightApp_Displayer::EraseAll( const bool forced, const bool updateViewer, SALOME_View* theViewFrame )
193 {
194   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
195
196   if ( vf ) {
197     vf->EraseAll( this, forced );
198     if ( updateViewer )
199       vf->Repaint();
200   }
201
202   LightApp_Application* app = dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
203   LightApp_Study* study = app ? dynamic_cast<LightApp_Study*>( app->activeStudy() ) : 0;  
204   if(study)
205     study->setVisibilityStateForAll(Qtx::HiddenState);
206 }
207
208 /*!
209   \return true if object is displayed in viewer
210   \param entry - object entry
211   \param theViewFrame - view
212 */
213 bool LightApp_Displayer::IsDisplayed( const QString& entry, SALOME_View* theViewFrame ) const
214 {
215   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
216   bool res = false;
217   if( vf )
218   {
219 #ifndef DISABLE_SALOMEOBJECT
220     Handle( SALOME_InteractiveObject ) temp = new SALOME_InteractiveObject();
221     temp->setEntry( entry.toUtf8() );
222     res = vf->isVisible( temp );
223 #endif
224   }
225   return res;
226 }
227
228 /*!
229   Updates active view
230 */
231 void LightApp_Displayer::UpdateViewer() const
232 {
233   SALOME_View* vf = GetActiveView();
234   if ( vf )
235     vf->Repaint();
236 }
237
238 /*!
239   \return presentation of object, built with help of CreatePrs method
240   \param entry - object entry
241   \param theViewFrame - view
242   \sa CreatePrs()
243 */
244 SALOME_Prs* LightApp_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
245 {
246   SALOME_Prs* prs = 0;
247
248   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
249
250   if ( vf )
251     prs = vf->CreatePrs( entry.toUtf8() );
252
253   return prs;
254 }
255
256 /*!
257   \return active view
258 */
259 SALOME_View* LightApp_Displayer::GetActiveView()
260 {
261   SUIT_Session* session = SUIT_Session::session();
262   if (  SUIT_Application* app = session->activeApplication() ) {
263     if ( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) ) {
264       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
265         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
266           return dynamic_cast<SALOME_View*>( vmod );
267       }
268     }
269   }
270   return 0;
271 }
272
273 /*!
274   \return true, if object can be displayed in this type of viewer
275   \param entry - object entry
276   \param viewer_type - type of viewer
277 */
278 bool LightApp_Displayer::canBeDisplayed( const QString& /*entry*/, const QString& /*viewer_type*/ ) const
279 {
280   return true;
281 }
282
283 /*!
284   \return true, if object can be displayed in any type of viewer
285   \param entry - object entry
286 */
287 bool LightApp_Displayer::canBeDisplayed( const QString& entry ) const
288 {
289   QString viewerType;
290   SUIT_Session* session = SUIT_Session::session();
291   if(  SUIT_Application* app = session->activeApplication() )
292     if( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) )
293       if( SUIT_ViewManager* vman = sApp->activeViewManager() )
294         if( SUIT_ViewModel* vmod = vman->getViewModel() )
295           viewerType = vmod->getType();
296   return canBeDisplayed( entry, viewerType );
297 }
298
299 /*!
300   \return displayer, corresponding to module
301   \param mod_name - name of module
302   \param load - is module has to be forced loaded
303 */
304 LightApp_Displayer* LightApp_Displayer::FindDisplayer( const QString& mod_name, const bool load )
305 {
306   SUIT_Session* session = SUIT_Session::session();
307   SUIT_Application* sapp = session ? session->activeApplication() : 0;
308   LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
309   if( !app )
310     return 0;
311
312   LightApp_Module* m = dynamic_cast<LightApp_Module*>( app ? app->module( mod_name ) : 0 );
313   bool wasLoaded = false;
314   if( !m && load )
315   {
316     m = dynamic_cast<LightApp_Module*>( app->loadModule( mod_name, false ) );
317         if( m ) {
318       app->addModule( m );
319           wasLoaded = true;
320         }
321   }
322
323   if( m )
324   {
325     m->connectToStudy( dynamic_cast<CAM_Study*>( app->activeStudy() ) );
326         if( wasLoaded ) 
327                 m->updateModuleVisibilityState();
328   }
329   return m ? m->displayer() : 0;
330 }
331
332 /*!
333   Find the active study and set the 'visibility state' property of the object
334   \param mod_name - name of module
335   \param load - is module has to be forced loaded
336 */
337 void LightApp_Displayer::setVisibilityState( const QString& theEntry, Qtx::VisibilityState theState) const {
338   LightApp_Application* app = dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
339   LightApp_Study* study = app ? dynamic_cast<LightApp_Study*>( app->activeStudy() ) : 0;
340   
341   if(study)
342     study->setVisibilityState( theEntry, theState);
343 }