This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.py
353 lines (263 loc) · 12.4 KB
/
MainWindow.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from PyQt5 import QtCore, QtWidgets, QtGui
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from MouseKeyboardInteractor import MouseKeyboardInteractor
import vtk
import Settings
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager
class MainWindow(QtWidgets.QMainWindow):
is_playing: bool = False
renderer = None
interactor = None
ren_window = None
mode_changed_signal = QtCore.pyqtSignal(float)
omega_changed_signal = QtCore.pyqtSignal(float)
cam_azimuth_val = 0
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.frame = QtWidgets.QFrame()
self.setup_menubar()
self.main_vlayout = QtWidgets.QVBoxLayout()
Settings.vtk_widget = QVTKRenderWindowInteractor(self.frame)
self.main_vlayout.addWidget(Settings.vtk_widget)
# Setup Sliders
self.setup_mode_slider()
self.setup_omega_slider()
# Setup Play/Pause Button
self.control_button = QtWidgets.QPushButton("Pause")
self.control_button.pressed.connect(self.play_pause_button)
self.main_vlayout.addWidget(self.control_button)
# Set up attach camera UI
self.attach_camera_layout = QtWidgets.QHBoxLayout()
self.attach_camera_button = QtWidgets.QPushButton("Attach Camera To Node")
self.attach_camera_button.pressed.connect(self.attach_camera)
self.attach_camera_layout.addWidget(self.attach_camera_button)
self.attach_cam_label = QtWidgets.QLabel()
self.attach_cam_label.setText("")
self.attach_camera_layout.addWidget(self.attach_cam_label)
self.main_vlayout.addLayout(self.attach_camera_layout)
# Setup Camera Delta Value Input
self.setup_camera_delta_layout()
# Add Camera Position Reset Button
self.reset_button = QtWidgets.QPushButton("Reset Camera Position")
self.reset_button.pressed.connect(self.reset_camera_position)
self.main_vlayout.addWidget(self.reset_button)
# Setup Window with layout
self.renderer = vtk.vtkRenderer()
Settings.vtk_widget.GetRenderWindow().AddRenderer(self.renderer)
self.interactor = Settings.vtk_widget.GetRenderWindow().GetInteractor()
self.ren_window = Settings.vtk_widget.GetRenderWindow()
self.frame.setLayout(self.main_vlayout)
self.setCentralWidget(self.frame)
style = MouseKeyboardInteractor(self)
style.SetDefaultRenderer(self.renderer)
self.interactor.SetInteractorStyle(style)
self.interactor.Initialize()
self.interactor.Start()
self.interactor.Render()
self.show()
@property
def active_camera(self):
return self.ren_window.GetRenderers().GetFirstRenderer().GetActiveCamera()
def reset_camera(self):
self.ren_window.GetRenderers().GetFirstRenderer().ResetCamera()
def setup_menubar(self):
xy_perspective = QtWidgets.QAction("XY", self)
xy_perspective.setStatusTip("Set XY Perspective")
xy_perspective.triggered.connect(self.set_xy_perspective)
yz_perspective = QtWidgets.QAction("YZ", self)
yz_perspective.setStatusTip("Set YZ Perspective")
yz_perspective.triggered.connect(self.set_yz_perspective)
xz_perspective = QtWidgets.QAction("XZ", self)
xz_perspective.setStatusTip("Set XZ Perspective")
xz_perspective.triggered.connect(self.set_xz_perspective)
iso_perspective = QtWidgets.QAction("ISO", self)
iso_perspective.setStatusTip("Set Iso Perspective")
iso_perspective.triggered.connect(self.set_iso_perspective)
self.statusBar()
main_menu = self.menuBar()
perspective_menu = main_menu.addMenu('&Perspective')
perspective_menu.addAction(xy_perspective)
perspective_menu.addAction(yz_perspective)
perspective_menu.addAction(xz_perspective)
perspective_menu.addAction(iso_perspective)
console_menu = main_menu.addMenu('&Console')
console = QtWidgets.QAction("Show Console", self)
console.setStatusTip("Open Python Console")
console.triggered.connect(self.show_console)
console_menu.addAction(console)
def display_pov_info_popup(self):
msg_box = QtWidgets.QMessageBox()
msg_box.setText("Can not set POV, attached to node\nDo you want to remove from node and change POV?")
msg_box.setIcon(QtWidgets.QMessageBox.Information)
msg_box.setWindowTitle("POV Warning")
remove_button = msg_box.addButton("Remove", QtWidgets.QMessageBox.YesRole)
msg_box.addButton("Cancel", QtWidgets.QMessageBox.NoRole)
msg_box.exec()
if msg_box.clickedButton() == remove_button:
return True
return False
def check_camera_attached(self):
if Settings.camera_is_attached:
will_remove_anchor = self.display_pov_info_popup()
if not will_remove_anchor:
return True
# Remove Camera
self.attach_camera()
return False
def show_console(self):
global ipython_widget # Prevent from being garbage collected
# Create an in-process kernel
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel(show_banner=False)
kernel = kernel_manager.kernel
kernel.gui = 'qt4'
kernel_client = kernel_manager.client()
kernel_client.start_channels()
ipython_widget = RichJupyterWidget()
ipython_widget.kernel_manager = kernel_manager
ipython_widget.kernel_client = kernel_client
ipython_widget.show()
ipython_widget.append_stream("from ConsoleInterface import *\n")
def set_xy_perspective(self):
print("XY")
z_offset = 30
if self.check_camera_attached():
return
self.active_camera.SetPosition(Settings.node_count / 2, 0, z_offset)
self.active_camera.SetFocalPoint(Settings.node_count / 2, 0, 0)
self.active_camera.SetRoll(0)
def set_yz_perspective(self):
print("YZ")
x_offset = 10
if self.check_camera_attached():
return
self.active_camera.SetPosition(Settings.node_count + x_offset, 0, 0)
self.active_camera.SetFocalPoint(Settings.node_count / 2, 0, 0)
self.active_camera.SetRoll(0)
def set_xz_perspective(self):
print("XZ")
y_offset = Settings.node_count*2
if self.check_camera_attached():
return
self.active_camera.SetPosition(Settings.node_count / 2, y_offset, 0)
self.active_camera.SetFocalPoint(Settings.node_count / 2, 0, 0)
self.active_camera.SetRoll(0)
def set_iso_perspective(self):
print("ISO")
x_offset = -20
z_offset = x_offset
y_offset = 10
if self.check_camera_attached():
return
self.active_camera.SetPosition(x_offset, y_offset, -z_offset)
self.active_camera.SetFocalPoint(Settings.node_count / 2, 0, 0)
self.active_camera.SetRoll(0)
def setup_mode_slider(self):
# Start setup mode slider layout
self.mode_slider_layout = QtWidgets.QHBoxLayout()
self.m_slider_label = QtWidgets.QLabel()
self.m_slider_label.setText("Mode: ")
self.m_slider_label.setAlignment(QtCore.Qt.AlignLeft)
self.mode_slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.mode_slider.setMinimum(0)
self.mode_slider.setMaximum(100)
self.mode_slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
self.mode_slider.setTickInterval(1)
normalized_val = (Settings.mode / Settings.mode_max) * 100
self.mode_slider.setValue(int(normalized_val))
self.mode_slider.valueChanged.connect(self.mode_value_change)
self.mode_slider_layout.addWidget(self.m_slider_label)
self.mode_slider_layout.addWidget(self.mode_slider)
self.main_vlayout.addLayout(self.mode_slider_layout)
# End setup mode slider layout
def setup_omega_slider(self):
# Start setup omega slider layout
self.omega_slider_layout = QtWidgets.QHBoxLayout()
self.o_slider_label = QtWidgets.QLabel()
self.o_slider_label.setText("Omega: ")
self.o_slider_label.setAlignment(QtCore.Qt.AlignLeft)
self.omega_slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.omega_slider.setMinimum(0)
self.omega_slider.setMaximum(10)
self.omega_slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
self.omega_slider.setTickInterval(1)
self.omega_slider.valueChanged.connect(self.omega_value_change)
self.omega_slider_layout.addWidget(self.o_slider_label)
self.omega_slider_layout.addWidget(self.omega_slider)
self.main_vlayout.addLayout(self.omega_slider_layout)
# End setup omega slider layout
def setup_camera_delta_layout(self):
minimum_val = -0x80000000
self.delta_layout = QtWidgets.QHBoxLayout()
self.dx_label = QtWidgets.QLabel()
self.dx_label.setText("Dx: ")
self.dx_label.setAlignment(QtCore.Qt.AlignLeft)
self.delta_layout.addWidget(self.dx_label)
self.dx_box = QtWidgets.QSpinBox()
self.dx_box.setMinimum(minimum_val)
self.dx_box.valueChanged[int].connect(lambda value: self.set_delta_values(value, 0))
self.delta_layout.addWidget(self.dx_box)
self.dy_label = QtWidgets.QLabel()
self.dy_label.setText("Dy: ")
self.dy_label.setAlignment(QtCore.Qt.AlignLeft)
self.delta_layout.addWidget(self.dy_label)
self.dy_box = QtWidgets.QSpinBox()
self.dy_box.setMinimum(minimum_val)
self.dy_box.valueChanged[int].connect(lambda value: self.set_delta_values(value, 1))
self.delta_layout.addWidget(self.dy_box)
self.dz_label = QtWidgets.QLabel()
self.dz_label.setText("Dz: ")
self.dz_label.setAlignment(QtCore.Qt.AlignLeft)
self.delta_layout.addWidget(self.dz_label)
self.dz_box = QtWidgets.QSpinBox()
self.dz_box.setMinimum(minimum_val)
self.dz_box.valueChanged[int].connect(lambda value: self.set_delta_values(value, 2))
self.delta_layout.addWidget(self.dz_box)
self.main_vlayout.addLayout(self.delta_layout)
def set_delta_values(self, value, dx_index):
Settings.camera_delta_values[dx_index] = value
Settings.update_slot.set_camera_delta_vals(Settings.camera_delta_values)
def reset_camera_position(self):
# Set default position
self.active_camera.SetPosition(Settings.node_count / 2, 30, 30)
self.active_camera.SetFocalPoint(Settings.node_count / 2, 0, 0)
def play_pause_button(self):
self.is_playing = not self.is_playing
if self.is_playing:
self.control_button.setText("Pause")
Settings.timer.start(50)
else:
self.control_button.setText("Play")
Settings.timer.stop()
def attach_camera(self):
Settings.attach_camera_to_node = not Settings.attach_camera_to_node
print("Camera Position: ", self.active_camera.GetPosition())
print("Camera Focal Point: ", self.active_camera.GetFocalPoint())
# Remove from node
if Settings.camera_is_attached:
Settings.camera_is_attached = False
# Unpin from node
Settings.update_slot.set_camera_pos_actor(None, None)
if Settings.attach_camera_to_node:
self.attach_camera_button.setText("Remove Camera From Node")
self.attach_cam_label.setText("Click node to set camera position")
else:
self.attach_camera_button.setText("Attach Camera To Node")
def swap_transparent(self):
Settings.is_transparent = not Settings.is_transparent
if Settings.is_transparent:
self.transparent_button.setText("Make Transparent")
else:
self.transparent_button.setText("Make Opaque")
def add_slot(self, vtk_update):
self.mode_changed_signal.connect(vtk_update.set_mode)
self.omega_changed_signal.connect(vtk_update.set_omega)
def mode_value_change(self, value):
Settings.mode = (value / 100) * Settings.mode_max
self.mode_changed_signal.emit(Settings.mode)
print("mode changed: ", Settings.mode)
def omega_value_change(self, value):
Settings.omega = value
self.omega_changed_signal.emit(Settings.omega)
print("omega changed: ", Settings.omega)