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