def test_qt_designer(): """Embed Canvas via Qt Designer""" default_app.use() if 'pyqt4' not in default_app.backend_name.lower(): raise SkipTest('Not using PyQt4 backend') # wrong backend from PyQt4 import uic fname = op.join(op.dirname(__file__), 'qt-designer.ui') WindowTemplate, TemplateBaseClass = uic.loadUiType(fname) default_app.create() # make sure we have an app, or the init will fail class MainWindow(TemplateBaseClass): def __init__(self): TemplateBaseClass.__init__(self) self.ui = WindowTemplate() self.ui.setupUi(self) self.show() win = MainWindow() try: win.show() canvas = Canvas(create_native=False) canvas.app.use() # Make sure the app exists (because create_native=0) canvas._set_backend(win.ui.canvas) canvas.create_native() @canvas.events.draw.connect def on_draw(ev): gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) canvas.swap_buffers() finally: win.close()
def _test_module_properties(_module=None): """Test application module""" if _module is None: default_app.use() _module = default_app.backend_module # Test that the keymap contains all keys supported by vispy. keymap = _module.KEYMAP vispy_keys = keymap.values() for keyname in dir(keys): if keyname.upper() != keyname: continue key = getattr(keys, keyname) assert key in vispy_keys # For Qt backend, we have a common implementation alt_modname = '' if _module.__name__.split('.')[-1] in ('_pyside', '_pyqt4'): alt_modname = _module.__name__.rsplit('.', 1)[0] + '._qt' # Test that all _vispy_x methods are there. exceptions = ( '_vispy_get_native_canvas', '_vispy_get_native_timer', '_vispy_get_native_app', '_vispy_mouse_move', '_vispy_mouse_press', '_vispy_mouse_release', '_vispy_get_geometry', '_process_backend_kwargs') # defined in base class Klass = _module.CanvasBackend KlassRef = vispy.app.base.BaseCanvasBackend base = KlassRef(None, None) for key in dir(KlassRef): if not key.startswith('__'): method = getattr(Klass, key) if key not in exceptions: print(key) args = [None] * (len(getargspec(method).args) - 1) assert_raises(NotImplementedError, getattr(base, key), *args) if hasattr(method, '__module__'): mod_str = method.__module__ # Py3k else: mod_str = method.im_func.__module__ assert mod_str in (_module.__name__, alt_modname), \ "Method %s.%s not defined in %s" \ % (Klass, key, _module.__name__) Klass = _module.TimerBackend KlassRef = vispy.app.timer.TimerBackend for key in dir(KlassRef): if not key.startswith('__'): method = getattr(Klass, key) if key not in exceptions: if hasattr(method, '__module__'): # Py3k assert method.__module__ in (_module.__name__, alt_modname) else: t = method.im_func.__module__ == _module.__name__ assert t Klass = _module.ApplicationBackend KlassRef = vispy.app.application.ApplicationBackend for key in dir(KlassRef): if not key.startswith('__'): method = getattr(Klass, key) if key not in exceptions: if hasattr(method, '__module__'): # Py3k assert method.__module__ in (_module.__name__, alt_modname) else: t = method.im_func.__module__ == _module.__name__ assert t # Test that all events seem to be emitted. # Get text fname = _module.__file__.strip('c') text = open(fname, 'rb').read().decode('utf-8') canvas = vispy.app.Canvas(create_native=False) # Stylus and touch are ignored because they are not yet implemented. # Mouse events are emitted from the CanvasBackend base class. ignore = set(['stylus', 'touch', 'mouse_press', 'paint', 'mouse_move', 'mouse_release']) eventNames = set(canvas.events._emitters.keys()) - ignore if not alt_modname: # Only check for non-proxy modules for name in eventNames: assert 'events.%s' % name in text, ('events.%s does not appear ' 'in %s' % (name, fname))
def test_multiple_canvases(): """Testing multiple canvases""" n_check = 3 default_app.use() if default_app.backend_name.lower() == 'glut': raise SkipTest('glut cannot use multiple canvases') with Canvas(app=default_app, size=_win_size, title='same_0') as c0: with Canvas(app=default_app, size=_win_size, title='same_1') as c1: ct = [0, 0] @c0.events.draw.connect def draw0(event): ct[0] += 1 c0.update() @c1.events.draw.connect # noqa, analysis:ignore def draw1(event): ct[1] += 1 c1.update() c0.show() # ensure visible c1.show() c0.update() # force first draw c1.update() timeout = time() + 2.0 while (ct[0] < n_check or ct[1] < n_check) and time() < timeout: default_app.process_events() print((ct, n_check)) assert_true(n_check <= ct[0] <= n_check + 1) assert_true(n_check <= ct[1] <= n_check + 1) # check timer global timer_ran timer_ran = False def on_timer(_): global timer_ran timer_ran = True timeout = time() + 2.0 Timer(0.1, app=default_app, connect=on_timer, iterations=1, start=True) while not timer_ran and time() < timeout: default_app.process_events() assert_true(timer_ran) kwargs = dict(app=default_app, autoswap=False, size=_win_size, show=True) with Canvas(title='0', **kwargs) as c0: with Canvas(title='1', **kwargs) as c1: bgcolors = [None] * 2 @c0.events.draw.connect def draw00(event): print(' {0:7}: {1}'.format('0', bgcolors[0])) if bgcolors[0] is not None: gl.glViewport(0, 0, *list(_win_size)) gl.glClearColor(*bgcolors[0]) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glFinish() @c1.events.draw.connect def draw11(event): print(' {0:7}: {1}'.format('1', bgcolors[1])) if bgcolors[1] is not None: gl.glViewport(0, 0, *list(_win_size)) gl.glClearColor(*bgcolors[1]) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glFinish() for ci, canvas in enumerate((c0, c1)): print('draw %s' % canvas.title) bgcolors[ci] = [0.5, 0.5, 0.5, 1.0] _update_process_check(canvas, 127) for ci, canvas in enumerate((c0, c1)): print('test') _update_process_check(canvas, 127, draw=False) bgcolors[ci] = [1., 1., 1., 1.] _update_process_check(canvas, 255) bgcolors[ci] = [0.25, 0.25, 0.25, 0.25] _update_process_check(canvas, 64)