Salome HOME
ecc4dcaebc46f7027658156e51b8513e0d28befe
[modules/gui.git] / src / SUIT / SUIT_Accel.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SUIT_Accel.cxx: implementation of the SUIT_Accel class.
24 //
25 #include "SUIT_Accel.h"
26
27 #include "SUIT_ViewWindow.h"
28 #include "SUIT_ViewManager.h"
29 #include "SUIT_ViewModel.h"
30
31 #include <QCoreApplication>
32 #include <QEvent>
33 #include <QKeyEvent>
34
35 /*!
36   \class SUIT_Accel
37   \brief Manager of keyboard accelerator bindings.
38 */
39
40 SUIT_Accel* SUIT_Accel::myself = 0;
41
42 /*! Constructor [private].*/
43 SUIT_Accel::SUIT_Accel()
44 : QObject( QCoreApplication::instance() )
45 {
46   QCoreApplication::instance()->installEventFilter( this );
47 }
48
49 /*! getAccel() : public interface for SUIT_Accel object.  Only one instance is created and returned. */
50 SUIT_Accel* SUIT_Accel::getAccel() 
51 {
52   if ( !myself )
53     myself = new SUIT_Accel();
54   return myself;
55 }
56
57 /*! setActionKey() : assign a ceratain action for a key accelerator */
58 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
59 {
60   IdActionMap idActionMap;
61   if ( myMap.contains( type ) )
62     idActionMap = myMap[type];
63
64   idActionMap[key] = action;
65   myMap[type] = idActionMap;
66
67   myOptMap[key] = true;
68 }
69
70 /*! unsetActionKey() : unregister a certain key accelerator */
71 void SUIT_Accel::unsetActionKey( const int key, const QString& type )
72 {
73   if ( myMap.contains( type ) )
74   {
75     IdActionMap idActionMap = myMap[type];
76     if ( idActionMap.contains( key ) )
77     {
78       idActionMap.remove( key );
79       myMap[type] = idActionMap;
80     }
81   }
82 }
83
84 /*! getParentViewWindow() : returns given object or any of its parents-grandparents-.. if it is a SUIT_ViewWindow */ 
85 SUIT_ViewWindow* getParentViewWindow( const QObject* obj )
86 {
87   if ( obj )
88   {
89     if ( obj->inherits( "SUIT_ViewWindow" ) )
90       return (SUIT_ViewWindow*)obj;
91     else
92       return getParentViewWindow( obj->parent() );
93   }
94   return 0;
95 }
96
97 /*! getKey() : returns integer key code (with modifiers) made of key pressed 'inside' given event */
98 int getKey( QKeyEvent* keyEvent )
99 {
100   int key = keyEvent->key(), state = keyEvent->modifiers();
101   if ( state & Qt::ShiftModifier )   
102     key += Qt::SHIFT;
103   if ( state & Qt::ControlModifier )
104     key += Qt::CTRL;
105   if ( state & Qt::AltModifier )
106     key += Qt::ALT;
107   if ( state & Qt::MetaModifier )
108     key += Qt::META;
109   return key;
110 }
111
112 /*! 
113   Returns key pressed if 
114   -# event was KeyPress 
115   -# pressed key is a registered accelerator
116 */
117 int SUIT_Accel::getAccelKey( QEvent *event )
118 {
119   if ( event && event->type() == QEvent::KeyPress ) {
120     int key = ::getKey( (QKeyEvent*)event );
121     if ( myOptMap.contains( key ) )
122       return key;
123   }
124   return 0;
125 }
126
127 /*! eventFilter() : filtering ALL events of QApplication. */
128 bool SUIT_Accel::eventFilter( QObject *obj, QEvent *event )
129 {
130   const int key = getAccelKey( event );
131   if ( key ) {
132     SUIT_ViewWindow* vw = ::getParentViewWindow( obj ); 
133     if ( vw ) {
134       if ( vw->getViewManager() && vw->getViewManager()->getViewModel() )
135       {
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] );
141           }
142         }
143       }
144     }
145   }
146   return false;
147 }
148