Back to index

salome-gui  6.5.0
SUIT_ShortcutMgr.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 #include "SUIT_ShortcutMgr.h"
00024 
00025 #include "SUIT_Session.h"
00026 #include "SUIT_ResourceMgr.h"
00027 
00028 #include <QtxAction.h>
00029 
00030 #include <QApplication>
00031 #include <QActionEvent>
00032 
00033 SUIT_ShortcutMgr* SUIT_ShortcutMgr::myShortcutMgr = NULL;
00034 
00038 SUIT_ShortcutMgr::SUIT_ShortcutMgr()
00039 : QObject()
00040 {
00041   qApp->installEventFilter( this );
00042 }
00043 
00047 SUIT_ShortcutMgr::~SUIT_ShortcutMgr()
00048 {
00049   qApp->removeEventFilter( this );
00050 }
00051 
00055 void SUIT_ShortcutMgr::Init()
00056 {
00057   if( myShortcutMgr==NULL )
00058     myShortcutMgr = new SUIT_ShortcutMgr();
00059 }
00060 
00064 SUIT_ShortcutMgr* SUIT_ShortcutMgr::getShortcutMgr()
00065 {
00066   Init();
00067   
00068   return myShortcutMgr;
00069 }
00070 
00076 bool SUIT_ShortcutMgr::eventFilter( QObject* o, QEvent* e )
00077 {
00078   if ( e->type() == QEvent::ActionAdded ) {
00079     QActionEvent* anActionEvent = (QActionEvent*)e;
00080     if (anActionEvent) {
00081       QtxAction* anAction = qobject_cast<QtxAction*>( anActionEvent->action() );
00082       if ( anAction )
00083        processAction( anAction );
00084     }
00085   }
00086 
00087   return QObject::eventFilter( o, e );
00088 }
00089 
00095 QKeySequence SUIT_ShortcutMgr::getShortcutByActionName( const QString& actionName ) const
00096 {
00097   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00098 
00099   QString section = actionName.section( resMgr->sectionsToken(), 0, 0 );
00100   section.prepend( QString("shortcuts") + resMgr->sectionsToken() );
00101   QString parameter = actionName.section( resMgr->sectionsToken(), 1, 1 );
00102 
00103   QString shortcutValue;
00104   bool hasValue = resMgr->value( section, parameter, shortcutValue, false );
00105 
00106   if ( !hasValue )
00107     return QKeySequence();
00108 
00109   return QKeySequence::fromString( shortcutValue );
00110 }
00111 
00116 void SUIT_ShortcutMgr::processAction( QtxAction* action )
00117 {
00118   QString shortcutActionName = action->shortcutActionName();
00119  
00120   if ( !shortcutActionName.isEmpty() ) {
00121     // Add action to the actions map
00122     if ( !myShortcutActions.contains( shortcutActionName, action ) ) {
00123       myShortcutActions.insert( shortcutActionName, action );
00124       connect( action, SIGNAL( destroyed( QObject* ) ), 
00125               this, SLOT ( onActionDestroyed( QObject* ) ) );
00126     }
00127 
00128     QKeySequence keySeq = getShortcutByActionName( shortcutActionName );
00129     action->setShortcut( keySeq );
00130   }
00131 }
00132 
00143 void SUIT_ShortcutMgr::setSectionEnabled( const QString& section, const bool on )
00144 {
00145   QMap<QString, QtxAction*>::ConstIterator it;
00146   for ( it = myShortcutActions.constBegin(); it != myShortcutActions.constEnd(); ++it ) {
00147     QtxAction* action = it.value();
00148     QString shortcutActionName = action->shortcutActionName();
00149     QString actionSection = shortcutActionName.section( ":", 0, 0 );
00150     if ( actionSection == section ) {
00151       // Check if the action parent widget equals to the active desktop
00152       SUIT_Application* app = SUIT_Session::session()->activeApplication();
00153       if ( !app )
00154        return;
00155       if ( action->parentWidget() == (QWidget*)app->desktop() )
00156        action->setEnabled( on );
00157     }
00158   }
00159 }
00160 
00164 void SUIT_ShortcutMgr::updateShortcuts()
00165 {
00166   QMap<QString, QtxAction*>::ConstIterator it;
00167   for ( it = myShortcutActions.constBegin(); it != myShortcutActions.constEnd(); ++it ) {
00168     QtxAction* action = it.value();
00169     QKeySequence keySeq = getShortcutByActionName( action->shortcutActionName() );
00170     action->setShortcut( keySeq );
00171   }
00172 }
00173 
00181 void SUIT_ShortcutMgr::onActionDestroyed( QObject* obj )
00182 {
00183   QtxAction* anAction = (QtxAction*)obj;
00184   
00185   if ( anAction )
00186     myShortcutActions.remove( anAction->shortcutActionName(), anAction );
00187 }