Salome HOME
17475e574e830a7cba7375fb8f8105f7ec3a67c9
[modules/gui.git] / src / LightApp / LightApp_Displayer.cxx
1
2 #include "LightApp_Displayer.h"
3 #include "LightApp_Application.h"
4 #include "LightApp_Module.h"
5
6 #include <CAM_Study.h>
7
8 #include <SALOME_InteractiveObject.hxx>
9
10 #include <SUIT_Session.h>
11 #include <SUIT_Desktop.h>
12 #include <SUIT_ViewManager.h>
13 #include <SUIT_ViewModel.h>
14 #include <SUIT_ViewWindow.h>
15
16 #include <qstring.h>
17
18 LightApp_Displayer::LightApp_Displayer()
19 {
20 }
21
22 LightApp_Displayer::~LightApp_Displayer()
23 {
24 }
25
26 void LightApp_Displayer::Display( const QString& entry, const bool updateViewer, SALOME_View* theViewFrame )
27 {
28   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
29   if ( vf )
30   {
31     SALOME_Prs* prs = buildPresentation( entry, vf );
32
33     if ( prs )
34     {
35       vf->BeforeDisplay( this );
36       vf->Display( prs );
37       vf->AfterDisplay( this );
38
39       if ( updateViewer )
40         vf->Repaint();
41
42       delete prs;  // delete presentation because displayer is its owner
43     }
44   }
45 }
46
47 void LightApp_Displayer::Redisplay( const QString& entry, const bool updateViewer )
48 {
49   // Remove the object permanently (<forced> == true)
50   SUIT_Session* ses = SUIT_Session::session();
51   SUIT_Application* app = ses->activeApplication();
52   if ( app )
53   {
54     SUIT_Desktop* desk = app->desktop();
55     QPtrList<SUIT_ViewWindow> wnds = desk->windows();
56     SUIT_ViewWindow* wnd;
57     for ( wnd = wnds.first(); wnd; wnd = wnds.next() )
58     {
59       SUIT_ViewManager* vman = wnd->getViewManager();
60       if( !vman )
61         continue;
62
63       SUIT_ViewModel* vmodel = vman->getViewModel();
64       if( !vmodel )
65         continue;
66         
67       SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
68       if( view && ( IsDisplayed( entry, view ) || view == GetActiveView() ) )
69       {
70         Erase( entry, true, false, view );
71         Display( entry, updateViewer, view );
72       }
73     }
74   }
75 }
76
77 void LightApp_Displayer::Erase( const QString& entry, const bool forced,
78                                 const bool updateViewer, SALOME_View* theViewFrame )
79 {
80   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
81
82   if ( vf ) {
83     SALOME_Prs* prs = vf->CreatePrs( entry.latin1() );
84     if ( prs ) {
85       vf->Erase( prs, forced );
86       if ( updateViewer )
87         vf->Repaint();
88       delete prs;  // delete presentation because displayer is its owner
89     }
90   }
91 }
92
93 void LightApp_Displayer::EraseAll( const bool forced, const bool updateViewer, SALOME_View* theViewFrame ) const
94 {
95   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
96
97   if ( vf ) {
98     vf->EraseAll( forced );
99     if ( updateViewer )
100       vf->Repaint();
101   }
102 }
103
104 bool LightApp_Displayer::IsDisplayed( const QString& entry, SALOME_View* theViewFrame ) const
105 {
106   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
107   if( vf )
108   {
109     Handle( SALOME_InteractiveObject ) temp = new SALOME_InteractiveObject();
110     temp->setEntry( entry.latin1() );
111     return vf->isVisible( temp );
112   }
113   else
114     return false;
115 }
116
117 void LightApp_Displayer::UpdateViewer() const
118 {
119   SALOME_View* vf = GetActiveView();
120   if ( vf )
121     vf->Repaint();
122 }
123
124 SALOME_Prs* LightApp_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
125 {
126   SALOME_Prs* prs = 0;
127
128   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
129
130   if ( vf )
131     prs = vf->CreatePrs( entry.latin1() );
132
133   return prs;
134 }
135
136 SALOME_View* LightApp_Displayer::GetActiveView()
137 {
138   SUIT_Session* session = SUIT_Session::session();
139   if (  SUIT_Application* app = session->activeApplication() ) {
140     if ( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) ) {
141       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
142         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
143           return dynamic_cast<SALOME_View*>( vmod );
144       }
145     }
146   }
147   return 0;
148 }
149
150 bool LightApp_Displayer::canBeDisplayed( const QString&, const QString& ) const
151 {
152   return true;
153 }
154
155 bool LightApp_Displayer::canBeDisplayed( const QString& entry ) const
156 {
157   QString viewerType;
158   SUIT_Session* session = SUIT_Session::session();
159   if(  SUIT_Application* app = session->activeApplication() )
160     if( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) )
161       if( SUIT_ViewManager* vman = sApp->activeViewManager() )
162         if( SUIT_ViewModel* vmod = vman->getViewModel() )
163           viewerType = vmod->getType();
164   return !viewerType.isNull() && canBeDisplayed( entry, viewerType );
165 }
166
167 LightApp_Displayer* LightApp_Displayer::FindDisplayer( const QString& mod_name, const bool load )
168 {
169   SUIT_Session* session = SUIT_Session::session();
170   SUIT_Application* sapp = session ? session->activeApplication() : 0;
171   LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
172   if( !app )
173     return 0;
174
175   LightApp_Module* m = dynamic_cast<LightApp_Module*>( app ? app->module( mod_name ) : 0 );
176   if( !m && load )
177   {
178     m = dynamic_cast<LightApp_Module*>( app->loadModule( mod_name ) );
179     if( m )
180       app->addModule( m );
181   }
182
183   if( m )
184   {
185     m->connectToStudy( dynamic_cast<CAM_Study*>( app->activeStudy() ) );
186     if( m!=app->activeModule() && load )
187     {
188       m->setMenuShown( false );
189       m->setToolShown( false );
190     }
191   }
192   return m ? m->displayer() : 0;
193 }