Kirigami-addons

SegmentedButton.qml
1// SPDX-FileCopyrightText: 2023 Carl Schwan <carlschwan@kde.org>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4import QtQuick 2.15
5import QtQuick.Controls 2.15 as QQC2
6import QtQuick.Templates 2.15 as T
7import QtQuick.Layouts 1.15
8import org.kde.kirigami 2.20 as Kirigami
9import org.kde.kirigamiaddons.delegates 1.0 as Delegates
10
11RowLayout {
12 id: root
13
14 property list<T.Action> actions
15
16 spacing: Math.round(Kirigami.Units.smallSpacing / 2)
17
18 Repeater {
19 id: buttonRepeater
20
21 model: root.actions
22
23 delegate: QQC2.AbstractButton {
24 id: buttonDelegate
25
26 required property int index
27 required property T.Action modelData
28
29 property bool highlightBackground: down || checked
30 property bool highlightBorder: enabled && down || checked || visualFocus || hovered
31
32 padding: Kirigami.Units.mediumSpacing
33
34 focus: index === 0
35
36 action: modelData
37
38 display: modelData.displayHint & Kirigami.DisplayHint.IconOnly ? QQC2.AbstractButton.IconOnly : QQC2.AbstractButton.TextBesideIcon
39
40 Layout.fillHeight: true
41 Layout.minimumWidth: height
42
43 icon {
44 width: Kirigami.Units.iconSizes.smallMedium
45 height: Kirigami.Units.iconSizes.smallMedium
46 }
47
48 contentItem: Delegates.DefaultContentItem {
49 itemDelegate: buttonDelegate
50 iconItem.Layout.fillWidth: buttonDelegate.modelData instanceof Kirigami.Action
51 ? buttonDelegate.modelData.displayHint & Kirigami.DisplayHint.IconOnly
52 : true
53
54 labelItem {
55 elide: Text.ElideRight
56 horizontalAlignment: Text.AlignHCenter
57 Layout.fillWidth: true
58 Accessible.ignored: true
59 }
60 }
61
62 background: Kirigami.ShadowedRectangle {
63
64 property color flatColor: Qt.rgba(
65 Kirigami.Theme.backgroundColor.r,
66 Kirigami.Theme.backgroundColor.g,
67 Kirigami.Theme.backgroundColor.b,
68 0
69 )
70
71 corners {
72 topLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
73 bottomLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
74
75 bottomRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
76 topRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
77 }
78
79 visible: !buttonDelegate.flat || buttonDelegate.editable || buttonDelegate.down || buttonDelegate.checked || buttonDelegate.highlighted || buttonDelegate.visualFocus || buttonDelegate.hovered
80
81 color: {
82 if (buttonDelegate.highlightBackground) {
83 return Kirigami.Theme.alternateBackgroundColor
84 } else if (buttonDelegate.flat) {
85 return flatColor
86 } else {
87 return Kirigami.Theme.backgroundColor
88 }
89 }
90
91 border {
92 color: {
93 if (buttonDelegate.highlightBorder) {
94 return Kirigami.Theme.focusColor
95 } else {
96 return Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast);
97 }
98 }
99 width: 1
100 }
101
102 Behavior on color {
103 enabled: buttonDelegate.highlightBackground
104 ColorAnimation {
105 duration: Kirigami.Units.shortDuration
106 easing.type: Easing.OutCubic
107 }
108 }
109 Behavior on border.color {
110 enabled: buttonDelegate.highlightBorder
111 ColorAnimation {
112 duration: Kirigami.Units.shortDuration
113 easing.type: Easing.OutCubic
114 }
115 }
116
117 Kirigami.ShadowedRectangle {
118 id: root
119
120 height: buttonDelegate.height
121 z: -1
122 color: Qt.rgba(0, 0, 0, 0.1)
123
124 opacity: buttonDelegate.down ? 0 : 1
125 visible: !buttonDelegate.editable && !buttonDelegate.flat && buttonDelegate.enabled
126
127 corners {
128 topLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
129 bottomLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
130
131 bottomRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
132 topRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
133 }
134
135 anchors {
136 top: parent.top
137 topMargin: 1
138 left: parent.left
139 right: parent.right
140 }
141 }
142 }
143 }
144 }
145}
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 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.