1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SUIT_ShortcutMgr.h"
25 #include "SUIT_Session.h"
26 #include "SUIT_ResourceMgr.h"
28 #include <QtxAction.h>
30 #include <QApplication>
31 #include <QActionEvent>
33 SUIT_ShortcutMgr* SUIT_ShortcutMgr::myShortcutMgr = NULL;
38 SUIT_ShortcutMgr::SUIT_ShortcutMgr()
41 qApp->installEventFilter( this );
47 SUIT_ShortcutMgr::~SUIT_ShortcutMgr()
49 qApp->removeEventFilter( this );
53 \brief Create new instance of shortcut manager.
55 void SUIT_ShortcutMgr::Init()
57 if( myShortcutMgr==NULL )
58 myShortcutMgr = new SUIT_ShortcutMgr();
62 \brief Return shortcut manager.
64 SUIT_ShortcutMgr* SUIT_ShortcutMgr::getShortcutMgr()
72 \brief Custom event filter for qapplication .
74 Redefined from QObject::eventFilter();
76 bool SUIT_ShortcutMgr::eventFilter( QObject* o, QEvent* e )
78 if ( e->type() == QEvent::ActionAdded ) {
79 QActionEvent* anActionEvent = (QActionEvent*)e;
81 QtxAction* anAction = qobject_cast<QtxAction*>( anActionEvent->action() );
83 processAction( anAction );
87 return QObject::eventFilter( o, e );
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
95 QKeySequence SUIT_ShortcutMgr::getShortcutByActionName( const QString& actionName ) const
97 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
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 );
103 QString shortcutValue;
104 bool hasValue = resMgr->value( section, parameter, shortcutValue, false );
107 return QKeySequence();
109 return QKeySequence::fromString( shortcutValue );
113 \brief Set shortcut to the given action if the shortcut is defined.
114 \param action action to process
116 void SUIT_ShortcutMgr::processAction( QtxAction* action )
118 QString shortcutActionName = action->shortcutActionName();
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* ) ) );
128 QKeySequence keySeq = getShortcutByActionName( shortcutActionName );
129 action->setShortcut( keySeq );
134 \brief Enable/disable a shortcuts section.
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.
140 \param section shorcuts section
141 \param on if \c true - action will be enabled, otherwise - disabled
143 void SUIT_ShortcutMgr::setSectionEnabled( const QString& section, const bool on )
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();
155 if ( action->parentWidget() == (QWidget*)app->desktop() )
156 action->setEnabled( on );
162 \brief Update shortcuts from preferences.
164 void SUIT_ShortcutMgr::updateShortcuts()
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 );
175 \brief Called when the corresponding action is destroyed.
177 Removes destroyed action from the actions list.
179 \param obj action being destroyed
181 void SUIT_ShortcutMgr::onActionDestroyed( QObject* obj )
183 QtxAction* anAction = (QtxAction*)obj;
186 myShortcutActions.remove( anAction->shortcutActionName(), anAction );