Debian Codegraph @

app/qml/Settings/ShortcutEditor.qml

raw
1 /*
2 * Copyright (C) 2016 Canonical Ltd
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Florian Boucault <[email protected]>
17 */
18
19 import QtQuick 2.6
20 import Terminal 0.1
21
22 Item {
23 id: shortcutEditor
24
25 property bool editing: false
26 property alias sequence: shortcut.sequence
27 property string label: shortcut.nativeText
28
29 signal finished()
30
31 function start() {
32 editing = true;
33 forceActiveFocus();
34 }
35
36 function finish() {
37 editing = false;
38 finished();
39 }
40
41 onActiveFocusChanged: if (!activeFocus) finish();
42
43 Shortcut {
44 id: shortcut
45 }
46
47 function readShortcut(event) {
48 if (event.key != Qt.Key_Shift
49 && event.key != Qt.Key_Alt
50 && event.key != Qt.Key_Control
51 && event.key != Qt.Key_Meta) {
52 return Shortcuts.keycodeToString(event.modifiers | event.key);
53 }
54 return "";
55 }
56
57 Keys.onPressed: {
58 event.accepted = true;
59
60 if (event.modifiers == Qt.NoModifier) {
61 if (event.key == Qt.Key_Escape) {
62 shortcutEditor.finish();
63 return;
64 } else if (event.key == Qt.Key_Backspace) {
65 shortcutEditor.sequence = "";
66 shortcutEditor.finish();
67 return;
68 }
69 }
70
71 var pressedSequence = readShortcut(event);
72 if (pressedSequence) {
73 shortcutEditor.sequence = pressedSequence;
74 shortcutEditor.finish();
75 }
76 }
77 }