made NativeShortcut private, some mods

This commit is contained in:
Skycoder42
2016-02-06 10:23:12 +01:00
parent 7f586d1c62
commit 45efa316a5
5 changed files with 93 additions and 77 deletions
+1 -1
View File
@@ -17,7 +17,7 @@
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>1</number> <number>0</number>
</property> </property>
<widget class="QWidget" name="playTab"> <widget class="QWidget" name="playTab">
<attribute name="title"> <attribute name="title">
+23 -48
View File
@@ -12,7 +12,7 @@ QHotkey::QHotkey(QObject *parent) :
QObject(parent), QObject(parent),
key(Qt::Key_unknown), key(Qt::Key_unknown),
mods(Qt::NoModifier), mods(Qt::NoModifier),
nativeHkey(), nativeShortcut(),
registered(false) registered(false)
{} {}
@@ -34,6 +34,11 @@ QHotkey::~QHotkey()
hotkeyPrivate->removeShortcut(this); hotkeyPrivate->removeShortcut(this);
} }
bool QHotkey::isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
return hotkeyPrivate->hasShortcut(key, modifiers);
}
QKeySequence QHotkey::shortcut() const QKeySequence QHotkey::shortcut() const
{ {
if(this->key == Qt::Key_unknown) if(this->key == Qt::Key_unknown)
@@ -52,11 +57,6 @@ Qt::KeyboardModifiers QHotkey::modifiers() const
return this->mods; return this->mods;
} }
QHotkey::NativeShortcut QHotkey::nativeShortcut() const
{
return this->nativeHkey;
}
bool QHotkey::isRegistered() const bool QHotkey::isRegistered() const
{ {
return this->registered; return this->registered;
@@ -90,8 +90,8 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
this->key = key; this->key = key;
this->mods = modifiers; this->mods = modifiers;
this->nativeHkey = hotkeyPrivate->nativeShortcut(key, modifiers); this->nativeShortcut = hotkeyPrivate->nativeShortcut(key, modifiers);
if(this->nativeHkey.isValid()) { if(this->nativeShortcut.isValid()) {
if(autoRegister) if(autoRegister)
return hotkeyPrivate->addShortcut(this); return hotkeyPrivate->addShortcut(this);
else else
@@ -100,7 +100,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
qWarning("QHotkey: Unable to map shortcut to native keys."); qWarning("QHotkey: Unable to map shortcut to native keys.");
this->key = Qt::Key_unknown; this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier; this->mods = Qt::NoModifier;
this->nativeHkey = NativeShortcut(); this->nativeShortcut = NativeShortcut();
return false; return false;
} }
} }
@@ -115,7 +115,7 @@ bool QHotkey::resetShortcut()
this->key = Qt::Key_unknown; this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier; this->mods = Qt::NoModifier;
this->nativeHkey = NativeShortcut(); this->nativeShortcut = NativeShortcut();
return true; return true;
} }
@@ -124,7 +124,7 @@ bool QHotkey::setRegistered(bool registered)
if(this->registered && !registered) if(this->registered && !registered)
return hotkeyPrivate->removeShortcut(this); return hotkeyPrivate->removeShortcut(this);
else if(!this->registered && registered) { else if(!this->registered && registered) {
if(!this->nativeHkey.isValid()) if(!this->nativeShortcut.isValid())
return false; return false;
else else
return hotkeyPrivate->addShortcut(this); return hotkeyPrivate->addShortcut(this);
@@ -133,41 +133,6 @@ bool QHotkey::setRegistered(bool registered)
} }
// ---------- QHotkey::NativeShortcut implementation ----------
QHotkey::NativeShortcut::NativeShortcut() :
key(0),
mods(0)
{}
QHotkey::NativeShortcut::NativeShortcut(const QHotkey::NativeShortcut &other) :
key(other.key),
mods(other.mods)
{}
QHotkey::NativeShortcut &QHotkey::NativeShortcut::operator =(const QHotkey::NativeShortcut &other)
{
this->key = other.key;
this->mods = other.mods;
return (*this);
}
bool QHotkey::NativeShortcut::isValid() const
{
return (this->key != 0);
}
bool QHotkey::NativeShortcut::operator==(const QHotkey::NativeShortcut &other) const
{
return (this->key == other.key && this->mods == other.mods);
}
uint qHash(QHotkey::NativeShortcut key, uint seed)
{
return qHash(key.key ^ key.mods, seed);
}
// ---------- QHotkeyPrivate implementation ---------- // ---------- QHotkeyPrivate implementation ----------
@@ -190,6 +155,11 @@ QHotkeyPrivate::~QHotkeyPrivate()
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 QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
QHotkey::NativeShortcut shortcut; QHotkey::NativeShortcut shortcut;
shortcut.key = nativeKeycode(keycode); shortcut.key = nativeKeycode(keycode);
@@ -197,11 +167,16 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb
return shortcut; return shortcut;
} }
bool QHotkeyPrivate::hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
{
return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers));
}
bool QHotkeyPrivate::addShortcut(QHotkey *hotkey) bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
{ {
if(hotkey->registered) if(hotkey->registered)
return false; return false;
QHotkey::NativeShortcut shortcut = hotkey->nativeHkey; QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
LOCKER; LOCKER;
if(!this->shortcuts.contains(shortcut)) { if(!this->shortcuts.contains(shortcut)) {
@@ -219,7 +194,7 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
{ {
if(!hotkey->registered) if(!hotkey->registered)
return false; return false;
QHotkey::NativeShortcut shortcut = hotkey->nativeHkey; QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
LOCKER; LOCKER;
if(this->shortcuts.remove(shortcut, hotkey) == 0) if(this->shortcuts.remove(shortcut, hotkey) == 0)
+17 -17
View File
@@ -14,29 +14,17 @@ class QHotkey : public QObject
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut) Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut)
public: public:
struct NativeShortcut {
quint32 key;
quint32 mods;
NativeShortcut();
NativeShortcut(const NativeShortcut &other);
bool isValid() const;
NativeShortcut &operator =(const NativeShortcut &other);
bool operator==(const NativeShortcut &other) const;
};
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);
~QHotkey(); ~QHotkey();
static bool isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers);
bool isRegistered() const; bool isRegistered() const;
QKeySequence shortcut() const; QKeySequence shortcut() const;
Qt::Key keyCode() const; Qt::Key keyCode() const;
Qt::KeyboardModifiers modifiers() const; Qt::KeyboardModifiers modifiers() const;
NativeShortcut nativeShortcut() const;
public slots: public slots:
bool setRegistered(bool registered); bool setRegistered(bool registered);
@@ -51,13 +39,25 @@ signals:
void registeredChanged(bool registered); void registeredChanged(bool registered);
private: private:
struct NativeShortcut {
quint32 key;
quint32 mods;
inline NativeShortcut();
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;
NativeShortcut nativeHkey; NativeShortcut nativeShortcut;
bool registered; bool registered;
}; };
uint qHash(QHotkey::NativeShortcut key, uint seed = 0);
#endif // QHOTKEY_H #endif // QHOTKEY_H
+42 -1
View File
@@ -12,13 +12,16 @@ public:
QHotkeyPrivate();//singleton!!! QHotkeyPrivate();//singleton!!!
~QHotkeyPrivate(); ~QHotkeyPrivate();
static QHotkeyPrivate *instance();
// QAbstractNativeEventFilter interface // QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
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 addShortcut(QHotkey *hotkey);
bool removeShortcut(QHotkey *hotkey);//platform implement bool removeShortcut(QHotkey *hotkey);
private: private:
QMutex mutex; QMutex mutex;
@@ -33,4 +36,42 @@ private:
bool unregisterShortcut(QHotkey::NativeShortcut shortcut);//platform implement bool unregisterShortcut(QHotkey::NativeShortcut shortcut);//platform implement
}; };
uint qHash(QHotkey::NativeShortcut key, uint seed = 0);
// ---------- QHotkey::NativeShortcut implementation ----------
inline QHotkey::NativeShortcut::NativeShortcut() :
key(0),
mods(0)
{}
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
{
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);
}
#endif // QHOTKEY_P_H #endif // QHOTKEY_P_H
+10 -10
View File
@@ -24,12 +24,12 @@ bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *messag
quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode) quint32 QHotkeyPrivate::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 vk = VkKeyScanW(keycode); const SHORT vKey = VkKeyScanW(keycode);
if(vk > -1) if(vKey > -1)
return LOBYTE(vk); return LOBYTE(vKey);
} }
//find key from "switch/case" --> Only finds a very small subset of keys //find key from switch/case --> Only finds a very small subset of keys
switch (keycode) switch (keycode)
{ {
case Qt::Key_Escape: case Qt::Key_Escape:
@@ -193,16 +193,16 @@ quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode)
quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
{ {
quint32 native = 0; quint32 nMods = 0;
if (modifiers & Qt::ShiftModifier) if (modifiers & Qt::ShiftModifier)
native |= MOD_SHIFT; nMods |= MOD_SHIFT;
if (modifiers & Qt::ControlModifier) if (modifiers & Qt::ControlModifier)
native |= MOD_CONTROL; nMods |= MOD_CONTROL;
if (modifiers & Qt::AltModifier) if (modifiers & Qt::AltModifier)
native |= MOD_ALT; nMods |= MOD_ALT;
if (modifiers & Qt::MetaModifier) if (modifiers & Qt::MetaModifier)
native |= MOD_WIN; nMods |= MOD_WIN;
return native; return nMods;
} }
bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)