forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotkeyedit.cc
118 lines (100 loc) · 2.4 KB
/
hotkeyedit.cc
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This file is (c) 2008-2011 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "hotkeyedit.hh"
#include <QKeyEvent>
HotKeyEdit::HotKeyEdit( QWidget * parent ):
QLineEdit( parent ),
currentModifiers( 0 ), currentKey1( 0 ), currentKey2( 0 ),
continuingCombo( false )
{
renderCurrentValue();
}
void HotKeyEdit::setHotKey( Config::HotKey const & hk )
{
currentModifiers = hk.modifiers;
currentKey1 = hk.key1;
currentKey2 = hk.key2;
renderCurrentValue();
}
Config::HotKey HotKeyEdit::getHotKey() const
{
Config::HotKey hk;
hk.modifiers = currentModifiers;
hk.key1 = currentKey1;
hk.key2 = currentKey2;
return hk;
}
void HotKeyEdit::renderCurrentValue()
{
QString result;
if ( currentKey1 )
{
result = QKeySequence( currentKey1 | currentModifiers ).toString( QKeySequence::PortableText );
if ( currentKey2 )
result += "+" + QKeySequence( currentKey2 ).toString( QKeySequence::PortableText );
}
setText( result );
}
void HotKeyEdit::keyPressEvent( QKeyEvent * event )
{
int key = event->key();
switch( key )
{
case 0:
case Qt::Key_unknown:
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Meta:
case Qt::Key_Alt:
case Qt::Key_AltGr:
continuingCombo = false;
QLineEdit::keyPressEvent( event );
break;
default:
{
if ( !event->modifiers() &&
( ( key == Qt::Key_Backspace ) || ( key == Qt::Key_Delete ) ) )
{
// Delete current combo
currentKey1 = 0;
currentKey2 = 0;
currentModifiers = 0;
continuingCombo = false;
}
else
if ( !continuingCombo )
{
if ( event->modifiers() || event->text().isEmpty() ) // Don't allow plain letters
{
currentKey2 = 0;
currentKey1 = key;
currentModifiers = event->modifiers();
continuingCombo = true;
}
}
else
{
currentKey2 = key;
continuingCombo = false;
}
renderCurrentValue();
}
break;
}
}
void HotKeyEdit::keyReleaseEvent( QKeyEvent * event )
{
switch( event->key() )
{
case 0:
case Qt::Key_unknown:
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Meta:
case Qt::Key_Alt:
case Qt::Key_AltGr:
continuingCombo = false;
break;
}
QLineEdit::keyReleaseEvent( event );
}