Added test (passed windows)

This commit is contained in:
Skycoder42
2016-02-05 23:25:36 +01:00
parent 4d71726f03
commit 3fe92f4859
6 changed files with 1392 additions and 68 deletions
+163 -1
View File
@@ -3,12 +3,174 @@
HotTestWidget::HotTestWidget(QWidget *parent) : HotTestWidget::HotTestWidget(QWidget *parent) :
QWidget(parent), QWidget(parent),
ui(new Ui::HotTestWidget) ui(new Ui::HotTestWidget),
hotkey_1(new QHotkey(this)),
hotkey_2(new QHotkey(this)),
hotkey_3(new QHotkey(this)),
hotkey_4(new QHotkey(this)),
hotkey_5(new QHotkey(this)),
testHotkeys()
{ {
ui->setupUi(this); ui->setupUi(this);
//1
connect(this->ui->hotkeyCheckbox_1, &QCheckBox::toggled,
this->hotkey_1, &QHotkey::setRegistered);
connect(this->ui->hotkeySequenceEdit_1, &QKeySequenceEdit::keySequenceChanged,
this, &HotTestWidget::setShortcut_1);
connect(this->hotkey_1, &QHotkey::activated,
this, &HotTestWidget::increase_1);
//2
connect(this->ui->hotkeyCheckbox_2, &QCheckBox::toggled,
this->hotkey_2, &QHotkey::setRegistered);
connect(this->ui->hotkeySequenceEdit_2, &QKeySequenceEdit::keySequenceChanged,
this, &HotTestWidget::setShortcut_2);
connect(this->hotkey_2, &QHotkey::activated,
this, &HotTestWidget::increase_2);
//3
connect(this->ui->hotkeyCheckbox_3, &QCheckBox::toggled,
this->hotkey_3, &QHotkey::setRegistered);
connect(this->ui->hotkeySequenceEdit_3, &QKeySequenceEdit::keySequenceChanged,
this, &HotTestWidget::setShortcut_3);
connect(this->hotkey_3, &QHotkey::activated,
this, &HotTestWidget::increase_3);
//4
connect(this->ui->hotkeyCheckbox_4, &QCheckBox::toggled,
this->hotkey_4, &QHotkey::setRegistered);
connect(this->ui->hotkeySequenceEdit_4, &QKeySequenceEdit::keySequenceChanged,
this, &HotTestWidget::setShortcut_4);
connect(this->hotkey_4, &QHotkey::activated,
this, &HotTestWidget::increase_4);
//5
connect(this->ui->hotkeyCheckbox_5, &QCheckBox::toggled,
this->hotkey_5, &QHotkey::setRegistered);
connect(this->ui->hotkeySequenceEdit_5, &QKeySequenceEdit::keySequenceChanged,
this, &HotTestWidget::setShortcut_5);
connect(this->hotkey_5, &QHotkey::activated,
this, &HotTestWidget::increase_5);
//test connections
this->testHotkeys += new QHotkey(Qt::Key_F, Qt::NoModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyFCheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_F12, Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyCtrlAltMetaF12CheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_Cancel, Qt::ControlModifier | Qt::ShiftModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyCtrlShiftCancelCheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_Delete, Qt::MetaModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyMetaDelCheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_NumLock, Qt::NoModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyNumlockCheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_5, Qt::ControlModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyCtrl5CheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_Tab, Qt::ShiftModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyShiftTabCheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_Comma, Qt::ShiftModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyShiftCheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_Semicolon, Qt::ShiftModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyShiftCheckBox_2, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_K, Qt::ShiftModifier | Qt::AltModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyShiftAltKCheckBox, &QCheckBox::toggle);
this->testHotkeys += new QHotkey(Qt::Key_K, Qt::ShiftModifier | Qt::AltModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyShiftAltKCheckBox_2, &QCheckBox::toggle);
} }
HotTestWidget::~HotTestWidget() HotTestWidget::~HotTestWidget()
{ {
delete ui; delete ui;
} }
void HotTestWidget::setShortcut_1(const QKeySequence &sequence)
{
this->hotkey_1->setShortcut(sequence, false);
}
void HotTestWidget::setShortcut_2(const QKeySequence &sequence)
{
this->hotkey_2->setShortcut(sequence, false);
}
void HotTestWidget::setShortcut_3(const QKeySequence &sequence)
{
this->hotkey_3->setShortcut(sequence, false);
}
void HotTestWidget::setShortcut_4(const QKeySequence &sequence)
{
this->hotkey_4->setShortcut(sequence, false);
}
void HotTestWidget::setShortcut_5(const QKeySequence &sequence)
{
this->hotkey_5->setShortcut(sequence, false);
}
void HotTestWidget::increase_1()
{
this->ui->hotkeyCount_1->display(this->ui->hotkeyCount_1->intValue() + 1);
}
void HotTestWidget::increase_2()
{
this->ui->hotkeyCount_2->display(this->ui->hotkeyCount_2->intValue() + 1);
}
void HotTestWidget::increase_3()
{
this->ui->hotkeyCount_3->display(this->ui->hotkeyCount_3->intValue() + 1);
}
void HotTestWidget::increase_4()
{
this->ui->hotkeyCount_4->display(this->ui->hotkeyCount_4->intValue() + 1);
}
void HotTestWidget::increase_5()
{
this->ui->hotkeyCount_5->display(this->ui->hotkeyCount_5->intValue() + 1);
}
void HotTestWidget::on_resetButton_1_clicked()
{
this->ui->hotkeyCount_1->display(0);
}
void HotTestWidget::on_resetButton_2_clicked()
{
this->ui->hotkeyCount_2->display(0);
}
void HotTestWidget::on_resetButton_3_clicked()
{
this->ui->hotkeyCount_3->display(0);
}
void HotTestWidget::on_resetButton_4_clicked()
{
this->ui->hotkeyCount_4->display(0);
}
void HotTestWidget::on_resetButton_5_clicked()
{
this->ui->hotkeyCount_5->display(0);
}
void HotTestWidget::on_groupBox_toggled(bool checked)
{
for(QHotkey *hotkey : this->testHotkeys)
hotkey->setRegistered(checked);
}
+30
View File
@@ -2,6 +2,7 @@
#define HOTTESTWIDGET_H #define HOTTESTWIDGET_H
#include <QWidget> #include <QWidget>
#include <QHotkey>
namespace Ui { namespace Ui {
class HotTestWidget; class HotTestWidget;
@@ -15,8 +16,37 @@ public:
explicit HotTestWidget(QWidget *parent = 0); explicit HotTestWidget(QWidget *parent = 0);
~HotTestWidget(); ~HotTestWidget();
private slots:
void setShortcut_1(const QKeySequence &sequence);
void setShortcut_2(const QKeySequence &sequence);
void setShortcut_3(const QKeySequence &sequence);
void setShortcut_4(const QKeySequence &sequence);
void setShortcut_5(const QKeySequence &sequence);
void increase_1();
void increase_2();
void increase_3();
void increase_4();
void increase_5();
void on_resetButton_1_clicked();
void on_resetButton_2_clicked();
void on_resetButton_3_clicked();
void on_resetButton_4_clicked();
void on_resetButton_5_clicked();
void on_groupBox_toggled(bool checked);
private: private:
Ui::HotTestWidget *ui; Ui::HotTestWidget *ui;
QHotkey *hotkey_1;
QHotkey *hotkey_2;
QHotkey *hotkey_3;
QHotkey *hotkey_4;
QHotkey *hotkey_5;
QList<QHotkey*> testHotkeys;
}; };
#endif // HOTTESTWIDGET_H #endif // HOTTESTWIDGET_H
File diff suppressed because it is too large Load Diff
+1
View File
@@ -0,0 +1 @@
#include "qhotkey.h"
+90 -43
View File
@@ -10,17 +10,22 @@ Q_GLOBAL_STATIC(QHotkeyPrivate, hotkeyPrivate)
QHotkey::QHotkey(QObject *parent) : QHotkey::QHotkey(QObject *parent) :
QObject(parent), QObject(parent),
enabled(true),
key(Qt::Key_unknown), key(Qt::Key_unknown),
mods(Qt::NoModifier), mods(Qt::NoModifier),
nativeShortcut(), nativeHkey(),
registered(false) registered(false)
{} {}
QHotkey::QHotkey(const QKeySequence &sequence, QObject *parent) : QHotkey::QHotkey(const QKeySequence &sequence, bool autoRegister, QObject *parent) :
QHotkey(parent) QHotkey(parent)
{ {
this->setShortcut(sequence); this->setShortcut(sequence, autoRegister);
}
QHotkey::QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
QHotkey(parent)
{
this->setShortcut(key, modifiers, autoRegister);
} }
QHotkey::~QHotkey() QHotkey::~QHotkey()
@@ -37,42 +42,19 @@ QKeySequence QHotkey::shortcut() const
return QKeySequence(this->key | this->mods); return QKeySequence(this->key | this->mods);
} }
bool QHotkey::setShortcut(const QKeySequence &shortcut) Qt::Key QHotkey::keyCode() const
{ {
if(this->registered) { return this->key;
if(!hotkeyPrivate->removeShortcut(this)) {
qWarning("QHotkey: Failed to unset previous shortcut. Cannot set new one.");
return false;
}
}
if(shortcut.isEmpty()) {
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeShortcut = NativeShortcut();
return true;
} else if(shortcut.count() > 1) {
qWarning("QHotkey: Keysequences with multiple shortcuts are not allowed! "
"Only the first shortcut will be used!");
}
this->key = Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask);
this->mods = Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask);
this->nativeShortcut = hotkeyPrivate->nativeShortcut(this->key, this->mods);
if(this->nativeShortcut.isValid())
return hotkeyPrivate->addShortcut(this);
else {
qWarning("QHotkey: Unable to map shortcut to native keys.");
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeShortcut = NativeShortcut();
return false;
}
} }
bool QHotkey::isEnabled() const Qt::KeyboardModifiers QHotkey::modifiers() const
{ {
return this->enabled; return this->mods;
}
QHotkey::NativeShortcut QHotkey::nativeShortcut() const
{
return this->nativeHkey;
} }
bool QHotkey::isRegistered() const bool QHotkey::isRegistered() const
@@ -80,9 +62,74 @@ bool QHotkey::isRegistered() const
return this->registered; return this->registered;
} }
void QHotkey::setEnabled(bool enabled) bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
{ {
this->enabled = enabled; if(shortcut.isEmpty()) {
return this->resetShortcut();
} else if(shortcut.count() > 1) {
qWarning("QHotkey: Keysequences with multiple shortcuts are not allowed! "
"Only the first shortcut will be used!");
}
return this->setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask),
autoRegister);
}
bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister)
{
if(this->registered) {
if(autoRegister) {
if(!hotkeyPrivate->removeShortcut(this))
return false;
} else {
qWarning("QHotkey: Can't change the shortcut while the hotkey is registered");
return false;
}
}
this->key = key;
this->mods = modifiers;
this->nativeHkey = hotkeyPrivate->nativeShortcut(key, modifiers);
if(this->nativeHkey.isValid()) {
if(autoRegister)
return hotkeyPrivate->addShortcut(this);
else
return true;
} else {
qWarning("QHotkey: Unable to map shortcut to native keys.");
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeHkey = NativeShortcut();
return false;
}
}
bool QHotkey::resetShortcut()
{
if(this->registered &&
!hotkeyPrivate->removeShortcut(this)) {
qWarning("QHotkey: Failed to unregister shortcut");
return false;
}
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeHkey = NativeShortcut();
return true;
}
bool QHotkey::setRegistered(bool registered)
{
if(this->registered && !registered)
return hotkeyPrivate->removeShortcut(this);
else if(!this->registered && registered) {
if(!this->nativeHkey.isValid())
return false;
else
return hotkeyPrivate->addShortcut(this);
} else
return true;
} }
@@ -154,7 +201,7 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
{ {
if(hotkey->registered) if(hotkey->registered)
return false; return false;
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut; QHotkey::NativeShortcut shortcut = hotkey->nativeHkey;
LOCKER; LOCKER;
if(!this->shortcuts.contains(shortcut)) { if(!this->shortcuts.contains(shortcut)) {
@@ -164,6 +211,7 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
this->shortcuts.insert(shortcut, hotkey); this->shortcuts.insert(shortcut, hotkey);
hotkey->registered = true; hotkey->registered = true;
emit hotkey->registeredChanged(true);
return true; return true;
} }
@@ -171,12 +219,13 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
{ {
if(!hotkey->registered) if(!hotkey->registered)
return false; return false;
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut; QHotkey::NativeShortcut shortcut = hotkey->nativeHkey;
LOCKER; LOCKER;
if(this->shortcuts.remove(shortcut, hotkey) == 0) if(this->shortcuts.remove(shortcut, hotkey) == 0)
return false; return false;
hotkey->registered = false; hotkey->registered = false;
emit hotkey->registeredChanged(true);
if(this->shortcuts.count(shortcut) == 0) if(this->shortcuts.count(shortcut) == 0)
return this->unregisterShortcut(shortcut); return this->unregisterShortcut(shortcut);
else else
@@ -188,8 +237,6 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
LOCKER; LOCKER;
QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated); QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated);
for(QHotkey *hkey : this->shortcuts.values(shortcut)) { for(QHotkey *hkey : this->shortcuts.values(shortcut))
if(hkey->isEnabled())
signal.invoke(hkey, Qt::QueuedConnection); signal.invoke(hkey, Qt::QueuedConnection);
}
} }
+15 -15
View File
@@ -10,8 +10,8 @@ class QHotkey : public QObject
Q_OBJECT Q_OBJECT
friend class QHotkeyPrivate; friend class QHotkeyPrivate;
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) Q_PROPERTY(bool registered READ isRegistered WRITE setRegistered NOTIFY registeredChanged)
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET reset) Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut)
public: public:
struct NativeShortcut { struct NativeShortcut {
@@ -28,33 +28,33 @@ public:
}; };
explicit QHotkey(QObject *parent = 0); explicit QHotkey(QObject *parent = 0);
explicit QHotkey(const QKeySequence &shortcut, 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);
~QHotkey(); ~QHotkey();
QKeySequence shortcut() const;
bool setShortcut(const QKeySequence& shortcut);
bool isEnabled() const;
bool isRegistered() const; bool isRegistered() const;
QKeySequence shortcut() const;
Qt::Key keyCode() const;
Qt::KeyboardModifiers modifiers() const;
NativeShortcut nativeShortcut() const;
public slots: public slots:
void setEnabled(bool enabled = true); bool setRegistered(bool registered);
inline void setDisabled(bool disabled = true) {
this->setEnabled(!disabled);
}
inline void reset() { bool setShortcut(const QKeySequence& shortcut, bool autoRegister = true);
this->setShortcut(QKeySequence()); bool setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister = true);
} bool resetShortcut();
signals: signals:
void activated(); void activated();
void registeredChanged(bool registered);
private: private:
bool enabled;
Qt::Key key; Qt::Key key;
Qt::KeyboardModifiers mods; Qt::KeyboardModifiers mods;
NativeShortcut nativeShortcut; NativeShortcut nativeHkey;
bool registered; bool registered;
}; };