Plugins/WizardPlugins/QRegExpWizard/QRegExpWizardDialog.py

branch
Py2 comp.
changeset 3484
645c12de6b0c
parent 3178
f25fc1364c88
parent 3345
071afe8be2a1
child 3656
441956d8fce5
equal deleted inserted replaced
3456:96232974dcdb 3484:645c12de6b0c
82 self.syntaxCombo.addItem("W3C XML Schema 1.1", QRegExp.W3CXmlSchema11) 82 self.syntaxCombo.addItem("W3C XML Schema 1.1", QRegExp.W3CXmlSchema11)
83 if qVersion() >= "5.0.0": 83 if qVersion() >= "5.0.0":
84 self.syntaxCombo.setCurrentIndex(1) 84 self.syntaxCombo.setCurrentIndex(1)
85 85
86 self.saveButton = self.buttonBox.addButton( 86 self.saveButton = self.buttonBox.addButton(
87 self.trUtf8("Save"), QDialogButtonBox.ActionRole) 87 self.tr("Save"), QDialogButtonBox.ActionRole)
88 self.saveButton.setToolTip( 88 self.saveButton.setToolTip(
89 self.trUtf8("Save the regular expression to a file")) 89 self.tr("Save the regular expression to a file"))
90 self.loadButton = self.buttonBox.addButton( 90 self.loadButton = self.buttonBox.addButton(
91 self.trUtf8("Load"), QDialogButtonBox.ActionRole) 91 self.tr("Load"), QDialogButtonBox.ActionRole)
92 self.loadButton.setToolTip( 92 self.loadButton.setToolTip(
93 self.trUtf8("Load a regular expression from a file")) 93 self.tr("Load a regular expression from a file"))
94 self.validateButton = self.buttonBox.addButton( 94 self.validateButton = self.buttonBox.addButton(
95 self.trUtf8("Validate"), QDialogButtonBox.ActionRole) 95 self.tr("Validate"), QDialogButtonBox.ActionRole)
96 self.validateButton.setToolTip( 96 self.validateButton.setToolTip(
97 self.trUtf8("Validate the regular expression")) 97 self.tr("Validate the regular expression"))
98 self.executeButton = self.buttonBox.addButton( 98 self.executeButton = self.buttonBox.addButton(
99 self.trUtf8("Execute"), QDialogButtonBox.ActionRole) 99 self.tr("Execute"), QDialogButtonBox.ActionRole)
100 self.executeButton.setToolTip( 100 self.executeButton.setToolTip(
101 self.trUtf8("Execute the regular expression")) 101 self.tr("Execute the regular expression"))
102 self.nextButton = self.buttonBox.addButton( 102 self.nextButton = self.buttonBox.addButton(
103 self.trUtf8("Next match"), QDialogButtonBox.ActionRole) 103 self.tr("Next match"), QDialogButtonBox.ActionRole)
104 self.nextButton.setToolTip( 104 self.nextButton.setToolTip(
105 self.trUtf8("Show the next match of the regular expression")) 105 self.tr("Show the next match of the regular expression"))
106 self.nextButton.setEnabled(False) 106 self.nextButton.setEnabled(False)
107 107
108 if fromEric: 108 if fromEric:
109 self.buttonBox.setStandardButtons( 109 self.buttonBox.setStandardButtons(
110 QDialogButtonBox.Cancel | QDialogButtonBox.Ok) 110 QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
111 self.copyButton = None 111 self.copyButton = None
112 else: 112 else:
113 self.copyButton = self.buttonBox.addButton( 113 self.copyButton = self.buttonBox.addButton(
114 self.trUtf8("Copy"), QDialogButtonBox.ActionRole) 114 self.tr("Copy"), QDialogButtonBox.ActionRole)
115 self.copyButton.setToolTip( 115 self.copyButton.setToolTip(
116 self.trUtf8("Copy the regular expression to the clipboard")) 116 self.tr("Copy the regular expression to the clipboard"))
117 self.buttonBox.setStandardButtons(QDialogButtonBox.Close) 117 self.buttonBox.setStandardButtons(QDialogButtonBox.Close)
118 self.variableLabel.hide() 118 self.variableLabel.hide()
119 self.variableLineEdit.hide() 119 self.variableLineEdit.hide()
120 self.variableLine.hide() 120 self.variableLine.hide()
121 self.regexpLineEdit.setFocus() 121 self.regexpLineEdit.setFocus()
336 """ 336 """
337 Private slot to save the regexp to a file. 337 Private slot to save the regexp to a file.
338 """ 338 """
339 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 339 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
340 self, 340 self,
341 self.trUtf8("Save regular expression"), 341 self.tr("Save regular expression"),
342 "", 342 "",
343 self.trUtf8("RegExp Files (*.rx);;All Files (*)"), 343 self.tr("RegExp Files (*.rx);;All Files (*)"),
344 None, 344 None,
345 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) 345 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
346 if fname: 346 if fname:
347 ext = QFileInfo(fname).suffix() 347 ext = QFileInfo(fname).suffix()
348 if not ext: 348 if not ext:
350 if ex: 350 if ex:
351 fname += ex 351 fname += ex
352 if QFileInfo(fname).exists(): 352 if QFileInfo(fname).exists():
353 res = E5MessageBox.yesNo( 353 res = E5MessageBox.yesNo(
354 self, 354 self,
355 self.trUtf8("Save regular expression"), 355 self.tr("Save regular expression"),
356 self.trUtf8("<p>The file <b>{0}</b> already exists." 356 self.tr("<p>The file <b>{0}</b> already exists."
357 " Overwrite it?</p>").format(fname), 357 " Overwrite it?</p>").format(fname),
358 icon=E5MessageBox.Warning) 358 icon=E5MessageBox.Warning)
359 if not res: 359 if not res:
360 return 360 return
361 361
362 syntax = self.syntaxCombo.itemData(self.syntaxCombo.currentIndex()) 362 syntax = self.syntaxCombo.itemData(self.syntaxCombo.currentIndex())
367 f.write(self.regexpLineEdit.text()) 367 f.write(self.regexpLineEdit.text())
368 f.close() 368 f.close()
369 except IOError as err: 369 except IOError as err:
370 E5MessageBox.information( 370 E5MessageBox.information(
371 self, 371 self,
372 self.trUtf8("Save regular expression"), 372 self.tr("Save regular expression"),
373 self.trUtf8("""<p>The regular expression could not""" 373 self.tr("""<p>The regular expression could not"""
374 """ be saved.</p><p>Reason: {0}</p>""") 374 """ be saved.</p><p>Reason: {0}</p>""")
375 .format(str(err))) 375 .format(str(err)))
376 376
377 @pyqtSlot() 377 @pyqtSlot()
378 def on_loadButton_clicked(self): 378 def on_loadButton_clicked(self):
379 """ 379 """
380 Private slot to load a regexp from a file. 380 Private slot to load a regexp from a file.
381 """ 381 """
382 fname = E5FileDialog.getOpenFileName( 382 fname = E5FileDialog.getOpenFileName(
383 self, 383 self,
384 self.trUtf8("Load regular expression"), 384 self.tr("Load regular expression"),
385 "", 385 "",
386 self.trUtf8("RegExp Files (*.rx);;All Files (*)")) 386 self.tr("RegExp Files (*.rx);;All Files (*)"))
387 if fname: 387 if fname:
388 try: 388 try:
389 f = open( 389 f = open(
390 Utilities.toNativeSeparators(fname), "r", encoding="utf-8") 390 Utilities.toNativeSeparators(fname), "r", encoding="utf-8")
391 regexp = f.read() 391 regexp = f.read()
398 regexp = lines[1] 398 regexp = lines[1]
399 self.regexpLineEdit.setText(regexp) 399 self.regexpLineEdit.setText(regexp)
400 except IOError as err: 400 except IOError as err:
401 E5MessageBox.information( 401 E5MessageBox.information(
402 self, 402 self,
403 self.trUtf8("Save regular expression"), 403 self.tr("Save regular expression"),
404 self.trUtf8("""<p>The regular expression could not""" 404 self.tr("""<p>The regular expression could not"""
405 """ be saved.</p><p>Reason: {0}</p>""") 405 """ be saved.</p><p>Reason: {0}</p>""")
406 .format(str(err))) 406 .format(str(err)))
407 407
408 @pyqtSlot() 408 @pyqtSlot()
409 def on_copyButton_clicked(self): 409 def on_copyButton_clicked(self):
410 """ 410 """
436 re.setPatternSyntax( 436 re.setPatternSyntax(
437 self.syntaxCombo.itemData(self.syntaxCombo.currentIndex())) 437 self.syntaxCombo.itemData(self.syntaxCombo.currentIndex()))
438 if re.isValid(): 438 if re.isValid():
439 E5MessageBox.information( 439 E5MessageBox.information(
440 self, 440 self,
441 self.trUtf8("Validation"), 441 self.tr("Validation"),
442 self.trUtf8("""The regular expression is valid.""")) 442 self.tr("""The regular expression is valid."""))
443 else: 443 else:
444 E5MessageBox.critical( 444 E5MessageBox.critical(
445 self, 445 self,
446 self.trUtf8("Error"), 446 self.tr("Error"),
447 self.trUtf8("""Invalid regular expression: {0}""") 447 self.tr("""Invalid regular expression: {0}""")
448 .format(re.errorString())) 448 .format(re.errorString()))
449 return 449 return
450 else: 450 else:
451 E5MessageBox.critical( 451 E5MessageBox.critical(
452 self, 452 self,
453 self.trUtf8("Error"), 453 self.tr("Error"),
454 self.trUtf8("""A regular expression must be given.""")) 454 self.tr("""A regular expression must be given."""))
455 455
456 @pyqtSlot() 456 @pyqtSlot()
457 def on_executeButton_clicked(self, startpos=0): 457 def on_executeButton_clicked(self, startpos=0):
458 """ 458 """
459 Private slot to execute the entered regexp on the test text. 459 Private slot to execute the entered regexp on the test text.
476 wildcard = syntax in [QRegExp.Wildcard, QRegExp.WildcardUnix] 476 wildcard = syntax in [QRegExp.Wildcard, QRegExp.WildcardUnix]
477 re.setPatternSyntax(syntax) 477 re.setPatternSyntax(syntax)
478 if not re.isValid(): 478 if not re.isValid():
479 E5MessageBox.critical( 479 E5MessageBox.critical(
480 self, 480 self,
481 self.trUtf8("Error"), 481 self.tr("Error"),
482 self.trUtf8("""Invalid regular expression: {0}""") 482 self.tr("""Invalid regular expression: {0}""")
483 .format(re.errorString())) 483 .format(re.errorString()))
484 return 484 return
485 offset = re.indexIn(text, startpos) 485 offset = re.indexIn(text, startpos)
486 captures = re.captureCount() 486 captures = re.captureCount()
487 row = 0 487 row = 0
490 self.resultTable.setColumnCount(0) 490 self.resultTable.setColumnCount(0)
491 self.resultTable.setColumnCount(3) 491 self.resultTable.setColumnCount(3)
492 self.resultTable.setRowCount(0) 492 self.resultTable.setRowCount(0)
493 self.resultTable.setRowCount(OFFSET) 493 self.resultTable.setRowCount(OFFSET)
494 self.resultTable.setItem( 494 self.resultTable.setItem(
495 row, 0, QTableWidgetItem(self.trUtf8("Regexp"))) 495 row, 0, QTableWidgetItem(self.tr("Regexp")))
496 self.resultTable.setItem(row, 1, QTableWidgetItem(regex)) 496 self.resultTable.setItem(row, 1, QTableWidgetItem(regex))
497 497
498 if offset != -1: 498 if offset != -1:
499 self.lastMatchEnd = offset + re.matchedLength() 499 self.lastMatchEnd = offset + re.matchedLength()
500 self.nextButton.setEnabled(True) 500 self.nextButton.setEnabled(True)
501 row += 1 501 row += 1
502 self.resultTable.setItem( 502 self.resultTable.setItem(
503 row, 0, QTableWidgetItem(self.trUtf8("Offset"))) 503 row, 0, QTableWidgetItem(self.tr("Offset")))
504 self.resultTable.setItem( 504 self.resultTable.setItem(
505 row, 1, QTableWidgetItem("{0:d}".format(offset))) 505 row, 1, QTableWidgetItem("{0:d}".format(offset)))
506 506
507 if not wildcard: 507 if not wildcard:
508 row += 1 508 row += 1
509 self.resultTable.setItem( 509 self.resultTable.setItem(
510 row, 0, QTableWidgetItem(self.trUtf8("Captures"))) 510 row, 0, QTableWidgetItem(self.tr("Captures")))
511 self.resultTable.setItem( 511 self.resultTable.setItem(
512 row, 1, QTableWidgetItem("{0:d}".format(captures))) 512 row, 1, QTableWidgetItem("{0:d}".format(captures)))
513 row += 1 513 row += 1
514 self.resultTable.setItem( 514 self.resultTable.setItem(
515 row, 1, QTableWidgetItem(self.trUtf8("Text"))) 515 row, 1, QTableWidgetItem(self.tr("Text")))
516 self.resultTable.setItem( 516 self.resultTable.setItem(
517 row, 2, QTableWidgetItem(self.trUtf8("Characters"))) 517 row, 2, QTableWidgetItem(self.tr("Characters")))
518 518
519 row += 1 519 row += 1
520 self.resultTable.setItem( 520 self.resultTable.setItem(
521 row, 0, QTableWidgetItem(self.trUtf8("Match"))) 521 row, 0, QTableWidgetItem(self.tr("Match")))
522 self.resultTable.setItem( 522 self.resultTable.setItem(
523 row, 1, QTableWidgetItem(re.cap(0))) 523 row, 1, QTableWidgetItem(re.cap(0)))
524 self.resultTable.setItem( 524 self.resultTable.setItem(
525 row, 2, 525 row, 2,
526 QTableWidgetItem("{0:d}".format(re.matchedLength()))) 526 QTableWidgetItem("{0:d}".format(re.matchedLength())))
531 row += 1 531 row += 1
532 self.resultTable.insertRow(row) 532 self.resultTable.insertRow(row)
533 self.resultTable.setItem( 533 self.resultTable.setItem(
534 row, 0, 534 row, 0,
535 QTableWidgetItem( 535 QTableWidgetItem(
536 self.trUtf8("Capture #{0}").format(i))) 536 self.tr("Capture #{0}").format(i)))
537 self.resultTable.setItem( 537 self.resultTable.setItem(
538 row, 1, 538 row, 1,
539 QTableWidgetItem(re.cap(i))) 539 QTableWidgetItem(re.cap(i)))
540 self.resultTable.setItem( 540 self.resultTable.setItem(
541 row, 2, 541 row, 2,
554 self.resultTable.setRowCount(2) 554 self.resultTable.setRowCount(2)
555 row += 1 555 row += 1
556 if startpos > 0: 556 if startpos > 0:
557 self.resultTable.setItem( 557 self.resultTable.setItem(
558 row, 0, 558 row, 0,
559 QTableWidgetItem(self.trUtf8("No more matches"))) 559 QTableWidgetItem(self.tr("No more matches")))
560 else: 560 else:
561 self.resultTable.setItem( 561 self.resultTable.setItem(
562 row, 0, 562 row, 0,
563 QTableWidgetItem(self.trUtf8("No matches"))) 563 QTableWidgetItem(self.tr("No matches")))
564 564
565 # remove the highlight 565 # remove the highlight
566 tc = self.textTextEdit.textCursor() 566 tc = self.textTextEdit.textCursor()
567 tc.setPosition(0) 567 tc.setPosition(0)
568 self.textTextEdit.setTextCursor(tc) 568 self.textTextEdit.setTextCursor(tc)
572 self.resultTable.verticalHeader().hide() 572 self.resultTable.verticalHeader().hide()
573 self.resultTable.horizontalHeader().hide() 573 self.resultTable.horizontalHeader().hide()
574 else: 574 else:
575 E5MessageBox.critical( 575 E5MessageBox.critical(
576 self, 576 self,
577 self.trUtf8("Error"), 577 self.tr("Error"),
578 self.trUtf8("""A regular expression and a text must""" 578 self.tr("""A regular expression and a text must"""
579 """ be given.""")) 579 """ be given."""))
580 580
581 @pyqtSlot() 581 @pyqtSlot()
582 def on_nextButton_clicked(self): 582 def on_nextButton_clicked(self):
583 """ 583 """
584 Private slot to find the next match. 584 Private slot to find the next match.
677 677
678 self.cw = QRegExpWizardWidget(self, fromEric) 678 self.cw = QRegExpWizardWidget(self, fromEric)
679 size = self.cw.size() 679 size = self.cw.size()
680 self.__layout.addWidget(self.cw) 680 self.__layout.addWidget(self.cw)
681 self.resize(size) 681 self.resize(size)
682 682 self.setWindowTitle(self.cw.windowTitle())
683 self.cw.buttonBox.accepted[()].connect(self.accept) 683
684 self.cw.buttonBox.rejected[()].connect(self.reject) 684 self.cw.buttonBox.accepted.connect(self.accept)
685 self.cw.buttonBox.rejected.connect(self.reject)
685 686
686 def getCode(self, indLevel, indString): 687 def getCode(self, indLevel, indString):
687 """ 688 """
688 Public method to get the source code. 689 Public method to get the source code.
689 690
707 super(QRegExpWizardWindow, self).__init__(parent) 708 super(QRegExpWizardWindow, self).__init__(parent)
708 self.cw = QRegExpWizardWidget(self, fromEric=False) 709 self.cw = QRegExpWizardWidget(self, fromEric=False)
709 size = self.cw.size() 710 size = self.cw.size()
710 self.setCentralWidget(self.cw) 711 self.setCentralWidget(self.cw)
711 self.resize(size) 712 self.resize(size)
713 self.setWindowTitle(self.cw.windowTitle())
712 714
713 self.setStyle( 715 self.setStyle(
714 Preferences.getUI("Style"), Preferences.getUI("StyleSheet")) 716 Preferences.getUI("Style"), Preferences.getUI("StyleSheet"))
715 717
716 self.cw.buttonBox.accepted[()].connect(self.close) 718 self.cw.buttonBox.accepted.connect(self.close)
717 self.cw.buttonBox.rejected[()].connect(self.close) 719 self.cw.buttonBox.rejected.connect(self.close)

eric ide

mercurial