PluginPySide2PyQt.py

changeset 0
4e3bb54c22d7
child 7
4a49d6b87459
equal deleted inserted replaced
-1:000000000000 0:4e3bb54c22d7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the PySide to PyQt4 (and vice versa) plug-in.
8 """
9
10 from __future__ import unicode_literals # __IGNORE_WARNING__
11
12 import os
13
14 from PyQt4.QtCore import QObject, QTranslator
15
16 from E5Gui.E5Application import e5App
17
18 # Start-Of-Header
19 name = "PySide to PyQt4 (and vice versa) Plug-in"
20 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
21 autoactivate = True
22 deactivateable = True
23 version = "0.1.0"
24 className = "PySide2PyQtPlugin"
25 packageName = "PySide2PyQt"
26 shortDescription = "Remove print() like debug statements."
27 longDescription = \
28 """This plug-in implements a tool to convert a PySide file""" \
29 """ to PyQt4 and vice versa. It works with the text of the""" \
30 """ current editor."""
31 needsRestart = False
32 pyqtApi = 2
33 # End-Of-Header
34
35 error = ""
36
37
38 class PySide2PyQtPlugin(QObject):
39 """
40 Class implementing the PySide to PyQt4 (and vice versa) plugin.
41 """
42 def __init__(self, ui):
43 """
44 Constructor
45
46 @param ui reference to the user interface object (UI.UserInterface)
47 """
48 QObject.__init__(self, ui)
49 self.__ui = ui
50
51 self.__translator = None
52 self.__loadTranslator()
53
54 def activate(self):
55 """
56 Public method to activate this plugin.
57
58 @return tuple of None and activation status (boolean)
59 """
60 global error
61 error = "" # clear previous error
62
63 self.__ui.showMenu.connect(self.__populateMenu)
64
65 return None, True
66
67 def deactivate(self):
68 """
69 Public method to deactivate this plugin.
70 """
71 self.__ui.showMenu.disconnect(self.__populateMenu)
72
73 def __loadTranslator(self):
74 """
75 Private method to load the translation file.
76 """
77 if self.__ui is not None:
78 loc = self.__ui.getLocale()
79 if loc and loc != "C":
80 locale_dir = os.path.join(
81 os.path.dirname(__file__), "PySide2PyQt", "i18n")
82 translation = "pyside2pyqt_{0}".format(loc)
83 translator = QTranslator(None)
84 loaded = translator.load(translation, locale_dir)
85 if loaded:
86 self.__translator = translator
87 e5App().installTranslator(self.__translator)
88 else:
89 print("Warning: translation file '{0}' could not be"
90 " loaded.".format(translation))
91 print("Using default.")
92
93 def __populateMenu(self, name, menu):
94 """
95 Private slot to populate the tools menu with our entries.
96
97 @param name name of the menu (string)
98 @param menu reference to the menu to be populated (QMenu)
99 """
100 if name != "Tools":
101 return
102
103 editor = e5App().getObject("ViewManager").activeWindow()
104 if editor is None:
105 return
106
107 if not menu.isEmpty():
108 menu.addSeparator()
109
110 menu.addAction(self.tr("PySide to PyQt4"), self.__pyside2Pyqt)
111 menu.addAction(self.tr("PyQt4 to PySide"), self.__pyqt2Pyside)
112
113 def __pyside2Pyqt(self):
114 """
115 Private slot to convert the code of the current editor from PySide
116 to PyQt4.
117 """
118 editor = e5App().getObject("ViewManager").activeWindow()
119 if editor is None:
120 return
121
122 text = editor.text()
123 newText = (text
124 .replace("PySide", "PyQt4")
125 .replace("Signal", "pyqtSignal")
126 .replace("Slot", "pyqtSlot")
127 .replace("Property", "pyqtProperty")
128 .replace("pyside-uic", "pyuic4")
129 .replace("pyside-rcc", "pyrcc4")
130 .replace("pyside-lupdate", "pylupdate4")
131 )
132 if newText != text:
133 editor.beginUndoAction()
134 editor.selectAll()
135 editor.replaceSelectedText(newText)
136 editor.endUndoAction()
137
138 def __pyqt2Pyside(self):
139 """
140 Private slot to convert the code of the current editor from PyQt4
141 to PySide.
142 """
143 editor = e5App().getObject("ViewManager").activeWindow()
144 if editor is None:
145 return
146
147 text = editor.text()
148 newText = (text
149 .replace("PyQt4", "PySide")
150 .replace("pyqtSignal", "Signal")
151 .replace("pyqtSlot", "Slot")
152 .replace("pyqtProperty", "Property")
153 .replace("pyuic4", "pyside-uic")
154 .replace("pyrcc4", "pyside-rcc")
155 .replace("pylupdate4", "pyside-lupdate")
156 )
157 if newText != text:
158 editor.beginUndoAction()
159 editor.selectAll()
160 editor.replaceSelectedText(newText)
161 editor.endUndoAction()

eric ide

mercurial