KCMUtils

AbstractKCM.qml
1/*
2 SPDX-FileCopyrightText: 2020 Marco Martin <mart@kde.org>
3 SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8import QtQuick
9import QtQuick.Controls as QQC2
10import org.kde.kirigami as Kirigami
11
12/**
13 * This component is intended to be used as root item for
14 * KCMs with arbitrary content. Unlike SimpleKCM this does NOT
15 * provide a scrollable view, The developer will have to manage
16 * their own scrollviews.
17 * Most of the times SimpleKCM should be used instead
18 * @code
19 * import QtQuick
20 * import QtQuick.Controls as QQC2
21 * import QtQuick.Layouts
22 * import org.kde.kcmutils as KCMUtils
23 *
24 * KCMUtils.AbstractKCM {
25 * RowLayout {
26 * QQC2.ScrollView { }
27 * QQC2.ScrollView { }
28 * }
29 * footer: QQC2.ToolBar { }
30 * }
31 * @endcode
32 * @inherits org.kde.kirigami.Page
33 * @since 6.0
34 */
35Kirigami.Page {
36 id: root
38 readonly property int margins: 6 // Layout_ChildMarginWidth from Breeze
39
40 /**
41 * framedView: bool
42 * Whether to use this component as the base of a "framed" KCM with an
43 * inner scrollview that draws its own frame.
44 * Default: true
45 */
46 property bool framedView: true
47
48 /**
49 * extraFooterTopPadding: bool
50 * Whether the footer should have extra space and a separator line drawn
51 * above it. Set this to true in a KCM with a custom footer and a ListView
52 * immediately above it.
53 * Default: false
54 */
55 property bool extraFooterTopPadding: false
56
57 /**
58 * headerPaddingEnabled: bool
59 * Whether the contents of the header will have automatic padding around it.
60 * Should be disabled when using an InlineMessage or custom content item in
61 * the header that's intended to touch the window edges.
62 * Default: true
63 */
64 property bool headerPaddingEnabled: true
65
66 /**
67 * footerPaddingEnabled: bool
68 * Whether the contents of the footer will have automatic padding around it.
69 * Should be disabled when using an InlineMessage or custom content item in
70 * the footer that's intended to touch the window edges.
71 * Default: true
72 */
73 property bool footerPaddingEnabled: true
74
75 property bool sidebarMode: false
76
77 function __itemVisible(item: Item): bool {
78 return item !== null && item.visible && item.implicitHeight > 0;
79 }
80
81 function __headerContentVisible(): bool {
82 return __itemVisible(headerParent.contentItem);
83 }
84 function __footerContentVisible(): bool {
85 return __itemVisible(footerParent.contentItem);
86 }
87
88 // Deliberately not checking for __footerContentVisible because
89 // we always want the footer line to be visible when the scrollview
90 // doesn't have a frame of its own, because System Settings always
91 // adds its own footer for the Apply, Help, and Defaults buttons
92 function __headerSeparatorVisible(): bool {
93 return !framedView && __headerContentVisible();
94 }
95 function __footerSeparatorVisible(): bool {
96 return !framedView && extraFooterTopPadding;
97 }
98
99 title: (typeof kcm !== "undefined") ? kcm.name : ""
100
101 // Make pages fill the whole view by default
102 Kirigami.ColumnView.fillWidth: sidebarMode
103 ? Kirigami.ColumnView.view
104 && (Kirigami.ColumnView.view.width < Kirigami.Units.gridUnit * 36
105 || Kirigami.ColumnView.index >= Kirigami.ColumnView.view.count - 1)
106 : true
107
108 padding: 0
109 topPadding: framedView && !__headerContentVisible() ? margins : 0
110 leftPadding: undefined
111 rightPadding: undefined
112 bottomPadding: framedView && !__footerContentVisible() ? margins : 0
113 verticalPadding: undefined
114 horizontalPadding: framedView ? margins : 0
115
116 header: Column {
118 id: headerParent
119
120 anchors {
121 left: parent.left
122 right: parent.right
123 }
124
125 height: root.__headerContentVisible() ? undefined : 0
126 padding: root.headerPaddingEnabled ? root.margins : 0
127 }
128
129 // When the scrollview isn't drawing its own frame, we need to add a
130 // line below the header (when visible) to separate it from the view
132 anchors {
133 left: parent.left
134 right: parent.right
135 }
136
137 visible: root.__headerSeparatorVisible()
138 }
139 }
140
141 // View background, shown when the scrollview isn't drawing its own frame
142 Rectangle {
143 anchors.fill: parent
144 visible: !root.framedView
145 Kirigami.Theme.colorSet: Kirigami.Theme.View
146 Kirigami.Theme.inherit: false
147 color: Kirigami.Theme.backgroundColor
148 }
149
150 footer: Column {
151 // When the scrollview isn't drawing its own frame, we need to add a
152 // line above the footer ourselves to separate it from the view
154 anchors {
155 left: parent.left
156 right: parent.right
157 }
158
159 visible: root.__footerSeparatorVisible()
160 }
161
163 id: footerParent
164
165 anchors {
166 left: parent.left
167 right: parent.right
168 }
169
170 height: root.__footerContentVisible() ? undefined : 0
171 padding: root.footerPaddingEnabled ? root.margins : 0
172 }
173 }
174
175 function __swapContentIntoContainer(property: string, container: Item): void {
176 const content = this[property];
177 const rootContainer = container.parent;
178
179 if (content && content !== rootContainer) {
180 // Revert the effect of repeated onHeaderChanged invocations
181 // during initialization in Page super-type.
182 content.anchors.top = undefined;
183
184 this[property] = rootContainer;
185 container.contentItem = content;
186 }
187 }
188
189 function __adoptOverlaySheets(): void {
190 // Search overlaysheets in contentItem, parent to root if found
191 for (const object of contentItem.data) {
192 if (object instanceof Kirigami.OverlaySheet) {
193 if (object.parent === null) {
194 object.parent = this;
195 }
196 data.push(object);
197 }
198 }
199 }
200
201 Component.onCompleted: {
202 __swapContentIntoContainer("header", headerParent);
203 __swapContentIntoContainer("footer", footerParent);
204 __adoptOverlaySheets();
205 }
206}
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:13:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.