Salome HOME
6e2caeda24bf2c99f986ef446788bf4064ce4b3f
[modules/gui.git] / src / SalomeApp / SalomeApp_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 "SalomeApp_Displayer.h"
21 #include "SalomeApp_Application.h"
22
23 #include <SALOME_InteractiveObject.hxx>
24
25 #include <SUIT_Session.h>
26 #include <SUIT_Desktop.h>
27 #include <SUIT_ViewManager.h>
28 #include <SUIT_ViewModel.h>
29 #include <SUIT_ViewWindow.h>
30
31 #include <qstring.h>
32
33 SalomeApp_Displayer::SalomeApp_Displayer()
34 {
35 }
36
37 SalomeApp_Displayer::~SalomeApp_Displayer()
38 {
39 }
40
41 void SalomeApp_Displayer::Display( const QString& entry, const bool updateViewer, SALOME_View* theViewFrame )
42 {
43   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
44   if ( vf )
45   {
46     SALOME_Prs* prs = buildPresentation( entry, vf );
47
48     if ( prs )
49     {
50       vf->BeforeDisplay( this );
51       vf->Display( prs );
52       vf->AfterDisplay( this );
53
54       if ( updateViewer )
55         vf->Repaint();
56
57       delete prs;  // delete presentation because displayer is its owner
58     }
59   }
60 }
61
62 void SalomeApp_Displayer::Redisplay( const QString& entry, const bool updateViewer )
63 {
64   // Remove the object permanently (<forced> == true)
65   SUIT_Session* ses = SUIT_Session::session();
66   SUIT_Application* app = ses->activeApplication();
67   if ( app )
68   {
69     SUIT_Desktop* desk = app->desktop();
70     QPtrList<SUIT_ViewWindow> wnds = desk->windows();
71     SUIT_ViewWindow* wnd;
72     for ( wnd = wnds.first(); wnd; wnd = wnds.next() )
73     {
74       SUIT_ViewManager* vman = wnd->getViewManager();
75       if( !vman )
76         continue;
77
78       SUIT_ViewModel* vmodel = vman->getViewModel();
79       if( !vmodel )
80         continue;
81         
82       SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
83       if( view && ( IsDisplayed( entry, view ) || view == GetActiveView() ) )
84       {
85         Erase( entry, true, false, view );
86         Display( entry, updateViewer, view );
87       }
88     }
89   }
90 }
91
92 void SalomeApp_Displayer::Erase( const QString& entry, const bool forced,
93                                  const bool updateViewer, SALOME_View* theViewFrame )
94 {
95   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
96
97   if ( vf ) {
98     SALOME_Prs* prs = vf->CreatePrs( entry.latin1() );
99     if ( prs ) {
100       vf->Erase( prs, forced );
101       if ( updateViewer )
102         vf->Repaint();
103       delete prs;  // delete presentation because displayer is its owner
104     }
105   }
106 }
107
108 void SalomeApp_Displayer::EraseAll( const bool forced, const bool updateViewer, SALOME_View* theViewFrame ) const
109 {
110   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
111
112   if ( vf ) {
113     vf->EraseAll( forced );
114     if ( updateViewer )
115       vf->Repaint();
116   }
117 }
118
119 bool SalomeApp_Displayer::IsDisplayed( const QString& entry, SALOME_View* theViewFrame ) const
120 {
121   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
122   if( vf )
123   {
124     Handle( SALOME_InteractiveObject ) temp = new SALOME_InteractiveObject();
125     temp->setEntry( entry.latin1() );
126     return vf->isVisible( temp );
127   }
128   else
129     return false;
130 }
131
132 void SalomeApp_Displayer::UpdateViewer() const
133 {
134   SALOME_View* vf = GetActiveView();
135   if ( vf )
136     vf->Repaint();
137 }
138
139 SALOME_Prs* SalomeApp_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
140 {
141   SALOME_Prs* prs = 0;
142
143   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
144
145   if ( vf )
146     prs = vf->CreatePrs( entry.latin1() );
147
148   return prs;
149 }
150
151 SALOME_View* SalomeApp_Displayer::GetActiveView()
152 {
153   SUIT_Session* session = SUIT_Session::session();
154   if (  SUIT_Application* app = session->activeApplication() ) {
155     if ( SalomeApp_Application* sApp = dynamic_cast<SalomeApp_Application*>( app ) ) {
156       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
157         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
158           return dynamic_cast<SALOME_View*>( vmod );
159       }
160     }
161   }
162   return 0;
163 }