-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatWindow.qml
97 lines (86 loc) · 2.59 KB
/
ChatWindow.qml
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
/**
* @file ChatWindow.qml
* @brief The chat window
* @author Giang Nguyen
* Contact: [email protected]
*/
import QtQuick 2.9
import QtQuick.Controls 2.5
Item {
id: root
property string username
property string partner
anchors.fill: parent
ListModel {
id: messages
}
ScrollView {
width: parent.width-14
height: parent.height-sendbtn.height-separateLine.height-10
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
clip:true
ListView {
id: messagebox
spacing: 5
model: messages
clip: true
delegate: Component {
Rectangle {
radius: 15
height: textElement.height+20
width: textElement.width+30
color: (content.indexOf("Me (") === 0) ? "lightblue" : "lightgrey"
anchors.left: (content.indexOf("Me (") !== 0) ? parent.left : undefined
anchors.right: (content.indexOf("Me (") === 0) ? parent.right : undefined
Text {
id: textElement
font.pointSize: 14
text: content
anchors.centerIn: parent
}
}
}
}
}
// send a message
function sendMessage() {
var newMessage = inputbox.content
inputbox.clear()
messages.append({content: "Me (" + username + "): " + newMessage})
messagebox.positionViewAtEnd()
chatServer.sendMessage(newMessage,username,partner)
}
// receive a message
function receiveMessage(sender,message,isNew) {
sender = (sender === username) ? "Me (" + username + ")" : sender;
if (isNew === "True")
messages.append({content: sender + ": " + message})
else
messages.insert(0,{content: sender + ": " + message});
messagebox.positionViewAtEnd()
}
Rectangle {
id: separateLine
height: 1
width: parent.width
color: "grey"
anchors.bottom: sendbtn.top
}
MPButton {
id: sendbtn
label: "Send"
anchors.right: parent.right
anchors.bottom: parent.bottom
onPressBtn: sendMessage()
}
MPInputBox {
id: inputbox
hint: "Type a message..."
Keys.onReturnPressed: sendMessage()
width: parent.width-sendbtn.width-20
height: sendbtn.height
anchors.left: parent.left
anchors.bottom: parent.bottom
}
}