eric6/Toolbox/PyQt4ImportHook.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7192
a22eee00b052
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2014 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing an import hook converting PyQt5 imports to PyQt4 imports.
8 """
9
10 from __future__ import unicode_literals
11
12 import sys
13 import importlib
14
15
16 class PyQt4Importer(object):
17 """
18 Class implementing an importer converting PyQt5 imports to PyQt4
19 imports.
20 """
21 def __init__(self):
22 """
23 Constructor
24 """
25 self.__path = None
26
27 def find_module(self, fullname, path=None):
28 """
29 Public method returning the module loader.
30
31 @param fullname name of the module to be loaded (string)
32 @param path path to resolve the module name (string)
33 @return module loader object
34 """
35 if fullname.startswith("PyQt5"):
36 self.__path = path
37 return self
38
39 return None
40
41 @staticmethod
42 def QComboBox_currentData(other, role=None):
43 """
44 Static method to emulate the currentData method of Qt5.
45
46 @param other reference to the combo box to get the user data of
47 @type QComboBox
48 @param role role of which data should be retrieved
49 @type int
50 @return stored data at current selection
51 @rtype any
52 """
53 import PyQt4.Qt
54 if role is None:
55 role = PyQt4.Qt.Qt.UserRole
56
57 idx = other.currentIndex()
58 return other.itemData(idx, role)
59
60 def load_module(self, fullname):
61 """
62 Public method to load a module.
63
64 @param fullname name of the module to be loaded (string)
65 @return reference to the loaded module (module)
66 """
67 if fullname in ["PyQt5.QtWidgets", "PyQt5.QtPrintSupport"]:
68 newname = "PyQt4.QtGui"
69 elif fullname in ["PyQt5.QtWebKitWidgets"]:
70 newname = "PyQt4.QtWebKit"
71 else:
72 newname = fullname.replace("PyQt5", "PyQt4")
73
74 module = importlib.import_module(newname)
75 sys.modules[fullname] = module
76 if fullname == "PyQt5.QtCore":
77 import PyQt4.QtGui
78 module.qInstallMessageHandler = module.qInstallMsgHandler
79 module.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel
80 module.QItemSelection = PyQt4.QtGui.QItemSelection
81 module.QSortFilterProxyModel = \
82 PyQt4.QtGui.QSortFilterProxyModel
83 module.QAbstractProxyModel = PyQt4.QtGui.QAbstractProxyModel
84 module.QStringListModel = PyQt4.QtGui.QStringListModel
85
86 PyQt4.QtGui.QComboBox.currentData = self.QComboBox_currentData
87 return module
88
89 try:
90 if "--pyqt4" in sys.argv:
91 sys.argv.remove("--pyqt4")
92 # fake a failed PyQt5 import
93 raise ImportError
94 import PyQt5 # __IGNORE_WARNING__
95 except ImportError:
96 sys.meta_path.insert(0, PyQt4Importer())
97
98 if sys.version_info[0] == 2:
99 try:
100 from PyQt5 import sip
101 except ImportError:
102 import sip
103 sip.setapi('QString', 2)
104 sip.setapi('QVariant', 2)
105 sip.setapi('QTextStream', 2)

eric ide

mercurial