Toolbox/PyQt4ImportHook.py

changeset 5814
50b299f6eb98
parent 5802
abfb67a6f376
child 5815
223fa32b3a32
equal deleted inserted replaced
5813:82349b9ceb4d 5814:50b299f6eb98
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import sys 12 import sys
13 import importlib
14
15 class PyQt4Importer(object):
16 """
17 Class implementing an importer converting PyQt5 imports to PyQt4
18 imports.
19 """
20 def __init__(self):
21 """
22 Constructor
23 """
24 self.__path = None
25
26 def find_module(self, fullname, path=None):
27 """
28 Public method returning the module loader.
29
30 @param fullname name of the module to be loaded (string)
31 @param path path to resolve the module name (string)
32 @return module loader object
33 """
34 if fullname.startswith("PyQt5"):
35 self.__path = path
36 return self
37
38 return None
39
40 @staticmethod
41 def QComboBox_currentData(other, role=None):
42 """
43 Public method to emulate the currentData method of Qt5.
44
45 @param role role of which data should be retrived
46 @return stored data at current selection
47 """
48 import PyQt4.Qt
49 if role is None:
50 role = PyQt4.Qt.Qt.UserRole
51
52 idx = other.currentIndex()
53 return other.itemData(idx, role)
54
55 def load_module(self, fullname):
56 """
57 Public method to load a module.
58
59 @param fullname name of the module to be loaded (string)
60 @return reference to the loaded module (module)
61 """
62 if fullname in ["PyQt5.QtWidgets", "PyQt5.QtPrintSupport"]:
63 newname = "PyQt4.QtGui"
64 elif fullname in ["PyQt5.QtWebKitWidgets"]:
65 newname = "PyQt4.QtWebKit"
66 else:
67 newname = fullname.replace("PyQt5", "PyQt4")
68
69 module = importlib.import_module(newname)
70 sys.modules[fullname] = module
71 if fullname == "PyQt5.QtCore":
72 import PyQt4.QtGui
73 module.qInstallMessageHandler = module.qInstallMsgHandler
74 module.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel
75 module.QItemSelection = PyQt4.QtGui.QItemSelection
76 module.QSortFilterProxyModel = \
77 PyQt4.QtGui.QSortFilterProxyModel
78 module.QAbstractProxyModel = PyQt4.QtGui.QAbstractProxyModel
79 module.QStringListModel = PyQt4.QtGui.QStringListModel
80
81 PyQt4.QtGui.QComboBox.currentData = self.QComboBox_currentData
82 return module
83
13 try: 84 try:
14 if "--pyqt4" in sys.argv: 85 if "--pyqt4" in sys.argv:
15 sys.argv.remove("--pyqt4") 86 sys.argv.remove("--pyqt4")
16 # fake a failed PyQt5 import 87 # fake a failed PyQt5 import
17 raise ImportError 88 raise ImportError
18 import PyQt5 # __IGNORE_WARNING__ 89 import PyQt5 # __IGNORE_WARNING__
19 except ImportError: 90 except ImportError:
20 import importlib
21
22 class PyQt4Importer(object):
23 """
24 Class implementing an importer converting PyQt5 imports to PyQt4
25 imports.
26 """
27 def __init__(self):
28 """
29 Constructor
30 """
31 self.__path = None
32
33 def find_module(self, fullname, path=None):
34 """
35 Public method returning the module loader.
36
37 @param fullname name of the module to be loaded (string)
38 @param path path to resolve the module name (string)
39 @return module loader object
40 """
41 if fullname.startswith("PyQt5"):
42 self.__path = path
43 return self
44
45 return None
46
47 @staticmethod
48 def QComboBox_currentData(other, role=None):
49 """
50 Public method to emulate the currentData method of Qt5.
51
52 @param role role of which data should be retrived
53 @return stored data at current selection
54 """
55 import PyQt4.Qt
56 if role is None:
57 role = PyQt4.Qt.Qt.UserRole
58
59 idx = other.currentIndex()
60 return other.itemData(idx, role)
61
62 def load_module(self, fullname):
63 """
64 Public method to load a module.
65
66 @param fullname name of the module to be loaded (string)
67 @return reference to the loaded module (module)
68 """
69 if fullname in ["PyQt5.QtWidgets", "PyQt5.QtPrintSupport"]:
70 newname = "PyQt4.QtGui"
71 elif fullname in ["PyQt5.QtWebKitWidgets"]:
72 newname = "PyQt4.QtWebKit"
73 else:
74 newname = fullname.replace("PyQt5", "PyQt4")
75
76 module = importlib.import_module(newname)
77 sys.modules[fullname] = module
78 if fullname == "PyQt5.QtCore":
79 import PyQt4.QtGui
80 module.qInstallMessageHandler = module.qInstallMsgHandler
81 module.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel
82 module.QItemSelection = PyQt4.QtGui.QItemSelection
83 module.QSortFilterProxyModel = \
84 PyQt4.QtGui.QSortFilterProxyModel
85 module.QAbstractProxyModel = PyQt4.QtGui.QAbstractProxyModel
86 module.QStringListModel = PyQt4.QtGui.QStringListModel
87
88 PyQt4.QtGui.QComboBox.currentData = self.QComboBox_currentData
89 return module
90
91 sys.meta_path.insert(0, PyQt4Importer()) 91 sys.meta_path.insert(0, PyQt4Importer())
92 92
93 if sys.version_info[0] == 2: 93 if sys.version_info[0] == 2:
94 import sip 94 import sip
95 sip.setapi('QString', 2) 95 sip.setapi('QString', 2)

eric ide

mercurial