KGlobalAccel

globalshortcut.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Michael Jansen <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "globalshortcut.h"
8 
9 #include "kglobalshortcutinfo_p.h"
10 
11 #include "component.h"
12 #include "globalshortcutcontext.h"
13 #include "globalshortcutsregistry.h"
14 #include "logging_p.h"
15 
16 #include <QKeySequence>
17 
18 GlobalShortcut::GlobalShortcut()
19  : _isPresent(false)
20  , _isRegistered(false)
21  , _isFresh(true)
22  , _context(nullptr)
23  , _uniqueName()
24  , _friendlyName()
25  , _keys()
26  , _defaultKeys()
27 {
28 }
29 
30 GlobalShortcut::GlobalShortcut(const QString &uniqueName, const QString &friendlyName, GlobalShortcutContext *context)
31  : _isPresent(false)
32  , _isRegistered(false)
33  , _isFresh(true)
34  , _context(context)
35  , _uniqueName(uniqueName)
36  , _friendlyName(friendlyName)
37  , _keys()
38  , _defaultKeys()
39 {
40  context->addShortcut(this);
41 }
42 
43 GlobalShortcut::~GlobalShortcut()
44 {
45  setInactive();
46 }
47 
48 GlobalShortcut::operator KGlobalShortcutInfo() const
49 {
51  info.d->uniqueName = _uniqueName;
52  info.d->friendlyName = _friendlyName;
53  info.d->contextUniqueName = context()->uniqueName();
54  info.d->contextFriendlyName = context()->friendlyName();
55  info.d->componentUniqueName = context()->component()->uniqueName();
56  info.d->componentFriendlyName = context()->component()->friendlyName();
57  for (int key : std::as_const(_keys)) {
58  info.d->keys.append(QKeySequence(key));
59  }
60  for (int key : std::as_const(_defaultKeys)) {
61  info.d->defaultKeys.append(QKeySequence(key));
62  }
63  return info;
64 }
65 
67 {
68  return _isRegistered;
69 }
70 
72 {
73  return _isFresh;
74 }
75 
77 {
78  return _isPresent;
79 }
80 
82 {
83  return uniqueName().startsWith(QLatin1String("_k_session:"));
84 }
85 
86 void GlobalShortcut::setIsFresh(bool value)
87 {
88  _isFresh = value;
89 }
90 
91 void GlobalShortcut::setIsPresent(bool value)
92 {
93  // (de)activate depending on old/new value
94  _isPresent = value;
95  value ? setActive() : setInactive();
96 }
97 
99 {
100  return _context;
101 }
102 
104 {
105  return _context;
106 }
107 
109 {
110  return _uniqueName;
111 }
112 
114 {
115  return _context->component()->unregisterShortcut(uniqueName());
116 }
117 
119 {
120  return _friendlyName;
121 }
122 
124 {
125  _friendlyName = name;
126 }
127 
129 {
130  return _keys;
131 }
132 
134 {
135  bool active = _isRegistered;
136  if (active) {
137  setInactive();
138  }
139 
140  _keys = QList<int>();
141 
142  for (int key : std::as_const(newKeys)) {
143  if (key != 0 && !GlobalShortcutsRegistry::self()->getShortcutByKey(key)) {
144  _keys.append(key);
145  } else {
146  qCDebug(KGLOBALACCELD) << _uniqueName << "skipping because key" << QKeySequence(key).toString() << "is already taken";
147  _keys.append(0);
148  }
149  }
150 
151  if (active) {
152  setActive();
153  }
154 }
155 
157 {
158  return _defaultKeys;
159 }
160 
162 {
163  _defaultKeys = newKeys;
164 }
165 
167 {
168  if (!_isPresent || _isRegistered) {
169  // The corresponding application is not present or the keys are
170  // already grabbed
171  return;
172  }
173 
174  for (int key : std::as_const(_keys)) {
175  if (key != 0 && !GlobalShortcutsRegistry::self()->registerKey(key, this)) {
176  qCDebug(KGLOBALACCELD) << uniqueName() << ": Failed to register " << QKeySequence(key).toString();
177  }
178  }
179 
180  _isRegistered = true;
181 }
182 
184 {
185  if (!_isRegistered) {
186  // The keys are not grabbed currently
187  return;
188  }
189 
190  for (int key : std::as_const(_keys)) {
191  if (key != 0 && !GlobalShortcutsRegistry::self()->unregisterKey(key, this)) {
192  qCDebug(KGLOBALACCELD) << uniqueName() << ": Failed to unregister " << QKeySequence(key).toString();
193  }
194  }
195 
196  _isRegistered = false;
197 }
bool isFresh() const
Check if the shortcut is fresh/new. Is an internal state.
QString & append(QChar ch)
bool isPresent() const
Check if the shortcut is present. It application is running.
GlobalShortcutContext * context()
Returns the context the shortcuts belongs to.
void setInactive()
Sets the shortcut inactive. No longer grabs the keys.
QString uniqueName() const
Returns the unique name aka id for the shortcuts.
void addShortcut(GlobalShortcut *shortcut)
Adds shortcut to the context.
void append(const T &value)
QString friendlyName() const
Returns the friendly name.
void setFriendlyName(const QString &)
Sets the friendly name for the shortcut. For display.
void unRegister()
Remove this shortcut and it&#39;s siblings.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QList< int > keys() const
Returns a list of keys associated with this shortcut.
bool isActive() const
Check if the shortcut is active. It&#39;s keys are grabbed.
QString toString(QKeySequence::SequenceFormat format) const const
QList< int > defaultKeys() const
Returns the default keys for this shortcut.
GlobalShortcut * getShortcutByKey(int key) const
Get the shortcut corresponding to key.
void setKeys(const QList< int >)
Sets the keys activated with this shortcut. The old keys are freed.
void setActive()
Activates the shortcut. The keys are grabbed.
void setDefaultKeys(const QList< int >)
Sets the default keys for this shortcut.
bool isSessionShortcut() const
Returns true if the shortcut is a session shortcut.
QString uniqueName() const
Get the name for the context.
QString friendlyName() const
Return the friendly display name for this shortcut.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 22:48:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.