Kirigami-addons

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

KDE's Doxygen guidelines are available online.