Salome HOME
f06c8c10b16fa0b1df29c11da46c25d7ce82f545
[modules/gui.git] / src / LightApp / LightApp_Displayer.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19
20 #include "LightApp_Displayer.h"
21 #include "LightApp_Application.h"
22 #include "LightApp_Module.h"
23
24 #include <CAM_Study.h>
25
26 #include <SUIT_Session.h>
27 #include <SUIT_Desktop.h>
28 #include <SUIT_ViewManager.h>
29 #include <SUIT_ViewModel.h>
30 #include <SUIT_ViewWindow.h>
31
32 #include <qstring.h>
33 #ifndef DISABLE_SALOMEOBJECT
34   #include "SALOME_InteractiveObject.hxx"
35 #endif
36
37 LightApp_Displayer::LightApp_Displayer()
38 {
39 }
40
41 LightApp_Displayer::~LightApp_Displayer()
42 {
43 }
44
45 void LightApp_Displayer::Display( const QString& entry, const bool updateViewer, SALOME_View* theViewFrame )
46 {
47   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
48   if ( vf )
49   {
50     SALOME_Prs* prs = buildPresentation( entry, vf );
51
52     if ( prs )
53     {
54       vf->BeforeDisplay( this );
55       vf->Display( prs );
56       vf->AfterDisplay( this );
57
58       if ( updateViewer )
59         vf->Repaint();
60
61       delete prs;  // delete presentation because displayer is its owner
62     }
63   }
64 }
65
66 void LightApp_Displayer::Redisplay( const QString& entry, const bool updateViewer )
67 {
68   // Remove the object permanently (<forced> == true)
69   SUIT_Session* ses = SUIT_Session::session();
70   SUIT_Application* app = ses->activeApplication();
71   if ( app )
72   {
73     SUIT_Desktop* desk = app->desktop();
74     QPtrList<SUIT_ViewWindow> wnds = desk->windows();
75     SUIT_ViewWindow* wnd;
76     for ( wnd = wnds.first(); wnd; wnd = wnds.next() )
77     {
78       SUIT_ViewManager* vman = wnd->getViewManager();
79       if( !vman )
80         continue;
81
82       SUIT_ViewModel* vmodel = vman->getViewModel();
83       if( !vmodel )
84         continue;
85         
86       SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
87       if( view && ( IsDisplayed( entry, view ) || view == GetActiveView() ) )
88       {
89         Erase( entry, true, false, view );
90         Display( entry, updateViewer, view );
91       }
92     }
93   }
94 }
95
96 void LightApp_Displayer::Erase( const QString& entry, const bool forced,
97                                 const bool updateViewer, SALOME_View* theViewFrame )
98 {
99   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
100
101   if ( vf ) {
102     SALOME_Prs* prs = vf->CreatePrs( entry.latin1() );
103     if ( prs ) {
104       vf->Erase( prs, forced );
105       if ( updateViewer )
106         vf->Repaint();
107       delete prs;  // delete presentation because displayer is its owner
108     }
109   }
110 }
111
112 void LightApp_Displayer::EraseAll( const bool forced, const bool updateViewer, SALOME_View* theViewFrame ) const
113 {
114   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
115
116   if ( vf ) {
117     vf->EraseAll( forced );
118     if ( updateViewer )
119       vf->Repaint();
120   }
121 }
122
123 bool LightApp_Displayer::IsDisplayed( const QString& entry, SALOME_View* theViewFrame ) const
124 {
125   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
126   bool res = false;
127   if( vf )
128   {
129 #ifndef DISABLE_SALOMEOBJECT
130     Handle( SALOME_InteractiveObject ) temp = new SALOME_InteractiveObject();
131     temp->setEntry( entry.latin1() );
132     res = vf->isVisible( temp );
133 #endif
134   }
135   return res;
136 }
137
138 void LightApp_Displayer::UpdateViewer() const
139 {
140   SALOME_View* vf = GetActiveView();
141   if ( vf )
142     vf->Repaint();
143 }
144
145 SALOME_Prs* LightApp_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
146 {
147   SALOME_Prs* prs = 0;
148
149   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
150
151   if ( vf )
152     prs = vf->CreatePrs( entry.latin1() );
153
154   return prs;
155 }
156
157 SALOME_View* LightApp_Displayer::GetActiveView()
158 {
159   SUIT_Session* session = SUIT_Session::session();
160   if (  SUIT_Application* app = session->activeApplication() ) {
161     if ( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) ) {
162       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
163         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
164           return dynamic_cast<SALOME_View*>( vmod );
165       }
166     }
167   }
168   return 0;
169 }
170
171 bool LightApp_Displayer::canBeDisplayed( const QString&, const QString& ) const
172 {
173   return true;
174 }
175
176 bool LightApp_Displayer::canBeDisplayed( const QString& entry ) const
177 {
178   QString viewerType;
179   SUIT_Session* session = SUIT_Session::session();
180   if(  SUIT_Application* app = session->activeApplication() )
181     if( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) )
182       if( SUIT_ViewManager* vman = sApp->activeViewManager() )
183         if( SUIT_ViewModel* vmod = vman->getViewModel() )
184           viewerType = vmod->getType();
185   return !viewerType.isNull() && canBeDisplayed( entry, viewerType );
186 }
187
188 LightApp_Displayer* LightApp_Displayer::FindDisplayer( const QString& mod_name, const bool load )
189 {
190   SUIT_Session* session = SUIT_Session::session();
191   SUIT_Application* sapp = session ? session->activeApplication() : 0;
192   LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
193   if( !app )
194     return 0;
195
196   LightApp_Module* m = dynamic_cast<LightApp_Module*>( app ? app->module( mod_name ) : 0 );
197   if( !m && load )
198   {
199     m = dynamic_cast<LightApp_Module*>( app->loadModule( mod_name ) );
200     if( m )
201       app->addModule( m );
202   }
203
204   if( m )
205   {
206     m->connectToStudy( dynamic_cast<CAM_Study*>( app->activeStudy() ) );
207     if( m!=app->activeModule() && load )
208     {
209       m->setMenuShown( false );
210       m->setToolShown( false );
211     }
212   }
213   return m ? m->displayer() : 0;
214 }