qt.py
83 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
Thomas Kluyver
|
r22654 | import sys | ||
Christoph
|
r24529 | import os | ||
Matthias Bussonnier
|
r26711 | from IPython.external.qt_for_kernel import QtCore, QtGui, enum_helper | ||
Matthias Bussonnier
|
r26359 | from IPython import get_ipython | ||
Thomas Kluyver
|
r21934 | |||
Thomas Kluyver
|
r22707 | # If we create a QApplication, keep a reference to it so that it doesn't get | ||
# garbage collected. | ||||
_appref = None | ||||
Christoph
|
r24529 | _already_warned = False | ||
Thomas A Caswell
|
r23367 | |||
Thomas A Caswell
|
r25035 | |||
Matthias Bussonnier
|
r26711 | def _exec(obj): | ||
# exec on PyQt6, exec_ elsewhere. | ||||
obj.exec() if hasattr(obj, "exec") else obj.exec_() | ||||
Matthias Bussonnier
|
r26359 | def _reclaim_excepthook(): | ||
shell = get_ipython() | ||||
if shell is not None: | ||||
sys.excepthook = shell.excepthook | ||||
Thomas Kluyver
|
r21934 | def inputhook(context): | ||
Thomas Kluyver
|
r22707 | global _appref | ||
Thomas Kluyver
|
r21934 | app = QtCore.QCoreApplication.instance() | ||
if not app: | ||||
Christoph
|
r24529 | if sys.platform == 'linux': | ||
Christoph
|
r24533 | if not os.environ.get('DISPLAY') \ | ||
and not os.environ.get('WAYLAND_DISPLAY'): | ||||
Christoph
|
r24529 | import warnings | ||
global _already_warned | ||||
if not _already_warned: | ||||
_already_warned = True | ||||
warnings.warn( | ||||
luz.paz
|
r24559 | 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' | ||
'not set or empty and Qt5 requires this environment ' | ||||
Christoph
|
r24533 | 'variable. Deactivate Qt5 code.' | ||
Christoph
|
r24529 | ) | ||
return | ||||
Matthias Bussonnier
|
r26711 | try: | ||
QtCore.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) | ||||
except AttributeError: # Only for Qt>=5.6, <6. | ||||
pass | ||||
try: | ||||
QtCore.QApplication.setHighDpiScaleFactorRoundingPolicy( | ||||
QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough | ||||
) | ||||
except AttributeError: # Only for Qt>=5.14. | ||||
pass | ||||
Thomas Kluyver
|
r22707 | _appref = app = QtGui.QApplication([" "]) | ||
Matthias Bussonnier
|
r26359 | |||
# "reclaim" IPython sys.excepthook after event loop starts | ||||
# without this, it defaults back to BaseIPythonApplication.excepthook | ||||
# and exceptions in the Qt event loop are rendered without traceback | ||||
# formatting and look like "bug in IPython". | ||||
QtCore.QTimer.singleShot(0, _reclaim_excepthook) | ||||
Thomas Kluyver
|
r21934 | event_loop = QtCore.QEventLoop(app) | ||
Thomas Kluyver
|
r22654 | |||
if sys.platform == 'win32': | ||||
# The QSocketNotifier method doesn't appear to work on Windows. | ||||
# Use polling instead. | ||||
timer = QtCore.QTimer() | ||||
timer.timeout.connect(event_loop.quit) | ||||
while not context.input_is_ready(): | ||||
timer.start(50) # 50 ms | ||||
event_loop.exec_() | ||||
timer.stop() | ||||
else: | ||||
# On POSIX platforms, we can use a file descriptor to quit the event | ||||
# loop when there is input ready to read. | ||||
Matthias Bussonnier
|
r26711 | notifier = QtCore.QSocketNotifier( | ||
context.fileno(), enum_helper("QtCore.QSocketNotifier.Type").Read | ||||
) | ||||
Thomas A Caswell
|
r25035 | try: | ||
# connect the callback we care about before we turn it on | ||||
Matthias Bussonnier
|
r25811 | notifier.activated.connect(lambda: event_loop.exit()) | ||
Thomas A Caswell
|
r25035 | notifier.setEnabled(True) | ||
# only start the event loop we are not already flipped | ||||
if not context.input_is_ready(): | ||||
Matthias Bussonnier
|
r26711 | _exec(event_loop) | ||
Thomas A Caswell
|
r25035 | finally: | ||
notifier.setEnabled(False) | ||||