8 """ |
8 """ |
9 |
9 |
10 import sys |
10 import sys |
11 import os |
11 import os |
12 import json |
12 import json |
|
13 import contextlib |
13 |
14 |
14 from PyQt5.QtCore import ( |
15 from PyQt5.QtCore import ( |
15 pyqtSlot, Qt, QMetaObject, QRegularExpression, QSortFilterProxyModel, |
16 pyqtSlot, Qt, QMetaObject, QRegularExpression, QSortFilterProxyModel, |
16 QProcess, QProcessEnvironment |
17 QProcess, QProcessEnvironment |
17 ) |
18 ) |
55 |
56 |
56 @param formName name of the file containing the form (string) |
57 @param formName name of the file containing the form (string) |
57 @param project reference to the project object |
58 @param project reference to the project object |
58 @param parent parent widget if the dialog (QWidget) |
59 @param parent parent widget if the dialog (QWidget) |
59 """ |
60 """ |
60 super(CreateDialogCodeDialog, self).__init__(parent) |
61 super().__init__(parent) |
61 self.setupUi(self) |
62 self.setupUi(self) |
62 |
63 |
63 self.okButton = self.buttonBox.button( |
64 self.okButton = self.buttonBox.button( |
64 QDialogButtonBox.StandardButton.Ok) |
65 QDialogButtonBox.StandardButton.Ok) |
65 |
66 |
94 ed = vm.getOpenEditor(self.srcFile) |
95 ed = vm.getOpenEditor(self.srcFile) |
95 if ed and not vm.checkDirty(ed): |
96 if ed and not vm.checkDirty(ed): |
96 self.__initError = True |
97 self.__initError = True |
97 return |
98 return |
98 |
99 |
99 try: |
100 with contextlib.suppress(ImportError): |
100 splitExt = os.path.splitext(self.srcFile) |
101 splitExt = os.path.splitext(self.srcFile) |
101 if len(splitExt) == 2: |
102 exts = [splitExt[1]] if len(splitExt) == 2 else None |
102 exts = [splitExt[1]] |
|
103 else: |
|
104 exts = None |
|
105 from Utilities import ModuleParser |
103 from Utilities import ModuleParser |
106 self.__module = ModuleParser.readModule( |
104 self.__module = ModuleParser.readModule( |
107 self.srcFile, extensions=exts, caching=False) |
105 self.srcFile, extensions=exts, caching=False) |
108 except ImportError: |
|
109 pass |
|
110 |
106 |
111 if self.__module is not None: |
107 if self.__module is not None: |
112 self.filenameEdit.setText(self.srcFile) |
108 self.filenameEdit.setText(self.srcFile) |
113 |
109 |
114 classesList = [] |
110 classesList = [] |
337 self.slotsModel.appendRow(itm) |
333 self.slotsModel.appendRow(itm) |
338 for methodDict in objectDict["methods"]: |
334 for methodDict in objectDict["methods"]: |
339 itm2 = QStandardItem(methodDict["signature"]) |
335 itm2 = QStandardItem(methodDict["signature"]) |
340 itm.appendRow(itm2) |
336 itm.appendRow(itm2) |
341 |
337 |
342 if self.__module is not None: |
338 if ( |
343 if ( |
339 self.__module is not None and |
344 methodDict["methods"][0] in signatureList or |
340 (methodDict["methods"][0] in signatureList or |
345 methodDict["methods"][1] in signatureList |
341 methodDict["methods"][1] in signatureList) |
346 ): |
342 ): |
347 itm2.setFlags( |
343 itm2.setFlags( |
348 Qt.ItemFlags(Qt.ItemFlag.ItemIsEnabled)) |
344 Qt.ItemFlags(Qt.ItemFlag.ItemIsEnabled)) |
349 itm2.setCheckState(Qt.CheckState.Checked) |
345 itm2.setCheckState(Qt.CheckState.Checked) |
350 if e5App().usesDarkPalette(): |
346 if e5App().usesDarkPalette(): |
351 itm2.setForeground(QBrush(QColor("#75bfff"))) |
347 itm2.setForeground(QBrush(QColor("#75bfff"))) |
352 else: |
348 else: |
353 itm2.setForeground(QBrush(Qt.GlobalColor.blue)) |
349 itm2.setForeground(QBrush(Qt.GlobalColor.blue)) |
354 continue |
350 continue |
355 |
351 |
356 itm2.setData(methodDict["pyqt_signature"], |
352 itm2.setData(methodDict["pyqt_signature"], |
357 pyqtSignatureRole) |
353 pyqtSignatureRole) |
358 itm2.setData(methodDict["python_signature"], |
354 itm2.setData(methodDict["python_signature"], |
359 pythonSignatureRole) |
355 pythonSignatureRole) |
375 |
371 |
376 def __generateCode(self): |
372 def __generateCode(self): |
377 """ |
373 """ |
378 Private slot to generate the code as requested by the user. |
374 Private slot to generate the code as requested by the user. |
379 """ |
375 """ |
380 # first decide on extension |
|
381 if ( |
376 if ( |
382 self.filenameEdit.text().endswith(".py") or |
377 self.filenameEdit.text().endswith(".rb") or |
383 self.filenameEdit.text().endswith(".pyw") |
378 self.project.getProjectLanguage() == "Ruby" |
384 ): |
379 ): |
385 self.__generatePythonCode() |
380 # Ruby code generation is not supported |
386 elif self.filenameEdit.text().endswith(".rb"): |
|
387 pass |
|
388 # second decide on project language |
|
389 elif self.project.getProjectLanguage() == "Python3": |
|
390 self.__generatePythonCode() |
|
391 elif self.project.getProjectLanguage() == "Ruby": |
|
392 pass |
381 pass |
393 else: |
382 else: |
394 # assume Python (our global default) |
383 # assume Python (our global default) |
395 self.__generatePythonCode() |
384 self.__generatePythonCode() |
396 |
385 |
509 if line.lstrip().startswith("def __init__"): |
498 if line.lstrip().startswith("def __init__"): |
510 indentStr = line.replace(line.lstrip(), "") |
499 indentStr = line.replace(line.lstrip(), "") |
511 break |
500 break |
512 |
501 |
513 # do the coding stuff |
502 # do the coding stuff |
514 if self.project.getProjectType() in ("PySide2", "PySide6"): |
503 pyqtSignatureFormat = ( |
515 pyqtSignatureFormat = '@Slot({0})' |
504 '@Slot({0})' |
516 else: |
505 if self.project.getProjectType() in ("PySide2", "PySide6") else |
517 pyqtSignatureFormat = '@pyqtSlot({0})' |
506 '@pyqtSlot({0})' |
|
507 ) |
518 for row in range(self.slotsModel.rowCount()): |
508 for row in range(self.slotsModel.rowCount()): |
519 topItem = self.slotsModel.item(row) |
509 topItem = self.slotsModel.item(row) |
520 for childRow in range(topItem.rowCount()): |
510 for childRow in range(topItem.rowCount()): |
521 child = topItem.child(childRow) |
511 child = topItem.child(childRow) |
522 if ( |
512 if ( |
566 sourceImpl.extend(slotsCode) |
556 sourceImpl.extend(slotsCode) |
567 else: |
557 else: |
568 sourceImpl[appendAtIndex:appendAtIndex] = slotsCode |
558 sourceImpl[appendAtIndex:appendAtIndex] = slotsCode |
569 |
559 |
570 # write the new code |
560 # write the new code |
571 if self.project.useSystemEol(): |
561 newline = (None if self.project.useSystemEol() |
572 newline = None |
562 else self.project.getEolString()) |
573 else: |
|
574 newline = self.project.getEolString() |
|
575 fn = self.filenameEdit.text() |
563 fn = self.filenameEdit.text() |
576 try: |
564 try: |
577 with open(fn, 'w', encoding="utf-8", newline=newline) as srcFile: |
565 with open(fn, 'w', encoding="utf-8", newline=newline) as srcFile: |
578 srcFile.write("".join(sourceImpl)) |
566 srcFile.write("".join(sourceImpl)) |
579 except OSError as why: |
567 except OSError as why: |