-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_ui.py
254 lines (209 loc) · 9.52 KB
/
client_ui.py
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from PyQt4 import QtCore, QtGui
from client import Client
class ClientUI(QtGui.QWidget):
def __init__(self):
super(ClientUI, self).__init__()
self.language = 'en'
self.setupUI()
self.setupEvents()
def setupUI(self):
self.main_layout = QtGui.QVBoxLayout(self)
## Title
# Raspberry picture
self.title_layout = QtGui.QHBoxLayout()
self.raspberry_picture = QtGui.QPixmap('pictures/raspberry.png')
self.raspberry_picture = self.raspberry_picture.scaledToHeight(60)
self.raspberry_label = QtGui.QLabel()
self.raspberry_label.setPixmap(self.raspberry_picture)
self.title_layout.addWidget(self.raspberry_label)
# App title
self.title_label = QtGui.QLabel("Raspberry-stream client")
title_font = QtGui.QFont()
title_font.setBold(True)
title_font.setPointSize(20)
self.title_label.setFont(title_font)
self.title_layout.addWidget(self.title_label)
# State box
self.state_box = QtGui.QGroupBox()
self.state_layout = QtGui.QHBoxLayout()
self.state_circle_red_picture = QtGui.QPixmap('pictures/red_button.png')
self.state_circle_red_picture = self.state_circle_red_picture.scaledToHeight(20)
self.state_circle_green_picture = QtGui.QPixmap('pictures/green_button.png')
self.state_circle_green_picture = self.state_circle_green_picture.scaledToHeight(20)
self.state_circle_label = QtGui.QLabel()
self.state_circle_label.setPixmap(self.state_circle_red_picture)
self.state_layout.addWidget(self.state_circle_label)
self.state_label = QtGui.QLabel("Not connected")
self.state_layout.addWidget(self.state_label)
self.state_box.setLayout(self.state_layout)
self.title_layout.addWidget(self.state_box)
self.main_layout.addLayout(self.title_layout)
# Parameters fillable content
self.param_box = QtGui.QGroupBox("Parameters")
self.param_layout = QtGui.QGridLayout()
self.address_layout = QtGui.QHBoxLayout()
self.address_label = QtGui.QLabel("Address : ")
self.address_lineedit = QtGui.QLineEdit()
self.address_button = QtGui.QPushButton("local") #192.168.0.41
self.param_layout.addWidget(self.address_label, 0, 0)
self.param_layout.addWidget(self.address_lineedit, 0, 1, 1, 3)
self.param_layout.addWidget(self.address_button, 0, 4, 1, 1)
self.port_layout = QtGui.QHBoxLayout()
self.port_label = QtGui.QLabel("Port : ")
self.port_lineedit = QtGui.QLineEdit()
self.param_layout.addWidget(self.port_label, 1, 0)
self.param_layout.addWidget(self.port_lineedit, 1, 1, 1, 4)
self.param_box.setLayout(self.param_layout)
self.main_layout.addWidget(self.param_box)
# Address inputs
self.input_box = QtGui.QGroupBox("Inputs")
self.input_layout = QtGui.QHBoxLayout()
self.input_link_label = QtGui.QLabel("Stream address : ")
self.input_layout.addWidget(self.input_link_label)
self.input_category = QtGui.QComboBox()
self.input_category.addItems(['test', 'open'])
self.input_layout.addWidget(self.input_category)
self.input_link_lineedit = QtGui.QLineEdit()
self.input_layout.addWidget(self.input_link_lineedit)
self.input_help_button = QtGui.QPushButton("?")
self.input_help_button.setFixedWidth(20)
self.input_layout.addWidget(self.input_help_button)
self.input_box.setLayout(self.input_layout)
self.main_layout.addWidget(self.input_box)
## Comments box
self.comment_box = QtGui.QGroupBox("Outputs")
self.comment_layout = QtGui.QVBoxLayout()
self.comment1_label = QtGui.QLabel('')
self.comment2_label = QtGui.QLabel('')
comment_font = QtGui.QFont()
comment_font.setItalic(True)
self.comment1_label.setFont(comment_font)
self.comment2_label.setFont(comment_font)
self.comment_layout.addWidget(self.comment1_label)
self.comment_layout.addWidget(self.comment2_label)
self.comment_box.setLayout(self.comment_layout)
self.main_layout.addWidget(self.comment_box)
self.button_layout = QtGui.QHBoxLayout()
self.connect_button = QtGui.QPushButton("Connect")
self.button_layout.addWidget(self.connect_button)
self.send_button = QtGui.QPushButton("Send")
self.button_layout.addWidget(self.send_button)
self.cancel_button = QtGui.QPushButton("Cancel")
self.button_layout.addWidget(self.cancel_button)
self.main_layout.addLayout(self.button_layout)
def setupEvents(self):
self.connect_button.clicked.connect(self.connect_client)
self.address_button.clicked.connect(self.local_address)
self.send_button.clicked.connect(self.send_address)
self.cancel_button.clicked.connect(self.close)
self.input_help_button.clicked.connect(self._help_button_clicked)
def local_address(self):
self.address_lineedit.setText('localhost')
def connect_client(self):
print('connect')
if self.connect_button.text() == 'Connect':
param = self._check_param()
print('param : ', param)
if param:
address = self.address_lineedit.text()
port = self.port_lineedit.text()
try:
self.client = Client(port, address)
self.client.connect()
self.state_label.setText('Connected')
self.connect_button.setText('Disconnect')
self._print_comment('Connected to %s' % self.address_lineedit.text())
self.state_circle_label.setPixmap(self.state_circle_green_picture)
self.check_thread = CheckThread(self.client)
self.check_thread.start()
except:
self._print_comment("<font color='red'>Error connecting</font>")
pass
elif self.connect_button.text() == 'Disconnect':
try:
self.client.send_message('fin')
self.state_label.setText('Not connected')
self.connect_button.setText('Connect')
self.state_circle_label.setPixmap(self.state_circle_red_picture)
self._print_comment('Disconnected!')
except:
pass
def _print_comment(self, message):
self.comment2_label.setText(self.comment1_label.text())
self.comment1_label.setText(message)
def _check_param(self):
address = self.address_lineedit.text().split('.')
if address != ['localhost']:
if len(address) != 4:
self._print_comment("<font color='red'>Address parameter incorrect.</font>")
return 0
for add in address:
if not add.isnumeric():
self._print_comment("<font color='red'>Address parameter incorrect.</font>")
return 0
if not self.port_lineedit.text().isnumeric():
self._print_comment("<font color='red'>Port parameter incorrect.</font>")
return 0
return 1
def _help_button_clicked(self):
self.help_window = Help_input(self, language=self.language)
self.help_window.show()
def send_address(self):
if self.state_label.text() == "Not connected":
self._print_comment("<font color='red'>Can't send %s because not connected to Server</font>" % self.input_link_lineedit.text())
else:
self.client.send_message(self.input_category.text()+' '+self.input_link_lineedit.text())
class Help_input(QtGui.QMainWindow):
def __init__(self, parent=None, language='en'):
super(Help_input, self).__init__(parent)
self.main_widget = QtGui.QWidget()
self.main_layout = QtGui.QVBoxLayout()
self.title_label = QtGui.QLabel("Input paramters")
self.title_label.setAlignment(QtCore.Qt.AlignCenter)
title_font = QtGui.QFont()
title_font.setBold(True)
title_font.setPointSize(20)
self.title_label.setFont(title_font)
self.main_layout.addWidget(self.title_label)
import json
help_data = json.load(open('help.json'))[language]
key_font = QtGui.QFont()
key_font.setBold(True)
self.help_box = QtGui.QGroupBox('')
self.help_layout = QtGui.QGridLayout()
idx = 0
for k in help_data.keys():
key = QtGui.QLabel(k)
key.setFont(key_font)
info = QtGui.QLabel(help_data[k])
self.help_layout.addWidget(key, idx, 0)
self.help_layout.addWidget(info, idx, 1)
idx += 1
self.help_box.setLayout(self.help_layout)
self.main_layout.addWidget(self.help_box)
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
class CheckThread(QtCore.QThread):
def __init__(self, client):
QtCore.QThread.__init__(self)
self.client = client
def __del__(self):
self.wait()
def run(self):
connected = True
while connected:
self.sleep(5)
if self.client.test_connection():
pass
else:
print('NOT CONNECTED')
connected = False
self.client.close()
pass
# Check
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = ClientUI()
w.show()
sys.exit(app.exec_())