1/*
2 *   SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3 *
4 *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6
7import QtQuick 2.5
8import QtQuick.Controls 2.5 as QQC2
9
10import org.kde.kirigami 2.14 as Kirigami
11
12Kirigami.BasicListItem
13{
14    id: item
15    property QtObject action: null
16    checked: action.checked
17    icon: action.iconName
18    separatorVisible: false
19    visible: action.enabled
20
21    onClicked: trigger()
22    Keys.onEnterPressed: trigger()
23    Keys.onReturnPressed: trigger()
24
25    function trigger() {
26        drawer.resetMenu()
27        action.trigger()
28    }
29
30    Kirigami.MnemonicData.enabled: item.enabled && item.visible
31    Kirigami.MnemonicData.controlType: Kirigami.MnemonicData.MenuItem
32    Kirigami.MnemonicData.label: action.text
33    label: Kirigami.MnemonicData.richTextLabel
34
35    QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
36    QQC2.ToolTip.visible: hovered && p0.nativeText.length > 0
37    QQC2.ToolTip.text: p0.nativeText
38
39    readonly property var p0: Shortcut {
40        sequence: item.Kirigami.MnemonicData.sequence
41        onActivated: item.clicked()
42    }
43
44    // Using the generic onPressed so individual instances can override
45    // behaviour using Keys.on{Up,Down}Pressed
46    Keys.onPressed: {
47        if (event.accepted) {
48            return
49        }
50
51        // Using forceActiveFocus here since the item may be in a focus scope
52        // and just setting focus won't focus the scope.
53        if (event.key === Qt.Key_Up) {
54            nextItemInFocusChain(false).forceActiveFocus()
55            event.accepted = true
56        } else if (event.key === Qt.Key_Down) {
57            nextItemInFocusChain(true).forceActiveFocus()
58            event.accepted = true
59        }
60    }
61}
62