Fix Clang Tidy warnings

readability-convert-member-functions-to-static
modernize-use-auto
readability-else-after-return
readability-static-accessed-through-instance
readability-inconsistent-declaration-parameter-name
This commit is contained in:
Shatur95
2020-10-15 21:50:58 +03:00
parent 53745ba936
commit f0799fd4a4
2 changed files with 51 additions and 57 deletions
+21 -27
View File
@@ -25,14 +25,13 @@ QHotkey::QHotkey(QObject *parent) :
QObject(parent), QObject(parent),
_keyCode(Qt::Key_unknown), _keyCode(Qt::Key_unknown),
_modifiers(Qt::NoModifier), _modifiers(Qt::NoModifier),
_nativeShortcut(),
_registered(false) _registered(false)
{} {}
QHotkey::QHotkey(const QKeySequence &sequence, bool autoRegister, QObject *parent) : QHotkey::QHotkey(const QKeySequence &shortcut, bool autoRegister, QObject *parent) :
QHotkey(parent) QHotkey(parent)
{ {
setShortcut(sequence, autoRegister); setShortcut(shortcut, autoRegister);
} }
QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) : QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
@@ -57,8 +56,7 @@ QKeySequence QHotkey::shortcut() const
{ {
if(_keyCode == Qt::Key_unknown) if(_keyCode == Qt::Key_unknown)
return QKeySequence(); return QKeySequence();
else return QKeySequence(static_cast<int>(_keyCode | _modifiers));
return QKeySequence(_keyCode | _modifiers);
} }
Qt::Key QHotkey::keyCode() const Qt::Key QHotkey::keyCode() const
@@ -83,9 +81,9 @@ bool QHotkey::isRegistered() const
bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister) bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
{ {
if(shortcut.isEmpty()) { if(shortcut.isEmpty())
return resetShortcut(); return resetShortcut();
} else if(shortcut.count() > 1) { if(shortcut.count() > 1) {
qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! " qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
"Only the first shortcut will be used!"); "Only the first shortcut will be used!");
} }
@@ -118,16 +116,15 @@ bool QHotkey::setShortcut(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool
if(_nativeShortcut.isValid()) { if(_nativeShortcut.isValid()) {
if(autoRegister) if(autoRegister)
return QHotkeyPrivate::instance()->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
else
return true; return true;
} else { }
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers; qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers;
_keyCode = Qt::Key_unknown; _keyCode = Qt::Key_unknown;
_modifiers = Qt::NoModifier; _modifiers = Qt::NoModifier;
_nativeShortcut = NativeShortcut(); _nativeShortcut = NativeShortcut();
return false; return false;
} }
}
bool QHotkey::resetShortcut() bool QHotkey::resetShortcut()
{ {
@@ -158,26 +155,24 @@ bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool aut
_nativeShortcut = nativeShortcut; _nativeShortcut = nativeShortcut;
if(autoRegister) if(autoRegister)
return QHotkeyPrivate::instance()->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
else
return true; return true;
} else { }
_keyCode = Qt::Key_unknown; _keyCode = Qt::Key_unknown;
_modifiers = Qt::NoModifier; _modifiers = Qt::NoModifier;
_nativeShortcut = NativeShortcut(); _nativeShortcut = NativeShortcut();
return true; return true;
} }
}
bool QHotkey::setRegistered(bool registered) bool QHotkey::setRegistered(bool registered)
{ {
if(_registered && !registered) if(_registered && !registered)
return QHotkeyPrivate::instance()->removeShortcut(this); return QHotkeyPrivate::instance()->removeShortcut(this);
else if(!_registered && registered) { if(!_registered && registered) {
if(!_nativeShortcut.isValid()) if(!_nativeShortcut.isValid())
return false; return false;
else
return QHotkeyPrivate::instance()->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
} else }
return true; return true;
} }
@@ -185,8 +180,7 @@ bool QHotkey::setRegistered(bool registered)
// ---------- QHotkeyPrivate implementation ---------- // ---------- QHotkeyPrivate implementation ----------
QHotkeyPrivate::QHotkeyPrivate() : QHotkeyPrivate::QHotkeyPrivate()
shortcuts()
{ {
Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated"); Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated");
qApp->eventDispatcher()->installNativeEventFilter(this); qApp->eventDispatcher()->installNativeEventFilter(this);
@@ -211,7 +205,7 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb
Q_ARG(Qt::Key, keycode), Q_ARG(Qt::Key, keycode),
Q_ARG(Qt::KeyboardModifiers, modifiers))) { Q_ARG(Qt::KeyboardModifiers, modifiers))) {
return QHotkey::NativeShortcut(); return QHotkey::NativeShortcut();
} else }
return res; return res;
} }
@@ -228,12 +222,12 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
Q_RETURN_ARG(bool, res), Q_RETURN_ARG(bool, res),
Q_ARG(QHotkey*, hotkey))) { Q_ARG(QHotkey*, hotkey))) {
return false; return false;
} else { }
if(res) if(res)
emit hotkey->registeredChanged(true); emit hotkey->registeredChanged(true);
return res; return res;
} }
}
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey) bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
{ {
@@ -248,12 +242,12 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
Q_RETURN_ARG(bool, res), Q_RETURN_ARG(bool, res),
Q_ARG(QHotkey*, hotkey))) { Q_ARG(QHotkey*, hotkey))) {
return false; return false;
} else { }
if(res) if(res)
emit hotkey->registeredChanged(false); emit hotkey->registeredChanged(false);
return res; return res;
} }
}
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut) void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
{ {
@@ -302,9 +296,9 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
if (!unregisterShortcut(shortcut)) { if (!unregisterShortcut(shortcut)) {
qCWarning(logQHotkey) << QHotkey::tr("Failed to unregister %1. Error: %2").arg(hotkey->shortcut().toString(), error); qCWarning(logQHotkey) << QHotkey::tr("Failed to unregister %1. Error: %2").arg(hotkey->shortcut().toString(), error);
return false; return false;
} else }
return true; return true;
} else }
return true; return true;
} }
@@ -313,12 +307,12 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Q
if(mapping.contains({keycode, modifiers})) if(mapping.contains({keycode, modifiers}))
return mapping.value({keycode, modifiers}); return mapping.value({keycode, modifiers});
bool ok1, ok2 = false; bool ok1 = false;
auto k = nativeKeycode(keycode, ok1); auto k = nativeKeycode(keycode, ok1);
bool ok2 = false;
auto m = nativeModifiers(modifiers, ok2); auto m = nativeModifiers(modifiers, ok2);
if(ok1 && ok2) if(ok1 && ok2)
return {k, m}; return {k, m};
else
return {}; return {};
} }
+9 -9
View File
@@ -22,7 +22,7 @@ protected:
// QHotkeyPrivate interface // QHotkeyPrivate interface
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE; quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE; quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
QString getX11String(Qt::Key keycode); static QString getX11String(Qt::Key keycode);
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE; bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE; bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
@@ -64,7 +64,7 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
Q_UNUSED(eventType) Q_UNUSED(eventType)
Q_UNUSED(result) Q_UNUSED(result)
xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message); auto *genericEvent = static_cast<xcb_generic_event_t *>(message);
if (genericEvent->response_type == XCB_KEY_PRESS) { if (genericEvent->response_type == XCB_KEY_PRESS) {
xcb_key_press_event_t keyEvent = *static_cast<xcb_key_press_event_t *>(message); xcb_key_press_event_t keyEvent = *static_cast<xcb_key_press_event_t *>(message);
this->prevEvent = keyEvent; this->prevEvent = keyEvent;
@@ -75,7 +75,7 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
} else if (genericEvent->response_type == XCB_KEY_RELEASE) { } else if (genericEvent->response_type == XCB_KEY_RELEASE) {
xcb_key_release_event_t keyEvent = *static_cast<xcb_key_release_event_t *>(message); xcb_key_release_event_t keyEvent = *static_cast<xcb_key_release_event_t *>(message);
this->prevEvent = keyEvent; this->prevEvent = keyEvent;
QTimer *timer = new QTimer(this); auto *timer = new QTimer(this);
timer->setSingleShot(true); timer->setSingleShot(true);
timer->setInterval(50); timer->setInterval(50);
connect(timer, &QTimer::timeout, this, [this, keyEvent, timer] { connect(timer, &QTimer::timeout, this, [this, keyEvent, timer] {
@@ -132,7 +132,7 @@ quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok)
if(res != 0) if(res != 0)
ok = true; ok = true;
return res; return res;
} else }
return 0; return 0;
} }
@@ -173,7 +173,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
error = errorHandler.errorString; error = errorHandler.errorString;
this->unregisterShortcut(shortcut); this->unregisterShortcut(shortcut);
return false; return false;
} else }
return true; return true;
} }
@@ -188,14 +188,14 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
XUngrabKey(display, XUngrabKey(display,
shortcut.key, shortcut.key,
shortcut.modifier | specialMod, shortcut.modifier | specialMod,
DefaultRootWindow(display)); XDefaultRootWindow(display));
} }
XSync(display, False); XSync(display, False);
if(errorHandler.hasError) { if(HotkeyErrorHandler::hasError) {
error = errorHandler.errorString; error = HotkeyErrorHandler::errorString;
return false; return false;
} else }
return true; return true;
} }