Salome HOME
89b0b39f8440fbc7c2888ac3dd2bda2110565053
[modules/gui.git] / src / SUIT / SUIT_ShortcutMgr.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, 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 #include "SUIT_ShortcutMgr.h"
24
25 #include "SUIT_Session.h"
26 #include "SUIT_ResourceMgr.h"
27
28 #include <QtxAction.h>
29
30 #include <QApplication>
31 #include <QActionEvent>
32
33 SUIT_ShortcutMgr* SUIT_ShortcutMgr::myShortcutMgr = NULL;
34
35 /*!
36   \brief Constructor
37 */
38 SUIT_ShortcutMgr::SUIT_ShortcutMgr()
39 : QObject()
40 {
41   qApp->installEventFilter( this );
42 }
43
44 /*!
45   \brief Destructor
46 */
47 SUIT_ShortcutMgr::~SUIT_ShortcutMgr()
48 {
49   qApp->removeEventFilter( this );
50 }
51
52 /*!
53   \brief Create new instance of shortcut manager.
54 */
55 void SUIT_ShortcutMgr::Init()
56 {
57   if( myShortcutMgr==NULL )
58     myShortcutMgr = new SUIT_ShortcutMgr();
59 }
60
61 /*!
62   \brief Return shortcut manager. 
63 */
64 SUIT_ShortcutMgr* SUIT_ShortcutMgr::getShortcutMgr()
65 {
66   Init();
67   
68   return myShortcutMgr;
69 }
70
71 /*!
72   \brief Custom event filter for qapplication .
73   
74   Redefined from QObject::eventFilter();
75 */
76 bool SUIT_ShortcutMgr::eventFilter( QObject* o, QEvent* e )
77 {
78   if ( e->type() == QEvent::ActionAdded ) {
79     QActionEvent* anActionEvent = (QActionEvent*)e;
80     if (anActionEvent) {
81       QtxAction* anAction = qobject_cast<QtxAction*>( anActionEvent->action() );
82       if ( anAction )
83         processAction( anAction );
84     }
85   }
86
87   return QObject::eventFilter( o, e );
88 }
89
90 /*!
91   \brief Return key sequence for shortcut action name.
92   \param actionName name of shortcut action in preferences
93   \return key sequence defined in preferences or empty sequence
94 */
95 QKeySequence SUIT_ShortcutMgr::getShortcutByActionName( const QString& actionName ) const
96 {
97   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
98
99   QString section = actionName.section( resMgr->sectionsToken(), 0, 0 );
100   section.prepend( QString("shortcuts") + resMgr->sectionsToken() );
101   QString parameter = actionName.section( resMgr->sectionsToken(), 1, 1 );
102
103   QString shortcutValue;
104   bool hasValue = resMgr->value( section, parameter, shortcutValue, false );
105
106   if ( !hasValue )
107     return QKeySequence();
108
109   return QKeySequence::fromString( shortcutValue );
110 }
111
112 /*!
113   \brief Set shortcut to the given action if the shortcut is defined.
114   \param action action to process
115  */
116 void SUIT_ShortcutMgr::processAction( QtxAction* action )
117 {
118   QString shortcutActionName = action->shortcutActionName();
119  
120   if ( !shortcutActionName.isEmpty() ) {
121     // Add action to the actions map
122     if ( !myShortcutActions.contains( shortcutActionName, action ) ) {
123       myShortcutActions.insert( shortcutActionName, action );
124       connect( action, SIGNAL( destroyed( QObject* ) ), 
125                this, SLOT ( onActionDestroyed( QObject* ) ) );
126     }
127
128     QKeySequence keySeq = getShortcutByActionName( shortcutActionName );
129     action->setShortcut( keySeq );
130   }
131 }
132
133 /*!
134   \brief Enable/disable a shortcuts section.
135
136   Enables or disables actions which belong to the given shortcuts section.
137   Only actions which have an active desktop as a parent widget 
138   are taken into account.
139
140   \param section shorcuts section
141   \param on if \c true - action will be enabled, otherwise - disabled
142 */
143 void SUIT_ShortcutMgr::setSectionEnabled( const QString& section, const bool on )
144 {
145   QMap<QString, QtxAction*>::ConstIterator it;
146   for ( it = myShortcutActions.constBegin(); it != myShortcutActions.constEnd(); ++it ) {
147     QtxAction* action = it.value();
148     QString shortcutActionName = action->shortcutActionName();
149     QString actionSection = shortcutActionName.section( ":", 0, 0 );
150     if ( actionSection == section ) {
151       // Check if the action parent widget equals to the active desktop
152       SUIT_Application* app = SUIT_Session::session()->activeApplication();
153       if ( !app )
154         return;
155       if ( action->parentWidget() == (QWidget*)app->desktop() )
156         action->setEnabled( on );
157     }
158   }
159 }
160
161 /*!
162   \brief Update shortcuts from preferences.
163 */
164 void SUIT_ShortcutMgr::updateShortcuts()
165 {
166   QMap<QString, QtxAction*>::ConstIterator it;
167   for ( it = myShortcutActions.constBegin(); it != myShortcutActions.constEnd(); ++it ) {
168     QtxAction* action = it.value();
169     QKeySequence keySeq = getShortcutByActionName( action->shortcutActionName() );
170     action->setShortcut( keySeq );
171   }
172 }
173
174 /*!
175   \brief Called when the corresponding action is destroyed.
176   
177   Removes destroyed action from the actions list.
178
179   \param obj action being destroyed
180 */
181 void SUIT_ShortcutMgr::onActionDestroyed( QObject* obj )
182 {
183   QtxAction* anAction = (QtxAction*)obj;
184   
185   if ( anAction )
186     myShortcutActions.remove( anAction->shortcutActionName(), anAction );
187 }