Back to index

salome-gui  6.5.0
SUIT_Accel.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 // SUIT_Accel.cxx: implementation of the SUIT_Accel class.
00024 //
00025 #include "SUIT_Accel.h"
00026 
00027 #include "SUIT_ViewWindow.h"
00028 #include "SUIT_ViewManager.h"
00029 #include "SUIT_ViewModel.h"
00030 
00031 #include <QCoreApplication>
00032 #include <QEvent>
00033 #include <QKeyEvent>
00034 
00040 SUIT_Accel* SUIT_Accel::myself = 0;
00041 
00043 SUIT_Accel::SUIT_Accel()
00044 : QObject( QCoreApplication::instance() )
00045 {
00046   QCoreApplication::instance()->installEventFilter( this );
00047 }
00048 
00050 SUIT_Accel* SUIT_Accel::getAccel() 
00051 {
00052   if ( !myself )
00053     myself = new SUIT_Accel();
00054   return myself;
00055 }
00056 
00058 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
00059 {
00060   IdActionMap idActionMap;
00061   if ( myMap.contains( type ) )
00062     idActionMap = myMap[type];
00063 
00064   idActionMap[key] = action;
00065   myMap[type] = idActionMap;
00066 
00067   myOptMap[key] = true;
00068 }
00069 
00071 void SUIT_Accel::unsetActionKey( const int key, const QString& type )
00072 {
00073   if ( myMap.contains( type ) )
00074   {
00075     IdActionMap idActionMap = myMap[type];
00076     if ( idActionMap.contains( key ) )
00077     {
00078       idActionMap.remove( key );
00079       myMap[type] = idActionMap;
00080     }
00081   }
00082 }
00083 
00085 SUIT_ViewWindow* getParentViewWindow( const QObject* obj )
00086 {
00087   if ( obj )
00088   {
00089     if ( obj->inherits( "SUIT_ViewWindow" ) )
00090       return (SUIT_ViewWindow*)obj;
00091     else
00092       return getParentViewWindow( obj->parent() );
00093   }
00094   return 0;
00095 }
00096 
00098 int getKey( QKeyEvent* keyEvent )
00099 {
00100   int key = keyEvent->key(), state = keyEvent->modifiers();
00101   if ( state & Qt::ShiftModifier )   
00102     key += Qt::SHIFT;
00103   if ( state & Qt::ControlModifier )
00104     key += Qt::CTRL;
00105   if ( state & Qt::AltModifier )
00106     key += Qt::ALT;
00107   if ( state & Qt::MetaModifier )
00108     key += Qt::META;
00109   return key;
00110 }
00111 
00117 int SUIT_Accel::getAccelKey( QEvent *event )
00118 {
00119   if ( event && event->type() == QEvent::KeyPress ) {
00120     int key = ::getKey( (QKeyEvent*)event );
00121     if ( myOptMap.contains( key ) )
00122       return key;
00123   }
00124   return 0;
00125 }
00126 
00128 bool SUIT_Accel::eventFilter( QObject *obj, QEvent *event )
00129 {
00130   const int key = getAccelKey( event );
00131   if ( key ) {
00132     SUIT_ViewWindow* vw = ::getParentViewWindow( obj ); 
00133     if ( vw ) {
00134       if ( vw->getViewManager() && vw->getViewManager()->getViewModel() )
00135       {
00136         QString type = vw->getViewManager()->getViewModel()->getType();
00137         if ( myMap.contains( type ) ) {
00138           IdActionMap idActionMap = myMap[type];
00139           if ( idActionMap.contains( key ) ) {
00140             return vw->onAccelAction( idActionMap[key] );
00141           }
00142         }
00143       }
00144     }
00145   }
00146   return false;
00147 }
00148