-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_gui.py
207 lines (155 loc) · 7.14 KB
/
main_gui.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
"""
main_gui.py
Responsible for all GUI setup and interaction
between its many different components.
"""
import sys, random, threading, Queue, time
from ui_mainwindow import Ui_MainWindow
from dialog_custom import CustomDialog
from server import ServerThread
from client import ClientThread
from scene_ui import SceneManager
from PySide.QtCore import (QTimer, QTimeLine, QPointF)
from PySide.QtGui import *
SCENE_WIDTH = 579
SCENE_HEIGHT = 309
ANIMATOR_TIMEOUT = 100 # graphics update intveral
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.run = True
self.mode = None # signals 'server'/'client' mode
self.input_values = [] # input values retrieve from UI
self.io_handlers() # initialize UI components
# communication/network variables
self.tcp_main_queue = Queue.Queue()
self.tcp_sim_queue = Queue.Queue()
self.thread_event = threading.Event()
self.data = "meaningless"
self.custom_data = ''
self.cm = SceneManager(self.graphicsView)
self.animations = []
self.animator = QTimer() # timer paired with main thread
self.animator.timeout.connect(self.update) # timeout callback
self.update() # initial call
# links buttons to event handlers
def io_handlers(self):
self.btn_start_server.clicked.connect(self.startServer)
self.btn_start_client.clicked.connect(self.startClient)
self.btn_stop.clicked.connect(self.stopSimulation)
self.btn_random_ball.clicked.connect(self.randomBall)
self.btn_custom_ball.clicked.connect(self.customBall)
# period callback used to animate 'graphicsView'
def update(self):
# moves item to location smoothly in one second
def config_animate_to(t,item,x,y):
# used to animate an item in specific ways
animation = QGraphicsItemAnimation()
# create a timeline (1 sec here)
timeline = QTimeLine(100)
timeline.setFrameRange(0,200) # 200 steps
#item should at 'x,y' by time 't's
animation.setPosAt(t,QPointF(x,y))
animation.setItem(item) # animate this item
animation.setTimeLine(timeline) # with this duration/steps
return animation
if (self.run): # still running
# check for items received via tcp
if (not self.tcp_sim_queue.empty()):
self.cm.deleteItems()
# item = self.tcp_sim_queue.get() # retrieve value
msg = self.tcp_sim_queue.get()
if (type(msg) == type('s')) :
# self.cm.addItem(item) # add new item
self.cm.addItem(msg)
else:
self.cm.addItems(msg)
elif (self.custom_data != ''): # data received from GUI
# print 'self.custom_data is %s' % self.custom_data #DEBUG
self.cm.addItem(self.custom_data)
self.custom_data = ''
if (str(self.mode) == 'SERVER'):
del self.animations[:] # empty list of prev animations
# retrieve all items in the graphicsView
scene_items = self.cm.getItems()
for item in scene_items:
next_x, next_y = self.cm.next_move(item) # determine next location
# create corresponding item animation (given displacement in local coordinates)
item_animation = config_animate_to(1,item,next_x,next_y)
self.animations.append(item_animation) # store item animation
# update location values of item (global coordiantes)
item.set_location(next_x+item.x_start,next_y+item.y_start)
# signal the start of all animations
[ animation.timeLine().start() for animation in self.animations]
# send all simulation items to server thread
if (len(scene_items) > 0):
# retrieve items encoded for TCP
tcp_list = self.cm.getItemsForTCP()
# print ('Observed: ' + str(tcp_list)) #DEBUG
self.tcp_main_queue.put(tcp_list)
self.animator.start(ANIMATOR_TIMEOUT)
def startServer(self):
self.btn_start_client.setEnabled(False) # disable 'client' button
# initiate server thread and pass GUI/simulation messgae queues
self.mode = ServerThread(self.tcp_main_queue,self.thread_event)
self.mode.daemon = True
self.mode.start()
def startClient(self):
try:
# initiate client thread and pass simulation GUI message queue
self.mode = ClientThread(self.tcp_sim_queue,self.thread_event)
self.mode.daemon = True # thread to close when main thread closes
self.mode.start()
# disable all buttons except 'Stop'
self.btn_start_server.setEnabled(False)
self.btn_start_client.setEnabled(False)
self.btn_random_ball.setEnabled(False)
self.btn_custom_ball.setEnabled(False)
except Exception:
print "No server available."
def stopSimulation(self):
# disable all buttons
self.btn_start_server.setEnabled(False)
self.btn_start_client.setEnabled(False)
self.btn_stop.setEnabled(False)
self.btn_random_ball.setEnabled(False)
self.btn_custom_ball.setEnabled(False)
self.run = False
self.thread_event.set()
print 'Main thread stopped'
def randomBall(self):
# select random radius, mass, x/y velocities
mass = random.randint(5, 20)
radius = int(mass * 2)
x_vel = random.randint(1,30)
y_vel = random.randint(1,30)
# select x/y positions within graphicsView
x_pos = random.randint(0,SCENE_WIDTH-radius)
y_pos = random.randint(0,SCENE_HEIGHT-radius)
# encode data for TCP transport
self.data = 'x' + str(x_pos) + 'y' + str(y_pos) + 'xv' + str(x_vel) + \
'yv' + str(y_vel) + 'm' + str(mass) + 'r' + str(radius)
# self.tcp_main_queue.put(self.data)
# self.tcp_sim_queue.put(self.data)
self.custom_data = self.data
def customBall(self):
# create dialog for input
dialog = CustomDialog()
# retrieve input from dialog
if (dialog.exec_()):
self.input_values = dialog.results
params = ['x','y','xv','yv','m','r']
self.data = ''
# encode data for TCP transmission
for i in range(0,len(self.input_values)):
self.data += params[i] + str(self.input_values[i])
# # input in queue for tcp thread
if (len(self.data) > 0):
self.custom_data = self.data
# self.tcp_main_queue.put(self.data)
if __name__ == '__main__':
app = QApplication(sys.argv)
frame = MainWindow()
frame.show()
sys.exit(app.exec_())