PluginPySide2PyQt.py

changeset 24
edefb5877e13
parent 21
757e8b5d9f5a
child 29
0083833659d1
equal deleted inserted replaced
22:d793a80af5ed 24:edefb5877e13
2 2
3 # Copyright (c) 2013 - 2014 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2013 - 2014 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing the PySide to PyQt4 (and vice versa) plug-in. 7 Module implementing the PySide to PyQt (and vice versa) plug-in.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import os 12 import os
13 13
14 from PyQt4.QtCore import QObject, QTranslator 14 from PyQt5.QtCore import QObject, QTranslator
15 from PyQt5.QtWidgets import QMenu
15 16
16 from E5Gui.E5Application import e5App 17 from E5Gui.E5Application import e5App
17 18
18 # Start-Of-Header 19 # Start-Of-Header
19 name = "PySide to PyQt4 (and vice versa) Plug-in" 20 name = "PySide to PyQt (and vice versa) Plug-in"
20 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 21 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
21 autoactivate = True 22 autoactivate = True
22 deactivateable = True 23 deactivateable = True
23 version = "1.0.0" 24 version = "2.0.0"
24 className = "PySide2PyQtPlugin" 25 className = "PySide2PyQtPlugin"
25 packageName = "PySide2PyQt" 26 packageName = "PySide2PyQt"
26 shortDescription = "Convert PySide file to PyQt4 and vice versa" 27 shortDescription = "Convert PySide file to PyQt and vice versa"
27 longDescription = \ 28 longDescription = \
28 """This plug-in implements a tool to convert a PySide file""" \ 29 """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 """ to PyQt4 or PyQt5 and vice versa. It works with the text of the""" \
30 """ current editor.""" 31 """ current editor."""
31 needsRestart = False 32 needsRestart = False
32 pyqtApi = 2 33 pyqtApi = 2
33 python2Compatible = True 34 python2Compatible = True
34 # End-Of-Header 35 # End-Of-Header
36 error = "" 37 error = ""
37 38
38 39
39 class PySide2PyQtPlugin(QObject): 40 class PySide2PyQtPlugin(QObject):
40 """ 41 """
41 Class implementing the PySide to PyQt4 (and vice versa) plugin. 42 Class implementing the PySide to PyQt (and vice versa) plugin.
42 """ 43 """
43 def __init__(self, ui): 44 def __init__(self, ui):
44 """ 45 """
45 Constructor 46 Constructor
46 47
49 QObject.__init__(self, ui) 50 QObject.__init__(self, ui)
50 self.__ui = ui 51 self.__ui = ui
51 52
52 self.__translator = None 53 self.__translator = None
53 self.__loadTranslator() 54 self.__loadTranslator()
55
56 self.__initMenu()
54 57
55 self.__editors = {} 58 self.__editors = {}
56 59
57 def activate(self): 60 def activate(self):
58 """ 61 """
111 else: 114 else:
112 print("Warning: translation file '{0}' could not be" 115 print("Warning: translation file '{0}' could not be"
113 " loaded.".format(translation)) 116 " loaded.".format(translation))
114 print("Using default.") 117 print("Using default.")
115 118
119 def __initMenu(self):
120 """
121 Private method to initialize the menu.
122 """
123 self.__menu = QMenu(self.tr("PySide to/from PyQt"))
124 self.__menu.addAction(self.tr("PySide to PyQt4"), self.__pyside2Pyqt)\
125 .setData("pyqt4")
126 self.__menu.addAction(self.tr("PySide to PyQt5"), self.__pyside2Pyqt)\
127 .setData("pyqt5")
128 self.__menu.addAction(self.tr("PyQt4 to PySide"), self.__pyqt2Pyside)\
129 .setData("pyqt4")
130 self.__menu.addAction(self.tr("PyQt5 to PySide"), self.__pyqt2Pyside)\
131 .setData("pyqt5")
132 self.__menu.setEnabled(False)
133
116 def __populateMenu(self, name, menu): 134 def __populateMenu(self, name, menu):
117 """ 135 """
118 Private slot to populate the tools menu with our entries. 136 Private slot to populate the tools menu with our entries.
119 137
120 @param name name of the menu (string) 138 @param name name of the menu (string)
126 editor = e5App().getObject("ViewManager").activeWindow() 144 editor = e5App().getObject("ViewManager").activeWindow()
127 145
128 if not menu.isEmpty(): 146 if not menu.isEmpty():
129 menu.addSeparator() 147 menu.addSeparator()
130 148
131 menu.addAction(self.tr("PySide to PyQt4"), self.__pyside2Pyqt)\ 149 act = menu.addMenu(self.__menu)
132 .setEnabled(editor is not None) 150 act.setEnabled(editor is not None)
133 menu.addAction(self.tr("PyQt4 to PySide"), self.__pyqt2Pyside)\
134 .setEnabled(editor is not None)
135 151
136 def __editorOpened(self, editor): 152 def __editorOpened(self, editor):
137 """ 153 """
138 Private slot called, when a new editor was opened. 154 Private slot called, when a new editor was opened.
139 155
143 if menu is not None: 159 if menu is not None:
144 self.__editors[editor] = [] 160 self.__editors[editor] = []
145 if not menu.isEmpty(): 161 if not menu.isEmpty():
146 act = menu.addSeparator() 162 act = menu.addSeparator()
147 self.__editors[editor].append(act) 163 self.__editors[editor].append(act)
148 act = menu.addAction(self.tr("PySide to PyQt4"), 164 act = menu.addMenu(self.__menu)
149 self.__pyside2Pyqt)
150 self.__editors[editor].append(act) 165 self.__editors[editor].append(act)
151 act = menu.addAction(self.tr("PyQt4 to PySide"), 166 self.__menu.setEnabled(True)
152 self.__pyqt2Pyside)
153 self.__editors[editor].append(act)
154 167
155 def __editorClosed(self, editor): 168 def __editorClosed(self, editor):
156 """ 169 """
157 Private slot called, when an editor was closed. 170 Private slot called, when an editor was closed.
158 171
159 @param editor reference to the editor (QScintilla.Editor) 172 @param editor reference to the editor (QScintilla.Editor)
160 """ 173 """
161 try: 174 try:
162 del self.__editors[editor] 175 del self.__editors[editor]
176 if not self.__editors:
177 self.__menu.setEnabled(False)
163 except KeyError: 178 except KeyError:
164 pass 179 pass
165 180
166 def __pyside2Pyqt(self): 181 def __pyside2Pyqt(self):
167 """ 182 """
168 Private slot to convert the code of the current editor from PySide 183 Private slot to convert the code of the current editor from PySide
169 to PyQt4. 184 to PyQt.
170 """ 185 """
171 editor = e5App().getObject("ViewManager").activeWindow() 186 editor = e5App().getObject("ViewManager").activeWindow()
172 if editor is None: 187 if editor is None:
173 return 188 return
174 189
190 act = self.sender()
191 if act is None:
192 return
193
175 text = editor.text() 194 text = editor.text()
176 newText = (text 195 pyqt = act.data()
177 .replace("PySide", "PyQt4") 196 if pyqt == "pyqt4":
178 .replace("Signal", "pyqtSignal") 197 newText = (text
179 .replace("Slot", "pyqtSlot") 198 .replace("PySide", "PyQt4")
180 .replace("Property", "pyqtProperty") 199 .replace("Signal", "pyqtSignal")
181 .replace("pyside-uic", "pyuic4") 200 .replace("Slot", "pyqtSlot")
182 .replace("pyside-rcc", "pyrcc4") 201 .replace("Property", "pyqtProperty")
183 .replace("pyside-lupdate", "pylupdate4") 202 .replace("pyside-uic", "pyuic4")
184 ) 203 .replace("pyside-rcc", "pyrcc4")
204 .replace("pyside-lupdate", "pylupdate4")
205 )
206 elif pyqt == "pyqt5":
207 # Note: this code does no Qt4 to Qt5 conversion
208 newText = (text
209 .replace("PySide", "PyQt5")
210 .replace("Signal", "pyqtSignal")
211 .replace("Slot", "pyqtSlot")
212 .replace("Property", "pyqtProperty")
213 .replace("pyside-uic", "pyuic5")
214 .replace("pyside-rcc", "pyrcc5")
215 .replace("pyside-lupdate", "pylupdate5")
216 )
217 else:
218 return
219
185 if newText != text: 220 if newText != text:
186 editor.beginUndoAction() 221 editor.beginUndoAction()
187 editor.selectAll() 222 editor.selectAll()
188 editor.replaceSelectedText(newText) 223 editor.replaceSelectedText(newText)
189 editor.endUndoAction() 224 editor.endUndoAction()
190 225
191 def __pyqt2Pyside(self): 226 def __pyqt2Pyside(self):
192 """ 227 """
193 Private slot to convert the code of the current editor from PyQt4 228 Private slot to convert the code of the current editor from PyQt
194 to PySide. 229 to PySide.
195 """ 230 """
196 editor = e5App().getObject("ViewManager").activeWindow() 231 editor = e5App().getObject("ViewManager").activeWindow()
197 if editor is None: 232 if editor is None:
198 return 233 return
199 234
235 act = self.sender()
236 if act is None:
237 return
238
200 text = editor.text() 239 text = editor.text()
201 newText = (text 240 pyqt = act.data()
202 .replace("PyQt4", "PySide") 241 if pyqt == "pyqt4":
203 .replace("pyqtSignal", "Signal") 242 newText = (text
204 .replace("pyqtSlot", "Slot") 243 .replace("PyQt4", "PySide")
205 .replace("pyqtProperty", "Property") 244 .replace("pyqtSignal", "Signal")
206 .replace("pyuic4", "pyside-uic") 245 .replace("pyqtSlot", "Slot")
207 .replace("pyrcc4", "pyside-rcc") 246 .replace("pyqtProperty", "Property")
208 .replace("pylupdate4", "pyside-lupdate") 247 .replace("pyuic4", "pyside-uic")
209 ) 248 .replace("pyrcc4", "pyside-rcc")
249 .replace("pylupdate4", "pyside-lupdate")
250 )
251 elif pyqt == "pyqt5":
252 # Note: this code does no Qt4 to Qt5 conversion
253 newText = (text
254 .replace("PyQt5", "PySide")
255 .replace("pyqtSignal", "Signal")
256 .replace("pyqtSlot", "Slot")
257 .replace("pyqtProperty", "Property")
258 .replace("pyuic5", "pyside-uic")
259 .replace("pyrcc5", "pyside-rcc")
260 .replace("pylupdate5", "pyside-lupdate")
261 )
262 else:
263 return
264
210 if newText != text: 265 if newText != text:
211 editor.beginUndoAction() 266 editor.beginUndoAction()
212 editor.selectAll() 267 editor.selectAll()
213 editor.replaceSelectedText(newText) 268 editor.replaceSelectedText(newText)
214 editor.endUndoAction() 269 editor.endUndoAction()

eric ide

mercurial