-
Notifications
You must be signed in to change notification settings - Fork 1
/
userInterface.py
170 lines (127 loc) · 5.58 KB
/
userInterface.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
from threading import Thread
from PyQt4 import QtGui, QtCore
import zmq, sys, json, time
from functools import partial
# the user interface, written in PyQT 4
# The GUI is designed to interact with the user. It also connects with the robot control (main.py).
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.readjson()
self.mapwin = MapWindow()
self.l1 = QtGui.QLabel('Welcome! I am the concierge. Feel free to talk with me!', self)
self.l1.setFont(QtGui.QFont('Bold', 40))
self.l1.resize(1500, 60)
self.l1.move(200, 100)
self.mapbt = QtGui.QPushButton('Map', self)
self.mapbt.setFont(QtGui.QFont('Bold', 40))
self.mapbt.adjustSize()
self.mapbt.move(200, 300)
self.mapbt.clicked.connect(self.map_clicked)
# Generate buttons and direction-windows in batch dynamically, according to JSON file
self.destination = [' ' for i in range(self.numLocation)]
self.route = [' ' for i in range(self.numLocation)]
self.dirbt = [' ' for i in range(self.numLocation)]
self.dirwin = [' ' for i in range(self.numLocation)]
for i in range(self.numLocation):
self.destination[i] = self.jsondata['map'][i]["destination"]
self.route[i] = self.jsondata['map'][i]["route"]
self.dirwin[i] = DirectionWindow(self.route[i])
self.dirbt[i] = QtGui.QPushButton(self.destination[i], self)
self.dirbt[i].setFont(QtGui.QFont('Bold', 40))
self.dirbt[i].adjustSize()
self.dirbt[i].move(200, 400 + i * 100)
self.dirbt[i].clicked.connect(partial(self.dir_clicked, self.dirwin[i]))
self.setWindowTitle('Concierge')
self.showMaximized()
# run the server thread
self.worker = WorkerThread()
self.worker.start()
self.worker.update_progress.connect(self.reactToMSG)
# handle the signal from the server thread
def reactToMSG(self, msg):
# reaction to msg
if "where am i" in msg:
self.mapwin.showMaximized()
usergui.hide()
for i in range(self.numLocation):
self.dirwin[i].hide()
elif "bar" in msg:
destid = self.destination.index('bar')
self.dirwin[destid].showMaximized()
usergui.hide()
self.mapwin.hide()
elif "robot battle event" in msg or "robot battery event" in msg:
destid = self.destination.index('The robot battle event!')
self.dirwin[destid].showMaximized()
usergui.hide()
self.mapwin.hide()
elif "thank you" in msg or "thanks" in msg or "sex" in msg or "6" in msg or "fix" in msg:
usergui.show()
self.mapwin.hide()
for i in range(self.numLocation):
self.dirwin[i].hide()
def readjson(self):
f = open('userInterface.json')
self.jsondata = json.load(f)
self.numLocation = len(self.jsondata['map']) # location & direction lists recorded in json
f.close
def map_clicked(self):
self.mapwin.showMaximized()
usergui.hide()
def dir_clicked(self, window):
window.showMaximized()
usergui.hide()
class WorkerThread(QtCore.QThread):
update_progress = QtCore.pyqtSignal(str) # signal
# communication with robot control (userInterface.py is server)
def run(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REP)
self.socket.bind("tcp://*:5555")
# listen to message from the client
try:
while True:
message = self.socket.recv() # receive msg from robot
self.socket.send(b"ack")
self.update_progress.emit(message) # send msg to the main thread
except:
print("Server error detected")
class MapWindow(QtGui.QMainWindow):
def __init__(self):
super(MapWindow, self).__init__()
self.l1 = QtGui.QLabel(self)
self.mapimg = QtGui.QPixmap('superfloor map.png')
self.l1.setPixmap(self.mapimg)
self.l1.resize(self.mapimg.width(), self.mapimg.height())
self.l1.move(350, 100)
self.b1 = QtGui.QPushButton('Back', self)
self.b1.setFont(QtGui.QFont('Bold', 40))
self.b1.resize(200, 60)
self.b1.move(100, 900)
self.b1.clicked.connect(self.b1_clicked)
self.setWindowTitle('Map')
def b1_clicked(self):
self.hide()
usergui.show()
class DirectionWindow(QtGui.QMainWindow):
def __init__(self, route):
super(DirectionWindow, self).__init__()
self.l1 = QtGui.QLabel(route, self)
self.l1.setFont(QtGui.QFont('Bold', 40))
self.l1.adjustSize()
self.l1.move(200, 100)
self.dirb1 = QtGui.QPushButton('Back', self)
self.dirb1.setFont(QtGui.QFont('Bold', 40))
self.dirb1.resize(200, 60)
self.dirb1.move(100, 900)
self.dirb1.clicked.connect(self.dirb1_clicked)
self.setWindowTitle('Direction')
def dirb1_clicked(self):
self.hide()
usergui.show()
if __name__ == "__main__":
app = QtGui.QApplication([])
usergui = MainWindow()
usergui.show()
app.exec_()