1 

2# http://pyrocko.org - GPLv3 

3# 

4# The Pyrocko Developers, 21st Century 

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

6 

7import os 

8import platform 

9import matplotlib 

10 

11from pyrocko import config 

12 

13# Needed by MacOS Big Sur 

14# https://stackoverflow.com/questions/64833558/apps-not-popping-up-on-macos-big-sur-11-0-1#_= 

15if platform.uname()[0] == 'Darwin': 

16 os.environ['QT_MAC_WANTS_LAYER'] = '1' 

17 

18gui_toolkit = config.effective_gui_toolkit() 

19 

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

21 

22if gui_toolkit == 'auto': 

23 try: 

24 if qt5_backend_available: 

25 import PyQt5 

26 use_pyqt5 = True 

27 else: 

28 use_pyqt5 = False 

29 except ImportError: 

30 use_pyqt5 = False 

31 

32elif gui_toolkit == 'qt4': 

33 use_pyqt5 = False 

34else: 

35 use_pyqt5 = True 

36 if not qt5_backend_available: 

37 raise Exception( 

38 'Qt5 was forced but matplotlib Qt5Agg backend is not availble') 

39 

40 

41if use_pyqt5: 

42 if matplotlib.get_backend().find('Qt4') != -1: # noqa 

43 matplotlib.use('Qt5Agg') 

44 

45 import PyQt5 as PyQt 

46 from PyQt5 import Qt 

47 from PyQt5 import QtCore as qc 

48 from PyQt5 import QtGui as qg 

49 from PyQt5 import QtWidgets as qw 

50 from PyQt5 import QtNetwork as qn 

51 from PyQt5 import QtOpenGL as qgl 

52 from PyQt5 import QtSvg as qsvg 

53 from PyQt5 import QtPrintSupport as qprint 

54 QSortFilterProxyModel = qc.QSortFilterProxyModel 

55 QItemSelectionModel = qc.QItemSelectionModel 

56 QItemSelection = qc.QItemSelection 

57 

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

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

60 

61 class QPixmapCache(qg.QPixmapCache): 

62 def cached(self, key): 

63 return self.find(key) 

64 

65else: 

66 if matplotlib.get_backend().find('Qt5') != -1: # noqa 

67 matplotlib.use('Qt4Agg') 

68 

69 import PyQt4 as PyQt 

70 from PyQt4 import Qt 

71 from PyQt4 import QtCore as qc 

72 from PyQt4 import QtGui as qg 

73 qw = qg 

74 from PyQt4 import QtNetwork as qn 

75 from PyQt4 import QtOpenGL as qgl 

76 from PyQt4 import QtSvg as qsvg 

77 qprint = qg 

78 QSortFilterProxyModel = qg.QSortFilterProxyModel 

79 QItemSelectionModel = qg.QItemSelectionModel 

80 QItemSelection = qg.QItemSelection 

81 

82 getSaveFileName = qw.QFileDialog.getSaveFileName 

83 

84 class QPixmapCache(qg.QPixmapCache): 

85 def cached(self, key): 

86 pixmap = qg.QPixmap() 

87 found = self.find(key, pixmap) 

88 if found: 

89 return pixmap 

90 return found 

91 

92 

93try: 

94 vers = qc.QVersionNumber.fromString 

95except AttributeError: 

96 def vers(s): 

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

98 

99# Application attribute has to be set for QWebView 

100if vers(Qt.QT_VERSION_STR) >= vers('5.4.0'): 

101 Qt.QCoreApplication.setAttribute(qc.Qt.AA_ShareOpenGLContexts, True)