Salome HOME
a72a317e1c0c002cdae784615b34a14451583ca7
[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_Desktop.h"
25 #include "SUIT_ViewManager.h"
26 #include "SUIT_ViewWindow.h"
27 #include "SUIT_ViewModel.h"
28
29 #include <qaccel.h>
30
31 /*!\class SUIT_Accel
32  * Class handles keyboard accelerator bindings.
33  */
34
35 /*! Constructor.*/
36 SUIT_Accel::SUIT_Accel(SUIT_Desktop* theDesktop)
37   : QObject( theDesktop, "SUIT_Accel" ),
38     myDesktop( theDesktop )
39 {
40   myAccel = new QAccel( theDesktop, "SUIT_Accel_interal_qaccel" );
41   connect( myAccel, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
42 }
43
44 /*! Destructor.*/
45 SUIT_Accel::~SUIT_Accel()
46 {
47 }
48
49 /*! setActionKey  assign a ceratain action for a key accelerator */
50 void SUIT_Accel::setActionKey( const int action, const int key, const QString& type )
51 {    
52   // 1. get or generate interal "id" of action
53   int id = myAccel->findKey( key );
54   if ( id == -1 )
55     id = myAccel->insertItem( key );
56
57   IdActionMap idActionMap;
58   if ( myMap.contains( type ) )
59     idActionMap = myMap[type];
60
61   idActionMap[id] = action;
62   myMap[type] = idActionMap;
63 }
64
65 /*! onActivated  slot called when a registered key accelerator was activated */
66 void SUIT_Accel::onActivated( int id )
67 {
68   if ( myDesktop ) {
69     if ( SUIT_ViewWindow* vw = myDesktop->activeWindow() ) {
70       QString type = vw->getViewManager()->getViewModel()->getType();
71       if (  myMap.contains( type ) ) {
72         IdActionMap idActionMap = myMap[type];
73         if ( idActionMap.contains( id ) ) {
74           vw->onAccelAction( idActionMap[id] );
75         }
76       }
77     }
78   }
79 }
80