Kirigami-addons

CategorizedSettings.qml
1// SPDX-FileCopyrightText: 2020 Tobias Fella <fella@posteo.de>
2// SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
3// SPDX-FileCopyrightText: 2021 Felipe Kinoshita <kinofhek@gmail.com>
4// SPDX-FileCopyrightText: 2021 Marco Martin <mart@kde.org>
5// SPDX-License-Identifier: LGPL-2.0-or-later
6
7import QtQml 2.15
8import QtQuick 2.15
9import QtQuick.Controls 2.15 as QQC2
10import QtQuick.Layouts 1.15
11import org.kde.kirigami 2.20 as Kirigami
12import org.kde.kirigamiaddons.delegates 1.0 as Delegates
13
14/**
15 * A container for setting actions showing them in a list view and displaying
16 * the actual page next to it.
17 *
18 * To open a setting page, you should use `PageRow::pushDialogLayers`
19 *
20 * @since KirigamiAddons 0.11.0
21 * @inherit kde::org::kirigami::PageRow
22 */
23Kirigami.PageRow {
24 id: root
25
26 /**
27 * @brief The default page that will be shown when opened.
28 *
29 * This only applies when the categorized settings object is wide enough to
30 * show multiple pages.
31 *
32 * @see actions
33 */
34 property string defaultPage
36 /**
37 * @brief The list of pages for the settings
38 */
39 property list<Kirigami.PagePoolAction> actions
40
41 property alias _stack: root
42 property Kirigami.PagePool _pool: Kirigami.PagePool {}
43
44 readonly property string title: root.depth < 2 ? i18ndc("kirigami-addons6", "@title:window", "Settings") :i18ndc("kirigami-addons6", "@title:window", "Settings — %1", root.get(1).title)
45
46 property bool completed: false
47
48 bottomPadding: 0
49 leftPadding: 0
50 rightPadding: 0
51 topPadding: 0
52
53 // Hack to not base all calculation based on main popup
54 function applicationWindow() {
55 return _fakeApplicationWindow;
56 }
57
58 readonly property QtObject _fakeApplicationWindow: QtObject {
59 readonly property Kirigami.PageRow pageStack: root
60 readonly property bool controlsVisible: true
61 readonly property QtObject globalDrawer: null
62 readonly property QtObject contextDrawer: null
63 readonly property double width: root.width
64 readonly property double height: root.height
65 readonly property var overlay: root
66 }
67
68 globalToolBar {
69 style: Kirigami.ApplicationHeaderStyle.ToolBar
70 showNavigationButtons: if (root.currentIndex > 0) {
71 Kirigami.ApplicationHeaderStyle.ShowBackButton
72 } else {
73 0
74 }
75 }
76
77 signal backRequested(var event)
78 onBackRequested: event => {
79 if (root.depth > 1 && !root.wideMode && root.currentIndex !== 0) {
80 event.accepted = true;
81 root.pop();
82 }
83 }
84
85 columnView.columnWidth: Kirigami.Units.gridUnit * 13
86
87 initialPage: Kirigami.ScrollablePage {
88 bottomPadding: 0
89 leftPadding: 0
90 rightPadding: 0
91 topPadding: 0
92
93 titleDelegate: RowLayout {
94 Layout.fillWidth: true
95
96 QQC2.ToolButton {
97 icon.name: "go-previous-view"
98 text: i18nc("@action:intoolbar", "Go back")
99 display: QQC2.AbstractButton.IconOnly
100 onClicked: pageStack.layers.pop()
101 visible: pageStack.layers.depth > 1
102 }
103
105 Layout.fillWidth: true
106 onTextChanged: listview.filterText = text.toLowerCase();
107 }
108 }
109
110 ListView {
111 id: listview
112
113 property string filterText: ""
114 property bool initDone: false
115
116 currentIndex: -1
117 topMargin: Math.round(Kirigami.Units.smallSpacing / 2)
118 bottomMargin: Math.round(Kirigami.Units.smallSpacing / 2)
119
120 onWidthChanged: if (!initDone && root.width >= Kirigami.Units.gridUnit * 30) {
121 let defaultAction = getActionByName(defaultPage);
122 if (defaultAction) {
123 defaultAction.trigger();
124 listview.currentIndex = indexOfAction(defaultAction);
125 } else {
126 actions[0].trigger();
127 listview.currentIndex = 0;
128 if (root.currentItem.title.length === 0) {
129 root.currentItem.title = actions[0].text;
130 }
131 }
132 initDone = true;
133 }
134 model: {
135 const isFiltering = filterText.length !== 0;
136 let filteredActions = [];
137 for (let i in actions) {
138 const action = actions[i];
139 const actionPassesFilter = action.text.toLowerCase().includes(filterText);
140 if (action.visible && (isFiltering ? actionPassesFilter : true)) {
141 filteredActions.push(action);
142 }
143 }
144 return filteredActions;
145 }
146 delegate: Delegates.RoundedItemDelegate {
147 id: settingDelegate
148
149 required property int index
150 required property var modelData
151
152 action: modelData
153 checked: ListView.view.currentIndex === settingDelegate.index
154
155 onClicked: {
156 ListView.view.currentIndex = settingDelegate.index;
157 if (root.currentItem.title.length === 0) {
158 root.currentItem.title = text;
159 }
160 }
161 }
162 }
163 }
164
165 Component.onCompleted: {
166 root.completed = true;
167 }
168
169 function getActionByName(actionName) {
170 for (let i in actions) {
171 if (actions[i].actionName == actionName) {
172 return actions[i];
173 }
174 }
175 }
176
177 function indexOfAction(action) {
178 for (let i in actions) {
179 if (actions[i] == action) {
180 return i;
181 }
182 }
183 }
184}
185
QString i18ndc(const char *domain, const char *context, const char *text, const TYPE &arg...)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.