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 2.15
7import QtQuick.Templates 2.15 as T
8import QtQuick.Controls 2.15 as Controls
9import QtQuick.Layouts 1.15
10
11import org.kde.kirigami 2.19 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: parent._internal_formcard_margins ? parent._internal_formcard_margins : Kirigami.Units.gridUnit
76 rightPadding: parent._internal_formcard_margins ? parent._internal_formcard_margins : Kirigami.Units.gridUnit
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 onToggled: root.toggled()
136 onClicked: root.clicked()
137 onPressAndHold: root.pressAndHold()
138 onDoubleClicked: root.doubleClicked()
139
140 onCheckedChanged: {
141 root.checked = checked;
142 checked = Qt.binding(() => root.checked);
143 }
144
145 Accessible.ignored: true
146 }
147
148 Private.ContentItemLoader {
149 Layout.leftMargin: visible ? root.trailingPadding : 0
150 visible: root.trailing
151 implicitHeight: visible ? root.trailing.implicitHeight : 0
152 implicitWidth: visible ? root.trailing.implicitWidth : 0
153 contentItem: root.trailing
154 }
155 }
156}
A background for Form delegates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.