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>
<x>0</x>
<y>0</y>
<width>656</width>
<height>467</height>
<width>424</width>
<height>373</height>
</rect>
</property>
<property name="windowTitle">
@@ -909,7 +909,7 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>0</height>
</size>
</property>
</spacer>
+12 -28
View File
@@ -1,13 +1,12 @@
#include "qhotkey.h"
#include "qhotkey_p.h"
#include <QGlobalStatic>
#include <QCoreApplication>
#include <QAbstractEventDispatcher>
#include <QMetaMethod>
Q_GLOBAL_STATIC(QHotkeyPrivate, hotkeyPrivate)
#define LOCKER QMutexLocker locker(&this->mutex)
#include <QDebug>
QHotkey::QHotkey(QObject *parent) :
QObject(parent),
key(Qt::Key_unknown),
@@ -31,12 +30,12 @@ QHotkey::QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister
QHotkey::~QHotkey()
{
if(this->registered)
hotkeyPrivate->removeShortcut(this);
QHotkeyPrivate::instance()->removeShortcut(this);
}
bool QHotkey::isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
return hotkeyPrivate->hasShortcut(key, modifiers);
return QHotkeyPrivate::instance()->hasShortcut(key, modifiers);
}
QKeySequence QHotkey::shortcut() const
@@ -80,7 +79,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
{
if(this->registered) {
if(autoRegister) {
if(!hotkeyPrivate->removeShortcut(this))
if(!QHotkeyPrivate::instance()->removeShortcut(this))
return false;
} else {
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->mods = modifiers;
this->nativeShortcut = hotkeyPrivate->nativeShortcut(key, modifiers);
if(this->nativeShortcut.isValid()) {
this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers);
if(QHotkeyPrivate::testValid(this->nativeShortcut)) {
if(autoRegister)
return hotkeyPrivate->addShortcut(this);
return QHotkeyPrivate::instance()->addShortcut(this);
else
return true;
} else {
@@ -108,7 +107,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
bool QHotkey::resetShortcut()
{
if(this->registered &&
!hotkeyPrivate->removeShortcut(this)) {
!QHotkeyPrivate::instance()->removeShortcut(this)) {
qWarning("QHotkey: Failed to unregister shortcut");
return false;
}
@@ -122,12 +121,12 @@ bool QHotkey::resetShortcut()
bool QHotkey::setRegistered(bool registered)
{
if(this->registered && !registered)
return hotkeyPrivate->removeShortcut(this);
return QHotkeyPrivate::instance()->removeShortcut(this);
else if(!this->registered && registered) {
if(!this->nativeShortcut.isValid())
if(!QHotkeyPrivate::testValid(this->nativeShortcut))
return false;
else
return hotkeyPrivate->addShortcut(this);
return QHotkeyPrivate::instance()->addShortcut(this);
} else
return true;
}
@@ -146,27 +145,12 @@ QHotkeyPrivate::QHotkeyPrivate() :
QHotkeyPrivate::~QHotkeyPrivate()
{
if(!this->shortcuts.isEmpty()) {
if(!this->shortcuts.isEmpty())
qWarning("QHotkey: QHotkeyPrivate destroyed with registered shortcuts!");
for(QHotkey::NativeShortcut shortcut : this->shortcuts.keys())
this->unregisterShortcut(shortcut);
}
if(qApp && qApp->eventDispatcher())
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)
{
return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers));
+2 -17
View File
@@ -9,12 +9,13 @@ class QHotkey : public QObject
{
Q_OBJECT
friend class QHotkeyPrivate;
friend class QHotkeyPrivateNative;
Q_PROPERTY(bool registered READ isRegistered WRITE setRegistered NOTIFY registeredChanged)
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut)
public:
typedef QPair<quint32, quint32> NativeShortcut;
explicit QHotkey(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);
@@ -40,22 +41,6 @@ signals:
void registeredChanged(bool registered);
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::KeyboardModifiers mods;
+4 -4
View File
@@ -9,11 +9,11 @@ public:
static OSStatus hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
static bool isHotkeyHandlerRegistered;
static QHash<QHotkey::NativeShortcut, EventHotKeyRef> hotkeyRefs;
static QHash<NativeShortcut, EventHotKeyRef> hotkeyRefs;
};
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)
{
@@ -174,7 +174,7 @@ quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
return nMods;
}
bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
bool QHotkeyPrivate::registerShortcut(NativeShortcut shortcut)
{
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);
OSStatus status = UnregisterEventHotKey(eventRef);
+23 -50
View File
@@ -5,80 +5,53 @@
#include <QAbstractNativeEventFilter>
#include <QMultiHash>
#include <QMutex>
#include <QGlobalStatic>
class QHotkeyPrivate : public QAbstractNativeEventFilter
{
friend class QHotkeyPrivateNative;
public:
QHotkeyPrivate();//singleton!!!
~QHotkeyPrivate();
static QHotkeyPrivate *instance();
static inline bool testValid(QHotkey::NativeShortcut nativeShortcut);
// QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
inline QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool addShortcut(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:
QMutex mutex;
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()\
{\
return hotkeyPrivate;\
}
// ---------- QHotkey::NativeShortcut implementation ----------
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)
inline bool QHotkeyPrivate::testValid(QHotkey::NativeShortcut nativeShortcut)
{
this->key = other.key;
this->mods = other.mods;
return (*this);
return (nativeShortcut.first != 0);
}
inline bool QHotkey::NativeShortcut::isValid() const
{
return (this->key != 0);
}
inline bool QHotkey::NativeShortcut::operator==(const QHotkey::NativeShortcut &other) const
{
return (this->key == other.key && this->mods == other.mods);
}
inline uint qHash(QHotkey::NativeShortcut key, uint seed)
{
return qHash(key.key ^ key.mods, seed);
inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
}
#endif // QHOTKEY_P_H
+31 -17
View File
@@ -3,25 +3,39 @@
#include <qt_windows.h>
#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(result);
MSG* msg = static_cast<MSG*>(message);
if(msg->message == WM_HOTKEY) {
QHotkey::NativeShortcut shortcut;
shortcut.key = HIWORD(msg->lParam);
shortcut.mods = LOWORD(msg->lParam);
this->activateShortcut(shortcut);
}
if(msg->message == WM_HOTKEY)
this->activateShortcut({HIWORD(msg->lParam), LOWORD(msg->lParam)});
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"
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;
if (modifiers & Qt::ShiftModifier)
@@ -205,12 +219,12 @@ quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
return nMods;
}
bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
{
BOOL ok = RegisterHotKey(NULL,
shortcut.key ^ shortcut.mods,
shortcut.mods,
shortcut.key);
HKEY_ID(shortcut),
shortcut.second,
shortcut.first);
if(ok)
return true;
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)
return true;
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;
DWORD num = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,