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 topPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
74 bottomPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
75 leftPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
76 rightPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
77
78 implicitWidth: contentItem.implicitWidth + leftPadding + rightPadding
79 implicitHeight: contentItem.implicitHeight + topPadding + bottomPadding
80
81 focusPolicy: Qt.StrongFocus
82 hoverEnabled: true
83 background: FormDelegateBackground { control: root }
84
85 Layout.fillWidth: true
86
87 Accessible.description: root.description
88 Accessible.role: Accessible.CheckBox
89 Accessible.onPressAction: switchItem.toggle()
90 Accessible.onToggleAction: switchItem.toggle()
91
92 contentItem: RowLayout {
93 spacing: 0
94
95 Private.ContentItemLoader {
96 Layout.rightMargin: visible ? root.leadingPadding : 0
97 visible: root.leading
98 implicitHeight: visible ? root.leading.implicitHeight : 0
99 implicitWidth: visible ? root.leading.implicitWidth : 0
100 contentItem: root.leading
101 }
102
103 ColumnLayout {
104 Layout.fillWidth: true
105 spacing: Kirigami.Units.smallSpacing
106
107 Controls.Label {
108 Layout.fillWidth: true
109 text: root.text
110 elide: Text.ElideRight
111 wrapMode: Text.Wrap
112 maximumLineCount: 2
113 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
114 Accessible.ignored: true
115 }
116
117 Controls.Label {
118 visible: root.description !== ""
119 Layout.fillWidth: true
120 text: root.description
121 wrapMode: Text.Wrap
122 color: Kirigami.Theme.disabledTextColor
123 Accessible.ignored: true
124 }
125 }
126
127 Controls.Switch {
128 id: switchItem
129 focusPolicy: Qt.NoFocus // provided by delegate
130 Layout.leftMargin: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
131
132 enabled: root.enabled
133 checked: root.checked
134
135 spacing: 0
136 contentItem: null
137
138 topPadding: 0
139 leftPadding: 0
140 rightPadding: 0
141 bottomPadding: 0
142
143 onToggled: root.toggled()
144 onClicked: root.clicked()
145 onPressAndHold: root.pressAndHold()
146 onDoubleClicked: root.doubleClicked()
147
148 onCheckedChanged: {
149 root.checked = checked;
150 checked = Qt.binding(() => root.checked);
151 }
152
153 Accessible.ignored: true
154 }
155
156 Private.ContentItemLoader {
157 Layout.leftMargin: visible ? root.trailingPadding : 0
158 visible: root.trailing
159 implicitHeight: visible ? root.trailing.implicitHeight : 0
160 implicitWidth: visible ? root.trailing.implicitWidth : 0
161 contentItem: root.trailing
162 }
163 }
164}
A background for Form delegates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:10:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.