def test_active_tabwidget_after_editor_containing_tabs_gets_focus(self): # Regression test: if an editor contains tabs, a change in focus # sets the editor area pane `active_tabwidget` to one of those tabs, # rather than the editor's tab, after certain operations (e.g., # navigating the editor tabs using keyboard shortcuts). window = TaskWindow() task = SplitEditorAreaPaneTestTask() editor_area = task.editor_area window.add_task(task) # Show the window. with event_loop(): window.open() with event_loop(): app = get_app_qt4() app.setActiveWindow(window.control) # Add and activate an editor which contains tabs. editor = ViewWithTabsEditor() with event_loop(): editor_area.add_editor(editor) with event_loop(): editor_area.activate_editor(editor) # Check that the active tabwidget is the right one. self.assertIs(editor_area.active_tabwidget, editor_area.control.tabwidget()) with event_loop(): window.close()
def test_set_trait_later_runs_later(self): # Regression test for enthought/pyface#272. application = SimpleApplication() application_running = [] def exit_app(): # Record whether the event loop is running or not, then exit. application_running.append(is_event_loop_running_qt4()) application.stop() application.on_trait_change(exit_app, 'application_running') # Make sure that the application stops after 10 seconds, no matter # what. qt_app = get_app_qt4() timeout_timer = QtCore.QTimer() timeout_timer.setSingleShot(True) timeout_timer.setInterval(10000) # 10 second timeout timeout_timer.timeout.connect(qt_app.quit) timeout_timer.start() try: application.start() finally: timeout_timer.stop() # Attempt to leave the QApplication in a reasonably clean # state in case of failure. qt_app.sendPostedEvents() qt_app.flush() self.assertTrue(application_running[0])
def test_set_trait_later_runs_later(self): # Regression test for enthought/pyface#272. application = SimpleApplication() application_running = [] def exit_app(): # Record whether the event loop is running or not, then exit. application_running.append( is_event_loop_running_qt4() ) application.stop() application.on_trait_change(exit_app, 'application_running') # Make sure that the application stops after 10 seconds, no matter # what. qt_app = get_app_qt4() timeout_timer = QtCore.QTimer() timeout_timer.setSingleShot(True) timeout_timer.setInterval(10000) # 10 second timeout timeout_timer.timeout.connect(qt_app.quit) timeout_timer.start() try: application.start() finally: timeout_timer.stop() # Attempt to leave the QApplication in a reasonably clean # state in case of failure. qt_app.sendPostedEvents() qt_app.flush() self.assertTrue(application_running[0])
def run(self): """ Run the application. Returns: -------- Whether the application started successfully (i.e., without a veto). """ # Make sure the GUI has been created (so that, if required, the splash # screen is shown). gui = self.gui started = self.start() if started: app = get_app_qt4(['']) # Create windows from the default or saved application layout. self._create_windows() kernel = self.get_service(IPYTHON_KERNEL_PROTOCOL) kernel.init_ipkernel('qt4') app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()")) app.aboutToQuit.connect(kernel.cleanup_consoles) gui.set_trait_later(self, 'application_initialized', self) kernel.ipkernel.start() return started
def start(self): import qt4reactor app = get_app_qt4() qt4reactor.install() from twisted.internet import reactor self.reactor = reactor reactor.runReturn()
def event_loop(): """ Post and process the Qt events at the exit of the code block. """ app = get_app_qt4() yield app.sendPostedEvents() app.processEvents()
def main(): app = get_app_qt4() main_window = QtGui.QMainWindow() main_window.resize(500,500) enable_window = create_chaco_plot(main_window) # The .control attribute references a QWidget that gives Chaco events # and that Chaco paints into. main_window.setCentralWidget(enable_window.control) main_window.show() start_event_loop_qt4(app) return main_window
def main(): app = get_app_qt4() main_window = QtGui.QMainWindow() main_window.resize(500, 500) enable_window = create_chaco_plot(main_window) # The .control attribute references a QWidget that gives Chaco events # and that Chaco paints into. main_window.setCentralWidget(enable_window.control) main_window.show() start_event_loop_qt4(app) return main_window
def delete_widget(widget, timeout=1.0): """ Context manager that executes the event loop so that we can safely delete a Qt widget. """ app = get_app_qt4() timer = QTimer() timer.setSingleShot(True) timer.setInterval(round(timeout * 1000)) timer.timeout.connect(app.quit) widget.destroyed.connect(app.quit) yield timer.start() app.exec_() if not timer.isActive(): # We exited the event loop on timeout. msg = "Could not destroy widget before timeout: {!r}" raise AssertionError(msg.format(widget))
def delete_widget(widget, timeout=1.0): """ Context manager that executes the event loop so that we can safely delete a Qt widget. """ app = get_app_qt4() timer = QTimer() timer.setSingleShot(True) timer.setInterval(timeout*1000) timer.timeout.connect(app.quit) widget.destroyed.connect(app.quit) yield timer.start() app.exec_() if not timer.isActive(): # We exited the event loop on timeout. msg = 'Could not destroy widget before timeout: {!r}' raise AssertionError(msg.format(widget))
def test_editor_tooltip_change_inactive(self): # regression test related to pyface#523 window = TaskWindow(size=(800, 600)) task = SplitEditorAreaPaneTestTask() editor_area = task.editor_area window.add_task(task) # Show the window. with event_loop(): window.open() with event_loop(): app = get_app_qt4() app.setActiveWindow(window.control) # Add and activate an editor which contains tabs. left_editor = ViewWithTabsEditor() right_editor = ViewWithTabsEditor() with event_loop(): editor_area.add_editor(left_editor) with event_loop(): editor_area.control.split(orientation=QtCore.Qt.Orientation.Horizontal) with event_loop(): editor_area.add_editor(right_editor) editor_area.activate_editor(right_editor) # change the name of the inactive editor left_editor.tooltip = "New Tooltip" # the text of the editor's tab should have changed left_tabwidget = editor_area.tabwidgets()[0] index = left_tabwidget.indexOf(left_editor.control) tab_tooltip = left_tabwidget.tabToolTip(index) self.assertEqual(tab_tooltip, "New Tooltip")
def test_active_editor_after_focus_change(self): window = TaskWindow(size=(800, 600)) task = SplitEditorAreaPaneTestTask() editor_area = task.editor_area window.add_task(task) # Show the window. with event_loop(): window.open() with event_loop(): app = get_app_qt4() app.setActiveWindow(window.control) # Add and activate an editor which contains tabs. left_editor = ViewWithTabsEditor() right_editor = ViewWithTabsEditor() with event_loop(): editor_area.add_editor(left_editor) with event_loop(): editor_area.control.split(orientation=QtCore.Qt.Horizontal) with event_loop(): editor_area.add_editor(right_editor) editor_area.activate_editor(right_editor) self.assertEqual(editor_area.active_editor, right_editor) with event_loop(): left_editor.control.setFocus() self.assertIsNotNone(editor_area.active_editor) self.assertEqual(editor_area.active_editor, left_editor) with event_loop(): window.close()
def demo_main(demo_class, size=(400, 400), title="Enable Demo"): "Takes the class of the demo to run as an argument." app = get_app_qt4() frame = demo_class(None, size=size, title=title) start_event_loop_qt4(app) return frame
""" import threading import time import unittest # Preamble: Try importing Qt, and set QT_FOUND to True on success. try: from pyface.util.guisupport import get_app_qt4 # This import is necessary to set the `ui_handler` global variable in # `traits.trait_notifiers`, which is responsible for dispatching the events # to the UI thread. from traitsui.qt4 import toolkit # noqa: F401 qt4_app = get_app_qt4() except Exception: QT_FOUND = False else: QT_FOUND = True from traits import trait_notifiers from traits.api import Callable, Float, HasTraits, on_trait_change class CalledAsMethod(HasTraits): foo = Float
notification really occurs on the UI thread. At present, `dispatch='ui'` and `dispatch='fast_ui'` have the same effect. """ # Preamble: Try importing Qt, and set QT_FOUND to True on success. try: from pyface.util.guisupport import get_app_qt4, start_event_loop_qt4 # This import is necessary to set the `ui_handler` global variable in # `traits.trait_notifiers`, which is responsible for dispatching the events # to the UI thread. from traitsui.qt4 import toolkit qt4_app = get_app_qt4() except Exception: QT_FOUND = False else: QT_FOUND = True import thread from threading import Thread import time from traits.api import Callable, Float, HasTraits, on_trait_change from traits.testing.unittest_tools import unittest
def demo_main(demo_class, size=(400,400), title="Enable Demo"): "Takes the class of the demo to run as an argument." app = get_app_qt4() frame = demo_class(None, size=size, title=title) start_event_loop_qt4(app) return frame