@@ -25,14 +25,13 @@ QHotkey::QHotkey(QObject *parent) :
25
25
QObject(parent),
26
26
_keyCode(Qt::Key_unknown),
27
27
_modifiers(Qt::NoModifier),
28
- _nativeShortcut(),
29
28
_registered(false )
30
29
{}
31
30
32
- QHotkey::QHotkey (const QKeySequence &sequence , bool autoRegister, QObject *parent) :
31
+ QHotkey::QHotkey (const QKeySequence &shortcut , bool autoRegister, QObject *parent) :
33
32
QHotkey(parent)
34
33
{
35
- setShortcut (sequence , autoRegister);
34
+ setShortcut (shortcut , autoRegister);
36
35
}
37
36
38
37
QHotkey::QHotkey (Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
@@ -57,8 +56,7 @@ QKeySequence QHotkey::shortcut() const
57
56
{
58
57
if (_keyCode == Qt::Key_unknown)
59
58
return QKeySequence ();
60
- else
61
- return QKeySequence (_keyCode | _modifiers);
59
+ return QKeySequence (static_cast <int >(_keyCode | _modifiers));
62
60
}
63
61
64
62
Qt::Key QHotkey::keyCode () const
@@ -83,9 +81,9 @@ bool QHotkey::isRegistered() const
83
81
84
82
bool QHotkey::setShortcut (const QKeySequence &shortcut, bool autoRegister)
85
83
{
86
- if (shortcut.isEmpty ()) {
84
+ if (shortcut.isEmpty ())
87
85
return resetShortcut ();
88
- } else if (shortcut.count () > 1 ) {
86
+ if (shortcut.count () > 1 ) {
89
87
qCWarning (logQHotkey, " Keysequences with multiple shortcuts are not allowed! "
90
88
" Only the first shortcut will be used!" );
91
89
}
@@ -118,15 +116,14 @@ bool QHotkey::setShortcut(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool
118
116
if (_nativeShortcut.isValid ()) {
119
117
if (autoRegister)
120
118
return QHotkeyPrivate::instance ()->addShortcut (this );
121
- else
122
- return true ;
123
- } else {
124
- qCWarning (logQHotkey) << " Unable to map shortcut to native keys. Key:" << keyCode << " Modifiers:" << modifiers;
125
- _keyCode = Qt::Key_unknown;
126
- _modifiers = Qt::NoModifier;
127
- _nativeShortcut = NativeShortcut ();
128
- return false ;
119
+ return true ;
129
120
}
121
+
122
+ qCWarning (logQHotkey) << " Unable to map shortcut to native keys. Key:" << keyCode << " Modifiers:" << modifiers;
123
+ _keyCode = Qt::Key_unknown;
124
+ _modifiers = Qt::NoModifier;
125
+ _nativeShortcut = NativeShortcut ();
126
+ return false ;
130
127
}
131
128
132
129
bool QHotkey::resetShortcut ()
@@ -158,35 +155,32 @@ bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool aut
158
155
_nativeShortcut = nativeShortcut;
159
156
if (autoRegister)
160
157
return QHotkeyPrivate::instance ()->addShortcut (this );
161
- else
162
- return true ;
163
- } else {
164
- _keyCode = Qt::Key_unknown;
165
- _modifiers = Qt::NoModifier;
166
- _nativeShortcut = NativeShortcut ();
167
158
return true ;
168
- }
159
+ }
160
+
161
+ _keyCode = Qt::Key_unknown;
162
+ _modifiers = Qt::NoModifier;
163
+ _nativeShortcut = NativeShortcut ();
164
+ return true ;
169
165
}
170
166
171
167
bool QHotkey::setRegistered (bool registered)
172
168
{
173
169
if (_registered && !registered)
174
170
return QHotkeyPrivate::instance ()->removeShortcut (this );
175
- else if (!_registered && registered) {
171
+ if (!_registered && registered) {
176
172
if (!_nativeShortcut.isValid ())
177
173
return false ;
178
- else
179
- return QHotkeyPrivate::instance ()->addShortcut (this );
180
- } else
181
- return true ;
174
+ return QHotkeyPrivate::instance ()->addShortcut (this );
175
+ }
176
+ return true ;
182
177
}
183
178
184
179
185
180
186
181
// ---------- QHotkeyPrivate implementation ----------
187
182
188
- QHotkeyPrivate::QHotkeyPrivate () :
189
- shortcuts()
183
+ QHotkeyPrivate::QHotkeyPrivate ()
190
184
{
191
185
Q_ASSERT_X (qApp, Q_FUNC_INFO, " QHotkey requires QCoreApplication to be instantiated" );
192
186
qApp->eventDispatcher ()->installNativeEventFilter (this );
@@ -211,8 +205,8 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb
211
205
Q_ARG (Qt::Key, keycode),
212
206
Q_ARG (Qt::KeyboardModifiers, modifiers))) {
213
207
return QHotkey::NativeShortcut ();
214
- } else
215
- return res;
208
+ }
209
+ return res;
216
210
}
217
211
218
212
bool QHotkeyPrivate::addShortcut (QHotkey *hotkey)
@@ -228,11 +222,11 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
228
222
Q_RETURN_ARG (bool , res),
229
223
Q_ARG (QHotkey*, hotkey))) {
230
224
return false ;
231
- } else {
232
- if (res)
233
- emit hotkey->registeredChanged (true );
234
- return res;
235
225
}
226
+
227
+ if (res)
228
+ emit hotkey->registeredChanged (true );
229
+ return res;
236
230
}
237
231
238
232
bool QHotkeyPrivate::removeShortcut (QHotkey *hotkey)
@@ -248,11 +242,11 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
248
242
Q_RETURN_ARG (bool , res),
249
243
Q_ARG (QHotkey*, hotkey))) {
250
244
return false ;
251
- } else {
252
- if (res)
253
- emit hotkey->registeredChanged (false );
254
- return res;
255
245
}
246
+
247
+ if (res)
248
+ emit hotkey->registeredChanged (false );
249
+ return res;
256
250
}
257
251
258
252
void QHotkeyPrivate::activateShortcut (QHotkey::NativeShortcut shortcut)
@@ -302,24 +296,24 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
302
296
if (!unregisterShortcut (shortcut)) {
303
297
qCWarning (logQHotkey) << QHotkey::tr (" Failed to unregister %1. Error: %2" ).arg (hotkey->shortcut ().toString (), error);
304
298
return false ;
305
- } else
306
- return true ;
307
- } else
299
+ }
308
300
return true ;
301
+ }
302
+ return true ;
309
303
}
310
304
311
305
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked (Qt::Key keycode, Qt::KeyboardModifiers modifiers)
312
306
{
313
307
if (mapping.contains ({keycode, modifiers}))
314
308
return mapping.value ({keycode, modifiers});
315
309
316
- bool ok1, ok2 = false ;
310
+ bool ok1 = false ;
317
311
auto k = nativeKeycode (keycode, ok1);
312
+ bool ok2 = false ;
318
313
auto m = nativeModifiers (modifiers, ok2);
319
314
if (ok1 && ok2)
320
315
return {k, m};
321
- else
322
- return {};
316
+ return {};
323
317
}
324
318
325
319
0 commit comments