1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File: QtxDockWindow.cxx
20 // Author: Sergey TELKOV
22 #include "QtxDockWindow.h"
26 #include <qdockarea.h>
27 #include <qmainwindow.h>
28 #include <qapplication.h>
31 \class QtxDockWindow::Watcher [Internal]
32 Internal object with event filter.
34 class QtxDockWindow::Watcher : public QObject
37 Watcher( QtxDockWindow* );
39 void shown( QtxDockWindow* );
40 void hided( QtxDockWindow* );
42 virtual bool eventFilter( QObject*, QEvent* );
45 virtual void customEvent( QCustomEvent* );
48 void installFilters();
55 void updateVisibility();
58 QtxDockWindow* myCont;
67 QtxDockWindow::Watcher::Watcher( QtxDockWindow* cont )
68 : QObject( cont ), myCont( cont ),
72 if ( myCont->mainWindow() )
73 myState = myCont->mainWindow()->appropriate( myCont );
75 myCont->installEventFilter( this );
76 myVisible = myCont->isVisibleTo( myCont->parentWidget() );
84 bool QtxDockWindow::Watcher::eventFilter( QObject* o, QEvent* e )
87 ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
88 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ||
89 e->type() == QEvent::ChildInserted ) )
90 QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
92 if ( o != myCont && e->type() == QEvent::IconChange )
95 if ( o != myCont && e->type() == QEvent::CaptionChange )
98 if ( ( o != myCont && ( e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
99 ( o == myCont && ( e->type() == QEvent::ChildRemoved ) ) ||
100 ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) )
107 Sets internal status to shown
109 void QtxDockWindow::Watcher::shown( QtxDockWindow* dw )
118 Sets internal status to hidden
120 void QtxDockWindow::Watcher::hided( QtxDockWindow* dw )
129 Shows corresponding dock window
131 void QtxDockWindow::Watcher::showContainer()
136 QtxDockWindow* cont = myCont;
143 Hides corresponding dock window
145 void QtxDockWindow::Watcher::hideContainer()
150 QtxDockWindow* cont = myCont;
157 Event filter of custom events
159 void QtxDockWindow::Watcher::customEvent( QCustomEvent* e )
169 Installs this object as event filter to all widgets inside corresponding main window
171 void QtxDockWindow::Watcher::installFilters()
176 QBoxLayout* bl = myCont->boxLayout();
180 for ( QLayoutIterator it = bl->iterator(); it.current(); ++it )
182 if ( it.current()->widget() )
183 it.current()->widget()->installEventFilter( this );
188 Updates visibility of all widgets inside corresponding main window
190 void QtxDockWindow::Watcher::updateVisibility()
195 QBoxLayout* bl = myCont->boxLayout();
200 for ( QLayoutIterator it = bl->iterator(); it.current() && !vis; ++it )
201 vis = it.current()->widget() && it.current()->widget()->isVisibleTo( myCont );
203 QMainWindow* mw = myCont->mainWindow();
204 if ( mw && myEmpty == vis )
208 mw->setAppropriate( myCont, myState );
211 myState = mw->appropriate( myCont );
212 mw->setAppropriate( myCont, false );
216 vis = !myEmpty && myVisible;
217 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
218 vis ? showContainer() : hideContainer();
222 Updates icon of corresponding main window
224 void QtxDockWindow::Watcher::updateIcon()
226 if ( !myCont || !myCont->widget() )
229 const QPixmap* ico = myCont->widget()->icon();
230 myCont->setIcon( ico ? *ico : QPixmap() );
234 Updates caption of corresponding main window
236 void QtxDockWindow::Watcher::updateCaption()
238 if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
239 myCont->setCaption( myCont->widget()->caption() );
245 QtxDockWindow::QtxDockWindow( Place p, QWidget* parent, const char* name, WFlags f )
246 : QDockWindow( p, parent, name, f ),
255 QtxDockWindow::QtxDockWindow( const bool watch, QWidget* parent, const char* name, WFlags f )
256 : QDockWindow( InDock, parent, name, f ),
261 myWatcher = new Watcher( this );
267 QtxDockWindow::QtxDockWindow( QWidget* parent, const char* name, WFlags f )
268 : QDockWindow( InDock, parent, name, f ),
277 QtxDockWindow::~QtxDockWindow()
282 Sets the dock window's main widget
283 \param wid - new main widget
285 void QtxDockWindow::setWidget( QWidget* wid )
288 wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
290 QDockWindow::setWidget( wid );
294 \return true if the dock window is stretchable
296 bool QtxDockWindow::isStretchable() const
302 Sets the dock window "stretchable" state
303 \param on - new state
305 void QtxDockWindow::setStretchable( const bool on )
307 if ( myStretch == on )
312 boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
314 if ( myStretch != isHorizontalStretchable() ||
315 myStretch != isVerticalStretchable() )
317 if ( orientation() == Horizontal )
318 setHorizontalStretchable( myStretch );
320 setVerticalStretchable( myStretch );
325 \return the recommended size for the widget
327 QSize QtxDockWindow::sizeHint() const
329 QSize sz = QDockWindow::sizeHint();
331 if ( place() == InDock && isStretchable() && area() )
333 if ( orientation() == Horizontal )
334 sz.setWidth( area()->width() );
336 sz.setHeight( area()->height() );
343 \return the recommended minimum size for the widget
345 QSize QtxDockWindow::minimumSizeHint() const
347 QSize sz = QDockWindow::minimumSizeHint();
349 if ( orientation() == Horizontal )
350 sz = QSize( 0, QDockWindow::minimumSizeHint().height() );
352 sz = QSize( QDockWindow::minimumSizeHint().width(), 0 );
354 if ( place() == InDock && isStretchable() && area() )
356 if ( orientation() == Horizontal )
357 sz.setWidth( area()->width() );
359 sz.setHeight( area()->height() );
366 \return corresponding main window
368 QMainWindow* QtxDockWindow::mainWindow() const
372 QWidget* wid = parentWidget();
375 if ( wid->inherits( "QMainWindow" ) )
376 mw = (QMainWindow*)wid;
377 wid = wid->parentWidget();
386 void QtxDockWindow::show()
389 myWatcher->shown( this );
397 void QtxDockWindow::hide()
400 myWatcher->hided( this );