1/*
2 * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7import QtQuick 2.12
8import QtQuick.Controls 2.12
9import QtQuick.Layouts 1.12
10import QtQml.Models 2.12
11
12import org.kde.kirigami 2.2 as Kirigami
13
14BaseCellDelegate {
15    id: delegate
16
17    property string iconName
18    property string text: model.display != undefined ? model.display : ""
19    property real iconSize: Kirigami.Units.iconSizes.small
20    property bool treeDecorationVisible: false
21
22    leftPadding: Kirigami.Units.largeSpacing
23
24    contentItem: RowLayout {
25        id: row
26        property Item treeDecoration
27        Kirigami.Icon {
28            id:blah
29            Layout.preferredWidth: delegate.iconName != "" ? delegate.iconSize : 0
30            Layout.preferredHeight: Layout.preferredWidth
31            source: delegate.iconName
32            fallback: ""
33        }
34        Label {
35            id: label
36
37            padding: Kirigami.Units.smallSpacing
38
39            Layout.fillWidth: true
40            Layout.fillHeight: true
41            text: delegate.text
42            horizontalAlignment: Text.AlignLeft
43            elide: Text.ElideRight
44        }
45    }
46
47    hoverEnabled: true
48
49    onTreeDecorationVisibleChanged: {
50        if (treeDecorationVisible) {
51            var component = Qt.createComponent("TreeDecoration.qml")
52            if (component.status == Component.Ready) {
53                var incubator = component.incubateObject(null, {
54                    hasSiblings: Qt.binding(() => model.kDescendantHasSiblings),
55                    level: Qt.binding(() => model.kDescendantLevel),
56                    expandable: Qt.binding(() => model.kDescendantExpandable),
57                    expanded: Qt.binding(() => model.kDescendantExpanded)
58                })
59                var finishCreation = () => {
60                    row.treeDecoration = incubator.object
61                    row.treeDecoration.clicked.connect(() => descendantsModel.toggleChildren(index))
62                    var children = Array.from(row.children)
63                    children.unshift(row.treeDecoration)
64                    row.children = children
65                }
66                if (incubator.status == Component.Ready) {
67                    finishCreation()
68                } else {
69                    incubator.onStatusChanged = (status) => {
70                        if (status == Component.Ready) {
71                            finishCreation()
72                        }
73                    }
74                }
75            }
76        } else {
77            if (row.treeDecoration) {
78                row.treeDecoration.destroy()
79            }
80        }
81    }
82    ToolTip.text: delegate.text
83    ToolTip.delay: Kirigami.Units.toolTipDelay
84    ToolTip.visible: delegate.hovered && label.truncated
85}
86