1/*
2    SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
3
4    SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick 2.6
8import QtQuick.Layouts 1.2
9import QtQuick.Templates @QQC2_VERSION@ as T
10import org.kde.plasma.core 2.0 as PlasmaCore
11import org.kde.kirigami 2.5 as Kirigami
12
13T.MenuItem {
14    id: controlRoot
15
16    palette: Kirigami.Theme.palette
17    implicitWidth: Math.max(background ? background.implicitWidth : 0,
18                            contentItem.implicitWidth + leftPadding + rightPadding + (arrow ? arrow.implicitWidth : 0))
19    implicitHeight: Math.max(background ? background.implicitHeight : 0,
20                             Math.max(contentItem.implicitHeight,
21                                      indicator ? indicator.implicitHeight : 0) + topPadding + bottomPadding)
22    baselineOffset: contentItem.y + contentItem.baselineOffset
23
24    leftPadding: highlight.margins.left
25    topPadding: highlight.margins.top
26    rightPadding: highlight.margins.right
27    bottomPadding: highlight.margins.bottom
28    spacing: PlasmaCore.Units.smallSpacing
29    hoverEnabled: true
30
31    Kirigami.MnemonicData.enabled: controlRoot.enabled && controlRoot.visible
32    Kirigami.MnemonicData.controlType: Kirigami.MnemonicData.MenuItem
33    Kirigami.MnemonicData.label: controlRoot.text
34    Shortcut {
35        //in case of explicit & the button manages it by itself
36        enabled: !(RegExp(/\&[^\&]/).test(controlRoot.text))
37        sequence: controlRoot.Kirigami.MnemonicData.sequence
38        onActivated: {
39            if (controlRoot.checkable) {
40                controlRoot.toggle();
41            } else {
42                controlRoot.clicked();
43            }
44        }
45    }
46
47    contentItem: RowLayout {
48        Item {
49           Layout.preferredWidth: (controlRoot.ListView.view && controlRoot.ListView.view.hasCheckables) || controlRoot.checkable ? controlRoot.indicator.width : Kirigami.Units.smallSpacing
50        }
51        PlasmaCore.IconItem {
52            Layout.alignment: Qt.AlignVCenter
53            visible: (controlRoot.ListView.view && controlRoot.ListView.view.hasIcons) || (controlRoot.icon != undefined && (controlRoot.icon.name.length > 0 || controlRoot.icon.source.length > 0))
54            source: controlRoot.icon ? (controlRoot.icon.name || controlRoot.icon.source) : ""
55            Layout.preferredHeight: Math.max(label.height, Kirigami.Units.iconSizes.small)
56            Layout.preferredWidth: Layout.preferredHeight
57        }
58        Label {
59            id: label
60            Layout.alignment: Qt.AlignVCenter
61            Layout.fillWidth: true
62
63            text: controlRoot.Kirigami.MnemonicData.richTextLabel
64            font: controlRoot.font
65            elide: Text.ElideRight
66            visible: controlRoot.text
67            horizontalAlignment: Text.AlignLeft
68            verticalAlignment: Text.AlignVCenter
69        }
70    }
71
72    arrow: PlasmaCore.IconItem {
73        x: controlRoot.mirrored ? controlRoot.padding : controlRoot.width - width - controlRoot.padding
74        y: controlRoot.topPadding + (controlRoot.availableHeight - height) / 2
75        source: controlRoot.mirrored ? "go-next-symbolic-rtl" : "go-next-symbolic"
76        status: controlRoot.highlighted ? PlasmaCore.Svg.Selected : PlasmaCore.Svg.Normal
77        width: Math.max(label.height, Kirigami.Units.iconSizes.small)
78        height: width
79        visible: controlRoot.subMenu
80    }
81
82    indicator: Loader {
83        x: controlRoot.mirrored ? controlRoot.width - width - controlRoot.rightPadding : controlRoot.leftPadding
84        y: controlRoot.topPadding + (controlRoot.availableHeight - height) / 2
85
86        visible: controlRoot.checkable
87        sourceComponent: controlRoot.autoExclusive ? radioComponent : checkComponent
88    }
89
90    Component {
91        id: radioComponent
92        RadioIndicator {
93            control: controlRoot
94        }
95    }
96    Component {
97        id: checkComponent
98        CheckIndicator {
99            control: controlRoot
100        }
101    }
102
103    background: Item {
104        implicitWidth: PlasmaCore.Units.gridUnit * 8
105
106        PlasmaCore.FrameSvgItem {
107            id: highlight
108            imagePath: "widgets/viewitem"
109            prefix: "hover"
110            colorGroup: PlasmaCore.ColorScope.colorGroup
111            anchors.fill: parent
112            opacity: {
113                if (controlRoot.highlighted || controlRoot.hovered || controlRoot.pressed) {
114                    return 1
115                } else {
116                    return 0
117                }
118            }
119            Behavior on opacity {
120                NumberAnimation {
121                    duration: PlasmaCore.Units.shortDuration
122                }
123            }
124        }
125    }
126}
127