]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxDockWindow.cxx
Salome HOME
9872d850582570bdc35511f2b5f71899678f32ca
[modules/gui.git] / src / Qtx / QtxDockWindow.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 // File:      QtxDockWindow.cxx
20 // Author:    Sergey TELKOV
21
22 #include "QtxDockWindow.h"
23
24 #include <qlayout.h>
25 #include <qpixmap.h>
26 #include <qdockarea.h>
27 #include <qmainwindow.h>
28 #include <qapplication.h>
29
30 /*!
31     Class: QtxDockWindow::Watcher [Internal]
32     Descr: Internal object with event filter.
33 */
34
35 class QtxDockWindow::Watcher : public QObject
36 {
37 public:
38   Watcher( QtxDockWindow* );
39
40   void           shown( QtxDockWindow* );
41   void           hided( QtxDockWindow* );
42
43   virtual bool   eventFilter( QObject*, QEvent* );
44
45 protected:
46   virtual void   customEvent( QCustomEvent* );
47
48 private:
49   void           installFilters();
50
51   void           showContainer();
52   void           hideContainer();
53
54   void           updateIcon();
55   void           updateCaption();
56   void           updateVisibility();
57
58 private:
59   QtxDockWindow* myCont;
60   bool           myState;
61   bool           myEmpty;
62   bool           myVisible;
63 };
64
65 QtxDockWindow::Watcher::Watcher( QtxDockWindow* cont )
66 : QObject( cont ), myCont( cont ),
67 myState( true ),
68 myEmpty( true )
69 {
70   if ( myCont->mainWindow() )
71     myState = myCont->mainWindow()->appropriate( myCont );
72
73   myCont->installEventFilter( this );
74   myVisible = myCont->isVisibleTo( myCont->parentWidget() );
75
76   installFilters();
77 }
78
79 bool QtxDockWindow::Watcher::eventFilter( QObject* o, QEvent* e )
80 {
81   if ( o == myCont &&
82        ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
83          e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ||
84          e->type() == QEvent::ChildInserted ) )
85     QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
86
87   if ( o != myCont && e->type() == QEvent::IconChange )
88     updateIcon();
89
90   if ( o != myCont && e->type() == QEvent::CaptionChange )
91     updateCaption();
92
93   if ( ( o != myCont && ( e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
94        ( o == myCont && ( e->type() == QEvent::ChildRemoved ) ) ||
95        ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) )
96     updateVisibility();
97
98   return false;
99 }
100
101 void QtxDockWindow::Watcher::shown( QtxDockWindow* dw )
102 {
103   if ( dw != myCont )
104     return;
105
106   myVisible = true;
107 }
108
109 void QtxDockWindow::Watcher::hided( QtxDockWindow* dw )
110 {
111   if ( dw != myCont )
112     return;
113
114   myVisible = false;
115 }
116
117 void QtxDockWindow::Watcher::showContainer()
118 {
119   if ( !myCont )
120     return;
121
122   QtxDockWindow* cont = myCont;
123   myCont = 0;
124   cont->show();
125   myCont = cont;
126 }
127
128 void QtxDockWindow::Watcher::hideContainer()
129 {
130   if ( !myCont )
131     return;
132
133   QtxDockWindow* cont = myCont;
134   myCont = 0;
135   cont->hide();
136   myCont = cont;
137 }
138
139 void QtxDockWindow::Watcher::customEvent( QCustomEvent* e )
140 {
141   installFilters();
142
143   updateIcon();
144   updateCaption();
145   updateVisibility();
146 }
147
148 void QtxDockWindow::Watcher::installFilters()
149 {
150   if ( !myCont )
151     return;
152
153   QBoxLayout* bl = myCont->boxLayout();
154   if ( !bl )
155     return;
156
157   for ( QLayoutIterator it = bl->iterator(); it.current(); ++it )
158   {
159     if ( it.current()->widget() )
160       it.current()->widget()->installEventFilter( this );
161   }
162 }
163
164 void QtxDockWindow::Watcher::updateVisibility()
165 {
166   if ( !myCont )
167     return;
168
169   QBoxLayout* bl = myCont->boxLayout();
170   if ( !bl )
171     return;
172
173   bool vis = false;
174   for ( QLayoutIterator it = bl->iterator(); it.current() && !vis; ++it )
175     vis = it.current()->widget() && it.current()->widget()->isVisibleTo( myCont );
176
177   QMainWindow* mw = myCont->mainWindow();
178   if ( mw && myEmpty == vis )
179   {
180     myEmpty = !vis;
181     if ( !myEmpty )
182       mw->setAppropriate( myCont, myState );
183     else
184     {
185       myState = mw->appropriate( myCont );
186       mw->setAppropriate( myCont, false );
187     }
188   }
189
190   vis = !myEmpty && myVisible;
191   if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
192     vis ? showContainer() : hideContainer();
193 }
194
195 void QtxDockWindow::Watcher::updateIcon()
196 {
197   if ( !myCont || !myCont->widget() )
198     return;
199   
200   const QPixmap* ico = myCont->widget()->icon();
201   myCont->setIcon( ico ? *ico : QPixmap() );
202 }
203
204 void QtxDockWindow::Watcher::updateCaption()
205 {
206   if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
207     myCont->setCaption( myCont->widget()->caption() );
208 }
209
210 /*!
211     Class: QtxDockWindow [Public]
212     Descr: 
213 */
214
215 QtxDockWindow::QtxDockWindow( Place p, QWidget* parent, const char* name, WFlags f )
216 : QDockWindow( p, parent, name, f ),
217 myWatcher( 0 ),
218 myStretch( false )
219 {
220 }
221
222 QtxDockWindow::QtxDockWindow( const bool watch, QWidget* parent, const char* name, WFlags f )
223 : QDockWindow( InDock, parent, name, f ),
224 myWatcher( 0 ),
225 myStretch( false )
226 {
227   if ( watch )
228     myWatcher = new Watcher( this );
229 }
230
231 QtxDockWindow::QtxDockWindow( QWidget* parent, const char* name, WFlags f )
232 : QDockWindow( InDock, parent, name, f ),
233 myWatcher( 0 ),
234 myStretch( false )
235 {
236 }
237
238 QtxDockWindow::~QtxDockWindow()
239 {
240 }
241
242 void QtxDockWindow::setWidget( QWidget* wid )
243 {
244   if ( wid )
245     wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
246
247   QDockWindow::setWidget( wid );
248 }
249
250 bool QtxDockWindow::isStretchable() const
251 {
252   return myStretch;
253 }
254
255 void QtxDockWindow::setStretchable( const bool on )
256 {
257   if ( myStretch == on )
258     return;
259
260   myStretch = on;
261
262   boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
263
264   if ( myStretch != isHorizontalStretchable() ||
265        myStretch != isVerticalStretchable() )
266   {
267           if ( orientation() == Horizontal )
268             setHorizontalStretchable( myStretch );
269           else
270             setVerticalStretchable( myStretch );
271   }
272 }
273
274 QSize QtxDockWindow::sizeHint() const
275 {
276   QSize sz = QDockWindow::sizeHint();
277
278   if ( place() == InDock && isStretchable() && area() )
279   {
280     if ( orientation() == Horizontal )
281       sz.setWidth( area()->width() );
282     else
283       sz.setHeight( area()->height() );
284   }
285
286   return sz;
287 }
288
289 QSize QtxDockWindow::minimumSizeHint() const
290 {
291   QSize sz = QDockWindow::minimumSizeHint();
292
293   if ( orientation() == Horizontal )
294           sz = QSize( 0, QDockWindow::minimumSizeHint().height() );
295   else
296     sz = QSize( QDockWindow::minimumSizeHint().width(), 0 );
297
298   if ( place() == InDock && isStretchable() && area() )
299   {
300     if ( orientation() == Horizontal )
301       sz.setWidth( area()->width() );
302     else
303       sz.setHeight( area()->height() );
304   }
305
306   return sz;
307 }
308
309 QMainWindow* QtxDockWindow::mainWindow() const
310 {
311   QMainWindow* mw = 0;
312
313   QWidget* wid = parentWidget();
314   while ( !mw && wid )
315   {
316     if ( wid->inherits( "QMainWindow" ) )
317       mw = (QMainWindow*)wid;
318     wid = wid->parentWidget();
319   }
320
321   return mw;
322 }
323
324 void QtxDockWindow::show()
325 {
326   if ( myWatcher )
327     myWatcher->shown( this );
328
329   QDockWindow::show();
330 }
331
332 void QtxDockWindow::hide()
333 {
334   if ( myWatcher )
335     myWatcher->hided( this );
336
337   QDockWindow::hide();
338 }