Kirigami-addons

ShortcutsEditor.qml
1// SPDX-FileCopyrightText: 2024 Carl Schwan <carlschwan@kde.org>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4import QtQuick
5import QtQuick.Controls as QQC2
6import QtQuick.Layouts
7
8import org.kde.kitemmodels
9import org.kde.kirigami as Kirigami
10import org.kde.kirigamiaddons.formcard as FormCard
11import org.kde.kirigamiaddons.delegates as Delegates
12import org.kde.kirigamiaddons.components as Components
13
14Kirigami.ScrollablePage {
15 id: root
16
17 property alias model: searchFilterProxyModel.sourceModel
18
19 title: i18ndc("kirigami-addons6", "@title:window", "Shortcuts")
20
21 actions: Kirigami.Action {
22 displayComponent: Kirigami.SearchField {
23 placeholderText: i18ndc("kirigami-addons6", "@label:textbox", "Filter…")
24 onTextChanged: searchFilterProxyModel.setFilterFixedString(text);
25 }
26 }
27
28 ListView {
29 id: listView
30
31 currentIndex: -1
32
34 id: searchFilterProxyModel
35
36 filterRoleName: 'actionName'
37 filterCaseSensitivity: Qt.CaseInsensitive
38 }
39
40 delegate: Delegates.RoundedItemDelegate {
41 id: shortcutDelegate
42
43 required property int index
44 required property string actionName
45 required property var shortcut
46 required property string shortcutDisplay
47 required property string alternateShortcuts
48
49 text: actionName.replace('&', '')
50
51 Accessible.description: shortcutDisplay
52
53 contentItem: RowLayout {
54 spacing: Kirigami.Units.smallSpacing
55
56 QQC2.Label {
57 text: shortcutDelegate.text
58 Layout.fillWidth: true
59 Accessible.ignored: true
60 }
61
62 QQC2.Label {
63 text: shortcutDelegate.shortcutDisplay
64 Accessible.ignored: true
65 }
66 }
67
68 onClicked: {
69 shortcutDialog.title = i18ndc("kirigami-addons6", "@title:window", "Shortcut: %1", shortcutDelegate.text);
70 shortcutDialog.keySequence = shortcutDelegate.shortcut;
71 shortcutDialog.index = shortcutDelegate.index;
72 shortcutDialog.alternateShortcuts = shortcutDelegate.alternateShortcuts;
73 shortcutDialog.open()
74 }
75 }
76
77 FormCard.FormCardDialog {
78 id: shortcutDialog
79
80 property alias keySequence: keySequenceItem.keySequence
81 property var alternateShortcuts
82 property int index: -1
83
84 parent: root.QQC2.Overlay.overlay
85
86 KeySequenceItem {
87 id: keySequenceItem
88
89 label: i18ndc("kirigami-addons6", "@label", "Shortcut:")
90 onKeySequenceModified: {
91 root.model.updateShortcut(shortcutDialog.index, 0, keySequence);
92 }
93
94 onErrorOccurred: (title, message) => {
95 root.QQC2.ApplicationWindow.showPassiveNotification(title + '\n' + message);
96 }
97
98 onShowStealStandardShortcutDialog: (title, message, sequence) => {
99 stealStandardShortcutDialog.title = title
100 stealStandardShortcutDialog.message = message;
101 stealStandardShortcutDialog.sequence = sequence;
102 stealStandardShortcutDialog.parent = root.QQC2.Overlay.overlay;
103 stealStandardShortcutDialog.sequenceItem = this;
104 stealStandardShortcutDialog.openDialog();
105 }
106 }
107
108 Components.MessageDialog {
109 id: stealStandardShortcutDialog
110
111 property string message
112 property var sequence
113 property KeySequenceItem sequenceItem
114
115 dialogType: Components.MessageDialog.Warning
116 dontShowAgainName: "stealStandardShortcutDialog"
117
118 QQC2.Label {
119 text: stealStandardShortcutDialog.message
120 Layout.fillWidth: true
121 wrapMode: Text.WordWrap
122 }
123
124 standardButtons: Kirigami.PromptDialog.Apply | Kirigami.PromptDialog.Cancel
125
126 onApplied: {
127 sequenceItem.stealStandardShortcut(sequence);
128 close();
129 }
130
131 onRejected: close()
132 }
133
134 Repeater {
135 id: alternateRepeater
136
137 model: shortcutDialog.alternateShortcuts
138 KeySequenceItem {
139 id: alternateKeySequenceItem
140
141 required property int index
142 required property var modelData
143
144 label: index === 0 ? i18ndc("kirigami-addons6", "@label", "Alternative:") : ''
145
146 keySequence: modelData
147 onKeySequenceModified: {
148 const alternates = root.model.updateShortcut(shortcutDialog.index, index + 1, keySequence);
149 if (alternates !== shortcutDialog.alternateShortcuts) {
150 shortcutDialog.alternateShortcuts = alternates;
151 }
152 }
153
154 onErrorOccurred: (title, message) => {
155 root.QQC2.ApplicationWindow.showPassiveNotification(title + '\n' + message);
156 }
157
158 onShowStealStandardShortcutDialog: (title, message, sequence) => {
159 stealStandardShortcutDialog.title = title
160 stealStandardShortcutDialog.message = message;
161 stealStandardShortcutDialog.sequence = sequence;
162 stealStandardShortcutDialog.parent = root.QQC2.Overlay.overlay;
163 stealStandardShortcutDialog.sequenceItem = this;
164 stealStandardShortcutDialog.openDialog();
165 }
166 }
167 }
168
169 KeySequenceItem {
170 id: alternateKeySequenceItem
171
172 label: alternateRepeater.count === 0 ? i18ndc("kirigami-addons6", "@label", "Alternative:") : ''
173
174 onKeySequenceModified: {
175 shortcutDialog.alternateShortcuts = root.model.updateShortcut(shortcutDialog.index, alternateRepeater.count + 1, keySequence);
176 keySequence = root.model.emptyKeySequence();
177 }
178
179 onErrorOccurred: (title, message) => {
180 root.QQC2.ApplicationWindow.showPassiveNotification(title + '\n' + message);
181 }
182
183 onShowStealStandardShortcutDialog: (title, message, sequence) => {
184 stealStandardShortcutDialog.title = title
185 stealStandardShortcutDialog.message = message;
186 stealStandardShortcutDialog.sequence = sequence;
187 stealStandardShortcutDialog.parent = root.QQC2.Overlay.overlay;
188 stealStandardShortcutDialog.sequenceItem = this;
189 stealStandardShortcutDialog.openDialog();
190 }
191 }
192
193 footer: RowLayout {
194 QQC2.DialogButtonBox {
195 Layout.fillWidth: true
196 standardButtons: QQC2.DialogButtonBox.Close | QQC2.DialogButtonBox.Reset
197 onRejected: shortcutDialog.close();
198 onReset: shortcutDialog.alternateShortcuts = root.model.reset(shortcutDialog.index)
199 leftPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
200 topPadding: Kirigami.Units.smallSpacing
201 rightPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
202 bottomPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
203 }
204 }
205 }
206
207 Kirigami.PlaceholderMessage {
208 width: parent.width - Kirigami.Units.gridUnit * 4
209 anchors.centerIn: parent
210 text: i18ndc("kirigami-addons6", "Placeholder message", "No shortcuts found")
211 visible: listView.count === 0
212 }
213 }
214
215 footer: QQC2.ToolBar {
216 padding: 0
217
218 contentItem: QQC2.DialogButtonBox {
219 padding: Kirigami.Units.largeSpacing
220 standardButtons: QQC2.Dialog.Save | QQC2.Dialog.Reset
221
222 onAccepted: {
223 root.model.save()
224 root.closeDialog();
225 }
226 onReset: root.model.resetAll()
227 }
228 }
229}
A single card that follows a form style.
Definition FormCard.qml:35
QString i18ndc(const char *domain, const char *context, const char *text, const TYPE &arg...)
const QList< QKeySequence > & close()
QString label(StandardShortcut id)
const QList< QKeySequence > & shortcut(StandardShortcut id)
qsizetype count() const const
void keySequence(QWidget *widget, const QKeySequence &keySequence)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.