PluginPySide2PyQt.py

changeset 43
25977715166f
parent 42
53379f82956b
child 46
be59c09708a8
equal deleted inserted replaced
42:53379f82956b 43:25977715166f
19 # Start-Of-Header 19 # Start-Of-Header
20 name = "PySide to PyQt (and vice versa) Plug-in" 20 name = "PySide to PyQt (and vice versa) Plug-in"
21 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 21 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
22 autoactivate = True 22 autoactivate = True
23 deactivateable = True 23 deactivateable = True
24 version = "2.1.1" 24 version = "2.2.0"
25 className = "PySide2PyQtPlugin" 25 className = "PySide2PyQtPlugin"
26 packageName = "PySide2PyQt" 26 packageName = "PySide2PyQt"
27 shortDescription = "Convert PySide file to PyQt and vice versa" 27 shortDescription = "Convert PySide file to PyQt and vice versa"
28 longDescription = \ 28 longDescription = \
29 """This plug-in implements a tool to convert a PySide file""" \ 29 """This plug-in implements a tool to convert a PySide/PySide2 file""" \
30 """ to PyQt4 or PyQt5 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""" \
31 """ current editor.""" 31 """ current editor."""
32 needsRestart = False 32 needsRestart = False
33 pyqtApi = 2 33 pyqtApi = 2
34 python2Compatible = True 34 python2Compatible = True
135 def __initMenu(self): 135 def __initMenu(self):
136 """ 136 """
137 Private method to initialize the menu. 137 Private method to initialize the menu.
138 """ 138 """
139 self.__menu = QMenu(self.tr("PySide to/from PyQt")) 139 self.__menu = QMenu(self.tr("PySide to/from PyQt"))
140 self.__menu.addAction(self.tr("PySide to PyQt4"), self.__pyside2Pyqt)\ 140 self.__menu.addAction(self.tr("PySide to PyQt4"),
141 .setData("pyqt4") 141 lambda: self.__pyside2Pyqt("pyside", "pyqt4"))
142 self.__menu.addAction(self.tr("PySide to PyQt5"), self.__pyside2Pyqt)\ 142 self.__menu.addAction(self.tr("PySide to PyQt5"),
143 .setData("pyqt5") 143 lambda: self.__pyside2Pyqt("pyside", "pyqt5"))
144 self.__menu.addAction(self.tr("PyQt4 to PySide"), self.__pyqt2Pyside)\ 144 self.__menu.addAction(self.tr("PySide2 to PyQt5"),
145 .setData("pyqt4") 145 lambda: self.__pyside2Pyqt("pyside2", "pyqt5"))
146 self.__menu.addAction(self.tr("PyQt5 to PySide"), self.__pyqt2Pyside)\ 146 self.__menu.addSeparator()
147 .setData("pyqt5") 147 self.__menu.addAction(self.tr("PyQt4 to PySide"),
148 lambda: self.__pyqt2Pyside("pyqt4", "pyside"))
149 self.__menu.addAction(self.tr("PyQt5 to PySide2"),
150 lambda: self.__pyqt2Pyside("pyqt5", "pyside2"))
151 self.__menu.addAction(self.tr("PyQt5 to PySide"),
152 lambda: self.__pyqt2Pyside("pyqt5", "pyside"))
148 self.__menu.setEnabled(False) 153 self.__menu.setEnabled(False)
149 154
150 def __populateMenu(self, name, menu): 155 def __populateMenu(self, name, menu):
151 """ 156 """
152 Private slot to populate the tools menu with our entries. 157 Private slot to populate the tools menu with our entries.
214 act = menu.addSeparator() 219 act = menu.addSeparator()
215 self.__editors[editor].append(act) 220 self.__editors[editor].append(act)
216 act = menu.addMenu(self.__menu) 221 act = menu.addMenu(self.__menu)
217 self.__editors[editor].append(act) 222 self.__editors[editor].append(act)
218 223
219 def __pyside2Pyqt(self): 224 def __pyside2Pyqt(self, pyside, pyqt):
220 """ 225 """
221 Private slot to convert the code of the current editor from PySide 226 Private slot to convert the code of the current editor from PySide
222 to PyQt. 227 to PyQt.
228
229 @param pyside PySide variant (pyside or pyside2)
230 @type str
231 @param pyqt PyQt variant (pyqt4 or pyqt5)
232 @type str
223 """ 233 """
224 editor = e5App().getObject("ViewManager").activeWindow() 234 editor = e5App().getObject("ViewManager").activeWindow()
225 if editor is None: 235 if editor is None:
226 return 236 return
227 237
228 act = self.sender()
229 if act is None:
230 return
231
232 text = editor.text() 238 text = editor.text()
233 pyqt = act.data()
234 if pyqt == "pyqt4": 239 if pyqt == "pyqt4":
235 newText = (text 240 newText = (text
236 .replace("PySide", "PyQt4") 241 .replace("PySide", "PyQt4")
237 .replace("Signal", "pyqtSignal") 242 .replace("Signal", "pyqtSignal")
238 .replace("Slot", "pyqtSlot") 243 .replace("Slot", "pyqtSlot")
240 .replace("pyside-uic", "pyuic4") 245 .replace("pyside-uic", "pyuic4")
241 .replace("pyside-rcc", "pyrcc4") 246 .replace("pyside-rcc", "pyrcc4")
242 .replace("pyside-lupdate", "pylupdate4") 247 .replace("pyside-lupdate", "pylupdate4")
243 ) 248 )
244 elif pyqt == "pyqt5": 249 elif pyqt == "pyqt5":
245 # Note: this code does no Qt4 to Qt5 conversion 250 newText = (text
246 newText = (text
247 .replace("PySide", "PyQt5")
248 .replace("Signal", "pyqtSignal") 251 .replace("Signal", "pyqtSignal")
249 .replace("Slot", "pyqtSlot") 252 .replace("Slot", "pyqtSlot")
250 .replace("Property", "pyqtProperty") 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")
251 .replace("pyside-uic", "pyuic5") 259 .replace("pyside-uic", "pyuic5")
252 .replace("pyside-rcc", "pyrcc5") 260 .replace("pyside-rcc", "pyrcc5")
253 .replace("pyside-lupdate", "pylupdate5") 261 .replace("pyside-lupdate", "pylupdate5")
254 ) 262 )
255 else: 263 else:
259 editor.beginUndoAction() 267 editor.beginUndoAction()
260 editor.selectAll() 268 editor.selectAll()
261 editor.replaceSelectedText(newText) 269 editor.replaceSelectedText(newText)
262 editor.endUndoAction() 270 editor.endUndoAction()
263 271
264 def __pyqt2Pyside(self): 272 def __pyqt2Pyside(self, pyqt, pyside):
265 """ 273 """
266 Private slot to convert the code of the current editor from PyQt 274 Private slot to convert the code of the current editor from PyQt
267 to PySide. 275 to PySide.
276
277 @param pyqt PyQt variant (pyqt4 or pyqt5)
278 @type str
279 @param pyside PySide variant (pyside or pyside2)
280 @type str
268 """ 281 """
269 editor = e5App().getObject("ViewManager").activeWindow() 282 editor = e5App().getObject("ViewManager").activeWindow()
270 if editor is None: 283 if editor is None:
271 return 284 return
272 285
273 act = self.sender()
274 if act is None:
275 return
276
277 text = editor.text() 286 text = editor.text()
278 pyqt = act.data()
279 if pyqt == "pyqt4": 287 if pyqt == "pyqt4":
280 newText = (text 288 newText = (text
281 .replace("PyQt4", "PySide") 289 .replace("PyQt4", "PySide")
282 .replace("pyqtSignal", "Signal") 290 .replace("pyqtSignal", "Signal")
283 .replace("pyqtSlot", "Slot") 291 .replace("pyqtSlot", "Slot")
284 .replace("pyqtProperty", "Property") 292 .replace("pyqtProperty", "Property")
285 .replace("pyuic4", "pyside-uic") 293 .replace("pyuic4", "pyside-uic")
286 .replace("pyrcc4", "pyside-rcc") 294 .replace("pyrcc4", "pyside-rcc")
287 .replace("pylupdate4", "pyside-lupdate") 295 .replace("pylupdate4", "pyside-lupdate")
288 ) 296 )
289 elif pyqt == "pyqt5": 297 elif pyqt == "pyqt5" and pyside == "pyside":
290 # Note: this code does no Qt4 to Qt5 conversion
291 newText = (text 298 newText = (text
292 .replace("PyQt5", "PySide") 299 .replace("PyQt5", "PySide")
293 .replace("pyqtSignal", "Signal") 300 .replace("pyqtSignal", "Signal")
294 .replace("pyqtSlot", "Slot") 301 .replace("pyqtSlot", "Slot")
295 .replace("pyqtProperty", "Property") 302 .replace("pyqtProperty", "Property")
296 .replace("pyuic5", "pyside-uic") 303 .replace("pyuic5", "pyside-uic")
297 .replace("pyrcc5", "pyside-rcc") 304 .replace("pyrcc5", "pyside-rcc")
298 .replace("pylupdate5", "pyside-lupdate") 305 .replace("pylupdate5", "pyside-lupdate")
299 ) 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 )
300 else: 317 else:
301 return 318 return
302 319
303 if newText != text: 320 if newText != text:
304 editor.beginUndoAction() 321 editor.beginUndoAction()

eric ide

mercurial