KDeclarative

KeySequenceItem.qml
1import QtQuick
2import QtQuick.Controls as QQC2
3import QtQuick.Layouts
4
5import org.kde.private.kquickcontrols as KQuickControlsPrivate
6
7RowLayout {
8 id: root
9
10 property bool showClearButton: true
11 property bool showCancelButton: false /// TODO KF6 default to true
12 property alias modifierOnlyAllowed: helper.modifierOnlyAllowed
13 property alias modifierlessAllowed: helper.modifierlessAllowed
14 property alias multiKeyShortcutsAllowed: helper.multiKeyShortcutsAllowed
15 property alias keySequence: helper.currentKeySequence
16
17 /**
18 * This property controls which types of shortcuts are checked for conflicts when the keySequence
19 * is set. If a conflict is detected, a messagebox will be shown asking the user to confirm their
20 * input. Valid values are combinations of the following flags:
21 * - @p ShortcutType.None Do not check for conflicts.
22 * - @p ShortcutType.StandardShortcuts Check against standard shortcuts. @see KStandardshortcut
23 * - @p ShortcutType.GlobalShortcuts Check against global shortcuts. @see KGlobalAccel
24 *
25 * The default is `ShortcutType.GlobalShortcuts | ShortcutType.StandardShortcut`
26 * @since 5.74
27 */
28 property alias checkForConflictsAgainst: helper.checkAgainstShortcutTypes
29
30 property string __previousSequence: ""
31
32 /**
33 * This signal is emitted after the user introduces a new key sequence
34 *
35 * @since 5.68
36 * @deprecated Use keySequenceModified()
37 */
38 signal captureFinished()
39
40 /***
41 * Emitted whenever the key sequence is modified by the user, interacting with the component
42 *
43 * Either by interacting capturing a key sequence or pressing the clear button.
44 *
45 * @since 5.99
46 */
47 signal keySequenceModified()
48
49 /**
50 * Start capturing a key sequence. This equivalent to the user clicking on the main button of the item
51 * @since 5.70
52 */
53 function startCapturing() {
54 mainButton.checked = true;
55 }
56
57 KQuickControlsPrivate.KeySequenceHelper {
58 id: helper
59 onGotKeySequence: keySequence => {
60 if (!isKeySequenceAvailable(keySequence)) {
61 currentKeySequence = root.__previousSequence;
62 }
63 mainButton.checked = false;
64 root.captureFinished();
65 root.keySequenceModified();
66 }
67 }
68
69 KQuickControlsPrivate.TranslationContext {
70 id: _tr
71 domain: "kdeclarative6"
72 }
73
74 QQC2.Button {
75 id: mainButton
76
77 icon.name: "configure"
78
79 checkable: true
80 focus: checked
81
82 hoverEnabled: true
83
84 text: {
85 const keySequence = helper.currentKeySequence;
86 const text = helper.keySequenceIsEmpty(keySequence)
87 ? (helper.isRecording
88 ? _tr.i18nc("What the user inputs now will be taken as the new shortcut", "Input")
89 : _tr.i18nc("No shortcut defined", "None"))
90 // Single ampersand gets interpreted by the button as a mnemonic
91 // and removed; replace it with a double ampersand so that it
92 // will be displayed by the button as a single ampersand, or
93 // else shortcuts with the actual ampersand character will
94 // appear to be partially empty.
95 : helper.keySequenceNativeText(keySequence).replace('&', '&&');
96 // These spaces are intentional
97 return " " + text + (helper.isRecording ? " ... " : " ");
98 }
99
100 Accessible.description: _tr.i18n("Click on the button, then enter the shortcut like you would in the program.\nExample for Ctrl+A: hold the Ctrl key and press A.")
101
102 QQC2.ToolTip {
103 visible: mainButton.hovered
104 text: mainButton.Accessible.description
105 }
106
107 onCheckedChanged: {
108 if (checked) {
109 root.__previousSequence = helper.keySequenceNativeText(root.keySequence)
110 helper.window = helper.renderWindow(parent.Window.window)
111 mainButton.forceActiveFocus()
112 helper.startRecording()
113 } else if (helper.isRecording) {
114 helper.cancelRecording()
115 }
116 }
117
118 onFocusChanged: {
119 if (!focus) {
120 mainButton.checked = false
121 }
122 }
123 }
124
125 QQC2.Button {
126 id: clearButton
127 Layout.fillHeight: true
128 Layout.preferredWidth: height
129 visible: root.showClearButton && !helper.isRecording
130 onClicked: {
131 root.keySequence = "";
132 root.keySequenceModified();
133 root.captureFinished(); // Not really capturing, but otherwise we cannot track this state, hence apps should use keySequenceModified
134 }
135
136 enabled: !helper.keySequenceIsEmpty(helper.currentKeySequence)
137
138 hoverEnabled: true
139 // icon name determines the direction of the arrow, NOT the direction of the app layout
140 icon.name: Qt.application.layoutDirection === Qt.LeftToRight ? "edit-clear-locationbar-rtl" : "edit-clear-locationbar-ltr"
141
142 Accessible.name: _tr.i18nc("@info:tooltip", "Clear Key Sequence")
143
144 QQC2.ToolTip {
145 visible: clearButton.hovered
146 text: clearButton.Accessible.name
147 }
148 }
149
150 QQC2.Button {
151 Layout.fillHeight: true
152 Layout.preferredWidth: height
153 onClicked: helper.cancelRecording()
154 visible: root.showCancelButton && helper.isRecording
155
156 icon.name: "dialog-cancel"
157
158 Accessible.name: _tr.i18nc("@info:tooltip", "Cancel Key Sequence Recording")
159
160 QQC2.ToolTip {
161 visible: parent.hovered
162 text: parent.Accessible.name
163 }
164 }
165}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.