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 //////////////////////////////////////////////////////////////////////
23 #include "SUIT_Accel.h"
24 #include "SUIT_ViewWindow.h"
25 #include "SUIT_ViewManager.h"
26 #include "SUIT_ViewModel.h"
28 #include <qobjectlist.h>
29 #include <qapplication.h>
30 #include <qnamespace.h>
34 * Class handles keyboard accelerator bindings.
36 SUIT_Accel* SUIT_Accel::myself = 0;
38 /*! Constructor [private].*/
39 SUIT_Accel::SUIT_Accel()
40 : QObject( qApp, "SUIT_Accel" )
42 qApp->installEventFilter( this );
45 /*! getAccel() : public interface for SUIT_Accel object. Only one instance is created and returned. */
46 SUIT_Accel* SUIT_Accel::getAccel()
49 myself = new SUIT_Accel();
53 /*! setActionKey() : assign a ceratain action for a key accelerator */
54 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
56 IdActionMap idActionMap;
57 if ( myMap.contains( type ) )
58 idActionMap = myMap[type];
60 idActionMap[key] = action;
61 myMap[type] = idActionMap;
66 /*! unsetActionKey() : unregister a certain key accelerator */
67 void SUIT_Accel::unsetActionKey( const int key, const QString& type )
69 if ( myMap.contains( type ) ) {
70 IdActionMap idActionMap = myMap[type];
71 if ( idActionMap.contains( key ) ) {
72 idActionMap.erase( key );
73 myMap[type] = idActionMap;
78 /*! getParentViewWindow() : returns given object or any of its parents-grandparents-.. if it is a SUIT_ViewWindow */
79 SUIT_ViewWindow* getParentViewWindow( const QObject* obj )
82 if ( obj->inherits( "SUIT_ViewWindow" ) )
83 return (SUIT_ViewWindow*)obj;
85 return getParentViewWindow( obj->parent() );
90 /*! getKey() : returns integer key code (with modifiers) made of key pressed 'inside' given event */
91 int getKey( QKeyEvent *keyEvent )
93 int key = keyEvent->key(),
94 state = keyEvent->state();
95 if ( state & Qt::ShiftButton )
97 if ( state & Qt::ControlButton )
99 if ( state & Qt::AltButton )
101 if ( state & Qt::MetaButton )
106 /*! getAccelKey() : returns key pressed if 1) event was KeyPress 2) pressed key is a registered accelerator */
107 int SUIT_Accel::getAccelKey( QEvent *event )
109 if ( event && event->type() == QEvent::KeyPress ) {
110 int key = ::getKey( (QKeyEvent*)event );
111 if ( myOptMap.contains( key ) )
117 /*! eventFilter() : filtering ALL events of QApplication. */
118 bool SUIT_Accel::eventFilter( QObject *obj, QEvent *event )
120 const int key = getAccelKey( event );
122 SUIT_ViewWindow* vw = ::getParentViewWindow( obj );
124 QString type = vw->getViewManager()->getViewModel()->getType();
125 if ( myMap.contains( type ) ) {
126 IdActionMap idActionMap = myMap[type];
127 if ( idActionMap.contains( key ) ) {
128 vw->onAccelAction( idActionMap[key] );