1
2/*
3 *  SPDX-FileCopyrightText: 2020 Marco Martin <mart@kde.org>
4 *
5 *  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8import QtQuick 2.12
9import QtQuick.Layouts 1.4
10import QtQuick.Controls 2.2 as QQC2
11import org.kde.kirigami 2.13 as Kirigami
12import org.kde.kitemmodels 1.0
13
14/**
15 * An item delegate for the TreeListView and TreeTableView components.
16 *
17 * It has the tree expander decoration but no content otherwise,
18 * which has to be set as contentItem
19 *
20 */
21Kirigami.AbstractListItem {
22    id: delegate
23    separatorVisible: false
24
25    property alias decoration: decoration
26
27    data: [
28        TreeViewDecoration {
29            id: decoration
30            anchors {
31                left: parent.left
32                top:parent.top
33                bottom: parent.bottom
34                leftMargin: delegate.padding
35            }
36            parent: delegate
37            parentDelegate: delegate
38            model: delegate.ListView.view ? delegate.ListView.view.descendantsModel :
39                   (delegate.TableView.view ? delegate.TableView.view.descendantsModel : null)
40        },
41        Binding {
42            target: contentItem.anchors
43            property: "left"
44            value: delegate.left
45        },
46        Binding {
47            target: contentItem.anchors
48            property: "leftMargin"
49            value: decoration.width + delegate.padding * 2 + Kirigami.Units.smallSpacing
50        }
51    ]
52
53    Keys.onLeftPressed: if (kDescendantExpandable && kDescendantExpanded) {
54        decoration.model.collapseChildren(index);
55    } else if (!kDescendantExpandable && kDescendantLevel > 0) {
56        if (delegate.ListView.view) {
57            const sourceIndex = decoration.model.mapToSource(decoration.model.index(index, 0));
58            const newIndex = decoration.model.mapFromSource(sourceIndex.parent);
59            delegate.ListView.view.currentIndex = newIndex.row;
60        }
61    }
62
63    Keys.onRightPressed: if (kDescendantExpandable) {
64        if (kDescendantExpanded && delegate.ListView.view) {
65            ListView.view.incrementCurrentIndex();
66        } else {
67            decoration.model.expandChildren(index);
68        }
69    }
70
71    onDoubleClicked: if (kDescendantExpandable) {
72        decoration.model.toggleChildren(index);
73    }
74
75    // FIXME: it should probably use leftInset property but Kirigami.AbstractListItem doesn't have it because can't import QQC2 more than 2.0
76    background.anchors {
77        left: delegate.left
78        leftMargin: decoration.width + delegate.padding * 2
79    }
80}
81