Show More
@@ -1,54 +1,56 b'' | |||||
1 | import sys |
|
1 | import sys | |
2 | import os |
|
2 | import os | |
3 | from IPython.external.qt_for_kernel import QtCore, QtGui |
|
3 | from IPython.external.qt_for_kernel import QtCore, QtGui | |
4 |
|
4 | |||
5 | # If we create a QApplication, keep a reference to it so that it doesn't get |
|
5 | # If we create a QApplication, keep a reference to it so that it doesn't get | |
6 | # garbage collected. |
|
6 | # garbage collected. | |
7 | _appref = None |
|
7 | _appref = None | |
8 | _already_warned = False |
|
8 | _already_warned = False | |
9 |
|
9 | |||
10 |
|
10 | |||
11 | def inputhook(context): |
|
11 | def inputhook(context): | |
12 | global _appref |
|
12 | global _appref | |
13 | app = QtCore.QCoreApplication.instance() |
|
13 | app = QtCore.QCoreApplication.instance() | |
14 | if not app: |
|
14 | if not app: | |
15 | if sys.platform == 'linux': |
|
15 | if sys.platform == 'linux': | |
16 | if not os.environ.get('DISPLAY') \ |
|
16 | if not os.environ.get('DISPLAY') \ | |
17 | and not os.environ.get('WAYLAND_DISPLAY'): |
|
17 | and not os.environ.get('WAYLAND_DISPLAY'): | |
18 | import warnings |
|
18 | import warnings | |
19 | global _already_warned |
|
19 | global _already_warned | |
20 | if not _already_warned: |
|
20 | if not _already_warned: | |
21 | _already_warned = True |
|
21 | _already_warned = True | |
22 | warnings.warn( |
|
22 | warnings.warn( | |
23 | 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' |
|
23 | 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' | |
24 | 'not set or empty and Qt5 requires this environment ' |
|
24 | 'not set or empty and Qt5 requires this environment ' | |
25 | 'variable. Deactivate Qt5 code.' |
|
25 | 'variable. Deactivate Qt5 code.' | |
26 | ) |
|
26 | ) | |
27 | return |
|
27 | return | |
28 | QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) |
|
28 | QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) | |
29 | _appref = app = QtGui.QApplication([" "]) |
|
29 | _appref = app = QtGui.QApplication([" "]) | |
30 | event_loop = QtCore.QEventLoop(app) |
|
30 | event_loop = QtCore.QEventLoop(app) | |
31 |
|
31 | |||
32 | if sys.platform == 'win32': |
|
32 | if sys.platform == 'win32': | |
33 | # The QSocketNotifier method doesn't appear to work on Windows. |
|
33 | # The QSocketNotifier method doesn't appear to work on Windows. | |
34 | # Use polling instead. |
|
34 | # Use polling instead. | |
35 | timer = QtCore.QTimer() |
|
35 | timer = QtCore.QTimer() | |
36 | timer.timeout.connect(event_loop.quit) |
|
36 | timer.timeout.connect(event_loop.quit) | |
37 | while not context.input_is_ready(): |
|
37 | while not context.input_is_ready(): | |
38 | timer.start(50) # 50 ms |
|
38 | timer.start(50) # 50 ms | |
39 | event_loop.exec_() |
|
39 | event_loop.exec_() | |
40 | timer.stop() |
|
40 | timer.stop() | |
41 | else: |
|
41 | else: | |
42 | # On POSIX platforms, we can use a file descriptor to quit the event |
|
42 | # On POSIX platforms, we can use a file descriptor to quit the event | |
43 | # loop when there is input ready to read. |
|
43 | # loop when there is input ready to read. | |
44 | notifier = QtCore.QSocketNotifier(context.fileno(), |
|
44 | notifier = QtCore.QSocketNotifier(context.fileno(), | |
45 | QtCore.QSocketNotifier.Read) |
|
45 | QtCore.QSocketNotifier.Read) | |
46 | try: |
|
46 | try: | |
47 | # connect the callback we care about before we turn it on |
|
47 | # connect the callback we care about before we turn it on | |
|
48 | # lambda is necessary as PyQT inspect the function signature to know | |||
|
49 | # what arguments to pass to. See https://github.com/ipython/ipython/pull/12355 | |||
48 | notifier.activated.connect(lambda: event_loop.exit()) |
|
50 | notifier.activated.connect(lambda: event_loop.exit()) | |
49 | notifier.setEnabled(True) |
|
51 | notifier.setEnabled(True) | |
50 | # only start the event loop we are not already flipped |
|
52 | # only start the event loop we are not already flipped | |
51 | if not context.input_is_ready(): |
|
53 | if not context.input_is_ready(): | |
52 | event_loop.exec_() |
|
54 | event_loop.exec_() | |
53 | finally: |
|
55 | finally: | |
54 | notifier.setEnabled(False) |
|
56 | notifier.setEnabled(False) |
General Comments 0
You need to be logged in to leave comments.
Login now