##// END OF EJS Templates
FIX: make sure the QSocketNotifier is disabled...
Thomas A Caswell -
Show More
@@ -1,49 +1,53
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 def inputhook(context):
11 def inputhook(context):
11 global _appref
12 global _appref
12 app = QtCore.QCoreApplication.instance()
13 app = QtCore.QCoreApplication.instance()
13 if not app:
14 if not app:
14 if sys.platform == 'linux':
15 if sys.platform == 'linux':
15 if not os.environ.get('DISPLAY') \
16 if not os.environ.get('DISPLAY') \
16 and not os.environ.get('WAYLAND_DISPLAY'):
17 and not os.environ.get('WAYLAND_DISPLAY'):
17 import warnings
18 import warnings
18 global _already_warned
19 global _already_warned
19 if not _already_warned:
20 if not _already_warned:
20 _already_warned = True
21 _already_warned = True
21 warnings.warn(
22 warnings.warn(
22 'The DISPLAY or WAYLAND_DISPLAY environment variable is '
23 'The DISPLAY or WAYLAND_DISPLAY environment variable is '
23 'not set or empty and Qt5 requires this environment '
24 'not set or empty and Qt5 requires this environment '
24 'variable. Deactivate Qt5 code.'
25 'variable. Deactivate Qt5 code.'
25 )
26 )
26 return
27 return
27 _appref = app = QtGui.QApplication([" "])
28 _appref = app = QtGui.QApplication([" "])
28 event_loop = QtCore.QEventLoop(app)
29 event_loop = QtCore.QEventLoop(app)
29
30
30 if sys.platform == 'win32':
31 if sys.platform == 'win32':
31 # The QSocketNotifier method doesn't appear to work on Windows.
32 # The QSocketNotifier method doesn't appear to work on Windows.
32 # Use polling instead.
33 # Use polling instead.
33 timer = QtCore.QTimer()
34 timer = QtCore.QTimer()
34 timer.timeout.connect(event_loop.quit)
35 timer.timeout.connect(event_loop.quit)
35 while not context.input_is_ready():
36 while not context.input_is_ready():
36 timer.start(50) # 50 ms
37 timer.start(50) # 50 ms
37 event_loop.exec_()
38 event_loop.exec_()
38 timer.stop()
39 timer.stop()
39 else:
40 else:
40 # On POSIX platforms, we can use a file descriptor to quit the event
41 # On POSIX platforms, we can use a file descriptor to quit the event
41 # loop when there is input ready to read.
42 # loop when there is input ready to read.
42 notifier = QtCore.QSocketNotifier(context.fileno(),
43 notifier = QtCore.QSocketNotifier(context.fileno(),
43 QtCore.QSocketNotifier.Read)
44 QtCore.QSocketNotifier.Read)
45 try:
44 # connect the callback we care about before we turn it on
46 # connect the callback we care about before we turn it on
45 notifier.activated.connect(event_loop.exit)
47 notifier.activated.connect(event_loop.exit)
46 notifier.setEnabled(True)
48 notifier.setEnabled(True)
47 # only start the event loop we are not already flipped
49 # only start the event loop we are not already flipped
48 if not context.input_is_ready():
50 if not context.input_is_ready():
49 event_loop.exec_()
51 event_loop.exec_()
52 finally:
53 notifier.setEnabled(False)
General Comments 0
You need to be logged in to leave comments. Login now