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 * @deprecated unused
51 * Default: false
52 */
53 property bool extraFooterTopPadding: false
54
55 /**
56 * headerPaddingEnabled: bool
57 * Whether the contents of the header will have automatic padding around it.
58 * Should be disabled when using an InlineMessage or custom content item in
59 * the header that's intended to touch the window edges.
60 * Default: true
61 */
62 property bool headerPaddingEnabled: true
63
64 /**
65 * footerPaddingEnabled: bool
66 * Whether the contents of the footer will have automatic padding around it.
67 * Should be disabled when using an InlineMessage or custom content item in
68 * the footer that's intended to touch the window edges.
69 * Default: true
70 */
71 property bool footerPaddingEnabled: true
72
73 property bool sidebarMode: false
74
75 function __itemVisible(item: Item): bool {
76 return item !== null && item.visible && item.implicitHeight > 0;
77 }
78
79 function __headerContentVisible(): bool {
80 return __itemVisible(headerParent.contentItem);
81 }
82 function __footerContentVisible(): bool {
83 return __itemVisible(footerParent.contentItem);
84 }
85
86 // Deliberately not checking for __footerContentVisible because
87 // we always want the footer line to be visible when the scrollview
88 // doesn't have a frame of its own, because System Settings always
89 // adds its own footer for the Apply, Help, and Defaults buttons
90 function __headerSeparatorVisible(): bool {
91 return !framedView && __headerContentVisible();
92 }
93 function __footerSeparatorVisible(): bool {
94 return !framedView && extraFooterTopPadding;
95 }
96
97 title: (typeof kcm !== "undefined") ? kcm.name : ""
98
99 // Make pages fill the whole view by default
100 Kirigami.ColumnView.fillWidth: sidebarMode
101 ? Kirigami.ColumnView.view
102 && (Kirigami.ColumnView.view.width < Kirigami.Units.gridUnit * 36
103 || Kirigami.ColumnView.index >= Kirigami.ColumnView.view.count - 1)
104 : true
105
106 padding: 0
107 topPadding: framedView && !__headerContentVisible() ? margins : 0
108 leftPadding: undefined
109 rightPadding: undefined
110 bottomPadding: framedView && !__footerContentVisible() ? margins : 0
111 verticalPadding: undefined
112 horizontalPadding: framedView ? margins : 0
113
114 header: Column {
116 id: headerParent
117
118 anchors {
119 left: parent.left
120 right: parent.right
121 }
122
123 height: root.__headerContentVisible() ? undefined : 0
124 padding: root.headerPaddingEnabled ? root.margins : 0
125 }
126
127 // When the scrollview isn't drawing its own frame, we need to add a
128 // line below the header (when visible) to separate it from the view
130 anchors {
131 left: parent.left
132 right: parent.right
133 }
134
135 visible: root.__headerSeparatorVisible()
136 }
137 }
138
139 // View background, shown when the scrollview isn't drawing its own frame
140 Rectangle {
141 anchors.fill: parent
142 visible: !root.framedView
143 Kirigami.Theme.colorSet: Kirigami.Theme.View
144 Kirigami.Theme.inherit: false
145 color: Kirigami.Theme.backgroundColor
146 }
147
148 footer: Column {
149 // When the scrollview isn't drawing its own frame, we need to add a
150 // line above the footer ourselves to separate it from the view
152 anchors {
153 left: parent.left
154 right: parent.right
155 }
156
157 visible: root.__footerSeparatorVisible()
158 }
159
161 id: footerParent
162
163 anchors {
164 left: parent.left
165 right: parent.right
166 }
167
168 height: root.__footerContentVisible() ? undefined : 0
169 padding: root.footerPaddingEnabled ? root.margins : 0
170 }
171 }
172
173 function __swapContentIntoContainer(property: string, container: Item): void {
174 const content = this[property];
175 const rootContainer = container.parent;
176
177 if (content && content !== rootContainer) {
178 // Revert the effect of repeated onHeaderChanged invocations
179 // during initialization in Page super-type.
180 content.anchors.top = undefined;
181
182 this[property] = rootContainer;
183 container.contentItem = content;
184 }
185 }
186
187 function __adoptOverlaySheets(): void {
188 // Search overlaysheets in contentItem, parent to root if found
189 for (const object of contentItem.data) {
190 if (object instanceof Kirigami.OverlaySheet) {
191 if (object.parent === null) {
192 object.parent = this;
193 }
194 data.push(object);
195 }
196 }
197 }
198
199 Component.onCompleted: {
200 __swapContentIntoContainer("header", headerParent);
201 __swapContentIntoContainer("footer", footerParent);
202 __adoptOverlaySheets();
203 }
204}
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 May 17 2024 11:48:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.