Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/qt_compat.py: 78%
37 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-25 15:33 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-25 15:33 +0000
2# http://pyrocko.org - GPLv3
3#
4# The Pyrocko Developers, 21st Century
5# ---|P------/S----------~Lg----------
7import os
8import platform
9import matplotlib
10import logging
12from pyrocko import config
14logger = logging.getLogger('pyrocko.gui.qt_compat')
16# Needed by MacOS Big Sur
17# https://stackoverflow.com/questions/64833558/apps-not-popping-up-on-macos-big-sur-11-0-1#_=
18if platform.uname()[0] == 'Darwin':
19 os.environ['QT_MAC_WANTS_LAYER'] = '1'
21gui_toolkit = config.effective_gui_toolkit()
23if gui_toolkit == 'qt4':
24 raise Exception(
25 'Pyrocko has dropped support for Qt4. The last release with Qt4 '
26 'support was v2021.09.14.')
28qt5_backend_available = 'Qt5Agg' in matplotlib.rcsetup.all_backends
30if not qt5_backend_available:
31 logger.warn(
32 'Matplotlib Qt5Agg backend is not available. Snufflings drawing '
33 'matplotlib figures may not work properly.')
35if matplotlib.get_backend().find('Qt4') != -1:
36 matplotlib.use('Qt5Agg')
38import PyQt5 as PyQt # noqa
39from PyQt5 import Qt # noqa
40from PyQt5 import QtCore as qc # noqa
41from PyQt5 import QtGui as qg # noqa
42from PyQt5 import QtWidgets as qw # noqa
43from PyQt5 import QtNetwork as qn # noqa
44from PyQt5 import QtSvg as qsvg # noqa
45from PyQt5 import QtPrintSupport as qprint # noqa
47def getSaveFileName(*args, **kwargs):
48 return qw.QFileDialog.getSaveFileName(*args, **kwargs)[0]
50class QPixmapCache(qg.QPixmapCache):
51 def cached(self, key):
52 return self.find(key)
54try:
55 vers = qc.QVersionNumber.fromString
56except AttributeError:
57 def vers(s):
58 return tuple(s.split('.'))
60# Application attribute has to be set for QWebView
61if vers(Qt.QT_VERSION_STR) >= vers('5.4.0'):
62 Qt.QCoreApplication.setAttribute(qc.Qt.AA_ShareOpenGLContexts, True)
65def get_em(painter):
66 return painter.boundingRect(qc.QRectF(), 0, 'M').width()