1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SUIT_Accel.cxx: implementation of the SUIT_Accel class.
25 #include "SUIT_Accel.h"
27 #include "SUIT_ViewWindow.h"
28 #include "SUIT_ViewManager.h"
29 #include "SUIT_ViewModel.h"
31 #include <QCoreApplication>
37 \brief Manager of keyboard accelerator bindings.
40 SUIT_Accel* SUIT_Accel::myself = 0;
42 /*! Constructor [private].*/
43 SUIT_Accel::SUIT_Accel()
44 : QObject( QCoreApplication::instance() )
46 QCoreApplication::instance()->installEventFilter( this );
49 /*! getAccel() : public interface for SUIT_Accel object. Only one instance is created and returned. */
50 SUIT_Accel* SUIT_Accel::getAccel()
53 myself = new SUIT_Accel();
57 /*! setActionKey() : assign a ceratain action for a key accelerator */
58 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
60 IdActionMap idActionMap;
61 if ( myMap.contains( type ) )
62 idActionMap = myMap[type];
64 idActionMap[key] = action;
65 myMap[type] = idActionMap;
70 /*! unsetActionKey() : unregister a certain key accelerator */
71 void SUIT_Accel::unsetActionKey( const int key, const QString& type )
73 if ( myMap.contains( type ) )
75 IdActionMap idActionMap = myMap[type];
76 if ( idActionMap.contains( key ) )
78 idActionMap.remove( key );
79 myMap[type] = idActionMap;
84 /*! getParentViewWindow() : returns given object or any of its parents-grandparents-.. if it is a SUIT_ViewWindow */
85 SUIT_ViewWindow* getParentViewWindow( const QObject* obj )
89 if ( obj->inherits( "SUIT_ViewWindow" ) )
90 return (SUIT_ViewWindow*)obj;
92 return getParentViewWindow( obj->parent() );
97 /*! getKey() : returns integer key code (with modifiers) made of key pressed 'inside' given event */
98 int getKey( QKeyEvent* keyEvent )
100 int key = keyEvent->key(), state = keyEvent->modifiers();
101 if ( state & Qt::ShiftModifier )
103 if ( state & Qt::ControlModifier )
105 if ( state & Qt::AltModifier )
107 if ( state & Qt::MetaModifier )
113 Returns key pressed if
114 -# event was KeyPress
115 -# pressed key is a registered accelerator
117 int SUIT_Accel::getAccelKey( QEvent *event )
119 if ( event && event->type() == QEvent::KeyPress ) {
120 int key = ::getKey( (QKeyEvent*)event );
121 if ( myOptMap.contains( key ) )
127 /*! eventFilter() : filtering ALL events of QApplication. */
128 bool SUIT_Accel::eventFilter( QObject *obj, QEvent *event )
130 const int key = getAccelKey( event );
132 SUIT_ViewWindow* vw = ::getParentViewWindow( obj );
134 if ( vw->getViewManager() && vw->getViewManager()->getViewModel() )
136 QString type = vw->getViewManager()->getViewModel()->getType();
137 if ( myMap.contains( type ) ) {
138 IdActionMap idActionMap = myMap[type];
139 if ( idActionMap.contains( key ) ) {
140 return vw->onAccelAction( idActionMap[key] );