Show More
@@ -1,122 +1,122 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Qt4's inputhook support function |
|
4 | 4 | |
|
5 | 5 | Author: Christian Boos |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from IPython.core.interactiveshell import InteractiveShell |
|
20 | 20 | from IPython.external.qt_for_kernel import QtCore, QtGui |
|
21 | 21 | from IPython.lib.inputhook import allow_CTRL_C, ignore_CTRL_C, stdin_ready |
|
22 | 22 | |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | # Code |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | |
|
27 | 27 | def create_inputhook_qt4(mgr, app=None): |
|
28 | 28 | """Create an input hook for running the Qt4 application event loop. |
|
29 | 29 | |
|
30 | 30 | Parameters |
|
31 | 31 | ---------- |
|
32 | 32 | mgr : an InputHookManager |
|
33 | 33 | |
|
34 | 34 | app : Qt Application, optional. |
|
35 | 35 | Running application to use. If not given, we probe Qt for an |
|
36 | 36 | existing application object, and create a new one if none is found. |
|
37 | 37 | |
|
38 | 38 | Returns |
|
39 | 39 | ------- |
|
40 | 40 | A pair consisting of a Qt Application (either the one given or the |
|
41 | 41 | one found or created) and a inputhook. |
|
42 | 42 | |
|
43 | 43 | Notes |
|
44 | 44 | ----- |
|
45 | 45 | We use a custom input hook instead of PyQt4's default one, as it |
|
46 | 46 | interacts better with the readline packages (issue #481). |
|
47 | 47 | |
|
48 | 48 | The inputhook function works in tandem with a 'pre_prompt_hook' |
|
49 | 49 | which automatically restores the hook as an inputhook in case the |
|
50 | 50 | latter has been temporarily disabled after having intercepted a |
|
51 | 51 | KeyboardInterrupt. |
|
52 | 52 | """ |
|
53 | 53 | |
|
54 | 54 | if app is None: |
|
55 | 55 | app = QtCore.QCoreApplication.instance() |
|
56 | 56 | if app is None: |
|
57 | 57 | app = QtGui.QApplication([" "]) |
|
58 | 58 | |
|
59 | 59 | # Re-use previously created inputhook if any |
|
60 | 60 | ip = InteractiveShell.instance() |
|
61 | 61 | if hasattr(ip, '_inputhook_qt4'): |
|
62 | 62 | return app, ip._inputhook_qt4 |
|
63 | 63 | |
|
64 | 64 | # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of |
|
65 | 65 | # hooks (they both share the got_kbdint flag) |
|
66 | 66 | |
|
67 | 67 | got_kbdint = [False] |
|
68 | 68 | |
|
69 | 69 | def inputhook_qt4(): |
|
70 | 70 | """PyOS_InputHook python hook for Qt4. |
|
71 | 71 | |
|
72 | 72 | Process pending Qt events and if there's no pending keyboard |
|
73 | 73 | input, spend a short slice of time (50ms) running the Qt event |
|
74 | 74 | loop. |
|
75 | 75 | |
|
76 | 76 | As a Python ctypes callback can't raise an exception, we catch |
|
77 | 77 | the KeyboardInterrupt and temporarily deactivate the hook, |
|
78 | 78 | which will let a *second* CTRL+C be processed normally and go |
|
79 | 79 | back to a clean prompt line. |
|
80 | 80 | """ |
|
81 | 81 | try: |
|
82 | 82 | allow_CTRL_C() |
|
83 | 83 | app = QtCore.QCoreApplication.instance() |
|
84 | 84 | if not app: # shouldn't happen, but safer if it happens anyway... |
|
85 | 85 | return 0 |
|
86 | 86 | app.processEvents(QtCore.QEventLoop.AllEvents, 300) |
|
87 | 87 | if not stdin_ready(): |
|
88 | 88 | timer = QtCore.QTimer() |
|
89 | 89 | timer.timeout.connect(app.quit) |
|
90 | 90 | while not stdin_ready(): |
|
91 | 91 | timer.start(50) |
|
92 | 92 | app.exec_() |
|
93 | 93 | timer.stop() |
|
94 | 94 | except KeyboardInterrupt: |
|
95 | 95 | ignore_CTRL_C() |
|
96 | 96 | got_kbdint[0] = True |
|
97 | 97 | print("\nKeyboardInterrupt - Ctrl-C again for new prompt") |
|
98 | 98 | mgr.clear_inputhook() |
|
99 | 99 | except: # NO exceptions are allowed to escape from a ctypes callback |
|
100 | 100 | ignore_CTRL_C() |
|
101 | mgr.clear_inputhook() | |
|
102 | 101 | from traceback import print_exc |
|
103 | 102 | print_exc() |
|
104 | 103 | print("Got exception from inputhook_qt4, unregistering.") |
|
104 | mgr.clear_inputhook() | |
|
105 | 105 | finally: |
|
106 | 106 | allow_CTRL_C() |
|
107 | 107 | return 0 |
|
108 | 108 | |
|
109 | 109 | def preprompthook_qt4(ishell): |
|
110 | 110 | """'pre_prompt_hook' used to restore the Qt4 input hook |
|
111 | 111 | |
|
112 | 112 | (in case the latter was temporarily deactivated after a |
|
113 | 113 | CTRL+C) |
|
114 | 114 | """ |
|
115 | 115 | if got_kbdint[0]: |
|
116 | 116 | mgr.set_inputhook(inputhook_qt4) |
|
117 | 117 | got_kbdint[0] = False |
|
118 | 118 | |
|
119 | 119 | ip._inputhook_qt4 = inputhook_qt4 |
|
120 | 120 | ip.set_hook('pre_prompt_hook', preprompthook_qt4) |
|
121 | 121 | |
|
122 | 122 | return app, inputhook_qt4 |
General Comments 0
You need to be logged in to leave comments.
Login now