1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
23 // File: QtxDockWidget.cxx
24 // Author: Sergey TELKOV
26 #include "QtxDockWidget.h"
30 #include <QMainWindow>
31 #include <QResizeEvent>
32 #include <QApplication>
35 \class QtxDockWidget::Watcher
37 \brief Internal class which goal is to watch parent dockable widget state changing.
40 class QtxDockWidget::Watcher : public QObject
43 Watcher( QtxDockWidget* );
45 void shown( QtxDockWidget* );
46 void hidden( QtxDockWidget* );
48 virtual bool eventFilter( QObject*, QEvent* );
51 bool isVisible() const;
54 enum { Update = QEvent::User, Remove };
56 virtual void customEvent( QEvent* );
59 void installFilters();
66 void updateVisibility();
68 void setEmpty( const bool );
69 void setVisible( const bool );
72 QtxDockWidget* myCont;
81 \param cont dockable widget to be watched
83 QtxDockWidget::Watcher::Watcher( QtxDockWidget* cont )
84 : QObject( cont ), myCont( cont ),
89 myCont->installEventFilter( this );
93 myShown = myCont->isVisibleTo( myCont->parentWidget() );
97 \brief Custom event filter.
98 \param o event receiver object
99 \param e event sent to object
100 \return \c true if further event processing should be stopped
102 bool QtxDockWidget::Watcher::eventFilter( QObject* o, QEvent* e )
104 if ( o == myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
105 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) )
110 if ( o == myCont && e->type() == QEvent::ChildAdded )
112 QChildEvent* ce = (QChildEvent*)e;
113 if ( ce->child()->isWidgetType() )
114 ce->child()->installEventFilter( this );
116 QApplication::postEvent( this, new QEvent( (QEvent::Type)Update ) );
119 if ( o != myCont && e->type() == QEvent::WindowIconChange )
122 if ( o != myCont && e->type() == QEvent::WindowTitleChange )
125 if ( o != myCont && ( e->type() == QEvent::HideToParent || e->type() == QEvent::ShowToParent ) )
128 if ( o == myCont && e->type() == QEvent::ChildRemoved )
130 QApplication::postEvent( this, new QEvent( (QEvent::Type)Remove ) );
137 \brief Set internal status to "shown"
138 \param dw dockable widget
140 void QtxDockWidget::Watcher::shown( QtxDockWidget* dw )
149 \brief Set internal status to "hidden"
150 \param dw dockable widget
152 void QtxDockWidget::Watcher::hidden( QtxDockWidget* dw )
160 bool QtxDockWidget::Watcher::isEmpty() const
165 bool QtxDockWidget::Watcher::isVisible() const
170 void QtxDockWidget::Watcher::setEmpty( const bool on )
175 void QtxDockWidget::Watcher::setVisible( const bool on )
181 \brief Show the dock window being watched
183 void QtxDockWidget::Watcher::showContainer()
188 bool vis = isVisible();
190 QtxDockWidget* cont = myCont;
199 \brief Hide the dock window being watched
201 void QtxDockWidget::Watcher::hideContainer()
206 bool vis = isVisible();
208 QtxDockWidget* cont = myCont;
217 \brief Proces custom events.
218 \param e custom event (not used)
220 void QtxDockWidget::Watcher::customEvent( QEvent* e )
222 if ( e->type() == (QEvent::Type)Update )
228 else if ( myCont && e->type() == (QEvent::Type)Remove && !myCont->widget() )
230 myCont->deleteLater();
236 \brief Install this object as event filter to all children widgets
237 of the dockable widget being watched.
239 void QtxDockWidget::Watcher::installFilters()
244 QLayout* l = myCont->layout();
248 for ( int i = 0; i < (int)l->count(); i++ )
250 if ( l->itemAt( i ) && l->itemAt( i )->widget() )
251 l->itemAt( i )->widget()->installEventFilter( this );
256 \brief Update visibility state of all children widgets of the dockable widget
259 void QtxDockWidget::Watcher::updateVisibility()
265 if ( myCont->widget() )
266 vis = myCont->widget()->isVisibleTo( myCont );
269 QLayout* l = myCont->layout();
272 for ( int i = 0; i < (int)l->count() && !vis; i++ )
273 vis = l->itemAt( i ) && l->itemAt( i )->widget() && l->itemAt( i )->widget()->isVisibleTo( myCont );
277 bool empty = isEmpty();
283 myCont->toggleViewAction()->setVisible( myState );
286 myState = myCont->toggleViewAction()->isVisible();
287 myCont->toggleViewAction()->setVisible( false );
291 vis = !empty && isVisible();
292 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
293 vis ? showContainer() : hideContainer();
297 \brief Update the icon of dockable window being watched
299 void QtxDockWidget::Watcher::updateIcon()
301 if ( !myCont || !myCont->widget() || myBlock )
305 myCont->setWindowIcon( myCont->widget()->windowIcon() );
310 \brief Update the title of dockable window being watched
312 void QtxDockWidget::Watcher::updateCaption()
314 if ( myCont && myCont->widget() && !myCont->widget()->windowTitle().isNull() )
315 myCont->setWindowTitle( myCont->widget()->windowTitle() );
320 \brief Enhanced dockable widget class.
325 \param title dockable widget title
326 \param parent parent widget
327 \param f widget flags
329 QtxDockWidget::QtxDockWidget( const QString& title, QWidget* parent, Qt::WindowFlags f )
330 : QDockWidget( title, parent, f ),
332 myOrientation( (Qt::Orientation)-1 )
339 \param watch if \c true the event filter is installed to watch wigdet state changes
340 to update it properly
341 \param parent parent widget
342 \param f widget flags
344 QtxDockWidget::QtxDockWidget( const bool watch, QWidget* parent, Qt::WindowFlags f )
345 : QDockWidget( parent, f ),
347 myOrientation( (Qt::Orientation)-1 )
350 myWatcher = new Watcher( this );
357 \param parent parent widget
358 \param f widget flags
360 QtxDockWidget::QtxDockWidget( QWidget* parent, Qt::WindowFlags f )
361 : QDockWidget( parent, f ),
369 QtxDockWidget::~QtxDockWidget()
374 \brief Get the recommended size for the widget.
375 \return recommended dockable widget size
377 QSize QtxDockWidget::sizeHint() const
379 QSize sz = QDockWidget::sizeHint();
381 // printf( "----------------> QtxDockWidget::sizeHint()\n" );
383 return QSize( 500, 100 );
389 \brief Get the recommended minimum size for the widget.
390 \return recommended dockable widget minimum size
392 QSize QtxDockWidget::minimumSizeHint() const
394 QSize sz = QDockWidget::minimumSizeHint();
400 \brief Show/hide the dockable window.
401 \param on new visibility state
403 void QtxDockWidget::setVisible( bool on )
407 widget()->updateGeometry();
409 QDockWidget::setVisible( on && ( myWatcher ? !myWatcher->isEmpty() : true ) );
414 myWatcher->shown( this );
416 myWatcher->hidden( this );
421 \brief Process resize event
424 void QtxDockWidget::resizeEvent( QResizeEvent* e )
426 QDockWidget::resizeEvent( e );
431 \brief Get dockable window orientation.
432 \return orientation type
434 Qt::Orientation QtxDockWidget::orientation() const
437 QWidget* wid = parentWidget();
440 mw = ::qobject_cast<QMainWindow*>( wid );
441 wid = wid->parentWidget();
444 Qt::Orientation res = (Qt::Orientation)-1;
449 Qt::DockWidgetArea area = mw->dockWidgetArea( (QtxDockWidget*)this );
452 case Qt::LeftDockWidgetArea:
453 case Qt::RightDockWidgetArea:
456 case Qt::TopDockWidgetArea:
457 case Qt::BottomDockWidgetArea:
458 res = Qt::Horizontal;
468 \brief Update dockable window state.
470 void QtxDockWidget::updateState()
472 Qt::Orientation o = orientation();
473 if ( myOrientation == o )
478 emit orientationChanged( myOrientation );
482 \fn QtxDockWidget::orientationChanged(Qt::Orientation o)
483 \brief Emitted when the dockable window orientation is changed.
484 \param o new window orientation