Kirigami-addons

FormSwitchDelegate.qml
1/*
2 * Copyright 2022 Devin Lin <devin@kde.org>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6import QtQuick
7import QtQuick.Templates as T
8import QtQuick.Controls as Controls
9import QtQuick.Layouts
10
11import org.kde.kirigami as Kirigami
12
13import "private" as Private
14
15/**
16 * @brief A Form delegate that corresponds to a switch.
17 *
18 * This component is used to create a purely on/off toggle for a single
19 * setting.
20 *
21 * Use the inherited QtQuick.Controls.AbstractButton.text property to define
22 * the main text of the button.
23 *
24 * If you need an on/off/tristate toggle, use a FormCheckDelegate instead.
25 *
26 * If you need multiple values for the same setting, use a
27 * FormComboBoxDelegate instead.
28 *
29 * If you need multiple toggles for the same setting, use a FormRadioDelegate
30 * instead.
31 *
32 * @since KirigamiAddons 0.11.0
33 *
34 * @see QtQuick.Controls.AbstractButton
35 * @see FormCheckDelegate
36 * @see FormComboBoxDelegate
37 * @see FormRadioDelegate
38 *
39 * @inherit QtQuick.Controls.SwitchDelegate
40 */
41T.SwitchDelegate {
42 id: root
44 /**
45 * @brief A label containing secondary text that appears under the inherited text property.
46 *
47 * This provides additional information shown in a faint gray color.
48 */
49 property string description: ""
50
51 /**
52 * @brief This property holds an item that will be displayed
53 * to the left of the delegate's contents.
54 */
55 property var leading: null
56
57 /**
58 * @brief This property holds the padding after the leading item.
59 */
60 property real leadingPadding: Kirigami.Units.smallSpacing
62 /**
63 * @brief This property holds an item that will be displayed
64 * to the right of the delegate's contents.
65 */
66 property var trailing: null
67
68 /**
69 * @brief This property holds the padding before the trailing item.
70 */
71 property real trailingPadding: Kirigami.Units.smallSpacing
72
73 horizontalPadding: Private.FormCardUnits.horizontalPadding
74 verticalPadding: Private.FormCardUnits.verticalPadding
75
76 implicitWidth: contentItem.implicitWidth + leftPadding + rightPadding
77 implicitHeight: contentItem.implicitHeight + topPadding + bottomPadding
78
79 focusPolicy: Qt.StrongFocus
80 hoverEnabled: true
81 background: FormDelegateBackground { control: root }
82
83 icon {
84 width: Kirigami.Units.iconSizes.smallMedium
85 height: Kirigami.Units.iconSizes.smallMedium
86 }
87
88 Layout.fillWidth: true
89
90 Accessible.description: root.description
91 Accessible.role: Accessible.CheckBox
92 Accessible.onPressAction: switchItem.toggle()
93 Accessible.onToggleAction: switchItem.toggle()
94
95 contentItem: RowLayout {
96 spacing: 0
97
98 Private.ContentItemLoader {
99 Layout.rightMargin: visible ? root.leadingPadding : 0
100 visible: root.leading
101 implicitHeight: visible ? root.leading.implicitHeight : 0
102 implicitWidth: visible ? root.leading.implicitWidth : 0
103 contentItem: root.leading
104 }
105
106 Kirigami.Icon {
107 visible: root.icon.name.length > 0 || root.icon.source.toString().length > 0
108 source: root.icon.name.length > 0 ? root.icon.name : root.icon.source
109 color: root.icon.color
110 Layout.rightMargin: visible ? Private.FormCardUnits.horizontalSpacing : 0
111 implicitWidth: visible ? root.icon.width : 0
112 implicitHeight: visible ? root.icon.height : 0
113 }
114
115 ColumnLayout {
116 Layout.fillWidth: true
117 spacing: Private.FormCardUnits.verticalSpacing
118
119 Controls.Label {
120 Layout.fillWidth: true
121 text: root.text
122 elide: Text.ElideRight
123 wrapMode: Text.Wrap
124 maximumLineCount: 2
125 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
126 Accessible.ignored: true
127 }
128
129 Controls.Label {
130 visible: root.description !== ""
131 Layout.fillWidth: true
132 text: root.description
133 wrapMode: Text.Wrap
134 color: Kirigami.Theme.disabledTextColor
135 Accessible.ignored: true
136 }
137 }
138
139 Controls.Switch {
140 id: switchItem
141 focusPolicy: Qt.NoFocus // provided by delegate
142 Layout.leftMargin: Private.FormCardUnits.horizontalSpacing
143
144 enabled: root.enabled
145 checked: root.checked
146
147 spacing: 0
148 contentItem: null
149
150 topPadding: 0
151 leftPadding: 0
152 rightPadding: 0
153 bottomPadding: 0
154
155 onToggled: root.toggled()
156 onClicked: root.clicked()
157 onPressAndHold: root.pressAndHold()
158 onDoubleClicked: root.doubleClicked()
159
160 onCheckedChanged: {
161 root.checked = checked;
162 checked = Qt.binding(() => root.checked);
163 }
164
165 Accessible.ignored: true
166 }
167
168 Private.ContentItemLoader {
169 Layout.leftMargin: visible ? root.trailingPadding : 0
170 visible: root.trailing
171 implicitHeight: visible ? root.trailing.implicitHeight : 0
172 implicitWidth: visible ? root.trailing.implicitWidth : 0
173 contentItem: root.trailing
174 }
175 }
176}
A background for Form delegates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.