8 """ |
8 """ |
9 |
9 |
10 import contextlib |
10 import contextlib |
11 import os |
11 import os |
12 |
12 |
13 from PyQt5.QtCore import QObject, QTranslator |
13 from PyQt6.QtCore import QObject, QTranslator |
14 from PyQt5.QtWidgets import QMenu |
14 from PyQt6.QtWidgets import QMenu |
15 |
15 |
16 from E5Gui.E5Application import e5App |
16 from EricWidgets.EricApplication import ericApp |
17 |
17 |
18 # Start-Of-Header |
18 # Start-Of-Header |
19 name = "PySide to PyQt (and vice versa) Plug-in" |
19 name = "PySide to PyQt (and vice versa) Plug-in" |
20 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
20 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
21 autoactivate = True |
21 autoactivate = True |
22 deactivateable = True |
22 deactivateable = True |
23 version = "3.1.1" |
23 version = "1.0.0" |
24 className = "PySide2PyQtPlugin" |
24 className = "PySide2PyQtPlugin" |
25 packageName = "PySide2PyQt" |
25 packageName = "PySide2PyQt" |
26 shortDescription = "Convert PySide file to PyQt and vice versa" |
26 shortDescription = "Convert PySide file to PyQt and vice versa" |
27 longDescription = ( |
27 longDescription = ( |
28 """This plug-in implements a tool to convert a PySide2 file to PyQt5""" |
28 """This plug-in implements a tool to convert a PySide2 file to PyQt5""" |
29 """ and vice versa. It works with the text of the current editor.""" |
29 """ and vice versa or a PySide6 file to PyQt6 and vice versa. It works""" |
|
30 """ with the text of the current editor.""" |
30 ) |
31 ) |
31 needsRestart = False |
32 needsRestart = False |
32 pyqtApi = 2 |
33 pyqtApi = 2 |
33 # End-Of-Header |
34 # End-Of-Header |
34 |
35 |
73 act = menu.addSeparator() |
76 act = menu.addSeparator() |
74 self.__mainActions.append(act) |
77 self.__mainActions.append(act) |
75 act = menu.addMenu(self.__menu) |
78 act = menu.addMenu(self.__menu) |
76 self.__mainActions.append(act) |
79 self.__mainActions.append(act) |
77 |
80 |
78 e5App().getObject("ViewManager").editorOpenedEd.connect( |
81 ericApp().getObject("ViewManager").editorOpenedEd.connect( |
79 self.__editorOpened) |
82 self.__editorOpened) |
80 e5App().getObject("ViewManager").editorClosedEd.connect( |
83 ericApp().getObject("ViewManager").editorClosedEd.connect( |
81 self.__editorClosed) |
84 self.__editorClosed) |
82 |
85 |
83 for editor in e5App().getObject("ViewManager").getOpenEditors(): |
86 for editor in ericApp().getObject("ViewManager").getOpenEditors(): |
84 self.__editorOpened(editor) |
87 self.__editorOpened(editor) |
85 |
88 |
86 return None, True |
89 return None, True |
87 |
90 |
88 def deactivate(self): |
91 def deactivate(self): |
95 if menu is not None: |
98 if menu is not None: |
96 for act in self.__mainActions: |
99 for act in self.__mainActions: |
97 menu.removeAction(act) |
100 menu.removeAction(act) |
98 self.__mainActions = [] |
101 self.__mainActions = [] |
99 |
102 |
100 e5App().getObject("ViewManager").editorOpenedEd.disconnect( |
103 ericApp().getObject("ViewManager").editorOpenedEd.disconnect( |
101 self.__editorOpened) |
104 self.__editorOpened) |
102 e5App().getObject("ViewManager").editorClosedEd.disconnect( |
105 ericApp().getObject("ViewManager").editorClosedEd.disconnect( |
103 self.__editorClosed) |
106 self.__editorClosed) |
104 |
107 |
105 for editor, acts in self.__editors.items(): |
108 for editor, acts in self.__editors.items(): |
106 editor.showMenu.disconnect(self.__editorShowMenu) |
109 editor.showMenu.disconnect(self.__editorShowMenu) |
107 menu = editor.getMenu("Tools") |
110 menu = editor.getMenu("Tools") |
122 translation = "pyside2pyqt_{0}".format(loc) |
125 translation = "pyside2pyqt_{0}".format(loc) |
123 translator = QTranslator(None) |
126 translator = QTranslator(None) |
124 loaded = translator.load(translation, locale_dir) |
127 loaded = translator.load(translation, locale_dir) |
125 if loaded: |
128 if loaded: |
126 self.__translator = translator |
129 self.__translator = translator |
127 e5App().installTranslator(self.__translator) |
130 ericApp().installTranslator(self.__translator) |
128 else: |
131 else: |
129 print("Warning: translation file '{0}' could not be" |
132 print("Warning: translation file '{0}' could not be" |
130 " loaded.".format(translation)) |
133 " loaded.".format(translation)) |
131 print("Using default.") |
134 print("Using default.") |
132 |
135 |
135 Private method to initialize the menu. |
138 Private method to initialize the menu. |
136 """ |
139 """ |
137 self.__menu = QMenu(self.tr("PySide to/from PyQt")) |
140 self.__menu = QMenu(self.tr("PySide to/from PyQt")) |
138 self.__menu.addAction(self.tr("PySide2 to PyQt5"), |
141 self.__menu.addAction(self.tr("PySide2 to PyQt5"), |
139 lambda: self.__pyside2Pyqt("pyside2", "pyqt5")) |
142 lambda: self.__pyside2Pyqt("pyside2", "pyqt5")) |
140 self.__menu.addSeparator() |
|
141 self.__menu.addAction(self.tr("PyQt5 to PySide2"), |
143 self.__menu.addAction(self.tr("PyQt5 to PySide2"), |
142 lambda: self.__pyqt2Pyside("pyqt5", "pyside2")) |
144 lambda: self.__pyqt2Pyside("pyqt5", "pyside2")) |
|
145 self.__menu.addSeparator() |
|
146 self.__menu.addAction(self.tr("PySide6 to PyQt6"), |
|
147 lambda: self.__pyside2Pyqt("pyside6", "pyqt6")) |
|
148 self.__menu.addAction(self.tr("PyQt6 to PySide6"), |
|
149 lambda: self.__pyqt2Pyside("pyqt6", "pyside6")) |
143 self.__menu.setEnabled(False) |
150 self.__menu.setEnabled(False) |
144 |
151 |
145 def __populateMenu(self, name, menu): |
152 def __populateMenu(self, name, menu): |
146 """ |
153 """ |
147 Private slot to populate the tools menu with our entries. |
154 Private slot to populate the tools menu with our entries. |
148 |
155 |
149 @param name name of the menu (string) |
156 @param name name of the menu |
150 @param menu reference to the menu to be populated (QMenu) |
157 @type str |
|
158 @param menu reference to the menu to be populated |
|
159 @type QMenu |
151 """ |
160 """ |
152 if name not in ["Tools", "PluginTools"]: |
161 if name not in ["Tools", "PluginTools"]: |
153 return |
162 return |
154 |
163 |
155 editor = e5App().getObject("ViewManager").activeWindow() |
164 editor = ericApp().getObject("ViewManager").activeWindow() |
156 |
165 |
157 if name == "Tools": |
166 if name == "Tools": |
158 if not menu.isEmpty(): |
167 if not menu.isEmpty(): |
159 menu.addSeparator() |
168 menu.addSeparator() |
160 act = menu.addMenu(self.__menu) |
169 act = menu.addMenu(self.__menu) |
164 |
173 |
165 def __editorOpened(self, editor): |
174 def __editorOpened(self, editor): |
166 """ |
175 """ |
167 Private slot called, when a new editor was opened. |
176 Private slot called, when a new editor was opened. |
168 |
177 |
169 @param editor reference to the new editor (QScintilla.Editor) |
178 @param editor reference to the new editor |
|
179 @type Editor |
170 """ |
180 """ |
171 menu = editor.getMenu("Tools") |
181 menu = editor.getMenu("Tools") |
172 if menu is not None: |
182 if menu is not None: |
173 self.__editors[editor] = [] |
183 self.__editors[editor] = [] |
174 if not menu.isEmpty(): |
184 if not menu.isEmpty(): |
181 |
191 |
182 def __editorClosed(self, editor): |
192 def __editorClosed(self, editor): |
183 """ |
193 """ |
184 Private slot called, when an editor was closed. |
194 Private slot called, when an editor was closed. |
185 |
195 |
186 @param editor reference to the editor (QScintilla.Editor) |
196 @param editor reference to the editor |
|
197 @type Editor |
187 """ |
198 """ |
188 with contextlib.suppress(KeyError): |
199 with contextlib.suppress(KeyError): |
189 del self.__editors[editor] |
200 del self.__editors[editor] |
190 if not self.__editors: |
201 if not self.__editors: |
191 self.__menu.setEnabled(False) |
202 self.__menu.setEnabled(False) |
193 def __editorShowMenu(self, menuName, menu, editor): |
204 def __editorShowMenu(self, menuName, menu, editor): |
194 """ |
205 """ |
195 Private slot called, when the the editor context menu or a submenu is |
206 Private slot called, when the the editor context menu or a submenu is |
196 about to be shown. |
207 about to be shown. |
197 |
208 |
198 @param menuName name of the menu to be shown (string) |
209 @param menuName name of the menu to be shown |
199 @param menu reference to the menu (QMenu) |
210 @type str |
|
211 @param menu reference to the menu |
|
212 @type QMenu |
200 @param editor reference to the editor |
213 @param editor reference to the editor |
|
214 @type Editor |
201 """ |
215 """ |
202 if ( |
216 if ( |
203 menuName == "Tools" and |
217 menuName == "Tools" and |
204 self.__menu.menuAction() not in menu.actions() |
218 self.__menu.menuAction() not in menu.actions() |
205 ): |
219 ): |
214 def __pyside2Pyqt(self, pyside, pyqt): |
228 def __pyside2Pyqt(self, pyside, pyqt): |
215 """ |
229 """ |
216 Private slot to convert the code of the current editor from PySide |
230 Private slot to convert the code of the current editor from PySide |
217 to PyQt. |
231 to PyQt. |
218 |
232 |
219 @param pyside PySide variant (pyside2) |
233 @param pyside PySide variant (pyside2 or pyside6) |
220 @type str |
234 @type str |
221 @param pyqt PyQt variant (pyqt5) |
235 @param pyqt PyQt variant (pyqt5 or pyqt6) |
222 @type str |
236 @type str |
223 """ |
237 """ |
224 editor = e5App().getObject("ViewManager").activeWindow() |
238 editor = ericApp().getObject("ViewManager").activeWindow() |
225 if editor is None: |
239 if editor is None: |
226 return |
240 return |
227 |
241 |
228 text = editor.text() |
242 text = editor.text() |
229 if pyqt == "pyqt5" and pyside == "pyside2": |
243 if pyqt == "pyqt5" and pyside == "pyside2": |
230 newText = ( |
244 newText = ( |
231 text |
245 text |
|
246 .replace("PySide2", "PyQt5") |
232 .replace("Signal", "pyqtSignal") |
247 .replace("Signal", "pyqtSignal") |
233 .replace("Slot", "pyqtSlot") |
248 .replace("Slot", "pyqtSlot") |
234 .replace("Property", "pyqtProperty") |
249 .replace("Property", "pyqtProperty") |
235 .replace("PySide2", "PyQt5") |
|
236 .replace("pyside2-uic", "pyuic5") |
250 .replace("pyside2-uic", "pyuic5") |
237 .replace("pyside2-rcc", "pyrcc5") |
251 .replace("pyside2-rcc", "pyrcc5") |
238 .replace("pyside2-lupdate", "pylupdate5") |
252 .replace("pyside2-lupdate", "pylupdate5") |
|
253 ) |
|
254 elif pyqt == "pyqt6" and pyside == "pyside6": |
|
255 newText = ( |
|
256 text |
|
257 .replace("PySide6", "PyQt6") |
|
258 .replace("Signal", "pyqtSignal") |
|
259 .replace("Slot", "pyqtSlot") |
|
260 .replace("Property", "pyqtProperty") |
|
261 .replace("pyside6-uic", "pyuic6") |
|
262 .replace("pyside6-lupdate", "pylupdate6") |
239 ) |
263 ) |
240 else: |
264 else: |
241 return |
265 return |
242 |
266 |
243 if newText != text: |
267 if newText != text: |
249 def __pyqt2Pyside(self, pyqt, pyside): |
273 def __pyqt2Pyside(self, pyqt, pyside): |
250 """ |
274 """ |
251 Private slot to convert the code of the current editor from PyQt |
275 Private slot to convert the code of the current editor from PyQt |
252 to PySide. |
276 to PySide. |
253 |
277 |
254 @param pyqt PyQt variant (pyqt5) |
278 @param pyqt PyQt variant (pyqt5 or pyqt6) |
255 @type str |
279 @type str |
256 @param pyside PySide variant (pyside2) |
280 @param pyside PySide variant (pyside2 or pyside6) |
257 @type str |
281 @type str |
258 """ |
282 """ |
259 editor = e5App().getObject("ViewManager").activeWindow() |
283 editor = ericApp().getObject("ViewManager").activeWindow() |
260 if editor is None: |
284 if editor is None: |
261 return |
285 return |
262 |
286 |
263 text = editor.text() |
287 text = editor.text() |
264 if pyqt == "pyqt5" and pyside == "pyside2": |
288 if pyqt == "pyqt5" and pyside == "pyside2": |
270 .replace("pyqtProperty", "Property") |
294 .replace("pyqtProperty", "Property") |
271 .replace("pyuic5", "pyside2-uic") |
295 .replace("pyuic5", "pyside2-uic") |
272 .replace("pyrcc5", "pyside2-rcc") |
296 .replace("pyrcc5", "pyside2-rcc") |
273 .replace("pylupdate5", "pyside2-lupdate") |
297 .replace("pylupdate5", "pyside2-lupdate") |
274 ) |
298 ) |
|
299 elif pyqt == "pyqt6" and pyside == "pyside6": |
|
300 newText = ( |
|
301 text |
|
302 .replace("PyQt6", "PySide6") |
|
303 .replace("pyqtSignal", "Signal") |
|
304 .replace("pyqtSlot", "Slot") |
|
305 .replace("pyqtProperty", "Property") |
|
306 .replace("pyuic6", "pyside6-uic") |
|
307 .replace("pylupdate6", "pyside6-lupdate") |
|
308 ) |
275 else: |
309 else: |
276 return |
310 return |
277 |
311 |
278 if newText != text: |
312 if newText != text: |
279 editor.beginUndoAction() |
313 editor.beginUndoAction() |