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 leftPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
82 topPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
83 bottomPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
84 rightPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
85
86 implicitWidth: contentItem.implicitWidth + leftPadding + rightPadding
87 implicitHeight: contentItem.implicitHeight + topPadding + bottomPadding
88
89 focusPolicy: Qt.StrongFocus
90 hoverEnabled: true
91 background: FormDelegateBackground { control: root }
92
93 icon {
94 width: Kirigami.Units.iconSizes.smallMedium
95 height: Kirigami.Units.iconSizes.smallMedium
96 }
97
98 Layout.fillWidth: true
99
100 contentItem: ColumnLayout {
101 spacing: Kirigami.Units.smallSpacing
102
103 RowLayout {
104 id: innerRowLayout
105
106 spacing: 0
107
108 Layout.fillWidth: true
109
110 Private.ContentItemLoader {
111 Layout.rightMargin: visible ? root.leadingPadding : 0
112 visible: root.leading
113 implicitHeight: visible ? root.leading.implicitHeight : 0
114 implicitWidth: visible ? root.leading.implicitWidth : 0
115 contentItem: root.leading
116 }
117
118 Controls.RadioButton {
119 id: radioButtonItem
120 focusPolicy: Qt.NoFocus // provided by delegate
121 Layout.rightMargin: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
122
123 enabled: root.enabled
124 checked: root.checked
125
126 contentItem: null // Remove right margin
127 spacing: 0
128
129 topPadding: 0
130 leftPadding: 0
131 rightPadding: 0
132 bottomPadding: 0
133
134 onToggled: root.toggled()
135 onClicked: root.clicked()
136 onPressAndHold: root.pressAndHold()
137 onDoubleClicked: root.doubleClicked()
138
139 onCheckedChanged: {
140 root.checked = checked;
141 checked = Qt.binding(() => root.checked);
142 }
143 }
144
145 Kirigami.Icon {
146 visible: root.icon.name.length > 0 || root.icon.source.toString().length > 0
147 source: root.icon.name.length > 0 ? root.icon.name : root.icon.source
148 color: root.icon.color
149 Layout.rightMargin: visible ? Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing : 0
150 implicitWidth: visible ? root.icon.width : 0
151 implicitHeight: visible ? root.icon.height : 0
152 }
153
154 Controls.Label {
155 Layout.fillWidth: true
156 text: root.text
157 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
158 elide: Text.ElideRight
159 wrapMode: Text.Wrap
160 maximumLineCount: 2
161 }
162
163 Private.ContentItemLoader {
164 Layout.leftMargin: visible ? root.trailingPadding : 0
165 visible: root.trailing
166 implicitHeight: visible ? root.trailing.implicitHeight : 0
167 implicitWidth: visible ? root.trailing.implicitWidth : 0
168 contentItem: root.trailing
169 }
170 }
171
172 Controls.Label {
173 id: internalDescriptionItem
174
175 visible: root.description !== ""
176 Layout.fillWidth: true
177 text: root.description
178 color: Kirigami.Theme.disabledTextColor
179 wrapMode: Text.Wrap
180 }
181 }
182}
183
184
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.