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 onBusyChanged: if (!window.pageStack.layers.busy && !initDone) {
32 const module = getModuleByName(defaultModule);
33 if (module) {
34 window.pageStack.layers.push(pageForModule(module));
35 }
36 initDone = true;
37 }
38 }
39
40 // search bar
41 FormCard.FormCard {
42 Layout.fillWidth: true
43 Layout.topMargin: Kirigami.Units.gridUnit
44
45 FormCard.AbstractFormDelegate {
46 Layout.fillWidth: true
47 background: null
48
49 topPadding: Kirigami.Units.smallSpacing
50 bottomPadding: Kirigami.Units.smallSpacing
51
52 contentItem: Kirigami.SearchField {
53 id: searchField
54 Layout.fillWidth: true
55 autoAccept: true
56 onTextChanged: repeater.filterText = text.toLowerCase();
57 background: null
58 }
59 }
60 }
61
62 Repeater {
63 id: repeater
64
65 property string filterText: ""
66
67 model: {
68 const isFiltering = filterText.length !== 0;
69 let filteredCategories = new Array();
70
71 for (let i in root.modules) {
72 const module = modules[i];
73 const modulePassesFilter = module.text.toLowerCase().includes(filterText);
74 if (module.visible && (isFiltering ? modulePassesFilter : true)) {
75 const category = filteredCategories.find((category) => category.name === module.category);
76 if (category) {
77 category.modules.push(module);
78 } else {
79 filteredCategories.push({
80 name: module.category,
81 modules: [module],
82 });
83 }
84 }
85 }
86 return filteredCategories;
87 }
88
89 ColumnLayout {
90 id: categoryDelegate
91
92 required property var modelData
93
94 spacing: 0
95
96 FormCard.FormHeader {
97 title: categoryDelegate.modelData.name === "_main_category" ? i18ndc("kirigami-addons6", "@title:group", "Settings") : modelData.name
98 }
99
100 // settings categories
102 id: settingsCard
103
104 Repeater {
105 id: repeater
106
107 model: categoryDelegate.modelData.modules
108 delegate: ColumnLayout {
109 id: moduleDelegate
110
111 required property int index
112 required property ConfigurationModule modelData
113
114 Layout.fillWidth: true
115
117 visible: moduleDelegate.index !== 0
118 }
119
121 id: delegateItem
122
123 onClicked: {
124 root.window.pageStack.layers.push(pageForModule(modelData));
125 }
126
127 contentItem: RowLayout {
128 Kirigami.Icon {
129 source: moduleDelegate.modelData.icon.name
130 Layout.rightMargin: Kirigami.Units.largeSpacing
131 implicitWidth: Kirigami.Units.iconSizes.medium
132 implicitHeight: Kirigami.Units.iconSizes.medium
133 }
134
135 Controls.Label {
136 Layout.fillWidth: true
137 text: moduleDelegate.modelData.text
138 elide: Text.ElideRight
139 }
140
141 Kirigami.Icon {
142 Layout.alignment: Qt.AlignRight
143 source: "arrow-right"
144 implicitWidth: Math.round(Kirigami.Units.iconSizes.small * 0.75)
145 implicitHeight: Math.round(Kirigami.Units.iconSizes.small * 0.75)
146 }
147 }
148 }
149 }
150 }
151 }
152
153 }
154 }
155
156 function pageForModule(module: ConfigurationModule) {
157 if (pageCache[module.moduleId]) {
158 return pageCache[module.moduleId];
159 } else {
160 const component = module.page();
161 if (component.status === Component.Error) {
162 console.error(component.errorString());
163 }
164 const page = component.createObject(root, module.initialProperties());
165 if (page.title.length === 0) {
166 page.title = module.text;
167 }
168 pageCache[module.moduleId] = page;
169 return page;
170 }
171 }
172
173 function getModuleByName(moduleId: string): ConfigurationModule {
174 return modules.find(module => module.moduleId == moduleId) ?? null;
175 }
176}
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...)
QString name(GameStandardAction id)
QWidget * window(QObject *job)
KGuiItem find()
Category category(StandardShortcut id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:05:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.