aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qquicklistview/data/qtbug48044.qml
blob: d318643c1c5aff32fa60f7cd5b669ab1982babfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import QtQuick 2.0

Item {
    width: 200
    height: 442

    ListModel {
        id: listModel
        ListElement {
            name: "h1"
            txt: "Header 1"
            header: true
            collapsed: true
        }
        ListElement {
            name: "h2"
            txt: "Header 2"
            header: true
            collapsed: true
        }
        ListElement {
            name: "h3"
            txt: "Header 3"
            header: true
            collapsed: true
        }

        function indexFromName(name) {
            for (var i = 0; i < count; i++)
                if (get(i).name === name)
                    return i

            console.warn("Did not find index for name " + name)
            return -1
        }
    }

    function populateModel(prefix, index, n) {
        for (var k = 1; k <= n; k++) {
            var name = prefix + k
            var data = {
                "collapsed": false,
                "name": name,
                "txt": name,
                "header": false
            }
            listModel.insert(index + k, data)
        }
    }

    function h2(open) {
        var i = listModel.indexFromName("h2")
        if (listModel.get(i).collapsed === !open)
            return

        listModel.setProperty(i, "collapsed", !open)

        var n = 15
        if (open) {
            h3(false)
            populateModel("c2_", listModel.indexFromName("h2"), n)
        } else {
            listModel.remove(i + 1, n)
        }

    }

    function h3(open) {
        var i = listModel.indexFromName("h3")
        if (listModel.get(i).collapsed === !open)
            return

        listModel.setProperty(i, "collapsed", !open)

        var n = 6
        if (open) {
            h2(false)
            populateModel("c3_", listModel.indexFromName("h3"), n)
        } else {
            listModel.remove(i + 1, n)
        }
    }

    ListView {
        id: listView
        width: parent.width
        height: parent.height
        cacheBuffer: 0
        model: listModel

        property bool transitionsDone: false
        property int runningTransitions: 0
        onRunningTransitionsChanged: {
            if (runningTransitions === 0)
                transitionsDone = true
        }

        displaced: Transition {
            id: dispTrans
            SequentialAnimation {
                ScriptAction {
                    script: listView.runningTransitions++
                }
                NumberAnimation {
                    property: "y";
                    duration: 250
                }
                ScriptAction {
                    script: listView.runningTransitions--
                }
            }
        }

        delegate: Rectangle {
            id: rect
            color: header ? "yellow" : "cyan"
            border.color: "black"
            height: 50
            width: parent.width

            Text {
                anchors.centerIn: parent
                font.pixelSize: 20
                text: txt
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    listView.currentIndex = index
                    var i = listModel.indexFromName("h3")
                    if (i === -1)
                        return;
                    var isCollapsed = listModel.get(i).collapsed
                    if (name === "h2")
                        h2(isCollapsed)
                    else if (name === "h3")
                        h3(isCollapsed)
                }
            }
        }
    }
}