Back to index

salome-gui  6.5.0
QtxWorkspaceAction.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:      QtxWorkspaceAction.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxWorkspaceAction.h"
00027 
00028 #include "QtxWorkspace.h"
00029 
00030 #include <QMenu>
00031 #include <QWidgetList>
00032 
00044 QtxWorkspaceAction::QtxWorkspaceAction( QtxWorkspace* ws, QObject* parent )
00045 : QtxActionSet( parent ),
00046   myWorkspace( ws ),
00047   myWindowsFlag( true )
00048 {
00049   insertAction( new QtxAction( tr( "Arranges the windows as overlapping tiles" ),
00050                                tr( "Cascade" ), 0, this ), Cascade );
00051   insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping tiles" ),
00052                                tr( "Tile" ), 0, this ), Tile );
00053   insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping horizontal tiles" ),
00054                                tr( "Tile horizontally" ), 0, this ), HTile );
00055   insertAction( new QtxAction( tr( "Arranges the windows as nonoverlapping vertical tiles" ),
00056                                tr( "Tile vertically" ), 0, this ), VTile );
00057 
00058   connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
00059 
00060   setMenuActions( Standard );
00061 }
00062 
00066 QtxWorkspaceAction::~QtxWorkspaceAction()
00067 {
00068 }
00069 
00074 QtxWorkspace* QtxWorkspaceAction::workspace() const
00075 {
00076   return myWorkspace;
00077 }
00078 
00087 void QtxWorkspaceAction::setMenuActions( const int flags )
00088 {
00089   action( Cascade )->setVisible( flags & Cascade );
00090   action( Tile )->setVisible( flags & Tile );
00091   action( VTile )->setVisible( flags & VTile );
00092   action( HTile )->setVisible( flags & HTile );
00093   myWindowsFlag = flags & Windows;
00094 }
00095 
00101 int QtxWorkspaceAction::menuActions() const
00102 {
00103   int ret = 0;
00104   ret = ret | ( action( Cascade )->isVisible() ? Cascade : 0 );
00105   ret = ret | ( action( Tile )->isVisible() ? Tile : 0 );
00106   ret = ret | ( action( VTile )->isVisible() ? VTile : 0 );
00107   ret = ret | ( action( HTile )->isVisible() ? HTile : 0 );
00108   ret = ret | ( myWindowsFlag ? Windows : 0 );
00109   return ret;
00110 }
00111 
00117 int QtxWorkspaceAction::accel( const int id ) const
00118 {
00119   int a = 0;
00120   if ( action( id ) )
00121     a = action( id )->shortcut();
00122   return a;
00123 }
00124 
00133 QIcon QtxWorkspaceAction::icon( const int id ) const
00134 {
00135   QIcon ico;
00136   if ( action( id ) )
00137     ico = action( id )->icon();
00138   return ico;
00139 }
00140 
00146 QString QtxWorkspaceAction::text( const int id ) const
00147 {
00148   QString txt;
00149   if ( action( id ) )
00150     txt = action( id )->text();
00151   return txt;
00152 }
00153 
00159 QString QtxWorkspaceAction::statusTip( const int id ) const
00160 {
00161   QString txt;
00162   if ( action( id ) )
00163     txt = action( id )->statusTip();
00164   return txt;
00165 }
00166 
00172 void QtxWorkspaceAction::setAccel( const int id, const int a )
00173 {
00174   if ( action( id ) )
00175     action( id )->setShortcut( a );
00176 }
00177 
00183 void QtxWorkspaceAction::setIcon( const int id, const QIcon& icon )
00184 {
00185   if ( action( id ) )
00186     action( id )->setIcon( icon );
00187 }
00188 
00194 void QtxWorkspaceAction::setText( const int id, const QString& txt )
00195 {
00196   if ( action( id ) )
00197     action( id )->setText( txt );
00198 }
00199 
00205 void QtxWorkspaceAction::setStatusTip( const int id, const QString& txt )
00206 {
00207   if ( action( id ) )
00208     action( id )->setStatusTip( txt );
00209 }
00210 
00215 void QtxWorkspaceAction::perform( const int type )
00216 {
00217   switch ( type )
00218   {
00219   case Cascade:
00220     cascade();
00221     break;
00222   case Tile:
00223     tile();
00224     break;
00225   case VTile:
00226     tileVertical();
00227     break;
00228   case HTile:
00229     tileHorizontal();
00230     break;
00231   }
00232 }
00233 
00237 void QtxWorkspaceAction::tile()
00238 {
00239   QtxWorkspace* ws = workspace();
00240   if ( ws )
00241     ws->tile();
00242 }
00243 
00247 void QtxWorkspaceAction::cascade()
00248 {
00249   QtxWorkspace* ws = workspace();
00250   if ( !ws )
00251     return;
00252 
00253   ws->cascade();
00254 
00255         int w = ws->width();
00256         int h = ws->height();
00257 
00258         QWidgetList winList = ws->windowList();
00259   for ( QWidgetList::iterator it = winList.begin(); it != winList.end(); ++it )
00260                 (*it)->resize( int( w * 0.8 ), int( h * 0.8 ) );
00261 }
00262 
00266 void QtxWorkspaceAction::tileVertical()
00267 {
00268   QtxWorkspace* ws = workspace();
00269   if ( ws )
00270     ws->tileVertical();
00271 }
00272 
00276 void QtxWorkspaceAction::tileHorizontal()
00277 {
00278   QtxWorkspace* ws = workspace();
00279   if ( ws )
00280     ws->tileHorizontal();
00281 }
00282 
00287 void QtxWorkspaceAction::addedTo( QWidget* w )
00288 {
00289   QtxActionSet::addedTo( w );
00290 
00291   QMenu* pm = ::qobject_cast<QMenu*>( w );
00292   if ( pm )
00293     connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00294 }
00295 
00300 void QtxWorkspaceAction::removedFrom( QWidget* w )
00301 {
00302   QtxActionSet::removedFrom( w );
00303 
00304   QMenu* pm = ::qobject_cast<QMenu*>( w );
00305   if ( pm )
00306     disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00307 }
00308 
00312 void QtxWorkspaceAction::updateContent()
00313 {
00314   bool count = workspace() ? workspace()->windowList().count() : 0;
00315   action( Cascade )->setEnabled( count );
00316   action( Tile )->setEnabled( count );
00317   action( HTile )->setEnabled( count );
00318   action( VTile )->setEnabled( count );
00319 
00320   updateWindows();
00321 }
00322 
00326 void QtxWorkspaceAction::updateWindows()
00327 {
00328   QtxWorkspace* ws = workspace();
00329   if ( !ws )
00330     return;
00331 
00332   QList<QAction*> lst = actions();
00333   for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
00334   {
00335     int id = actionId( *it );
00336     if ( id >= Windows )
00337       removeAction( *it );
00338   }
00339 
00340   bool base = action( Cascade )->isVisible() || action( Tile )->isVisible() ||
00341               action( HTile )->isVisible() || action( VTile )->isVisible();
00342 
00343   QList<QAction*> items;
00344   QMap<QAction*, int> map;
00345   if ( menuActions() & Windows )
00346   {
00347     int index = 1;
00348     QWidgetList wList = ws->windowList();
00349     for ( QWidgetList::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
00350     {
00351       QWidget* wid = *it;
00352       QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
00353       a->setChecked( wid == ws->activeWindow() );
00354       items.append( a );
00355       map.insert( a, Windows + index );
00356     }
00357 
00358     if ( base && !items.isEmpty() )
00359     {
00360       QAction* sep = new QtxAction( this );
00361       sep->setSeparator( true );
00362       items.prepend( sep );
00363       map.insert( sep, Windows );
00364     }
00365   }
00366 
00367   if ( !items.isEmpty() )
00368     insertActions( items );
00369 
00370   for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
00371     setActionId( itr.key(), itr.value() );
00372 }
00373 
00379 void QtxWorkspaceAction::onAboutToShow()
00380 {
00381   QMenu* pm = ::qobject_cast<QMenu*>( sender() );
00382   if ( pm )
00383     updateContent();
00384 }
00385 
00393 void QtxWorkspaceAction::activateItem( const int idx )
00394 {
00395   QtxWorkspace* ws = workspace();
00396   if ( !ws )
00397     return;
00398 
00399   QWidgetList wList = ws->windowList();
00400   if ( idx >= 0 && idx < (int)wList.count() )
00401     wList.at( idx )->setFocus();
00402 }
00403 
00411 void QtxWorkspaceAction::onTriggered( int id )
00412 {
00413   if ( id < Windows )
00414     perform( id );
00415   else
00416     activateItem( id - Windows - 1 );
00417 }