1/* Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
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 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "globalshortcutcontext.h"
20
21#include "globalshortcut.h"
22
23#include "kdebug.h"
24
25GlobalShortcutContext::GlobalShortcutContext(
26 const QString &uniqueName,
27 const QString &friendlyName,
28 KdeDGlobalAccel::Component *component)
29
30 : _uniqueName(uniqueName),
31 _friendlyName(friendlyName),
32 _component(component),
33 _actions()
34 {}
35
36
37GlobalShortcutContext::~GlobalShortcutContext()
38 {
39 qDeleteAll(_actions); _actions.clear();
40 }
41
42
43void GlobalShortcutContext::addShortcut(GlobalShortcut *shortcut)
44 {
45 _actions.insert(shortcut->uniqueName(), shortcut);
46 }
47
48
49QList<KGlobalShortcutInfo> GlobalShortcutContext::allShortcutInfos() const
50 {
51 QList<KGlobalShortcutInfo> rc;
52 Q_FOREACH (GlobalShortcut *shortcut, _actions)
53 {
54 rc.append(static_cast<KGlobalShortcutInfo>(*shortcut));
55 }
56 return rc;
57 }
58
59
60KdeDGlobalAccel::Component const *GlobalShortcutContext::component() const
61 {
62 return _component;
63 }
64
65
66KdeDGlobalAccel::Component *GlobalShortcutContext::component()
67 {
68 return _component;
69 }
70
71
72QString GlobalShortcutContext::friendlyName() const
73 {
74 return _friendlyName;
75 }
76
77
78GlobalShortcut *GlobalShortcutContext::getShortcutByKey(int key) const
79 {
80 // Qt triggers both shortcuts that include Shift+Backtab and Shift+Tab
81 // when user presses Shift+Tab. Do the same here.
82 int keySym = key & ~Qt::KeyboardModifierMask;
83 int keyMod = key & Qt::KeyboardModifierMask;
84 if ((keyMod & Qt::SHIFT) && (keySym == Qt::Key_Backtab ||
85 keySym == Qt::Key_Tab))
86 {
87 Q_FOREACH(GlobalShortcut *sc, _actions)
88 {
89 if (sc->keys().contains(keyMod | Qt::Key_Tab) ||
90 sc->keys().contains(keyMod | Qt::Key_Backtab))
91 return sc;
92 }
93 }
94 else
95 {
96 Q_FOREACH(GlobalShortcut *sc, _actions)
97 {
98 if (sc->keys().contains(key)) return sc;
99 }
100 }
101 return NULL;
102 }
103
104
105GlobalShortcut *GlobalShortcutContext::takeShortcut(GlobalShortcut *shortcut)
106 {
107 // Try to take the shortcut. Result could be null if the shortcut doesn't
108 // belong to this component.
109 return _actions.take(shortcut->uniqueName());
110 }
111
112
113QString GlobalShortcutContext::uniqueName() const
114 {
115 return _uniqueName;
116 }
117