1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxDockWidget.cxx
23 // Author: Sergey TELKOV
25 #include "QtxDockWidget.h"
29 #include <QMainWindow>
30 #include <QResizeEvent>
31 #include <QApplication>
34 \class QtxDockWidget::Watcher
36 \brief Internal class which goal is to watch parent dockable widget state changing.
39 class QtxDockWidget::Watcher : public QObject
42 Watcher( QtxDockWidget* );
44 void shown( QtxDockWidget* );
45 void hidden( QtxDockWidget* );
47 virtual bool eventFilter( QObject*, QEvent* );
50 bool isVisible() const;
53 enum { Update = QEvent::User, Remove };
55 virtual void customEvent( QEvent* );
58 void installFilters();
65 void updateVisibility();
67 void setEmpty( const bool );
68 void setVisible( const bool );
71 QtxDockWidget* myCont;
80 \param cont dockable widget to be watched
82 QtxDockWidget::Watcher::Watcher( QtxDockWidget* cont )
83 : QObject( cont ), myCont( cont ),
88 myCont->installEventFilter( this );
92 myShown = myCont->isVisibleTo( myCont->parentWidget() );
96 \brief Custom event filter.
97 \param o event receiver object
98 \param e event sent to object
99 \return \c true if further event processing should be stopped
101 bool QtxDockWidget::Watcher::eventFilter( QObject* o, QEvent* e )
103 if ( o == myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
104 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) )
109 if ( o == myCont && e->type() == QEvent::ChildAdded )
111 QChildEvent* ce = (QChildEvent*)e;
112 if ( ce->child()->isWidgetType() )
113 ce->child()->installEventFilter( this );
115 QApplication::postEvent( this, new QEvent( (QEvent::Type)Update ) );
118 if ( o != myCont && e->type() == QEvent::WindowIconChange )
121 if ( o != myCont && e->type() == QEvent::WindowTitleChange )
124 if ( o != myCont && ( e->type() == QEvent::HideToParent || e->type() == QEvent::ShowToParent ) )
127 if ( o == myCont && e->type() == QEvent::ChildRemoved )
129 QApplication::postEvent( this, new QEvent( (QEvent::Type)Remove ) );
136 \brief Set internal status to "shown"
137 \param dw dockable widget
139 void QtxDockWidget::Watcher::shown( QtxDockWidget* dw )
148 \brief Set internal status to "hidden"
149 \param dw dockable widget
151 void QtxDockWidget::Watcher::hidden( QtxDockWidget* dw )
159 bool QtxDockWidget::Watcher::isEmpty() const
164 bool QtxDockWidget::Watcher::isVisible() const
169 void QtxDockWidget::Watcher::setEmpty( const bool on )
174 void QtxDockWidget::Watcher::setVisible( const bool on )
180 \brief Show the dock window being watched
182 void QtxDockWidget::Watcher::showContainer()
187 bool vis = isVisible();
189 QtxDockWidget* cont = myCont;
198 \brief Hide the dock window being watched
200 void QtxDockWidget::Watcher::hideContainer()
205 bool vis = isVisible();
207 QtxDockWidget* cont = myCont;
216 \brief Proces custom events.
217 \param e custom event (not used)
219 void QtxDockWidget::Watcher::customEvent( QEvent* e )
221 if ( e->type() == Update )
227 else if ( myCont && e->type() == Remove && !myCont->widget() )
229 myCont->deleteLater();
235 \brief Install this object as event filter to all children widgets
236 of the dockable widget being watched.
238 void QtxDockWidget::Watcher::installFilters()
243 QLayout* l = myCont->layout();
247 for ( int i = 0; i < (int)l->count(); i++ )
249 if ( l->itemAt( i ) && l->itemAt( i )->widget() )
250 l->itemAt( i )->widget()->installEventFilter( this );
255 \brief Update visibility state of all children widgets of the dockable widget
258 void QtxDockWidget::Watcher::updateVisibility()
264 if ( myCont->widget() )
265 vis = myCont->widget()->isVisibleTo( myCont );
268 QLayout* l = myCont->layout();
271 for ( int i = 0; i < (int)l->count() && !vis; i++ )
272 vis = l->itemAt( i ) && l->itemAt( i )->widget() && l->itemAt( i )->widget()->isVisibleTo( myCont );
276 bool empty = isEmpty();
282 myCont->toggleViewAction()->setVisible( myState );
285 myState = myCont->toggleViewAction()->isVisible();
286 myCont->toggleViewAction()->setVisible( false );
290 vis = !empty && isVisible();
291 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
292 vis ? showContainer() : hideContainer();
296 \brief Update the icon of dockable window being watched
298 void QtxDockWidget::Watcher::updateIcon()
300 if ( !myCont || !myCont->widget() || myBlock )
304 myCont->setWindowIcon( myCont->widget()->windowIcon() );
309 \brief Update the title of dockable window being watched
311 void QtxDockWidget::Watcher::updateCaption()
313 if ( myCont && myCont->widget() && !myCont->widget()->windowTitle().isNull() )
314 myCont->setWindowTitle( myCont->widget()->windowTitle() );
319 \brief Enhanced dockable widget class.
324 \param title dockable widget title
325 \param parent parent widget
326 \param f widget flags
328 QtxDockWidget::QtxDockWidget( const QString& title, QWidget* parent, Qt::WindowFlags f )
329 : QDockWidget( title, parent, f ),
331 myOrientation( (Qt::Orientation)-1 )
338 \param watch if \c true the event filter is installed to watch wigdet state changes
339 to update it properly
340 \param parent parent widget
341 \param f widget flags
343 QtxDockWidget::QtxDockWidget( const bool watch, QWidget* parent, Qt::WindowFlags f )
344 : QDockWidget( parent, f ),
346 myOrientation( (Qt::Orientation)-1 )
349 myWatcher = new Watcher( this );
356 \param parent parent widget
357 \param f widget flags
359 QtxDockWidget::QtxDockWidget( QWidget* parent, Qt::WindowFlags f )
360 : QDockWidget( parent, f ),
368 QtxDockWidget::~QtxDockWidget()
373 \brief Get the recommended size for the widget.
374 \return recommended dockable widget size
376 QSize QtxDockWidget::sizeHint() const
378 QSize sz = QDockWidget::sizeHint();
380 printf( "----------------> QtxDockWidget::sizeHint()\n" );
382 return QSize( 500, 100 );
388 \brief Get the recommended minimum size for the widget.
389 \return recommended dockable widget minimum size
391 QSize QtxDockWidget::minimumSizeHint() const
393 QSize sz = QDockWidget::minimumSizeHint();
399 \brief Show/hide the dockable window.
400 \param on new visibility state
402 void QtxDockWidget::setVisible( bool on )
406 widget()->updateGeometry();
408 QDockWidget::setVisible( on && ( myWatcher ? !myWatcher->isEmpty() : true ) );
413 myWatcher->shown( this );
415 myWatcher->hidden( this );
420 \brief Process resize event
423 void QtxDockWidget::resizeEvent( QResizeEvent* e )
425 QDockWidget::resizeEvent( e );
430 \brief Get dockable window orientation.
431 \return orientation type
433 Qt::Orientation QtxDockWidget::orientation() const
436 QWidget* wid = parentWidget();
439 mw = ::qobject_cast<QMainWindow*>( wid );
440 wid = wid->parentWidget();
443 Qt::Orientation res = (Qt::Orientation)-1;
448 Qt::DockWidgetArea area = mw->dockWidgetArea( (QtxDockWidget*)this );
451 case Qt::LeftDockWidgetArea:
452 case Qt::RightDockWidgetArea:
455 case Qt::TopDockWidgetArea:
456 case Qt::BottomDockWidgetArea:
457 res = Qt::Horizontal;
467 \brief Update dockable window state.
469 void QtxDockWidget::updateState()
471 Qt::Orientation o = orientation();
472 if ( myOrientation == o )
477 emit orientationChanged( myOrientation );
481 \fn QtxDockWidget::orientationChanged(Qt::Orientation o)
482 \brief Emitted when the dockable window orientation is changed.
483 \param o new window orientation