Project/CreateDialogCodeDialog.py

changeset 945
8cd4d08fa9f6
parent 791
9ec2ac20e54e
child 1016
72b6b0778e06
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
23 23
24 import UI.PixmapCache 24 import UI.PixmapCache
25 25
26 from eric5config import getConfig 26 from eric5config import getConfig
27 27
28 pyqtSignatureRole = Qt.UserRole + 1 28 pyqtSignatureRole = Qt.UserRole + 1
29 pythonSignatureRole = Qt.UserRole + 2 29 pythonSignatureRole = Qt.UserRole + 2
30 rubySignatureRole = Qt.UserRole + 3 30 rubySignatureRole = Qt.UserRole + 3
31
31 32
32 class CreateDialogCodeDialog(QDialog, Ui_CreateDialogCodeDialog): 33 class CreateDialogCodeDialog(QDialog, Ui_CreateDialogCodeDialog):
33 """ 34 """
34 Class implementing a dialog to generate code for a Qt4 dialog. 35 Class implementing a dialog to generate code for a Qt4 dialog.
35 """ 36 """
36 def __init__(self, formName, project, parent = None): 37 def __init__(self, formName, project, parent=None):
37 """ 38 """
38 Constructor 39 Constructor
39 40
40 @param formName name of the file containing the form (string) 41 @param formName name of the file containing the form (string)
41 @param project reference to the project object 42 @param project reference to the project object
71 ed = vm.getOpenEditor(self.srcFile) 72 ed = vm.getOpenEditor(self.srcFile)
72 if ed and not vm.checkDirty(ed): 73 if ed and not vm.checkDirty(ed):
73 self.__initError = True 74 self.__initError = True
74 return 75 return
75 76
76 self.__module = ModuleParser.readModule(self.srcFile, caching = False) 77 self.__module = ModuleParser.readModule(self.srcFile, caching=False)
77 78
78 if self.__module is not None: 79 if self.__module is not None:
79 self.filenameEdit.setText(self.srcFile) 80 self.filenameEdit.setText(self.srcFile)
80 81
81 classesList = [] 82 classesList = []
158 signatures.append(meth.name) 159 signatures.append(meth.name)
159 return signatures 160 return signatures
160 161
161 def __mapType(self, type_): 162 def __mapType(self, type_):
162 """ 163 """
163 Private method to map a type as reported by Qt's meta object to the 164 Private method to map a type as reported by Qt's meta object to the
164 correct Python type. 165 correct Python type.
165 166
166 @param type_ type as reported by Qt (QByteArray) 167 @param type_ type as reported by Qt (QByteArray)
167 @return mapped Python type (string) 168 @return mapped Python type (string)
168 """ 169 """
212 name, metaMethod.signature())) 213 name, metaMethod.signature()))
213 itm.appendRow(itm2) 214 itm.appendRow(itm2)
214 if self.__module is not None: 215 if self.__module is not None:
215 method = "on_{0}_{1}".format( 216 method = "on_{0}_{1}".format(
216 name, metaMethod.signature().split("(")[0]) 217 name, metaMethod.signature().split("(")[0])
217 method2 = "{0}({1})".format(method, 218 method2 = "{0}({1})".format(method,
218 ", ".join([self.__mapType(t) 219 ", ".join([self.__mapType(t)
219 for t in metaMethod.parameterTypes()])) 220 for t in metaMethod.parameterTypes()]))
220 221
221 if method2 in signatureList or method in signatureList: 222 if method2 in signatureList or method in signatureList:
222 itm2.setFlags(Qt.ItemFlags(Qt.ItemIsEnabled)) 223 itm2.setFlags(Qt.ItemFlags(Qt.ItemIsEnabled))
223 itm2.setCheckState(Qt.Checked) 224 itm2.setCheckState(Qt.Checked)
224 itm2.setForeground(QBrush(Qt.blue)) 225 itm2.setForeground(QBrush(Qt.blue))
225 continue 226 continue
226 227
227 pyqtSignature = \ 228 pyqtSignature = \
228 ", ".join([self.__mapType(t) 229 ", ".join([self.__mapType(t)
229 for t in metaMethod.parameterTypes()]) 230 for t in metaMethod.parameterTypes()])
230 231
231 parameterNames = metaMethod.parameterNames() 232 parameterNames = metaMethod.parameterNames()
232 if parameterNames: 233 if parameterNames:
233 for index in range(len(parameterNames)): 234 for index in range(len(parameterNames)):
237 methNamesSig = \ 238 methNamesSig = \
238 ", ".join([bytes(n).decode() for n in parameterNames]) 239 ", ".join([bytes(n).decode() for n in parameterNames])
239 240
240 if methNamesSig: 241 if methNamesSig:
241 pythonSignature = "on_{0}_{1}(self, {2})".format( 242 pythonSignature = "on_{0}_{1}(self, {2})".format(
242 name, 243 name,
243 metaMethod.signature().split("(")[0], 244 metaMethod.signature().split("(")[0],
244 methNamesSig) 245 methNamesSig)
245 else: 246 else:
246 pythonSignature = "on_{0}_{1}(self)".format( 247 pythonSignature = "on_{0}_{1}(self)".format(
247 name, 248 name,
248 metaMethod.signature().split("(")[0]) 249 metaMethod.signature().split("(")[0])
249 itm2.setData(pyqtSignature, pyqtSignatureRole) 250 itm2.setData(pyqtSignature, pyqtSignatureRole)
250 itm2.setData(pythonSignature, pythonSignatureRole) 251 itm2.setData(pythonSignature, pythonSignatureRole)
251 252
252 itm2.setFlags(Qt.ItemFlags( 253 itm2.setFlags(Qt.ItemFlags(
294 295
295 if self.__module is None: 296 if self.__module is None:
296 # new file 297 # new file
297 try: 298 try:
298 tmplName = os.path.join(getConfig('ericCodeTemplatesDir'), "impl.py.tmpl") 299 tmplName = os.path.join(getConfig('ericCodeTemplatesDir'), "impl.py.tmpl")
299 tmplFile = open(tmplName, 'r', encoding = "utf-8") 300 tmplFile = open(tmplName, 'r', encoding="utf-8")
300 template = tmplFile.read() 301 template = tmplFile.read()
301 tmplFile.close() 302 tmplFile.close()
302 except IOError as why: 303 except IOError as why:
303 E5MessageBox.critical(self, 304 E5MessageBox.critical(self,
304 self.trUtf8("Code Generation"), 305 self.trUtf8("Code Generation"),
308 return 309 return
309 310
310 objName = self.__objectName() 311 objName = self.__objectName()
311 if objName: 312 if objName:
312 template = template\ 313 template = template\
313 .replace("$FORMFILE$", 314 .replace("$FORMFILE$",
314 os.path.splitext(os.path.basename(self.formFile))[0])\ 315 os.path.splitext(os.path.basename(self.formFile))[0])\
315 .replace("$FORMCLASS$", objName)\ 316 .replace("$FORMCLASS$", objName)\
316 .replace("$CLASSNAME$", self.classNameCombo.currentText())\ 317 .replace("$CLASSNAME$", self.classNameCombo.currentText())\
317 .replace("$SUPERCLASS$", self.__className()) 318 .replace("$SUPERCLASS$", self.__className())
318 319
325 indentStr = line.replace(line.lstrip(), "") 326 indentStr = line.replace(line.lstrip(), "")
326 break 327 break
327 else: 328 else:
328 # extend existing file 329 # extend existing file
329 try: 330 try:
330 srcFile = open(self.srcFile, 'r', encoding = "utf-8") 331 srcFile = open(self.srcFile, 'r', encoding="utf-8")
331 sourceImpl = srcFile.readlines() 332 sourceImpl = srcFile.readlines()
332 srcFile.close() 333 srcFile.close()
333 if not sourceImpl[-1].endswith("\n"): 334 if not sourceImpl[-1].endswith("\n"):
334 sourceImpl[-1] = "{0}{1}".format(sourceImpl[-1], "\n") 335 sourceImpl[-1] = "{0}{1}".format(sourceImpl[-1], "\n")
335 except IOError as why: 336 except IOError as why:
348 del sourceImpl[-1] 349 del sourceImpl[-1]
349 else: 350 else:
350 appendAtIndex = cls.endlineno - 1 351 appendAtIndex = cls.endlineno - 1
351 352
352 # determine indent string 353 # determine indent string
353 for line in sourceImpl[cls.lineno:cls.endlineno+1]: 354 for line in sourceImpl[cls.lineno:cls.endlineno + 1]:
354 if line.lstrip().startswith("def __init__"): 355 if line.lstrip().startswith("def __init__"):
355 indentStr = line.replace(line.lstrip(), "") 356 indentStr = line.replace(line.lstrip(), "")
356 break 357 break
357 358
358 # do the coding stuff 359 # do the coding stuff
385 try: 386 try:
386 if self.project.useSystemEol(): 387 if self.project.useSystemEol():
387 newline = None 388 newline = None
388 else: 389 else:
389 newline = self.project.getEolString() 390 newline = self.project.getEolString()
390 srcFile = open(self.filenameEdit.text(), 'w', encoding = "utf-8", 391 srcFile = open(self.filenameEdit.text(), 'w', encoding="utf-8",
391 newline = newline) 392 newline=newline)
392 srcFile.write("".join(sourceImpl)) 393 srcFile.write("".join(sourceImpl))
393 srcFile.close() 394 srcFile.close()
394 except IOError as why: 395 except IOError as why:
395 E5MessageBox.critical(self, 396 E5MessageBox.critical(self,
396 self.trUtf8("Code Generation"), 397 self.trUtf8("Code Generation"),

eric ide

mercurial