-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.qml
217 lines (203 loc) · 7.22 KB
/
main.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Chat Client")
property bool isConnected: false
Connections {
target: client
function onNewMessage(type, name, msg) {
var nameColor
switch (name + ""){
case "Server":
case "System":
nameColor = "red"
break
case "You":
nameColor = "blue"
break;
default:
nameColor = "green"
}
listModelMessages.append({
textColor : nameColor,
name: name + "",
body: msg + ""
})
listViewMessages.positionViewAtEnd()
}
function onNewStatus(s){
isConnected = s
if(!s){
textFieldName.clear()
listModelMessages.clear()
listModelMessages.append({
textColor: "red",
name: "System",
body: "Welcome to chat client This is a loooooong text This is a loooooong textThis is a loooooong text"
})
listViewMessages.positionViewAtEnd()
}
}
function onChangeName(s){
dialogChangeName.originName = s + ""
dialogChangeName.open()
}
}
Component{
id: msgDelegate
Item{
id: msgWrapper
width: root.width
height: 80
ColumnLayout{
anchors.left: parent.left
Text{
id: msgName
text: name
color: textColor
Layout.preferredHeight: 20
Layout.fillWidth: true
}
Text{
id: msgBody
text: body
wrapMode: Text.WordWrap
Layout.preferredHeight: 60
Layout.fillWidth: true
Layout.maximumWidth: root.width
Layout.alignment: Qt.AlignTop
}
}
}
}
Dialog{
id: dialogChangeName
x: (parent.width - width) / 2
y: (parent.height - height) / 2
title: qsTr("Change your name")
focus: true
property string originName
RowLayout{
TextField{
id: textFieldChangeName
placeholderText: qsTr("User name is in use...")
validator: RegExpValidator { regExp: /^[\w'\-,.][^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$/ }
focus: true
Layout.fillWidth: true
onAccepted: buttonChangeName.clicked()
}
Button{
id:buttonChangeName
text: qsTr("Change")
enabled: textFieldChangeName.acceptableInput
onClicked:{
console.log(textFieldChangeName.text)
if(textFieldChangeName.text === dialogChangeName.originName){
textFieldChangeName.clear()
return
}
textFieldName.text = textFieldChangeName.text
client.changeClientName(textFieldChangeName.text)
dialogChangeName.accept()
}
}
}
}
ColumnLayout {
id: root
anchors.fill: parent
anchors.margins: 10
RowLayout {
Layout.fillWidth: true
TextField {
id: textFieldIp
enabled: !isConnected
placeholderText: qsTr("Server IP")
text: "127.0.0.1"
validator: RegExpValidator { regExp: /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$/ }
Layout.fillWidth: true
onAccepted: buttonConnect.clicked()
}
TextField {
id: textFieldPort
enabled: !isConnected
placeholderText: qsTr("Server port")
text: "8080"
validator: RegExpValidator { regExp: /^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$/ }
Layout.fillWidth: true
onAccepted: buttonConnect.clicked()
}
TextField{
id: textFieldName
enabled: !isConnected
placeholderText: qsTr("Name")
validator: RegExpValidator { regExp: /^[\w'\-,.][^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$/ }
Layout.fillWidth: true
onAccepted: buttonConnect.clicked()
focus: !isConnected
}
Button {
id: buttonConnect
text: qsTr(isConnected?"Disconnect":"Connect")
enabled: (textFieldIp.acceptableInput && textFieldPort.acceptableInput && textFieldName.acceptableInput) || isConnected
onClicked:{
if (isConnected)
client.disconnectFromServer()
else
client.connectToServer(textFieldIp.text, textFieldPort.text, textFieldName.text)
}
}
}
ListView {
id: listViewMessages
Layout.fillHeight: true
Layout.fillWidth: true
clip: true
model: ListModel {
id: listModelMessages
ListElement {
textColor: "red"
name: "System"
body: "Welcome to chat client This is a loooooong text This is a loooooong textThis is a loooooong text"
}
}
delegate: msgDelegate
ScrollBar.vertical: ScrollBar {
active: true
}
}
RowLayout {
Layout.fillWidth: true
TextField {
id: textFieldMessage
enabled: isConnected
placeholderText: qsTr("Type your message ...")
Layout.fillWidth: true
onAccepted: buttonSend.clicked()
focus: isConnected
}
Button {
id: buttonSend
text: qsTr("Send")
enabled : isConnected
onClicked: {
client.sendMessage(qsTr("msg"), textFieldMessage.text)
textFieldMessage.clear()
}
}
Button {
text: qsTr("Test")
onClicked: {
dialogChangeName.originName = "Amber"
dialogChangeName.open()
console.log("here")
}
}
}
}
}