1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

 

from pyrocko import config 

import matplotlib 

 

gui_toolkit = config.effective_gui_toolkit() 

 

qt5_backend_available = 'Qt5Agg' in matplotlib.rcsetup.all_backends 

 

if gui_toolkit == 'auto': 

try: 

if qt5_backend_available: 

import PyQt5 

use_pyqt5 = True 

else: 

use_pyqt5 = False 

except ImportError: 

use_pyqt5 = False 

 

elif gui_toolkit == 'qt4': 

use_pyqt5 = False 

else: 

use_pyqt5 = True 

if not qt5_backend_available: 

raise Exception( 

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

 

 

if use_pyqt5: 

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

matplotlib.use('Qt5Agg') 

 

import PyQt5 as PyQt 

from PyQt5 import Qt 

from PyQt5 import QtCore as qc 

from PyQt5 import QtGui as qg 

from PyQt5 import QtWidgets as qw 

from PyQt5 import QtNetwork as qn 

from PyQt5 import QtOpenGL as qgl 

from PyQt5 import QtSvg as qsvg 

from PyQt5 import QtPrintSupport as qprint 

QSortFilterProxyModel = qc.QSortFilterProxyModel 

QItemSelectionModel = qc.QItemSelectionModel 

QItemSelection = qc.QItemSelection 

 

def getSaveFileName(*args, **kwargs): 

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

 

class QPixmapCache(qg.QPixmapCache): 

def cached(self, key): 

return self.find(key) 

 

else: 

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

matplotlib.use('Qt4Agg') 

 

import PyQt4 as PyQt 

from PyQt4 import Qt 

from PyQt4 import QtCore as qc 

from PyQt4 import QtGui as qg 

qw = qg 

from PyQt4 import QtNetwork as qn 

from PyQt4 import QtOpenGL as qgl 

from PyQt4 import QtSvg as qsvg 

qprint = qg 

QSortFilterProxyModel = qg.QSortFilterProxyModel 

QItemSelectionModel = qg.QItemSelectionModel 

QItemSelection = qg.QItemSelection 

 

getSaveFileName = qw.QFileDialog.getSaveFileName 

 

class QPixmapCache(qg.QPixmapCache): 

def cached(self, key): 

pixmap = qg.QPixmap() 

found = self.find(key, pixmap) 

if found: 

return pixmap 

return found 

 

 

try: 

vers = qc.QVersionNumber.fromString 

except AttributeError: 

def vers(s): 

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

 

# Application attribute has to be set for QWebView 

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

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