Kirigami-addons

RadioSelector.qml
1// SPDX-License-Identifier: GPL-2.0-or-later
2// SPDX-FileCopyrightText: 2023 Mathis BrĂ¼chert <mbb@kaidan.im>
3
4import QtQuick
5import QtQuick.Controls as QQC2
6import QtQuick.Templates as T
7import QtQuick.Layouts
8import org.kde.kirigami as Kirigami
9
10/**
11* @brief A Component that allows sitching between multiple options.
12*
13* Example:
14*
15* @code{.qml}
16* Components.RadioSelector {
17* consistentWidth: false
18* actions: [
19* Kirigami.Action {
20* text: i18nc("@option:radio", "Week")
21* icon.name: "view-calendar-week-symbolic"
22* },
23* Kirigami.Action {
24* text: i18nc("@option:radio", "3 Days")
25* icon.name: "view-calendar-upcoming-days-symbolic"
26* },
27* Kirigami.Action {
28* text: i18nc("@option:radio", "1 Day")
29* icon.name: "view-calendar-day-symbolic"
30* }
31* ]
32* }
33* @endcode
34* @since Kirigami Addons 1.6.0.
35*/
36Item {
37 id: root
38
39 /**
40 * @brief This property holds a list of actions, each holding one of the
41 * options.
42 */
43 property list<T.Action> actions
44
45 /**
46 * @brief This property holds whether all the items should have the same
47 * width.
48 */
49 property bool consistentWidth: false
51 /**
52 * @brief This property holds the currently selected option. By default,
53 * it's the first actions or -1 if no actions is set.
54 */
55 property int selectedIndex: actions.length > 0 ? 0 : -1
56
57 Layout.minimumWidth: consistentWidth ? 0 : switchLayout.implicitWidth
58 Layout.fillWidth: consistentWidth
59
60 implicitHeight: switchLayout.implicitHeight
61
62 onSelectedIndexChanged: if (selectedIndex >= 0 && repeater.count < selectedIndex && !repeater.childAt(selectedIndex).checked) {
63 repeater.childAt(selectedIndex).clicked();
64 }
65
66 QQC2.ButtonGroup {
67 id: buttonGroup
68
69 buttons: switchLayout.children.filter((child) => child !== repeater)
70 }
71
72 RowLayout {
73 id: switchLayout
74
75 anchors {
76 top: root.top
77 left: root.left
78 right: root.right
79 }
80
81 Repeater {
82 id: repeater
83
84 model: actions
85 delegate: QQC2.ToolButton {
86 id: button
87
88 required property T.Action modelData
89 required property int index
90
91 Layout.fillWidth: true
92 Layout.preferredWidth: root.consistentWidth ? (root.width/repeater.count)-(switchLayout.spacing/repeater.count-1) : button.implicitWidth
93 Layout.minimumHeight: Kirigami.Units.gridUnit * 2
94
95 checkable: true
96 action: modelData
97 text: modelData.text
98 icon.name: modelData.icon.name
99 visible: !(modelData instanceof Kirigami.Action) || modelData.visible
100
101 icon {
102 width: Kirigami.Units.iconSizes.sizeForLabels
103 height: Kirigami.Units.iconSizes.sizeForLabels
104 }
105
106 background: Rectangle {
107 radius: Kirigami.Units.cornerRadius
108 color: Kirigami.Theme.textColor
109 opacity: Kirigami.Settings.hasTransientTouchInput ? 0 : ( button.hovered ? 0.1 : 0)
110
111 Behavior on opacity {
112 PropertyAnimation {
113 duration: Kirigami.Units.shortDuration
114 easing.type: Easing.InOutCubic
115 }
116 }
117 }
118
119 contentItem: RowLayout {
120 Item {
121 Layout.leftMargin: icon.visible ? Kirigami.Units.smallSpacing : Kirigami.Units.largeSpacing
122 Layout.fillWidth: true
123 }
124
125 Kirigami.Icon {
126 id: icon
127
128 color: button.checked ? Kirigami.Theme.hoverColor : Kirigami.Theme.textColor
129 visible: button.icon.name
130 source: button.icon.name
131 implicitHeight: button.icon.height
132 implicitWidth: button.icon.width
133 Behavior on color {
134 PropertyAnimation {
135 duration: Kirigami.Units.longDuration
136 easing.type: Easing.InOutCubic
137 }
138 }
139 }
140
141 QQC2.Label {
142 Layout.alignment: Qt.AlignVCenter
143
144 font.bold: button.checked
145 color: Kirigami.Theme.textColor
146 text: button.text
147
148 Behavior on font.bold {
149 PropertyAnimation {
150 duration: Kirigami.Units.longDuration
151 easing.type: Easing.InOutCubic
152 }
153 }
154
155 Accessible.ignored: true
156 }
157
158 Item {
159 Layout.fillWidth: true
160 Layout.rightMargin: Kirigami.Units.largeSpacing
161 }
162 }
163
164 onClicked: {
165 button.checked = true;
166 root.selectedIndex = index;
167 }
168
169 Component.onCompleted: if (index === root.selectedIndex) {
170 button.checked = true;
171 root.selectedIndex = index;
172 }
173 }
174 }
175 }
176
177 Kirigami.ShadowedRectangle {
178 id: marker
179
180 x: buttonGroup.checkedButton.x
181 y: switchLayout.y
182 z: switchLayout.z - 1
183
184 height: switchLayout.implicitHeight
185 width: buttonGroup.checkedButton.width
186 radius: Kirigami.Units.cornerRadius
187
188 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.hoverColor, Kirigami.Theme.backgroundColor, 0.8)
189 border {
190 width: 1
191 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.hoverColor, Kirigami.Theme.backgroundColor, 0.5)
192 }
193 shadow {
194 size: 7
195 yOffset: 3
196 color: Qt.rgba(0, 0, 0, 0.15)
197 }
198
199 Behavior on x {
200 PropertyAnimation {
201 id: x_anim
202
203 duration: Kirigami.Units.longDuration
204 easing.type: Easing.InOutCubic
205 }
206 }
207
208 Behavior on width {
209 PropertyAnimation {
210 id: width_anim
211
212 duration: Kirigami.Units.longDuration
213 easing.type: Easing.InOutCubic
214 }
215 }
216 }
217}
Type type(const QSqlDatabase &db)
QString name(StandardAction id)
AlignVCenter
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
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.