From 483e07365ee943b19f8078a4951090b6a9b87a65 Mon Sep 17 00:00:00 2001 From: Shatur95 Date: Wed, 1 Jul 2020 23:16:13 +0300 Subject: [PATCH] Improve error messages * Show information about shortcut. * Make errors translatable. --- QHotkey/qhotkey.cpp | 14 ++++++++++---- QHotkey/qhotkey_mac.cpp | 6 ++---- QHotkey/qhotkey_p.h | 2 ++ QHotkey/qhotkey_win.cpp | 6 ++---- QHotkey/qhotkey_x11.cpp | 6 ++---- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index ccff07f..7b6a399 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -279,8 +279,10 @@ bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey) QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut; if(!shortcuts.contains(shortcut)) { - if(!registerShortcut(shortcut)) + if(!registerShortcut(shortcut)) { + qCWarning(logQHotkey) << QHotkey::tr("Failed to register %1. Error: %2").arg(hotkey->shortcut().toString(), error); return false; + } } shortcuts.insert(shortcut, hotkey); @@ -296,9 +298,13 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey) return false; hotkey->_registered = false; emit hotkey->registeredChanged(true); - if(shortcuts.count(shortcut) == 0) - return unregisterShortcut(shortcut); - else + if(shortcuts.count(shortcut) == 0) { + if (!unregisterShortcut(shortcut)) { + qCWarning(logQHotkey) << QHotkey::tr("Failed to unregister %1. Error: %2").arg(hotkey->shortcut().toString(), error); + return false; + } else + return true; + } else return true; } diff --git a/QHotkey/qhotkey_mac.cpp b/QHotkey/qhotkey_mac.cpp index 11e765e..0e06299 100644 --- a/QHotkey/qhotkey_mac.cpp +++ b/QHotkey/qhotkey_mac.cpp @@ -227,8 +227,7 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut) 0, &eventRef); if (status != noErr) { - qCWarning(logQHotkey) << "Failed to register hotkey. Error:" - << status; + error = QString::number(status); return false; } else { this->hotkeyRefs.insert(shortcut, eventRef); @@ -241,8 +240,7 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut) EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut); OSStatus status = UnregisterEventHotKey(eventRef); if (status != noErr) { - qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:" - << status; + error = QString::number(status); return false; } else { this->hotkeyRefs.remove(shortcut); diff --git a/QHotkey/qhotkey_p.h b/QHotkey/qhotkey_p.h index 8fce3ae..eec3bd6 100644 --- a/QHotkey/qhotkey_p.h +++ b/QHotkey/qhotkey_p.h @@ -33,6 +33,8 @@ class QHOTKEY_SHARED_EXPORT QHotkeyPrivate : public QObject, public QAbstractNat virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement + QString error; + private: QHash, QHotkey::NativeShortcut> mapping; QMultiHash shortcuts; diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp index 244ed51..22c64a8 100644 --- a/QHotkey/qhotkey_win.cpp +++ b/QHotkey/qhotkey_win.cpp @@ -267,8 +267,7 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut) if(ok) return true; else { - qCWarning(logQHotkey) << "Failed to register hotkey. Error:" - << qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError())); + error = QHotkeyPrivateWin::formatWinError(::GetLastError()); return false; } } @@ -279,8 +278,7 @@ bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut) if(ok) return true; else { - qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:" - << qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError())); + error = QHotkeyPrivateWin::formatWinError(::GetLastError()); return false; } } diff --git a/QHotkey/qhotkey_x11.cpp b/QHotkey/qhotkey_x11.cpp index 5606177..1c99939 100644 --- a/QHotkey/qhotkey_x11.cpp +++ b/QHotkey/qhotkey_x11.cpp @@ -170,8 +170,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut) XSync(display, False); if(errorHandler.hasError) { - qCWarning(logQHotkey) << "Failed to register hotkey. Error:" - << qPrintable(errorHandler.errorString); + error = errorHandler.errorString; this->unregisterShortcut(shortcut); return false; } else @@ -194,8 +193,7 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut) XSync(display, False); if(errorHandler.hasError) { - qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:" - << qPrintable(errorHandler.errorString); + error = errorHandler.errorString; return false; } else return true;