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

KDE's Doxygen guidelines are available online.