-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshortcutedit.cpp
75 lines (64 loc) · 1.56 KB
/
shortcutedit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <QKeyEvent>
#include <QSettings>
#include "shortcutedit.h"
ShortcutEdit::ShortcutEdit(QWidget *parent)
: QLineEdit(parent)
{
}
ShortcutEdit::~ShortcutEdit()
{
}
/**
* @brief ShortcutEdit::setKey Set key sequence.
* @param key Key sequence.
*/
void ShortcutEdit::setKey(const QKeySequence &key)
{
if (!key.isEmpty()) {
m_key = key;
this->setText(m_key.toString(QKeySequence::NativeText));
}
}
/**
* @brief ShortcutEdit::getKey Get key sequence.
* @return Key sequence.
*/
QKeySequence ShortcutEdit::getKey() const
{
return m_key;
}
void ShortcutEdit::keyPressEvent(QKeyEvent *event)
{
int key;
Qt::KeyboardModifiers modifiers;
// get key and modifiers
key = event->key();
modifiers = event->modifiers() & ~Qt::KeypadModifier;
// modify key and modifiers
switch (key) {
case Qt::Key_unknown: // unknow
case Qt::Key_AltGr:
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Meta:
case Qt::Key_Alt:
key = Qt::Key_No;
break;
default:
break;
}
// build key sequence
if (key != Qt::Key_No && modifiers) {
m_key = QKeySequence(key | modifiers);
this->setText(m_key.toString(QKeySequence::NativeText));
emit shortcutChanged(m_key);
}
// clear key sequence
if (!modifiers && (key == Qt::Key_Delete
|| key == Qt::Key_Backspace
|| key == Qt::Key_Space)) {
m_key = QKeySequence();
this->setText(tr("Empty"));
emit shortcutChanged(m_key);
}
}