aboutsummaryrefslogtreecommitdiffstats
path: root/examples/declarative/modelviews/visualdatamodel/visualdatamodel.qml
blob: 2049fbd3bacd8e04b12b0ad4b67b1a554ed3d064 (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
import QtQuick 2.0

Rectangle {
    id: root

    property Bubble messageBubble: initialBubble
    property ListModel script: Conversation {}
    property int scriptIndex: 0
    property string sender: "Me"
    property int messageCounter: 0

    function send() {
        messageView.positionViewAtEnd()
        visualModel.insert(visualModel.count, messageBubble)
        messageBubble = bubbleComponent.createObject(composer, { "messageId": ++messageCounter } )
        messageView.positionViewAtEnd()
    }

    width: 480; height: 640

    gradient: Gradient {
        GradientStop { position: 0.0; color: "#000000" }
        GradientStop { position: 1.0; color: "#080808" }
    }

    Component {
        id: bubbleComponent

        ComposerBubble {
            sender: root.sender
        }
    }

    ListView {
        id: messageView
        anchors {
            left: parent.left; top: parent.top; right: parent.right; bottom: composer.top
            topMargin: 1; bottomMargin: 2
        }
        spacing: 2

        add: Transition {
            ParentAnimation {
                via: root
                NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad; duration: 1500 }
            }
        }

        cacheBuffer: 256

        model: VisualItemModel {
            id: visualModel

            VisualDataModel {
                model: ListModel {
                    id: messageModel
                }

                delegate: Bubble {
                    y: -height
                }
            }

            onUpdated: {
                for (var i = 0; i < inserts.length; ++i) {
                    for (var j = inserts[i].start; j < inserts[i].end; ++j) {
                        var message = messageModel.get(visualModel.getItemInfo(j).index)
                        if (!message.outbound)
                            continue
                        for (var k = 0; k < visualModel.children.length; ++k) {
                            var item = visualModel.children[k]
                            if (item.messageId != message.messageId)
                                continue
                            visualModel.replace(j, item)
                            // visualModel.move(item.visualIndex, j + 1
                            // visualModel.replace(j + 1, j)
                            break
                        }
                    }
                }
            }
        }
    }

    Timer {
        interval: 10000
        repeat: true
        running: true
        onTriggered: {
            var message = script.get(scriptIndex);

            messageModel.append({
                "sender": message.sender,
                "message": message.message,
                "avatar": message.avatar,
                "outbound": false,
                "messageId": -1,
                "time": Qt.formatTime(Date.now())
            })

            scriptIndex = (scriptIndex + 1) % script.count
            interval = Math.random() * 30000
        }
    }

    Item {
        id: composer

        height: messageBubble.height - messageBubble.margin
        anchors { left: parent.left; right: parent.right; bottom: parent.bottom }

        ComposerBubble {
            id: initialBubble

            sender: root.sender
            messageId: 0
        }
    }
}