--- a/PluginPySide2PyQt.py Sat Apr 26 15:53:30 2014 +0200 +++ b/PluginPySide2PyQt.py Sat Jul 12 14:55:23 2014 +0200 @@ -4,29 +4,30 @@ # """ -Module implementing the PySide to PyQt4 (and vice versa) plug-in. +Module implementing the PySide to PyQt (and vice versa) plug-in. """ from __future__ import unicode_literals import os -from PyQt4.QtCore import QObject, QTranslator +from PyQt5.QtCore import QObject, QTranslator +from PyQt5.QtWidgets import QMenu from E5Gui.E5Application import e5App # Start-Of-Header -name = "PySide to PyQt4 (and vice versa) Plug-in" +name = "PySide to PyQt (and vice versa) Plug-in" author = "Detlev Offenbach <detlev@die-offenbachs.de>" autoactivate = True deactivateable = True -version = "1.0.0" +version = "2.0.0" className = "PySide2PyQtPlugin" packageName = "PySide2PyQt" -shortDescription = "Convert PySide file to PyQt4 and vice versa" +shortDescription = "Convert PySide file to PyQt and vice versa" longDescription = \ """This plug-in implements a tool to convert a PySide file""" \ - """ to PyQt4 and vice versa. It works with the text of the""" \ + """ to PyQt4 or PyQt5 and vice versa. It works with the text of the""" \ """ current editor.""" needsRestart = False pyqtApi = 2 @@ -38,7 +39,7 @@ class PySide2PyQtPlugin(QObject): """ - Class implementing the PySide to PyQt4 (and vice versa) plugin. + Class implementing the PySide to PyQt (and vice versa) plugin. """ def __init__(self, ui): """ @@ -52,6 +53,8 @@ self.__translator = None self.__loadTranslator() + self.__initMenu() + self.__editors = {} def activate(self): @@ -113,6 +116,21 @@ " loaded.".format(translation)) print("Using default.") + def __initMenu(self): + """ + Private method to initialize the menu. + """ + self.__menu = QMenu(self.tr("PySide to/from PyQt")) + self.__menu.addAction(self.tr("PySide to PyQt4"), self.__pyside2Pyqt)\ + .setData("pyqt4") + self.__menu.addAction(self.tr("PySide to PyQt5"), self.__pyside2Pyqt)\ + .setData("pyqt5") + self.__menu.addAction(self.tr("PyQt4 to PySide"), self.__pyqt2Pyside)\ + .setData("pyqt4") + self.__menu.addAction(self.tr("PyQt5 to PySide"), self.__pyqt2Pyside)\ + .setData("pyqt5") + self.__menu.setEnabled(False) + def __populateMenu(self, name, menu): """ Private slot to populate the tools menu with our entries. @@ -128,10 +146,8 @@ if not menu.isEmpty(): menu.addSeparator() - menu.addAction(self.tr("PySide to PyQt4"), self.__pyside2Pyqt)\ - .setEnabled(editor is not None) - menu.addAction(self.tr("PyQt4 to PySide"), self.__pyqt2Pyside)\ - .setEnabled(editor is not None) + act = menu.addMenu(self.__menu) + act.setEnabled(editor is not None) def __editorOpened(self, editor): """ @@ -145,12 +161,9 @@ if not menu.isEmpty(): act = menu.addSeparator() self.__editors[editor].append(act) - act = menu.addAction(self.tr("PySide to PyQt4"), - self.__pyside2Pyqt) + act = menu.addMenu(self.__menu) self.__editors[editor].append(act) - act = menu.addAction(self.tr("PyQt4 to PySide"), - self.__pyqt2Pyside) - self.__editors[editor].append(act) + self.__menu.setEnabled(True) def __editorClosed(self, editor): """ @@ -160,28 +173,50 @@ """ try: del self.__editors[editor] + if not self.__editors: + self.__menu.setEnabled(False) except KeyError: pass def __pyside2Pyqt(self): """ Private slot to convert the code of the current editor from PySide - to PyQt4. + to PyQt. """ editor = e5App().getObject("ViewManager").activeWindow() if editor is None: return + act = self.sender() + if act 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") - ) + pyqt = act.data() + if pyqt == "pyqt4": + 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") + ) + elif pyqt == "pyqt5": + # Note: this code does no Qt4 to Qt5 conversion + newText = (text + .replace("PySide", "PyQt5") + .replace("Signal", "pyqtSignal") + .replace("Slot", "pyqtSlot") + .replace("Property", "pyqtProperty") + .replace("pyside-uic", "pyuic5") + .replace("pyside-rcc", "pyrcc5") + .replace("pyside-lupdate", "pylupdate5") + ) + else: + return + if newText != text: editor.beginUndoAction() editor.selectAll() @@ -190,23 +225,43 @@ def __pyqt2Pyside(self): """ - Private slot to convert the code of the current editor from PyQt4 + Private slot to convert the code of the current editor from PyQt to PySide. """ editor = e5App().getObject("ViewManager").activeWindow() if editor is None: return + act = self.sender() + if act 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") - ) + pyqt = act.data() + if pyqt == "pyqt4": + 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") + ) + elif pyqt == "pyqt5": + # Note: this code does no Qt4 to Qt5 conversion + newText = (text + .replace("PyQt5", "PySide") + .replace("pyqtSignal", "Signal") + .replace("pyqtSlot", "Slot") + .replace("pyqtProperty", "Property") + .replace("pyuic5", "pyside-uic") + .replace("pyrcc5", "pyside-rcc") + .replace("pylupdate5", "pyside-lupdate") + ) + else: + return + if newText != text: editor.beginUndoAction() editor.selectAll()