Salome HOME
887b98bca581da81dc33aa02b618d1938107a5fb
[modules/gui.git] / src / SUIT / SUIT_Accel.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 // SUIT_Accel.cxx: implementation of the SUIT_Accel class.
20 //
21 //////////////////////////////////////////////////////////////////////
22
23 #include "SUIT_Accel.h"
24 #include "SUIT_ViewWindow.h"
25 #include "SUIT_ViewManager.h"
26 #include "SUIT_ViewModel.h"
27
28 #include <qobjectlist.h>
29 #include <qapplication.h>
30 #include <qnamespace.h>
31
32
33 /*!\class SUIT_Accel
34  * Class handles keyboard accelerator bindings.
35  */
36 SUIT_Accel* SUIT_Accel::myself = 0;
37
38 /*! Constructor [private].*/
39 SUIT_Accel::SUIT_Accel()
40   : QObject( qApp, "SUIT_Accel" )
41 {
42   qApp->installEventFilter( this );
43 }
44
45 /*! getAccel() : public interface for SUIT_Accel object.  Only one instance is created and returned. */
46 SUIT_Accel* SUIT_Accel::getAccel() 
47 {
48   if ( !myself )
49     myself = new SUIT_Accel();
50   return myself;
51 }
52
53 /*! setActionKey() : assign a ceratain action for a key accelerator */
54 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
55 {
56   IdActionMap idActionMap;
57   if ( myMap.contains( type ) )
58     idActionMap = myMap[type];
59
60   idActionMap[key] = action;
61   myMap[type] = idActionMap;
62
63   myOptMap[key] = true;
64 }
65
66 /*! unsetActionKey() : unregister a certain key accelerator */
67 void SUIT_Accel::unsetActionKey( const int key, const QString& type )
68 {
69   if ( myMap.contains( type ) ) {
70     IdActionMap idActionMap = myMap[type];
71     if ( idActionMap.contains( key ) ) {
72       idActionMap.erase( key );
73       myMap[type] = idActionMap;
74     }
75   }
76 }
77
78 /*! getParentViewWindow() : returns given object or any of its parents-grandparents-.. if it is a SUIT_ViewWindow */ 
79 SUIT_ViewWindow* getParentViewWindow( const QObject* obj )
80 {
81   if ( obj ) {
82     if ( obj->inherits( "SUIT_ViewWindow" ) )
83       return (SUIT_ViewWindow*)obj;
84     else
85       return getParentViewWindow( obj->parent() );
86   }
87   return 0;
88 }
89
90 /*! getKey() : returns integer key code (with modifiers) made of key pressed 'inside' given event */
91 int getKey( QKeyEvent *keyEvent )
92 {
93   int key = keyEvent->key(), 
94     state = keyEvent->state();
95   if ( state & Qt::ShiftButton )   
96     key += Qt::SHIFT;
97   if ( state & Qt::ControlButton ) 
98     key += Qt::CTRL;
99   if ( state & Qt::AltButton )     
100     key += Qt::ALT;
101   if ( state & Qt::MetaButton )    
102     key += Qt::META;
103   return key;
104 }
105
106 /*! getAccelKey() : returns key pressed if 1) event was KeyPress 2) pressed key is a registered accelerator */ 
107 int SUIT_Accel::getAccelKey( QEvent *event )
108 {
109   if ( event && event->type() == QEvent::KeyPress ) {
110     int key = ::getKey( (QKeyEvent*)event );
111     if ( myOptMap.contains( key ) )
112       return key;
113   }
114   return 0;
115 }
116
117 /*! eventFilter() : filtering ALL events of QApplication. */
118 bool SUIT_Accel::eventFilter( QObject *obj, QEvent *event )
119 {
120   const int key = getAccelKey( event );
121   if ( key ) {
122     SUIT_ViewWindow* vw = ::getParentViewWindow( obj ); 
123     if ( vw ) {
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] );
129           return true;
130         }
131       }
132     }
133   }
134   return false;
135 }
136