Refactored the PyQt4Importer.

Thu, 20 Jul 2017 18:42:33 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 20 Jul 2017 18:42:33 +0200
changeset 5814
50b299f6eb98
parent 5813
82349b9ceb4d
child 5815
223fa32b3a32

Refactored the PyQt4Importer.

Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Source/eric6.Toolbox.PyQt4ImportHook.html file | annotate | diff | comparison | revisions
Toolbox/PyQt4ImportHook.py file | annotate | diff | comparison | revisions
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Source/eric6.Toolbox.PyQt4ImportHook.html	Wed Jul 19 20:16:25 2017 +0200
+++ b/Documentation/Source/eric6.Toolbox.PyQt4ImportHook.html	Thu Jul 20 18:42:33 2017 +0200
@@ -42,8 +42,8 @@
 <a NAME="PyQt4Importer" ID="PyQt4Importer"></a>
 <h2>PyQt4Importer</h2>
 <p>
-        Class implementing an importer converting PyQt5 imports to PyQt4
-        imports.
+    Class implementing an importer converting PyQt5 imports to PyQt4
+    imports.
 </p>
 <h3>Derived from</h3>
 object
@@ -79,12 +79,12 @@
 <h4>PyQt4Importer (Constructor)</h4>
 <b>PyQt4Importer</b>(<i></i>)
 <p>
-            Constructor
+        Constructor
 </p><a NAME="PyQt4Importer.find_module" ID="PyQt4Importer.find_module"></a>
 <h4>PyQt4Importer.find_module</h4>
 <b>find_module</b>(<i>fullname, path=None</i>)
 <p>
-            Public method returning the module loader.
+        Public method returning the module loader.
 </p><dl>
 <dt><i>fullname</i></dt>
 <dd>
@@ -102,7 +102,7 @@
 <h4>PyQt4Importer.load_module</h4>
 <b>load_module</b>(<i>fullname</i>)
 <p>
-            Public method to load a module.
+        Public method to load a module.
 </p><dl>
 <dt><i>fullname</i></dt>
 <dd>
@@ -117,7 +117,7 @@
 <h4>PyQt4Importer.QComboBox_currentData (static)</h4>
 <b>QComboBox_currentData</b>(<i>role=None</i>)
 <p>
-            Public method to emulate the currentData method of Qt5.
+        Public method to emulate the currentData method of Qt5.
 </p><dl>
 <dt><i>role</i></dt>
 <dd>
--- a/Toolbox/PyQt4ImportHook.py	Wed Jul 19 20:16:25 2017 +0200
+++ b/Toolbox/PyQt4ImportHook.py	Thu Jul 20 18:42:33 2017 +0200
@@ -10,6 +10,77 @@
 from __future__ import unicode_literals
 
 import sys
+import importlib
+
+class PyQt4Importer(object):
+    """
+    Class implementing an importer converting PyQt5 imports to PyQt4
+    imports.
+    """
+    def __init__(self):
+        """
+        Constructor
+        """
+        self.__path = None
+    
+    def find_module(self, fullname, path=None):
+        """
+        Public method returning the module loader.
+        
+        @param fullname name of the module to be loaded (string)
+        @param path path to resolve the module name (string)
+        @return module loader object
+        """
+        if fullname.startswith("PyQt5"):
+            self.__path = path
+            return self
+        
+        return None
+    
+    @staticmethod
+    def QComboBox_currentData(other, role=None):
+        """
+        Public method to emulate the currentData method of Qt5.
+        
+        @param role role of which data should be retrived
+        @return stored data at current selection
+        """
+        import PyQt4.Qt
+        if role is None:
+            role = PyQt4.Qt.Qt.UserRole
+        
+        idx = other.currentIndex()
+        return other.itemData(idx, role)
+    
+    def load_module(self, fullname):
+        """
+        Public method to load a module.
+        
+        @param fullname name of the module to be loaded (string)
+        @return reference to the loaded module (module)
+        """
+        if fullname in ["PyQt5.QtWidgets", "PyQt5.QtPrintSupport"]:
+            newname = "PyQt4.QtGui"
+        elif fullname in ["PyQt5.QtWebKitWidgets"]:
+            newname = "PyQt4.QtWebKit"
+        else:
+            newname = fullname.replace("PyQt5", "PyQt4")
+        
+        module = importlib.import_module(newname)
+        sys.modules[fullname] = module
+        if fullname == "PyQt5.QtCore":
+            import PyQt4.QtGui
+            module.qInstallMessageHandler = module.qInstallMsgHandler
+            module.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel
+            module.QItemSelection = PyQt4.QtGui.QItemSelection
+            module.QSortFilterProxyModel = \
+                PyQt4.QtGui.QSortFilterProxyModel
+            module.QAbstractProxyModel = PyQt4.QtGui.QAbstractProxyModel
+            module.QStringListModel = PyQt4.QtGui.QStringListModel
+            
+            PyQt4.QtGui.QComboBox.currentData = self.QComboBox_currentData
+        return module
+
 try:
     if "--pyqt4" in sys.argv:
         sys.argv.remove("--pyqt4")
@@ -17,77 +88,6 @@
         raise ImportError
     import PyQt5    # __IGNORE_WARNING__
 except ImportError:
-    import importlib
-
-    class PyQt4Importer(object):
-        """
-        Class implementing an importer converting PyQt5 imports to PyQt4
-        imports.
-        """
-        def __init__(self):
-            """
-            Constructor
-            """
-            self.__path = None
-        
-        def find_module(self, fullname, path=None):
-            """
-            Public method returning the module loader.
-            
-            @param fullname name of the module to be loaded (string)
-            @param path path to resolve the module name (string)
-            @return module loader object
-            """
-            if fullname.startswith("PyQt5"):
-                self.__path = path
-                return self
-            
-            return None
-        
-        @staticmethod
-        def QComboBox_currentData(other, role=None):
-            """
-            Public method to emulate the currentData method of Qt5.
-            
-            @param role role of which data should be retrived
-            @return stored data at current selection
-            """
-            import PyQt4.Qt
-            if role is None:
-                role = PyQt4.Qt.Qt.UserRole
-            
-            idx = other.currentIndex()
-            return other.itemData(idx, role)
-        
-        def load_module(self, fullname):
-            """
-            Public method to load a module.
-            
-            @param fullname name of the module to be loaded (string)
-            @return reference to the loaded module (module)
-            """
-            if fullname in ["PyQt5.QtWidgets", "PyQt5.QtPrintSupport"]:
-                newname = "PyQt4.QtGui"
-            elif fullname in ["PyQt5.QtWebKitWidgets"]:
-                newname = "PyQt4.QtWebKit"
-            else:
-                newname = fullname.replace("PyQt5", "PyQt4")
-            
-            module = importlib.import_module(newname)
-            sys.modules[fullname] = module
-            if fullname == "PyQt5.QtCore":
-                import PyQt4.QtGui
-                module.qInstallMessageHandler = module.qInstallMsgHandler
-                module.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel
-                module.QItemSelection = PyQt4.QtGui.QItemSelection
-                module.QSortFilterProxyModel = \
-                    PyQt4.QtGui.QSortFilterProxyModel
-                module.QAbstractProxyModel = PyQt4.QtGui.QAbstractProxyModel
-                module.QStringListModel = PyQt4.QtGui.QStringListModel
-                
-                PyQt4.QtGui.QComboBox.currentData = self.QComboBox_currentData
-            return module
-
     sys.meta_path.insert(0, PyQt4Importer())
     
     if sys.version_info[0] == 2:

eric ide

mercurial