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 KCM
23 *
24 * KCM.AbstractKCM {
25 * RowLayout {
26 * QQC2.ScrollView {
27 * // ...
28 * }
29 * QQC2.ScrollView {
30 * // ...
31 * }
32 * }
33 * footer: QQC2.ToolBar {
34 * // ...
35 * }
36 * }
37 * @endcode
38 * @inherits org.kde.kirigami.Page
39 * @since 6.0
40 */
41Kirigami.Page {
42 id: root
44 readonly property int margins: 6 // Layout_ChildMarginWidth from Breeze
45
46 /**
47 * framedView: bool
48 * Whether to use this component as the base of a "framed" KCM with an
49 * inner scrollview that draws its own frame.
50 * Default: true
51 */
52 property bool framedView: true
53
54 /**
55 * extraFooterTopPadding: bool
56 * @deprecated unused
57 * Default: false
58 */
59 property bool extraFooterTopPadding: false
60
61 property bool sidebarMode: false
62
63 function __itemVisible(item: Item): bool {
64 return item !== null && item.visible && item.implicitHeight > 0;
65 }
66
67 function __headerContentVisible(): bool {
68 return __itemVisible(headerParent.contentItem);
69 }
70 function __footerContentVisible(): bool {
71 return __itemVisible(footerParent.contentItem);
72 }
73
74 // Deliberately not checking for __footerContentVisible because
75 // we always want the footer line to be visible when the scrollview
76 // doesn't have a frame of its own, because System Settings always
77 // adds its own footer for the Apply, Help, and Defaults buttons
78 function __headerSeparatorVisible(): bool {
79 return !framedView && __headerContentVisible();
80 }
81 function __footerSeparatorVisible(): bool {
82 return !framedView && extraFooterTopPadding;
83 }
84
85 title: (typeof kcm !== "undefined") ? kcm.name : ""
86
87 // Make pages fill the whole view by default
88 Kirigami.ColumnView.fillWidth: sidebarMode
89 ? Kirigami.ColumnView.view
90 && (Kirigami.ColumnView.view.width < Kirigami.Units.gridUnit * 36
91 || Kirigami.ColumnView.index >= Kirigami.ColumnView.view.count - 1)
92 : true
93
94 padding: 0
95 topPadding: framedView && !__headerContentVisible() ? margins : 0
96 leftPadding: undefined
97 rightPadding: undefined
98 bottomPadding: framedView && !__footerContentVisible() ? margins : 0
99 verticalPadding: undefined
100 horizontalPadding: framedView ? margins : 0
101
102 header: Kirigami.Padding {
103 id: headerParent
104
105 height: root.__headerContentVisible()
106 ? undefined
107 : (root.__headerSeparatorVisible()
108 ? headerSeparator.implicitHeight
109 : 0)
110
111 padding: root.margins
112 bottomPadding: root.__headerSeparatorVisible()
113 ? verticalPadding + headerSeparator.implicitHeight
114 : undefined
115
116 // When the scrollview isn't drawing its own frame, we need to add a
117 // line below the header (when visible) to separate it from the view
119 id: headerSeparator
120 anchors {
121 left: parent.left
122 right: parent.right
123 bottom: parent.bottom
124 }
125 visible: root.__headerSeparatorVisible()
126 }
127 }
128
129 // View background, shown when the scrollview isn't drawing its own frame
130 Rectangle {
131 anchors.fill: parent
132 visible: !root.framedView
133 Kirigami.Theme.colorSet: Kirigami.Theme.View
134 Kirigami.Theme.inherit: false
135 color: Kirigami.Theme.backgroundColor
136 }
137
138 footer: Kirigami.Padding {
139 id: footerParent
140
141 height: root.__footerContentVisible()
142 ? undefined
143 : (root.__footerSeparatorVisible()
144 ? footerSeparator.implicitHeight
145 : 0)
146
147 padding: root.margins
148 topPadding: root.__footerSeparatorVisible()
149 ? verticalPadding + footerSeparator.implicitHeight
150 : undefined
151
152 // When the scrollview isn't drawing its own frame, we need to add a
153 // line above the footer ourselves to separate it from the view
155 id: footerSeparator
156 anchors {
157 top: parent.top
158 left: parent.left
159 right: parent.right
160 }
161 visible: root.__footerSeparatorVisible()
162 }
163 }
164
165 function __swapContentIntoContainer(property: string, container: Item) {
166 const content = this[property];
167
168 if (content && content !== container) {
169 // Revert the effect of repeated onHeaderChanged invocations
170 // during initialization in Page super-type.
171 content.anchors.top = undefined;
172
173 this[property] = container;
174 container.contentItem = content;
175 container.visible = true;
176 }
177 }
178
179 Component.onCompleted: {
180 __swapContentIntoContainer("header", headerParent);
181 __swapContentIntoContainer("footer", footerParent);
182
183 //Search overlaysheets in contentItem, parent to root if found
184 for (const obj of contentItem.data) {
185 if (obj instanceof Kirigami.OverlaySheet) {
186 if (!obj.parent) {
187 obj.parent = this;
188 }
189 data.push(obj);
190 }
191 }
192 }
193}
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 Tue Mar 26 2024 11:17:56 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.