Salome HOME
Fix for problem with table of contents resizing.
[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/ or email : webmaster.salome@opencascade.com
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 /*!
34   Default constructor
35 */
36 SalomeApp_Displayer::SalomeApp_Displayer()
37 {
38 }
39
40 /*!
41   Destructor
42 */
43 SalomeApp_Displayer::~SalomeApp_Displayer()
44 {
45 }
46
47 /*!
48   Displays object in view
49   \param entry - object entry
50   \param updateViewer - is it necessary to update viewer
51   \param theViewFrame - view
52 */
53 void SalomeApp_Displayer::Display( const QString& entry, const bool updateViewer, SALOME_View* theViewFrame )
54 {
55   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
56   if ( vf )
57   {
58     SALOME_Prs* prs = buildPresentation( entry, vf );
59
60     if ( prs )
61     {
62       vf->BeforeDisplay( this );
63       vf->Display( prs );
64       vf->AfterDisplay( this );
65
66       if ( updateViewer )
67         vf->Repaint();
68
69       delete prs;  // delete presentation because displayer is its owner
70     }
71   }
72 }
73
74 /*!
75   Redisplays object in view
76   \param entry - object entry
77   \param updateViewer - is it necessary to update viewer
78 */
79 void SalomeApp_Displayer::Redisplay( const QString& entry, const bool updateViewer )
80 {
81   // Remove the object permanently (<forced> == true)
82   SUIT_Session* ses = SUIT_Session::session();
83   SUIT_Application* app = ses->activeApplication();
84   if ( app )
85   {
86     SUIT_Desktop* desk = app->desktop();
87     QPtrList<SUIT_ViewWindow> wnds = desk->windows();
88     SUIT_ViewWindow* wnd;
89     for ( wnd = wnds.first(); wnd; wnd = wnds.next() )
90     {
91       SUIT_ViewManager* vman = wnd->getViewManager();
92       if( !vman )
93         continue;
94
95       SUIT_ViewModel* vmodel = vman->getViewModel();
96       if( !vmodel )
97         continue;
98         
99       SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
100       if( view && ( IsDisplayed( entry, view ) || view == GetActiveView() ) )
101       {
102         Erase( entry, true, false, view );
103         Display( entry, updateViewer, view );
104       }
105     }
106   }
107 }
108
109 /*!
110   Erases object in view
111   \param entry - object entry
112   \param forced - deletes object from viewer (otherwise it will be erased, but cached)
113   \param updateViewer - is it necessary to update viewer
114   \param theViewFrame - view
115 */
116 void SalomeApp_Displayer::Erase( const QString& entry, const bool forced,
117                                  const bool updateViewer, SALOME_View* theViewFrame )
118 {
119   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
120
121   if ( vf ) {
122     SALOME_Prs* prs = vf->CreatePrs( entry.latin1() );
123     if ( prs ) {
124       vf->Erase( prs, forced );
125       if ( updateViewer )
126         vf->Repaint();
127       delete prs;  // delete presentation because displayer is its owner
128     }
129   }
130 }
131
132 /*!
133   Erases all objects in view
134   \param forced - deletes objects from viewer
135   \param updateViewer - is it necessary to update viewer
136   \param theViewFrame - view
137 */
138 void SalomeApp_Displayer::EraseAll( const bool forced, const bool updateViewer, SALOME_View* theViewFrame ) const
139 {
140   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
141
142   if ( vf ) {
143     vf->EraseAll( forced );
144     if ( updateViewer )
145       vf->Repaint();
146   }
147 }
148
149 /*!
150   \return true if object is displayed in viewer
151   \param entry - object entry
152   \param theViewFrame - view
153 */
154 bool SalomeApp_Displayer::IsDisplayed( const QString& entry, SALOME_View* theViewFrame ) const
155 {
156   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
157   if( vf )
158   {
159     Handle( SALOME_InteractiveObject ) temp = new SALOME_InteractiveObject();
160     temp->setEntry( entry.latin1() );
161     return vf->isVisible( temp );
162   }
163   else
164     return false;
165 }
166
167 /*!
168   Updates active view
169 */
170 void SalomeApp_Displayer::UpdateViewer() const
171 {
172   SALOME_View* vf = GetActiveView();
173   if ( vf )
174     vf->Repaint();
175 }
176
177 /*!
178   \return presentation of object, built with help of CreatePrs method
179   \param entry - object entry
180   \param theViewFrame - view
181   \sa CreatePrs()
182 */
183 SALOME_Prs* SalomeApp_Displayer::buildPresentation( const QString& entry, SALOME_View* theViewFrame )
184 {
185   SALOME_Prs* prs = 0;
186
187   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
188
189   if ( vf )
190     prs = vf->CreatePrs( entry.latin1() );
191
192   return prs;
193 }
194
195 /*!
196   \return active view
197 */
198 SALOME_View* SalomeApp_Displayer::GetActiveView()
199 {
200   SUIT_Session* session = SUIT_Session::session();
201   if (  SUIT_Application* app = session->activeApplication() ) {
202     if ( SalomeApp_Application* sApp = dynamic_cast<SalomeApp_Application*>( app ) ) {
203       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
204         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
205           return dynamic_cast<SALOME_View*>( vmod );
206       }
207     }
208   }
209   return 0;
210 }