Create an input hook for running the Qt4 application event loop. Parameters ---------- mgr : an InputHookManager app : Qt Application, optional. Running application to use. If not given, we probe Qt for an existing application object, and create a new one if none i
(mgr, app=None)
| 36 | #----------------------------------------------------------------------------- |
| 37 | |
| 38 | def create_inputhook_qt4(mgr, app=None): |
| 39 | """Create an input hook for running the Qt4 application event loop. |
| 40 | |
| 41 | Parameters |
| 42 | ---------- |
| 43 | mgr : an InputHookManager |
| 44 | |
| 45 | app : Qt Application, optional. |
| 46 | Running application to use. If not given, we probe Qt for an |
| 47 | existing application object, and create a new one if none is found. |
| 48 | |
| 49 | Returns |
| 50 | ------- |
| 51 | A pair consisting of a Qt Application (either the one given or the |
| 52 | one found or created) and a inputhook. |
| 53 | |
| 54 | Notes |
| 55 | ----- |
| 56 | We use a custom input hook instead of PyQt4's default one, as it |
| 57 | interacts better with the readline packages (issue #481). |
| 58 | |
| 59 | The inputhook function works in tandem with a 'pre_prompt_hook' |
| 60 | which automatically restores the hook as an inputhook in case the |
| 61 | latter has been temporarily disabled after having intercepted a |
| 62 | KeyboardInterrupt. |
| 63 | """ |
| 64 | |
| 65 | if app is None: |
| 66 | app = QtCore.QCoreApplication.instance() |
| 67 | if app is None: |
| 68 | app = QtGui.QApplication([" "]) |
| 69 | |
| 70 | # Re-use previously created inputhook if any |
| 71 | ip = InteractiveShell.instance() |
| 72 | if hasattr(ip, '_inputhook_qt4'): |
| 73 | return app, ip._inputhook_qt4 |
| 74 | |
| 75 | # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of |
| 76 | # hooks (they both share the got_kbdint flag) |
| 77 | |
| 78 | def inputhook_qt4(): |
| 79 | """PyOS_InputHook python hook for Qt4. |
| 80 | |
| 81 | Process pending Qt events and if there's no pending keyboard |
| 82 | input, spend a short slice of time (50ms) running the Qt event |
| 83 | loop. |
| 84 | |
| 85 | As a Python ctypes callback can't raise an exception, we catch |
| 86 | the KeyboardInterrupt and temporarily deactivate the hook, |
| 87 | which will let a *second* CTRL+C be processed normally and go |
| 88 | back to a clean prompt line. |
| 89 | """ |
| 90 | try: |
| 91 | allow_CTRL_C() |
| 92 | app = QtCore.QCoreApplication.instance() |
| 93 | if not app: # shouldn't happen, but safer if it happens anyway... |
| 94 | return 0 |
| 95 | app.processEvents(QtCore.QEventLoop.AllEvents, 300) |