Back to index

salome-gui  6.5.0
QtxWorkstackAction.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:      QtxWorkstackAction.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxWorkstackAction.h"
00027 
00028 #include "QtxWorkstack.h"
00029 
00030 #include <QMenu>
00031 #include <QWidgetList>
00032 
00044 QtxWorkstackAction::QtxWorkstackAction( QtxWorkstack* ws, QObject* parent )
00045 : QtxActionSet( parent ),
00046   myWorkstack( ws ),
00047   myWindowsFlag( true )
00048 {
00049   if ( myWorkstack )
00050     insertAction( myWorkstack->action( QtxWorkstack::SplitVertical ), SplitVertical );
00051   else
00052     insertAction( new QtxAction( tr( "Split the active window on two vertical parts" ),
00053                                  tr( "Split vertically" ), 0, this ), SplitVertical );
00054 
00055   if ( myWorkstack )
00056     insertAction( myWorkstack->action( QtxWorkstack::SplitHorizontal ), SplitHorizontal );
00057   else
00058     insertAction( new QtxAction( tr( "Split the active window on two horizontal parts" ),
00059                                  tr( "Split horizontally" ), 0, this ), SplitHorizontal );
00060 
00061   connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
00062 
00063   setMenuActions( Standard );
00064 }
00065 
00069 QtxWorkstackAction::~QtxWorkstackAction()
00070 {
00071 }
00072 
00077 QtxWorkstack* QtxWorkstackAction::workstack() const
00078 {
00079   return myWorkstack;
00080 }
00081 
00090 void QtxWorkstackAction::setMenuActions( const int flags )
00091 {
00092   action( SplitVertical )->setVisible( flags & SplitVertical );
00093   action( SplitHorizontal )->setVisible( flags & SplitHorizontal );
00094   myWindowsFlag = flags & Windows;
00095 }
00096 
00102 int QtxWorkstackAction::menuActions() const
00103 {
00104   int ret = 0;
00105   ret = ret | ( action( SplitVertical )->isVisible() ? SplitVertical : 0 );
00106   ret = ret | ( action( SplitHorizontal )->isVisible() ? SplitHorizontal : 0 );
00107   ret = ret | ( myWindowsFlag ? Windows : 0 );
00108   return ret;
00109 }
00110 
00116 int QtxWorkstackAction::accel( const int id ) const
00117 {
00118   int a = 0;
00119   if ( action( id ) )
00120     a = action( id )->shortcut();
00121   return a;
00122 }
00123 
00132 QIcon QtxWorkstackAction::icon( const int id ) const
00133 {
00134   QIcon ico;
00135   if ( action( id ) )
00136     ico = action( id )->icon();
00137   return ico;
00138 }
00139 
00145 QString QtxWorkstackAction::text( const int id ) const
00146 {
00147   QString txt;
00148   if ( action( id ) )
00149     txt = action( id )->text();
00150   return txt;
00151 }
00152 
00158 QString QtxWorkstackAction::statusTip( const int id ) const
00159 {
00160   QString txt;
00161   if ( action( id ) )
00162     txt = action( id )->statusTip();
00163   return txt;
00164 }
00165 
00171 void QtxWorkstackAction::setAccel( const int id, const int a )
00172 {
00173   if ( action( id ) )
00174     action( id )->setShortcut( a );
00175 }
00176 
00182 void QtxWorkstackAction::setIcon( const int id, const QIcon& icon )
00183 {
00184   if ( action( id ) )
00185     action( id )->setIcon( icon );
00186 }
00187 
00193 void QtxWorkstackAction::setText( const int id, const QString& txt )
00194 {
00195   if ( action( id ) )
00196     action( id )->setText( txt );
00197 }
00198 
00204 void QtxWorkstackAction::setStatusTip( const int id, const QString& txt )
00205 {
00206   if ( action( id ) )
00207     action( id )->setStatusTip( txt );
00208 }
00209 
00214 void QtxWorkstackAction::perform( const int type )
00215 {
00216   /*
00217   switch ( type )
00218   {
00219   case SplitVertical:
00220     splitVertical();
00221     break;
00222   case SplitHorizontal:
00223     splitHorizontal();
00224     break;
00225   }
00226   */
00227 }
00228 
00232 void QtxWorkstackAction::splitVertical()
00233 {
00234   QtxWorkstack* ws = workstack();
00235   if ( ws )
00236     ws->splitVertical();
00237 }
00238 
00242 void QtxWorkstackAction::splitHorizontal()
00243 {
00244   QtxWorkstack* ws = workstack();
00245   if ( ws )
00246     ws->splitHorizontal();
00247 }
00248 
00253 void QtxWorkstackAction::addedTo( QWidget* w )
00254 {
00255   QtxActionSet::addedTo( w );
00256 
00257   QMenu* pm = ::qobject_cast<QMenu*>( w );
00258   if ( pm )
00259     connect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00260 }
00261 
00266 void QtxWorkstackAction::removedFrom( QWidget* w )
00267 {
00268   QtxActionSet::removedFrom( w );
00269 
00270   QMenu* pm = ::qobject_cast<QMenu*>( w );
00271   if ( pm )
00272     disconnect( pm, SIGNAL( aboutToShow() ), this, SLOT( onAboutToShow() ) );
00273 }
00274 
00278 void QtxWorkstackAction::updateContent()
00279 {
00280   bool count = workstack() ? workstack()->splitWindowList().count() > 1 : 0;
00281   action( SplitVertical )->setEnabled( count );
00282   action( SplitHorizontal )->setEnabled( count );
00283 
00284   updateWindows();
00285 }
00286 
00290 void QtxWorkstackAction::updateWindows()
00291 {
00292   QtxWorkstack* ws = workstack();
00293   if ( !ws )
00294     return;
00295 
00296   QList<QAction*> lst = actions();
00297   for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
00298   {
00299     int id = actionId( *it );
00300     if ( id >= Windows )
00301       removeAction( *it );
00302   }
00303 
00304   bool base = action( SplitVertical )->isVisible() || action( SplitHorizontal )->isVisible();
00305 
00306   QList<QAction*> items;
00307   QMap<QAction*, int> map;
00308   if ( menuActions() & Windows )
00309   {
00310     int index = 1;
00311     QWidgetList wList = ws->windowList();
00312     for ( QWidgetList::iterator it = wList.begin(); it != wList.end(); ++it, index++ )
00313     {
00314       QWidget* wid = *it;
00315       QAction* a = new QtxAction( wid->windowTitle(), wid->windowTitle(), 0, this, true );
00316       a->setChecked( wid == ws->activeWindow() );
00317       items.append( a );
00318       map.insert( a, Windows + index );
00319     }
00320 
00321     if ( base && !items.isEmpty() )
00322     {
00323       QAction* sep = new QtxAction( this );
00324       sep->setSeparator( true );
00325       items.prepend( sep );
00326       map.insert( sep, Windows );
00327     }
00328   }
00329 
00330   if ( !items.isEmpty() )
00331     insertActions( items );
00332 
00333   for ( QMap<QAction*, int>::const_iterator itr = map.begin(); itr != map.end(); ++itr )
00334     setActionId( itr.key(), itr.value() );
00335 }
00336 
00342 void QtxWorkstackAction::onAboutToShow()
00343 {
00344   QMenu* pm = ::qobject_cast<QMenu*>( sender() );
00345   if ( pm )
00346     updateContent();
00347 }
00348 
00356 void QtxWorkstackAction::activateItem( const int idx )
00357 {
00358   QtxWorkstack* ws = workstack();
00359   if ( !ws )
00360     return;
00361 
00362   QWidgetList wList = ws->windowList();
00363   if ( idx >= 0 && idx < (int)wList.count() )
00364     wList.at( idx )->setFocus();
00365 }
00366 
00374 void QtxWorkstackAction::onTriggered( int id )
00375 {
00376   if ( id < Windows )
00377     perform( id );
00378   else
00379     activateItem( id - Windows - 1 );
00380 }