aboutsummaryrefslogtreecommitdiffstats
path: root/examples/declarative/modelviews/visualdatamodel/visualdatamodel.qml
blob: 660245907122f273d8c844f4a60daf295f8797a5 (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
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() {
        messageBubble.sending = true
        visualModel.insert(visualModel.count, messageBubble)
        messageBubble = bubbleComponent.createObject(composer, { "messageId": ++messageCounter } )
    }

    width: 480; height: 640

    gradient: Gradient {
        GradientStop { position: 0.0; color: "#B0E2FF" }
        GradientStop { position: 1.0; color: "#87CEFA" }
    }

    Component {
        id: bubbleComponent

        ComposerBubble {
            sender: root.sender
        }
    }

    ListView {
        id: messageView
        anchors { left: parent.left; top: parent.top; right: parent.right; bottom: composer.top; margins: 2 }
        spacing: 5

        add: Transition {
            NumberAnimation { properties: "y"; easing.type: Easing.InOutQuad; duration: 1500 }
        }

        model: VisualItemModel {
            id: visualModel

            VisualDataModel {
                model: ListModel {
                    id: messageModel
                }

                delegate: Bubble {
                    y: -height
                    outbound: model.outbound
                    sender: model.sender
                    message:  model.message
                }
            }

            onItemDataInserted: {
                for (var i = 0; i < indexes.length; ++i) {
                    for (var j = indexes[i].start; j < indexes[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)
                            break
                        }
                    }

                }
            }

        }
    }

    Timer {
        interval: 10000
        repeat: true
        running: true
        onTriggered: {
            messageModel.append(script.get(scriptIndex))
            scriptIndex = (scriptIndex + 1) % script.count
            interval = Math.random() * 30000
        }
    }

    Item {
        id: composer

        height: messageBubble.height
        anchors { left: parent.left; right: parent.right; bottom: parent.bottom; margins: 2 }

        Behavior on height {
            NumberAnimation { duration: 500 }
        }

        ComposerBubble {
            id: initialBubble

            sender: root.sender
            messageId: 0
        }

        Rectangle {
            id: sendButton

            anchors {
                left: messageBubble.right; right: parent.right; top: parent.top; bottom: parent.bottom
                leftMargin: 2; rightMargin: 1; bottomMargin: 1
            }
            radius: 6

            gradient: Gradient {
                GradientStop { position: 0.0; color: "#A2CD5A" }
                GradientStop { position: 1.0; color: sendArea.pressed ? "#556B2F" : "#6E8B3D" }
            }

            Text {
                anchors.centerIn: parent
                color: "white"
                font.pixelSize: 14
                text: "Send"
            }

            MouseArea {
                id: sendArea
                anchors.fill: parent
                onClicked: root.send()
            }
        }
    }
}