Warning, /libraries/kirigami-addons/src/components/SegmentedButton.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 import QtQuick 2.15
0005 import QtQuick.Controls 2.15 as QQC2
0006 import QtQuick.Templates 2.15 as T
0007 import QtQuick.Layouts 1.15
0008 import org.kde.kirigami 2.20 as Kirigami
0009 import org.kde.kirigamiaddons.delegates 1.0 as Delegates
0010 
0011 RowLayout {
0012     id: root
0013 
0014     property list<T.Action> actions
0015 
0016     spacing: Math.round(Kirigami.Units.smallSpacing / 2)
0017 
0018     Repeater {
0019         id: buttonRepeater
0020 
0021         model: root.actions
0022 
0023         delegate: QQC2.AbstractButton {
0024             id: buttonDelegate
0025 
0026             required property int index
0027             required property T.Action modelData
0028 
0029             property bool highlightBackground: down || checked
0030             property bool highlightBorder: enabled && down || checked || highlighted || visualFocus || hovered
0031 
0032             padding: Kirigami.Units.mediumSpacing
0033 
0034             focus: index === 0
0035 
0036             action: modelData
0037 
0038             display: modelData.displayHint & Kirigami.DisplayHint.IconOnly ? QQC2.AbstractButton.IconOnly : QQC2.AbstractButton.TextBesideIcon
0039 
0040             Layout.fillHeight: true
0041             Layout.minimumWidth: height
0042 
0043             icon {
0044                 width: Kirigami.Units.iconSizes.smallMedium
0045                 height: Kirigami.Units.iconSizes.smallMedium
0046             }
0047 
0048             contentItem: Delegates.DefaultContentItem {
0049                 itemDelegate: buttonDelegate
0050                 iconItem.Layout.fillWidth: buttonDelegate.modelData instanceof Kirigami.Action
0051                     ? buttonDelegate.modelData.displayHint & Kirigami.DisplayHint.IconOnly
0052                     : true
0053 
0054                 labelItem {
0055                     elide: Text.ElideRight
0056                     horizontalAlignment: Text.AlignHCenter
0057                     Layout.fillWidth: true
0058                     Accessible.ignored: true
0059                 }
0060             }
0061 
0062             background: Kirigami.ShadowedRectangle {
0063 
0064                 property color flatColor: Qt.rgba(
0065                     Kirigami.Theme.backgroundColor.r,
0066                     Kirigami.Theme.backgroundColor.g,
0067                     Kirigami.Theme.backgroundColor.b,
0068                     0
0069                 )
0070 
0071                 corners {
0072                     topLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
0073                     bottomLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
0074 
0075                     bottomRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
0076                     topRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
0077                 }
0078 
0079                 visible: !buttonDelegate.flat || buttonDelegate.editable || buttonDelegate.down || buttonDelegate.checked || buttonDelegate.highlighted || buttonDelegate.visualFocus || buttonDelegate.hovered
0080 
0081                 color: {
0082                     if (buttonDelegate.highlightBackground) {
0083                         return Kirigami.Theme.alternateBackgroundColor
0084                     } else if (buttonDelegate.flat) {
0085                         return flatColor
0086                     } else {
0087                         return Kirigami.Theme.backgroundColor
0088                     }
0089                 }
0090 
0091                 border {
0092                     color: {
0093                         if (buttonDelegate.highlightBorder) {
0094                             return Kirigami.Theme.focusColor
0095                         } else {
0096                             return Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast);
0097                         }
0098                     }
0099                     width: 1
0100                 }
0101 
0102                 Behavior on color {
0103                     enabled: buttonDelegate.highlightBackground
0104                     ColorAnimation {
0105                         duration: Kirigami.Units.shortDuration
0106                         easing.type: Easing.OutCubic
0107                     }
0108                 }
0109                 Behavior on border.color {
0110                     enabled: buttonDelegate.highlightBorder
0111                     ColorAnimation {
0112                         duration: Kirigami.Units.shortDuration
0113                         easing.type: Easing.OutCubic
0114                     }
0115                 }
0116 
0117                 Kirigami.ShadowedRectangle {
0118                     id: root
0119 
0120                     height: buttonDelegate.height
0121                     z: -1
0122                     color: Qt.rgba(0, 0, 0, 0.1)
0123 
0124                     opacity: buttonDelegate.down ? 0 : 1
0125                     visible: !buttonDelegate.editable && !buttonDelegate.flat && buttonDelegate.enabled
0126 
0127                     corners {
0128                         topLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
0129                         bottomLeftRadius: buttonDelegate.index === 0 ? Kirigami.Units.mediumSpacing : 0
0130 
0131                         bottomRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
0132                         topRightRadius: buttonDelegate.index === buttonRepeater.count - 1 ? Kirigami.Units.mediumSpacing : 0
0133                     }
0134 
0135                     anchors {
0136                         top: parent.top
0137                         topMargin: 1
0138                         left: parent.left
0139                         right: parent.right
0140                     }
0141                 }
0142             }
0143         }
0144     }
0145 }