def run(self): """ Run the model synchronously. Shows a modal dialog until the run completes. Returns the dialog. """ runner = self.runner gui_handler = lambda *args: deferred_call(self.handle_error, *args) runner.error_handler = gui_handler dialog = RunDialog(parent=self.parent, title='Running model...', text='Please wait while the model runs.') dialog.observe('rejected', lambda change: runner.cancel()) dialog.show() event_loop = QtCore.QEventLoop() timer = QtCore.QTimer() def start_run(): runner.run() timer.setInterval(50) timer.timeout.connect(check_loop) timer.start() def check_loop(): dialog.output = runner.output() if not runner.running: timer.stop() event_loop.quit() timed_call(50, start_run) event_loop.exec_() dialog.accept() return dialog
def delete_widget(self, widget, timeout=1.0): """Runs the real Qt event loop until the widget provided has been deleted. Raises ConditionTimeoutError on timeout. Parameters ---------- widget : QObject The widget whose deletion will stop the event loop. timeout : float Number of seconds to run the event loop in the case that the widget is not deleted. """ timer = QtCore.QTimer() timer.setSingleShot(True) timer.setInterval(timeout * 1000) timer.timeout.connect(self.qt_app.quit) widget.destroyed.connect(self.qt_app.quit) yield timer.start() self.qt_app.exec_() if not timer.isActive(): # We exited the event loop on timeout raise ConditionTimeoutError( 'Could not destroy widget before timeout: {!r}'.format(widget))
def init_signal(self): """allow clean shutdown on sigint""" signal.signal(signal.SIGINT, lambda sig, frame: self.exit(-2)) # need a timer, so that QApplication doesn't block until a real # Qt event fires (can require mouse movement) # timer trick from http://stackoverflow.com/q/4938723/938949 timer = QtCore.QTimer() # Let the interpreter run each 200 ms: timer.timeout.connect(lambda: None) timer.start(200) # hold onto ref, so the timer doesn't get cleaned up self._sigint_timer = timer
def event_loop_until_condition(self, condition, timeout=10.0): """Runs the real Qt event loop until the provided condition evaluates to True. Raises ConditionTimeoutError if the timeout occurs before the condition is satisfied. Parameters ---------- condition : callable A callable to determine if the stop criteria have been met. This should accept no arguments. timeout : float Number of seconds to run the event loop in the case that the trait change does not occur. """ def handler(): if condition(): self.qt_app.quit() condition_timer = QtCore.QTimer() condition_timer.setInterval(50) condition_timer.timeout.connect(handler) timeout_timer = QtCore.QTimer() timeout_timer.setSingleShot(True) timeout_timer.setInterval(timeout * 1000) timeout_timer.timeout.connect(self.qt_app.quit) timeout_timer.start() condition_timer.start() try: self.qt_app.exec_() if not timeout_timer.isActive(): # We exited the event loop on timeout raise ConditionTimeoutError('Timed out waiting for condition') finally: timeout_timer.stop() condition_timer.stop()
def __init__(self, key_event_handler=None, mouse_event_handler=None, cam_width=1024, cam_height=768, cam_near=0.01, cam_far=10.0, camera_intrinsics=None, parent=None): if QtVirtualCameraWidget.ShareWidget is None: ## create a dummy widget to allow sharing objects (textures, shaders, etc) between views QtVirtualCameraWidget.ShareWidget = QtOpenGL.QGLWidget() QtOpenGL.QGLWidget.__init__(self, parent, QtVirtualCameraWidget.ShareWidget) self.setFocusPolicy(QtCore.Qt.ClickFocus) self.bgtexture = visualization.BackgroundImage() self.camera_intrinsics = camera_intrinsics self.camera_pose = None self.camera_width = float(cam_width) self.camera_height = float(cam_height) self.camera_near = float(cam_near) self.camera_far = float(cam_far) self.screen_width = cam_width self.screen_height = cam_height self.items = [] self.key_event_handler = key_event_handler self.mouse_event_handler = mouse_event_handler self.noRepeatKeys = [ QtCore.Qt.Key_Right, QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Down, QtCore.Qt.Key_PageUp, QtCore.Qt.Key_PageDown ] self.keysPressed = {} self.keyTimer = QtCore.QTimer() self.keyTimer.timeout.connect(self.evalKeyState) self.makeCurrent()