Kirigami-addons

ConfigMobilePage.qml
1/*
2 * SPDX-FileCopyrightText: 2011-2014 Sebastian Kügler <sebas@kde.org>
3 * SPDX-FileCopyrightText: 2021 Devin Lin <devin@kde.org>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Layouts
9import QtQuick.Controls as Controls
10import org.kde.kirigami as Kirigami
11import org.kde.kirigamiaddons.formcard as FormCard
12import org.kde.kirigamiaddons.settings
13
14FormCard.FormCardPage {
15 id: root
16
17 required property string defaultModule
18 required property list<ConfigurationModule> modules
19 required property Kirigami.ApplicationWindow window
20
21 // Do not use Map, it crashes very frequently
22 property var pageCache: Object.create(null)
23
24 property bool initDone: false
25
26 title: i18ndc("kirigami-addons6", "@title", "Settings")
27
28 Connections {
29 target: window.pageStack.layers
30
31 function onBusyChanged(): void {
32 if (!window.pageStack.layers.busy && !initDone) {
33 const module = getModuleByName(defaultModule);
34 if (module) {
35 window.pageStack.layers.push(pageForModule(module));
36 }
37 initDone = true;
38 }
39 }
40 }
41
42 // search bar
43 FormCard.FormCard {
44 Layout.fillWidth: true
45 Layout.topMargin: Kirigami.Units.gridUnit
46
47 FormCard.AbstractFormDelegate {
48 Layout.fillWidth: true
49 background: null
50
51 topPadding: Kirigami.Units.smallSpacing
52 bottomPadding: Kirigami.Units.smallSpacing
53
54 contentItem: Kirigami.SearchField {
55 id: searchField
56 Layout.fillWidth: true
57 autoAccept: true
58 onTextChanged: repeater.filterText = text.toLowerCase();
59 background: null
60 }
61 }
62 }
63
64 Repeater {
65 id: repeater
66
67 property string filterText: ""
68
69 model: {
70 const isFiltering = filterText.length !== 0;
71 let filteredCategories = new Array();
72
73 for (let i in root.modules) {
74 const module = modules[i];
75 const modulePassesFilter = module.text.toLowerCase().includes(filterText);
76 if (module.visible && (isFiltering ? modulePassesFilter : true)) {
77 const category = filteredCategories.find((category) => category.name === module.category);
78 if (category) {
79 category.modules.push(module);
80 } else {
81 filteredCategories.push({
82 name: module.category,
83 modules: [module],
84 });
85 }
86 }
87 }
88 return filteredCategories;
89 }
90
91 ColumnLayout {
92 id: categoryDelegate
93
94 required property var modelData
95
96 spacing: 0
97
98 FormCard.FormHeader {
99 title: categoryDelegate.modelData.name === "_main_category" ? i18ndc("kirigami-addons6", "@title:group", "Settings") : modelData.name
100 }
101
102 // settings categories
104 id: settingsCard
105
106 Repeater {
107 id: repeater
108
109 model: categoryDelegate.modelData.modules
110 delegate: ColumnLayout {
111 id: moduleDelegate
112
113 required property int index
114 required property ConfigurationModule modelData
115
116 Layout.fillWidth: true
117
119 visible: moduleDelegate.index !== 0
120 }
121
123 id: delegateItem
124
125 onClicked: {
126 root.window.pageStack.layers.push(pageForModule(modelData));
127 }
128
129 contentItem: RowLayout {
130 Kirigami.Icon {
131 source: moduleDelegate.modelData.icon.name
132 Layout.rightMargin: Kirigami.Units.largeSpacing
133 implicitWidth: Kirigami.Units.iconSizes.medium
134 implicitHeight: Kirigami.Units.iconSizes.medium
135 }
136
137 Controls.Label {
138 Layout.fillWidth: true
139 text: moduleDelegate.modelData.text
140 elide: Text.ElideRight
141 }
142
143 Kirigami.Icon {
144 Layout.alignment: Qt.AlignRight
145 source: "arrow-right"
146 implicitWidth: Math.round(Kirigami.Units.iconSizes.small * 0.75)
147 implicitHeight: Math.round(Kirigami.Units.iconSizes.small * 0.75)
148 }
149 }
150 }
151 }
152 }
153 }
154
155 }
156 }
157
158 function pageForModule(module: ConfigurationModule) {
159 if (pageCache[module.moduleId]) {
160 return pageCache[module.moduleId];
161 } else {
162 const component = module.page();
163 if (component.status === Component.Error) {
164 console.error(component.errorString());
165 }
166 const page = component.createObject(root, module.initialProperties());
167 if (page.title.length === 0) {
168 page.title = module.text;
169 }
170 pageCache[module.moduleId] = page;
171 return page;
172 }
173 }
174
175 function getModuleByName(moduleId: string): ConfigurationModule {
176 return modules.find(module => module.moduleId == moduleId) ?? null;
177 }
178}
A base item for delegates to be used in a FormCard.
This object holds the information of configuration module.
A single card that follows a form style.
Definition FormCard.qml:35
A context-aware separator.
QString i18ndc(const char *domain, const char *context, const char *text, const TYPE &arg...)
QWidget * window(QObject *job)
QString name(StandardAction id)
KGuiItem find()
Category category(StandardShortcut id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:11 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.