changed private part completly -> rdy win

This commit is contained in:
Skycoder42
2016-02-06 13:54:20 +01:00
parent a5eb16973d
commit e5d4d103be
6 changed files with 75 additions and 119 deletions
+3 -3
View File
@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>656</width> <width>424</width>
<height>467</height> <height>373</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@@ -909,7 +909,7 @@
<property name="sizeHint" stdset="0"> <property name="sizeHint" stdset="0">
<size> <size>
<width>20</width> <width>20</width>
<height>40</height> <height>0</height>
</size> </size>
</property> </property>
</spacer> </spacer>
+12 -28
View File
@@ -1,13 +1,12 @@
#include "qhotkey.h" #include "qhotkey.h"
#include "qhotkey_p.h" #include "qhotkey_p.h"
#include <QGlobalStatic>
#include <QCoreApplication> #include <QCoreApplication>
#include <QAbstractEventDispatcher> #include <QAbstractEventDispatcher>
#include <QMetaMethod> #include <QMetaMethod>
Q_GLOBAL_STATIC(QHotkeyPrivate, hotkeyPrivate)
#define LOCKER QMutexLocker locker(&this->mutex) #define LOCKER QMutexLocker locker(&this->mutex)
#include <QDebug>
QHotkey::QHotkey(QObject *parent) : QHotkey::QHotkey(QObject *parent) :
QObject(parent), QObject(parent),
key(Qt::Key_unknown), key(Qt::Key_unknown),
@@ -31,12 +30,12 @@ QHotkey::QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister
QHotkey::~QHotkey() QHotkey::~QHotkey()
{ {
if(this->registered) if(this->registered)
hotkeyPrivate->removeShortcut(this); QHotkeyPrivate::instance()->removeShortcut(this);
} }
bool QHotkey::isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers) bool QHotkey::isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers)
{ {
return hotkeyPrivate->hasShortcut(key, modifiers); return QHotkeyPrivate::instance()->hasShortcut(key, modifiers);
} }
QKeySequence QHotkey::shortcut() const QKeySequence QHotkey::shortcut() const
@@ -80,7 +79,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
{ {
if(this->registered) { if(this->registered) {
if(autoRegister) { if(autoRegister) {
if(!hotkeyPrivate->removeShortcut(this)) if(!QHotkeyPrivate::instance()->removeShortcut(this))
return false; return false;
} else { } else {
qWarning("QHotkey: Can't change the shortcut while the hotkey is registered"); qWarning("QHotkey: Can't change the shortcut while the hotkey is registered");
@@ -90,10 +89,10 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
this->key = key; this->key = key;
this->mods = modifiers; this->mods = modifiers;
this->nativeShortcut = hotkeyPrivate->nativeShortcut(key, modifiers); this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers);
if(this->nativeShortcut.isValid()) { if(QHotkeyPrivate::testValid(this->nativeShortcut)) {
if(autoRegister) if(autoRegister)
return hotkeyPrivate->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
else else
return true; return true;
} else { } else {
@@ -108,7 +107,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
bool QHotkey::resetShortcut() bool QHotkey::resetShortcut()
{ {
if(this->registered && if(this->registered &&
!hotkeyPrivate->removeShortcut(this)) { !QHotkeyPrivate::instance()->removeShortcut(this)) {
qWarning("QHotkey: Failed to unregister shortcut"); qWarning("QHotkey: Failed to unregister shortcut");
return false; return false;
} }
@@ -122,12 +121,12 @@ bool QHotkey::resetShortcut()
bool QHotkey::setRegistered(bool registered) bool QHotkey::setRegistered(bool registered)
{ {
if(this->registered && !registered) if(this->registered && !registered)
return hotkeyPrivate->removeShortcut(this); return QHotkeyPrivate::instance()->removeShortcut(this);
else if(!this->registered && registered) { else if(!this->registered && registered) {
if(!this->nativeShortcut.isValid()) if(!QHotkeyPrivate::testValid(this->nativeShortcut))
return false; return false;
else else
return hotkeyPrivate->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
} else } else
return true; return true;
} }
@@ -146,27 +145,12 @@ QHotkeyPrivate::QHotkeyPrivate() :
QHotkeyPrivate::~QHotkeyPrivate() QHotkeyPrivate::~QHotkeyPrivate()
{ {
if(!this->shortcuts.isEmpty()) { if(!this->shortcuts.isEmpty())
qWarning("QHotkey: QHotkeyPrivate destroyed with registered shortcuts!"); qWarning("QHotkey: QHotkeyPrivate destroyed with registered shortcuts!");
for(QHotkey::NativeShortcut shortcut : this->shortcuts.keys())
this->unregisterShortcut(shortcut);
}
if(qApp && qApp->eventDispatcher()) if(qApp && qApp->eventDispatcher())
qApp->eventDispatcher()->removeNativeEventFilter(this); qApp->eventDispatcher()->removeNativeEventFilter(this);
} }
QHotkeyPrivate *QHotkeyPrivate::instance()
{
return hotkeyPrivate;
}
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
QHotkey::NativeShortcut shortcut;
shortcut.key = nativeKeycode(keycode);
shortcut.mods = nativeModifiers(modifiers);
return shortcut;
}
bool QHotkeyPrivate::hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) bool QHotkeyPrivate::hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
{ {
return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers)); return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers));
+2 -17
View File
@@ -9,12 +9,13 @@ class QHotkey : public QObject
{ {
Q_OBJECT Q_OBJECT
friend class QHotkeyPrivate; friend class QHotkeyPrivate;
friend class QHotkeyPrivateNative;
Q_PROPERTY(bool registered READ isRegistered WRITE setRegistered NOTIFY registeredChanged) Q_PROPERTY(bool registered READ isRegistered WRITE setRegistered NOTIFY registeredChanged)
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut) Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut)
public: public:
typedef QPair<quint32, quint32> NativeShortcut;
explicit QHotkey(QObject *parent = 0); explicit QHotkey(QObject *parent = 0);
explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = true, QObject *parent = 0); explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = true, QObject *parent = 0);
explicit QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister = true, QObject *parent = 0); explicit QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister = true, QObject *parent = 0);
@@ -40,22 +41,6 @@ signals:
void registeredChanged(bool registered); void registeredChanged(bool registered);
private: private:
struct NativeShortcut {
quint32 key;
quint32 mods;
inline NativeShortcut();
inline NativeShortcut(quint32 key, quint32 mods);
inline NativeShortcut(const NativeShortcut &other);
inline bool isValid() const;
inline NativeShortcut &operator =(const NativeShortcut &other);
inline bool operator==(const NativeShortcut &other) const;
};
friend uint qHash(QHotkey::NativeShortcut,uint);
Qt::Key key; Qt::Key key;
Qt::KeyboardModifiers mods; Qt::KeyboardModifiers mods;
+4 -4
View File
@@ -9,11 +9,11 @@ public:
static OSStatus hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data); static OSStatus hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
static bool isHotkeyHandlerRegistered; static bool isHotkeyHandlerRegistered;
static QHash<QHotkey::NativeShortcut, EventHotKeyRef> hotkeyRefs; static QHash<NativeShortcut, EventHotKeyRef> hotkeyRefs;
}; };
bool QHotkeyPrivateNative::isHotkeyHandlerRegistered = false; bool QHotkeyPrivateNative::isHotkeyHandlerRegistered = false;
QHash<QHotkey::NativeShortcut, EventHotKeyRef> QHotkeyPrivateNative::hotkeyRefs; QHash<NativeShortcut, EventHotKeyRef> QHotkeyPrivateNative::hotkeyRefs;
bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *message, long *result) bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{ {
@@ -174,7 +174,7 @@ quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
return nMods; return nMods;
} }
bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivate::registerShortcut(NativeShortcut shortcut)
{ {
if (!QHotkeyPrivateNative::isHotkeyHandlerRegistered) if (!QHotkeyPrivateNative::isHotkeyHandlerRegistered)
{ {
@@ -204,7 +204,7 @@ bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
} }
} }
bool QHotkeyPrivate::unregisterShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivate::unregisterShortcut(NativeShortcut shortcut)
{ {
EventHotKeyRef eventRef = QHotkeyPrivateNative::hotkeyRefs.value(shortcut); EventHotKeyRef eventRef = QHotkeyPrivateNative::hotkeyRefs.value(shortcut);
OSStatus status = UnregisterEventHotKey(eventRef); OSStatus status = UnregisterEventHotKey(eventRef);
+22 -49
View File
@@ -5,80 +5,53 @@
#include <QAbstractNativeEventFilter> #include <QAbstractNativeEventFilter>
#include <QMultiHash> #include <QMultiHash>
#include <QMutex> #include <QMutex>
#include <QGlobalStatic>
class QHotkeyPrivate : public QAbstractNativeEventFilter class QHotkeyPrivate : public QAbstractNativeEventFilter
{ {
friend class QHotkeyPrivateNative;
public: public:
QHotkeyPrivate();//singleton!!! QHotkeyPrivate();//singleton!!!
~QHotkeyPrivate(); ~QHotkeyPrivate();
static QHotkeyPrivate *instance(); static QHotkeyPrivate *instance();
static inline bool testValid(QHotkey::NativeShortcut nativeShortcut);
// QAbstractNativeEventFilter interface inline QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers); bool hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool addShortcut(QHotkey *hotkey); bool addShortcut(QHotkey *hotkey);
bool removeShortcut(QHotkey *hotkey); bool removeShortcut(QHotkey *hotkey);
protected:
void activateShortcut(QHotkey::NativeShortcut shortcut);
virtual quint32 nativeKeycode(Qt::Key keycode) = 0;//platform implement
virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) = 0;//platform implement
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
private: private:
QMutex mutex; QMutex mutex;
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts; QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
void activateShortcut(QHotkey::NativeShortcut shortcut);
quint32 nativeKeycode(Qt::Key keycode);//platform implement
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers);//platform implement
bool registerShortcut(QHotkey::NativeShortcut shortcut);//platform implement
bool unregisterShortcut(QHotkey::NativeShortcut shortcut);//platform implement
}; };
uint qHash(QHotkey::NativeShortcut key, uint seed = 0); #define NATIVE_INSTANCE(ClassName) \
Q_GLOBAL_STATIC(ClassName, hotkeyPrivate) \
\
QHotkeyPrivate *QHotkeyPrivate::instance()\
// ---------- QHotkey::NativeShortcut implementation ---------- {\
return hotkeyPrivate;\
inline QHotkey::NativeShortcut::NativeShortcut() :
key(0),
mods(0)
{}
inline QHotkey::NativeShortcut::NativeShortcut(quint32 key, quint32 mods) :
key(key),
mods(mods)
{}
inline QHotkey::NativeShortcut::NativeShortcut(const QHotkey::NativeShortcut &other) :
key(other.key),
mods(other.mods)
{}
inline QHotkey::NativeShortcut &QHotkey::NativeShortcut::operator =(const QHotkey::NativeShortcut &other)
{
this->key = other.key;
this->mods = other.mods;
return (*this);
} }
inline bool QHotkey::NativeShortcut::isValid() const inline bool QHotkeyPrivate::testValid(QHotkey::NativeShortcut nativeShortcut)
{ {
return (this->key != 0); return (nativeShortcut.first != 0);
} }
inline bool QHotkey::NativeShortcut::operator==(const QHotkey::NativeShortcut &other) const inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
{ return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
return (this->key == other.key && this->mods == other.mods);
}
inline uint qHash(QHotkey::NativeShortcut key, uint seed)
{
return qHash(key.key ^ key.mods, seed);
} }
#endif // QHOTKEY_P_H #endif // QHOTKEY_P_H
+31 -17
View File
@@ -3,25 +3,39 @@
#include <qt_windows.h> #include <qt_windows.h>
#include <QDebug> #include <QDebug>
static QString formatWinError(DWORD winError); #define HKEY_ID(nativeShortcut) (((nativeShortcut.first ^ (nativeShortcut.second << 8)) & 0x0FFF) | 0x7000)
bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *message, long *result) class QHotkeyPrivateWin : public QHotkeyPrivate
{
public:
// QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
protected:
// QHotkeyPrivate interface
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
private:
static QString formatWinError(DWORD winError);
};
NATIVE_INSTANCE(QHotkeyPrivateWin)
bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{ {
Q_UNUSED(eventType); Q_UNUSED(eventType);
Q_UNUSED(result); Q_UNUSED(result);
MSG* msg = static_cast<MSG*>(message); MSG* msg = static_cast<MSG*>(message);
if(msg->message == WM_HOTKEY) { if(msg->message == WM_HOTKEY)
QHotkey::NativeShortcut shortcut; this->activateShortcut({HIWORD(msg->lParam), LOWORD(msg->lParam)});
shortcut.key = HIWORD(msg->lParam);
shortcut.mods = LOWORD(msg->lParam);
this->activateShortcut(shortcut);
}
return false; return false;
} }
quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode) quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode)
{ {
if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character" if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character"
const SHORT vKey = VkKeyScanW(keycode); const SHORT vKey = VkKeyScanW(keycode);
@@ -191,7 +205,7 @@ quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode)
} }
} }
quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers)
{ {
quint32 nMods = 0; quint32 nMods = 0;
if (modifiers & Qt::ShiftModifier) if (modifiers & Qt::ShiftModifier)
@@ -205,12 +219,12 @@ quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
return nMods; return nMods;
} }
bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
{ {
BOOL ok = RegisterHotKey(NULL, BOOL ok = RegisterHotKey(NULL,
shortcut.key ^ shortcut.mods, HKEY_ID(shortcut),
shortcut.mods, shortcut.second,
shortcut.key); shortcut.first);
if(ok) if(ok)
return true; return true;
else { else {
@@ -219,9 +233,9 @@ bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
} }
} }
bool QHotkeyPrivate::unregisterShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
{ {
BOOL ok = UnregisterHotKey(NULL, shortcut.key ^ shortcut.mods); BOOL ok = UnregisterHotKey(NULL, HKEY_ID(shortcut));
if(ok) if(ok)
return true; return true;
else { else {
@@ -230,7 +244,7 @@ bool QHotkeyPrivate::unregisterShortcut(QHotkey::NativeShortcut shortcut)
} }
} }
static QString formatWinError(DWORD winError) QString QHotkeyPrivateWin::formatWinError(DWORD winError)
{ {
wchar_t *buffer = NULL; wchar_t *buffer = NULL;
DWORD num = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, DWORD num = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,