-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSkyPython.py
More file actions
295 lines (238 loc) · 10.8 KB
/
Copy pathSkyPython.py
File metadata and controls
295 lines (238 loc) · 10.8 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'''
// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Original Author: John Taylor
//
// Notification of Change: The original java source code has been
// modified in that it has been rewritten in the python programming
// language and additionally, may contain components and ideas that are
// not found in the original source code.
Copyright 2013 Neil Borle and Paul Lu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Created on 2013-06-03
@author: Neil Borle and Morgan Redshaw
'''
import sys
import math
import time
from PySide import QtCore
from PySide.QtGui import QApplication
from PySide.QtGui import QMainWindow, QGraphicsView, QGraphicsScene
from SharedPreferences import SharedPreferences
from src.views.PreferencesButton import PreferencesButton
from src.views.ZoomButton import ZoomButton
from src.views.WidgetFader import WidgetFader
from src.touch.MapMover import MapMover
from src.touch.DragRotateZoomGestureDetector import DragRotateZoomGestureDetector as DRZDetector
from src.layers.LayerManager import instantiate_layer_manager
from src.control.AstronomerModel import AstronomerModel
from src.control.ControllerGroup import create_controller_group
from src.renderer.SkyRenderer import SkyRenderer
from src.renderer.RendererController import RendererController
from src.control.ZeroMagneticDeclinationCalculator import ZeroMagneticDeclinationCalculator as ZMDC
from src.control.MagneticDeclinationCalculatorSwitcher import MagneticDeclinationCalculatorSwitcher as MDCS
def start_application(mode=None):
'''
Either start the application normally or
start it in debug with images. The later causes
6 processes to be spawned with different camera
positions in the night sky.
'''
if mode == None:
worker()
else:
import multiprocessing
for i in range(0, 6):
p = multiprocessing.Process(target=worker, args=(i,))
p.start()
time.sleep(2)
def worker(index=None):
'''
Creates a single instance of the application.
'''
app = QApplication(sys.argv)
w = SkyPython(index)
w.show()
app.installEventFilter(w)
r = app.exec_()
sys.exit(r)
class SkyPython(QMainWindow):
'''
The central widget and heart of the application.
Performs all of the initialization of components
and contains updates and event handling.
'''
class RendererModelUpdateClosure():
'''
A runnable class that updates the state of the
model after a change has occurred. Is placed
on the event queue.
'''
def run(self):
pointing = self.model.get_pointing()
direction_x = pointing.gc_line_of_sight.x
direction_y = pointing.gc_line_of_sight.y
direction_z = pointing.gc_line_of_sight.z
up_x = pointing.gc_perpendicular.x
up_y = pointing.gc_perpendicular.y
up_z = pointing.gc_perpendicular.z
self.renderer_controller.queue_set_view_orientation(direction_x, direction_y, direction_z, \
up_x, up_y, up_z)
acceleration = self.model.acceleration
self.renderer_controller.queue_text_angle(math.atan2(-acceleration.x, -acceleration.y))
self.renderer_controller.queue_viewer_up_direction(self.model.get_zenith().copy())
field_of_view = self.model.field_of_view
self.renderer_controller.queue_field_of_view(field_of_view)
def __init__(self, m_model, controller):
'''
constructor
'''
self.model = m_model
self.renderer_controller = controller
layer_manager = None
model = None
controller = None
sky_renderer = None
renderer_controller = None
def pref_change(self, prefs, manager, layers, event):
'''
If a preference button has been pressed, change
the preferences according to the selection..
'''
for layer in layers:
prefs.PREFERENCES[layer] = not prefs.PREFERENCES[layer]
manager.on_shared_preference_change(prefs, layer)
return True
def eventFilter(self, source, event):
'''
Check if the event is a button press in the UI, if not
then check if it was a mouse or key press in DRZ_detector.
'''
pref_pressed = self.pref_buttons.checkForButtonPress(source, event)
zoom_pressed = self.zoom_button.checkForButtonPress(source, event, self.controller)
if pref_pressed or zoom_pressed:
self.show_menus_func()
if pref_pressed:
self.pref_change(self.shared_prefs, self.layer_manager,
pref_pressed, event)
else:
update = self.DRZ_detector.on_motion_event(event)
if pref_pressed or zoom_pressed or update:
self.update_rendering()
return True
return QMainWindow.eventFilter(self, source, event)
def initialize_model_view_controller(self):
'''
Set up the graphics view/scene and set the GLWidget.
Also sets up the the model and the controller for the model.
'''
self.model = AstronomerModel(ZMDC())
# There is no onResume in python so start the controller group here
self.controller = create_controller_group(self.shared_prefs)
self.controller.set_model(self.model)
self.controller.start()
self.layer_manager = instantiate_layer_manager(self.model, self.shared_prefs)
self.view = QGraphicsView()
self.scene = QGraphicsScene()
if self.DEBUG_MODE != None:
self.sky_renderer = SkyRenderer(self.DEBUG_MODE)
else:
self.sky_renderer = SkyRenderer()
self.sky_renderer.setAutoFillBackground(False)
# set up the view with the glwidget inside
self.view.setViewport(self.sky_renderer)
self.view.setScene(self.scene)
self.setCentralWidget(self.view)
self.renderer_controller = RendererController(self.sky_renderer, None)
self.renderer_controller.add_update_closure(\
self.RendererModelUpdateClosure(self.model, self.renderer_controller))
self.layer_manager.register_with_renderer(self.renderer_controller)
self.wire_up_screen_controls()
# NOTE: THIS BOOLEAN WILL NEED TO BE REMOVED EVENTUALLY
self.magnetic_switcher = MDCS(self.model, self.USE_AUTO_MODE)
self.run_queue()
def wire_up_screen_controls(self):
'''
Set up all of the screen controls, so that the user can zoom in/out
and select which layers they wish to view.
'''
screen_width = self.sky_renderer.render_state.screen_width
screen_height = self.sky_renderer.render_state.screen_height
self.pref_buttons = PreferencesButton(self.view)
self.zoom_button = ZoomButton(self.view)
position_y = ((screen_height - 336) / 2) + 1
self.pref_buttons.setGeometry(QtCore.QRect(1, position_y, 55, 336))
position_x = ((screen_width - 221) / 2) + 1
position_y = ((screen_height - 31) * 9/10) + 1
self.zoom_button.setGeometry(QtCore.QRect(position_x, position_y, 221, 31))
self.pref_buttons_fader = WidgetFader(self.pref_buttons, 2500)
self.zoom_button_fader = WidgetFader(self.zoom_button, 2500)
self.map_mover = MapMover(self.model, self.controller, self.shared_prefs,
screen_height)
self.DRZ_detector = DRZDetector(self.map_mover, self.show_menus_func)
def show_menus_func(self):
self.pref_buttons_fader.make_active()
self.zoom_button_fader.make_active()
def update_rendering(self):
'''
re-render the sky and run updates
'''
self.sky_renderer.updateGL()
self.run_queue()
self.sky_renderer.updateGL()
def run_queue(self):
'''
In the absence of another thread doing updates,
force the updates by running all the runnables on the queue
'''
num = len(list(self.renderer_controller.queuer.queue))
while num > 0:
runnable = self.renderer_controller.queuer.get()
runnable.run()
self.renderer_controller.queuer.task_done()
num -= 1
def __init__(self, debug_index=None):
'''
Set up all state and control that the application requires.
'''
QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_AcceptTouchEvents)
self.DEBUG_MODE = debug_index
self.USE_AUTO_MODE = False
self.shared_prefs = SharedPreferences()
self.initialize_model_view_controller()
self.controller.set_auto_mode(self.USE_AUTO_MODE)
# put the window at the screen position (100, 30)
# with size 480 by 800
self.setGeometry(100, 30, 480, 800)
self.show()
# every 5 seconds re-render the sky and run updates
self.update_rendering()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_rendering)
self.timer.setInterval(5000)
self.timer.start()
if __name__ == "__main__":
import os
os.chdir("../..")
start_application()