1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 // SUIT_Accel.cxx: implementation of the SUIT_Accel class.
21 #include "SUIT_Accel.h"
22 #include "SUIT_ViewWindow.h"
23 #include "SUIT_ViewManager.h"
24 #include "SUIT_ViewModel.h"
26 #include <qobjectlist.h>
27 #include <qapplication.h>
28 #include <qnamespace.h>
31 SUIT_Accel* SUIT_Accel::myself = 0;
33 /*! Constructor [private].*/
34 SUIT_Accel::SUIT_Accel()
35 : QObject( qApp, "SUIT_Accel" )
37 qApp->installEventFilter( this );
40 /*! getAccel() : public interface for SUIT_Accel object. Only one instance is created and returned. */
41 SUIT_Accel* SUIT_Accel::getAccel()
44 myself = new SUIT_Accel();
48 /*! setActionKey() : assign a ceratain action for a key accelerator */
49 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
51 IdActionMap idActionMap;
52 if ( myMap.contains( type ) )
53 idActionMap = myMap[type];
55 idActionMap[key] = action;
56 myMap[type] = idActionMap;
61 /*! unsetActionKey() : unregister a certain key accelerator */
62 void SUIT_Accel::unsetActionKey( const int key, const QString& type )
64 if ( myMap.contains( type ) ) {
65 IdActionMap idActionMap = myMap[type];
66 if ( idActionMap.contains( key ) ) {
67 idActionMap.erase( key );
68 myMap[type] = idActionMap;
73 /*! getParentViewWindow() : returns given object or any of its parents-grandparents-.. if it is a SUIT_ViewWindow */
74 SUIT_ViewWindow* getParentViewWindow( const QObject* obj )
77 if ( obj->inherits( "SUIT_ViewWindow" ) )
78 return (SUIT_ViewWindow*)obj;
80 return getParentViewWindow( obj->parent() );
85 /*! getKey() : returns integer key code (with modifiers) made of key pressed 'inside' given event */
86 int getKey( QKeyEvent *keyEvent )
88 int key = keyEvent->key(),
89 state = keyEvent->state();
90 if ( state & Qt::ShiftButton )
92 if ( state & Qt::ControlButton )
94 if ( state & Qt::AltButton )
96 if ( state & Qt::MetaButton )
101 /*! getAccelKey() : returns key pressed if 1) event was KeyPress 2) pressed key is a registered accelerator */
102 int SUIT_Accel::getAccelKey( QEvent *event )
104 if ( event && event->type() == QEvent::KeyPress ) {
105 int key = ::getKey( (QKeyEvent*)event );
106 if ( myOptMap.contains( key ) )
112 /*! eventFilter() : filtering ALL events of QApplication. */
113 bool SUIT_Accel::eventFilter( QObject *obj, QEvent *event )
115 const int key = getAccelKey( event );
117 SUIT_ViewWindow* vw = ::getParentViewWindow( obj );
119 QString type = vw->getViewManager()->getViewModel()->getType();
120 if ( myMap.contains( type ) ) {
121 IdActionMap idActionMap = myMap[type];
122 if ( idActionMap.contains( key ) ) {
123 return vw->onAccelAction( idActionMap[key] );