Back to index

salome-gui  6.5.0
QtxDockAction.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxDockAction.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxDockAction.h"
00027 
00028 #include <QMenu>
00029 #include <QToolBar>
00030 #include <QDockWidget>
00031 #include <QMainWindow>
00032 
00046 QtxDockAction::QtxDockAction( QMainWindow* mw )
00047 : QtxAction( "Windows and Toolbars", "Windows and Toolbars", 0, mw ),
00048   myType( Both ),
00049   myMain( mw )
00050 {
00051   initialize();
00052 }
00053 
00060 QtxDockAction::QtxDockAction( const QString& text, const QString& menuText, QMainWindow* mw )
00061 : QtxAction( text, menuText, 0, mw ),
00062   myType( Both ),
00063   myMain( mw )
00064 {
00065   initialize();
00066 }
00067 
00075 QtxDockAction::QtxDockAction( const QString& text, const QIcon& icon, const QString& menuText, QMainWindow* mw )
00076 : QtxAction( text, icon, menuText, 0, mw ),
00077   myType( Both ),
00078   myMain( mw )
00079 {
00080   initialize();
00081 }
00082 
00088 QtxDockAction::~QtxDockAction()
00089 {
00090   // to avoid memory leak
00091   if( QMenu* aMenu = menu() )
00092   {
00093     delete aMenu;
00094     aMenu = 0;
00095   }
00096 }
00097 
00101 void QtxDockAction::update()
00102 {
00103   updateMenu();
00104 }
00105 
00110 QMainWindow* QtxDockAction::mainWindow() const
00111 {
00112   return myMain;
00113 }
00114 
00119 int QtxDockAction::dockType() const
00120 {
00121   return myType;
00122 }
00123 
00128 void QtxDockAction::setDockType( const int type )
00129 {
00130   myType = type;
00131 }
00132 
00137 void QtxDockAction::onAboutToShow()
00138 {
00139   updateMenu();
00140   setVisible( menu() && !menu()->isEmpty() );
00141 }
00142 
00147 void QtxDockAction::toolBars( QList<QToolBar*>& lst ) const
00148 {
00149   lst.clear();
00150 
00151   QMainWindow* mw = mainWindow();
00152   if ( !mw )
00153     return;
00154 
00155   QList<QToolBar*> toolbars = qFindChildren<QToolBar*>( mw );
00156   for ( QList<QToolBar*>::iterator it = toolbars.begin(); it != toolbars.end(); ++it )
00157   {
00158     QToolBar* tb = *it;
00159     if ( tb->parentWidget() == mw )
00160       lst.append( tb );
00161   }
00162 }
00163 
00168 void QtxDockAction::dockWidgets( QList<QDockWidget*>& lst ) const
00169 {
00170   lst.clear();
00171 
00172   QMainWindow* mw = mainWindow();
00173   if ( !mw )
00174     return;
00175 
00176   QList<QDockWidget*> dockwidgets = qFindChildren<QDockWidget*>( mw );
00177   for ( QList<QDockWidget*>::iterator it = dockwidgets.begin(); it != dockwidgets.end(); ++it )
00178   {
00179     QDockWidget* dw = *it;
00180     if ( dw->parentWidget() == mw )
00181       lst.append( dw );
00182   }
00183 }
00184 
00185 /*
00186 void QtxDockAction::updateInfo( QDockWindow* dw )
00187 {
00188   QAction* a = action( dw );
00189   if ( !a )
00190     return;
00191 
00192   a->setText( dw->caption() );
00193   a->setMenuText( dw->caption() );
00194 
00195   if ( isToolBar( dw ) )
00196     a->setStatusTip( tr( "Toggles toolbar \"%1\" on/off" ).arg( dw->caption() ) );
00197   else
00198     a->setStatusTip( tr( "Toggles window \"%1\" on/off" ).arg( dw->caption() ) );
00199 
00200   const QPixmap* icon = dw->icon();
00201   if ( icon )
00202     a->setIconSet( *icon );
00203 }
00204 */
00205 
00214 void QtxDockAction::addedTo( QWidget* w )
00215 {
00216   if ( w->inherits( "QMenu" ) )
00217     connect( w, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00218 }
00219 
00228 void QtxDockAction::removedFrom( QWidget* w )
00229 {
00230   if ( w->inherits( "QMenu" ) )
00231     disconnect( w, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00232 }
00233 
00237 void QtxDockAction::initialize()
00238 {
00239   setMenu( new QMenu( 0 ) );
00240 }
00241 
00245 void QtxDockAction::updateMenu()
00246 {
00247   QMenu* pm = menu();
00248   if ( !pm )
00249     return;
00250 
00251   pm->clear();
00252 
00253   if ( dockType() == DockWidget || dockType() == Both )
00254   {
00255     QList<QDockWidget*> dwList;
00256     dockWidgets( dwList );
00257     for ( QList<QDockWidget*>::iterator it = dwList.begin(); it != dwList.end(); ++it )
00258       pm->addAction( (*it)->toggleViewAction() );
00259   }
00260 
00261   pm->addSeparator();
00262 
00263   if ( dockType() == ToolBar || dockType() == Both )
00264   {
00265     QList<QToolBar*> tbList;
00266     toolBars( tbList );
00267     for ( QList<QToolBar*>::iterator it = tbList.begin(); it != tbList.end(); ++it )
00268       pm->addAction( (*it)->toggleViewAction() );
00269   }
00270 
00271   Qtx::simplifySeparators( pm );
00272 }