PluginPySide2PyQt.py

changeset 50
5e4b312590ba
parent 49
406f0965f449
child 52
c88593752c63
equal deleted inserted replaced
49:406f0965f449 50:5e4b312590ba
4 # 4 #
5 5
6 """ 6 """
7 Module implementing the PySide to PyQt (and vice versa) plug-in. 7 Module implementing the PySide to PyQt (and vice versa) plug-in.
8 """ 8 """
9
10 from __future__ import unicode_literals
11 9
12 import os 10 import os
13 11
14 from PyQt5.QtCore import QObject, QTranslator 12 from PyQt5.QtCore import QObject, QTranslator
15 from PyQt5.QtWidgets import QMenu 13 from PyQt5.QtWidgets import QMenu
19 # Start-Of-Header 17 # Start-Of-Header
20 name = "PySide to PyQt (and vice versa) Plug-in" 18 name = "PySide to PyQt (and vice versa) Plug-in"
21 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 19 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
22 autoactivate = True 20 autoactivate = True
23 deactivateable = True 21 deactivateable = True
24 version = "2.2.1" 22 version = "3.0.0"
25 className = "PySide2PyQtPlugin" 23 className = "PySide2PyQtPlugin"
26 packageName = "PySide2PyQt" 24 packageName = "PySide2PyQt"
27 shortDescription = "Convert PySide file to PyQt and vice versa" 25 shortDescription = "Convert PySide file to PyQt and vice versa"
28 longDescription = \ 26 longDescription = (
29 """This plug-in implements a tool to convert a PySide/PySide2 file""" \ 27 """This plug-in implements a tool to convert a PySide2 file to PyQt5"""
30 """ to PyQt4 or PyQt5 and vice versa. It works with the text of the""" \ 28 """ and vice versa. It works with the text of the current editor."""
31 """ current editor.""" 29 )
32 needsRestart = False 30 needsRestart = False
33 pyqtApi = 2 31 pyqtApi = 2
34 python2Compatible = True
35 # End-Of-Header 32 # End-Of-Header
36 33
37 error = "" 34 error = ""
38 35
39 36
135 def __initMenu(self): 132 def __initMenu(self):
136 """ 133 """
137 Private method to initialize the menu. 134 Private method to initialize the menu.
138 """ 135 """
139 self.__menu = QMenu(self.tr("PySide to/from PyQt")) 136 self.__menu = QMenu(self.tr("PySide to/from PyQt"))
140 self.__menu.addAction(self.tr("PySide to PyQt4"),
141 lambda: self.__pyside2Pyqt("pyside", "pyqt4"))
142 self.__menu.addAction(self.tr("PySide to PyQt5"),
143 lambda: self.__pyside2Pyqt("pyside", "pyqt5"))
144 self.__menu.addAction(self.tr("PySide2 to PyQt5"), 137 self.__menu.addAction(self.tr("PySide2 to PyQt5"),
145 lambda: self.__pyside2Pyqt("pyside2", "pyqt5")) 138 lambda: self.__pyside2Pyqt("pyside2", "pyqt5"))
146 self.__menu.addSeparator() 139 self.__menu.addSeparator()
147 self.__menu.addAction(self.tr("PyQt4 to PySide"),
148 lambda: self.__pyqt2Pyside("pyqt4", "pyside"))
149 self.__menu.addAction(self.tr("PyQt5 to PySide2"), 140 self.__menu.addAction(self.tr("PyQt5 to PySide2"),
150 lambda: self.__pyqt2Pyside("pyqt5", "pyside2")) 141 lambda: self.__pyqt2Pyside("pyqt5", "pyside2"))
151 self.__menu.addAction(self.tr("PyQt5 to PySide"),
152 lambda: self.__pyqt2Pyside("pyqt5", "pyside"))
153 self.__menu.setEnabled(False) 142 self.__menu.setEnabled(False)
154 143
155 def __populateMenu(self, name, menu): 144 def __populateMenu(self, name, menu):
156 """ 145 """
157 Private slot to populate the tools menu with our entries. 146 Private slot to populate the tools menu with our entries.
224 def __pyside2Pyqt(self, pyside, pyqt): 213 def __pyside2Pyqt(self, pyside, pyqt):
225 """ 214 """
226 Private slot to convert the code of the current editor from PySide 215 Private slot to convert the code of the current editor from PySide
227 to PyQt. 216 to PyQt.
228 217
229 @param pyside PySide variant (pyside or pyside2) 218 @param pyside PySide variant (pyside2)
230 @type str 219 @type str
231 @param pyqt PyQt variant (pyqt4 or pyqt5) 220 @param pyqt PyQt variant (pyqt5)
232 @type str 221 @type str
233 """ 222 """
234 editor = e5App().getObject("ViewManager").activeWindow() 223 editor = e5App().getObject("ViewManager").activeWindow()
235 if editor is None: 224 if editor is None:
236 return 225 return
237 226
238 text = editor.text() 227 text = editor.text()
239 if pyqt == "pyqt4": 228 if pyqt == "pyqt5" and pyside == "pyside2":
240 newText = (text 229 newText = (
241 .replace("PySide", "PyQt4") 230 text
242 .replace("Signal", "pyqtSignal") 231 .replace("Signal", "pyqtSignal")
243 .replace("Slot", "pyqtSlot") 232 .replace("Slot", "pyqtSlot")
244 .replace("Property", "pyqtProperty") 233 .replace("Property", "pyqtProperty")
245 .replace("pyside-uic", "pyuic4") 234 .replace("PySide2", "PyQt5")
246 .replace("pyside-rcc", "pyrcc4") 235 .replace("pyside2-uic", "pyuic5")
247 .replace("pyside-lupdate", "pylupdate4") 236 .replace("pyside2-rcc", "pyrcc5")
248 ) 237 .replace("pyside2-lupdate", "pylupdate5")
249 elif pyqt == "pyqt5": 238 )
250 newText = (text
251 .replace("Signal", "pyqtSignal")
252 .replace("Slot", "pyqtSlot")
253 .replace("Property", "pyqtProperty")
254 .replace("PySide2", "PyQt5")
255 .replace("pyside2-uic", "pyuic5")
256 .replace("pyside2-rcc", "pyrcc5")
257 .replace("pyside2-lupdate", "pylupdate5")
258 .replace("PySide", "PyQt5")
259 .replace("pyside-uic", "pyuic5")
260 .replace("pyside-rcc", "pyrcc5")
261 .replace("pyside-lupdate", "pylupdate5")
262 )
263 else: 239 else:
264 return 240 return
265 241
266 if newText != text: 242 if newText != text:
267 editor.beginUndoAction() 243 editor.beginUndoAction()
272 def __pyqt2Pyside(self, pyqt, pyside): 248 def __pyqt2Pyside(self, pyqt, pyside):
273 """ 249 """
274 Private slot to convert the code of the current editor from PyQt 250 Private slot to convert the code of the current editor from PyQt
275 to PySide. 251 to PySide.
276 252
277 @param pyqt PyQt variant (pyqt4 or pyqt5) 253 @param pyqt PyQt variant (pyqt5)
278 @type str 254 @type str
279 @param pyside PySide variant (pyside or pyside2) 255 @param pyside PySide variant (pyside2)
280 @type str 256 @type str
281 """ 257 """
282 editor = e5App().getObject("ViewManager").activeWindow() 258 editor = e5App().getObject("ViewManager").activeWindow()
283 if editor is None: 259 if editor is None:
284 return 260 return
285 261
286 text = editor.text() 262 text = editor.text()
287 if pyqt == "pyqt4": 263 if pyqt == "pyqt5" and pyside == "pyside2":
288 newText = (text 264 newText = (
289 .replace("PyQt4", "PySide") 265 text
290 .replace("pyqtSignal", "Signal") 266 .replace("PyQt5", "PySide2")
291 .replace("pyqtSlot", "Slot") 267 .replace("pyqtSignal", "Signal")
292 .replace("pyqtProperty", "Property") 268 .replace("pyqtSlot", "Slot")
293 .replace("pyuic4", "pyside-uic") 269 .replace("pyqtProperty", "Property")
294 .replace("pyrcc4", "pyside-rcc") 270 .replace("pyuic5", "pyside2-uic")
295 .replace("pylupdate4", "pyside-lupdate") 271 .replace("pyrcc5", "pyside2-rcc")
296 ) 272 .replace("pylupdate5", "pyside2-lupdate")
297 elif pyqt == "pyqt5" and pyside == "pyside": 273 )
298 newText = (text
299 .replace("PyQt5", "PySide")
300 .replace("pyqtSignal", "Signal")
301 .replace("pyqtSlot", "Slot")
302 .replace("pyqtProperty", "Property")
303 .replace("pyuic5", "pyside-uic")
304 .replace("pyrcc5", "pyside-rcc")
305 .replace("pylupdate5", "pyside-lupdate")
306 )
307 elif pyqt == "pyqt5" and pyside == "pyside2":
308 newText = (text
309 .replace("PyQt5", "PySide2")
310 .replace("pyqtSignal", "Signal")
311 .replace("pyqtSlot", "Slot")
312 .replace("pyqtProperty", "Property")
313 .replace("pyuic5", "pyside2-uic")
314 .replace("pyrcc5", "pyside2-rcc")
315 .replace("pylupdate5", "pyside2-lupdate")
316 )
317 else: 274 else:
318 return 275 return
319 276
320 if newText != text: 277 if newText != text:
321 editor.beginUndoAction() 278 editor.beginUndoAction()

eric ide

mercurial