PluginPySide2PyQt.py

changeset 0
4e3bb54c22d7
child 7
4a49d6b87459
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PluginPySide2PyQt.py	Wed Dec 18 15:48:49 2013 +0100
@@ -0,0 +1,161 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the PySide to PyQt4 (and vice versa) plug-in.
+"""
+
+from __future__ import unicode_literals    # __IGNORE_WARNING__
+
+import os
+
+from PyQt4.QtCore import QObject, QTranslator
+
+from E5Gui.E5Application import e5App
+
+# Start-Of-Header
+name = "PySide to PyQt4 (and vice versa) Plug-in"
+author = "Detlev Offenbach <detlev@die-offenbachs.de>"
+autoactivate = True
+deactivateable = True
+version = "0.1.0"
+className = "PySide2PyQtPlugin"
+packageName = "PySide2PyQt"
+shortDescription = "Remove print() like debug statements."
+longDescription = \
+    """This plug-in implements a tool to convert a PySide file""" \
+    """ to PyQt4 and vice versa. It works with the text of the""" \
+    """ current editor."""
+needsRestart = False
+pyqtApi = 2
+# End-Of-Header
+
+error = ""
+
+
+class PySide2PyQtPlugin(QObject):
+    """
+    Class implementing the PySide to PyQt4 (and vice versa) plugin.
+    """
+    def __init__(self, ui):
+        """
+        Constructor
+        
+        @param ui reference to the user interface object (UI.UserInterface)
+        """
+        QObject.__init__(self, ui)
+        self.__ui = ui
+        
+        self.__translator = None
+        self.__loadTranslator()
+    
+    def activate(self):
+        """
+        Public method to activate this plugin.
+        
+        @return tuple of None and activation status (boolean)
+        """
+        global error
+        error = ""     # clear previous error
+        
+        self.__ui.showMenu.connect(self.__populateMenu)
+        
+        return None, True
+    
+    def deactivate(self):
+        """
+        Public method to deactivate this plugin.
+        """
+        self.__ui.showMenu.disconnect(self.__populateMenu)
+    
+    def __loadTranslator(self):
+        """
+        Private method to load the translation file.
+        """
+        if self.__ui is not None:
+            loc = self.__ui.getLocale()
+            if loc and loc != "C":
+                locale_dir = os.path.join(
+                    os.path.dirname(__file__), "PySide2PyQt", "i18n")
+                translation = "pyside2pyqt_{0}".format(loc)
+                translator = QTranslator(None)
+                loaded = translator.load(translation, locale_dir)
+                if loaded:
+                    self.__translator = translator
+                    e5App().installTranslator(self.__translator)
+                else:
+                    print("Warning: translation file '{0}' could not be"
+                          " loaded.".format(translation))
+                    print("Using default.")
+    
+    def __populateMenu(self, name, menu):
+        """
+        Private slot to populate the tools menu with our entries.
+        
+        @param name name of the menu (string)
+        @param menu reference to the menu to be populated (QMenu)
+        """
+        if name != "Tools":
+            return
+        
+        editor = e5App().getObject("ViewManager").activeWindow()
+        if editor is None:
+            return
+        
+        if not menu.isEmpty():
+            menu.addSeparator()
+        
+        menu.addAction(self.tr("PySide to PyQt4"), self.__pyside2Pyqt)
+        menu.addAction(self.tr("PyQt4 to PySide"), self.__pyqt2Pyside)
+    
+    def __pyside2Pyqt(self):
+        """
+        Private slot to convert the code of the current editor from PySide
+        to PyQt4.
+        """
+        editor = e5App().getObject("ViewManager").activeWindow()
+        if editor is None:
+            return
+        
+        text = editor.text()
+        newText = (text
+                   .replace("PySide", "PyQt4")
+                   .replace("Signal", "pyqtSignal")
+                   .replace("Slot", "pyqtSlot")
+                   .replace("Property", "pyqtProperty")
+                   .replace("pyside-uic", "pyuic4")
+                   .replace("pyside-rcc", "pyrcc4")
+                   .replace("pyside-lupdate", "pylupdate4")
+                   )
+        if newText != text:
+            editor.beginUndoAction()
+            editor.selectAll()
+            editor.replaceSelectedText(newText)
+            editor.endUndoAction()
+    
+    def __pyqt2Pyside(self):
+        """
+        Private slot to convert the code of the current editor from PyQt4
+        to PySide.
+        """
+        editor = e5App().getObject("ViewManager").activeWindow()
+        if editor is None:
+            return
+        
+        text = editor.text()
+        newText = (text
+                   .replace("PyQt4", "PySide")
+                   .replace("pyqtSignal", "Signal")
+                   .replace("pyqtSlot", "Slot")
+                   .replace("pyqtProperty", "Property")
+                   .replace("pyuic4", "pyside-uic")
+                   .replace("pyrcc4", "pyside-rcc")
+                   .replace("pylupdate4", "pyside-lupdate")
+                   )
+        if newText != text:
+            editor.beginUndoAction()
+            editor.selectAll()
+            editor.replaceSelectedText(newText)
+            editor.endUndoAction()

eric ide

mercurial