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