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

1 

2# http://pyrocko.org - GPLv3 

3# 

4# The Pyrocko Developers, 21st Century 

5# ---|P------/S----------~Lg---------- 

6 

7import os 

8import platform 

9import matplotlib 

10import logging 

11 

12from pyrocko import config 

13 

14logger = logging.getLogger('pyrocko.gui.qt_compat') 

15 

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' 

20 

21gui_toolkit = config.effective_gui_toolkit() 

22 

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.') 

27 

28qt5_backend_available = 'Qt5Agg' in matplotlib.rcsetup.all_backends 

29 

30if not qt5_backend_available: 

31 logger.warn( 

32 'Matplotlib Qt5Agg backend is not available. Snufflings drawing ' 

33 'matplotlib figures may not work properly.') 

34 

35if matplotlib.get_backend().find('Qt4') != -1: 

36 matplotlib.use('Qt5Agg') 

37 

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 

46 

47def getSaveFileName(*args, **kwargs): 

48 return qw.QFileDialog.getSaveFileName(*args, **kwargs)[0] 

49 

50class QPixmapCache(qg.QPixmapCache): 

51 def cached(self, key): 

52 return self.find(key) 

53 

54try: 

55 vers = qc.QVersionNumber.fromString 

56except AttributeError: 

57 def vers(s): 

58 return tuple(s.split('.')) 

59 

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) 

63 

64 

65def get_em(painter): 

66 return painter.boundingRect(qc.QRectF(), 0, 'M').width()