Plugins/WizardPlugins/PyRegExpWizard/PyRegExpWizardDialog.py

changeset 945
8cd4d08fa9f6
parent 882
34b86be88bf0
child 1112
8a7d1b9d18db
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
21 from .PyRegExpWizardCharactersDialog import PyRegExpWizardCharactersDialog 21 from .PyRegExpWizardCharactersDialog import PyRegExpWizardCharactersDialog
22 22
23 import UI.PixmapCache 23 import UI.PixmapCache
24 24
25 import Utilities 25 import Utilities
26
26 27
27 class PyRegExpWizardWidget(QWidget, Ui_PyRegExpWizardDialog): 28 class PyRegExpWizardWidget(QWidget, Ui_PyRegExpWizardDialog):
28 """ 29 """
29 Class implementing the Python re wizard dialog. 30 Class implementing the Python re wizard dialog.
30 """ 31 """
31 def __init__(self, parent = None, fromEric = True): 32 def __init__(self, parent=None, fromEric=True):
32 """ 33 """
33 Constructor 34 Constructor
34 35
35 @param parent parent widget (QWidget) 36 @param parent parent widget (QWidget)
36 @param fromEric flag indicating a call from within eric5 37 @param fromEric flag indicating a call from within eric5
37 """ 38 """
38 QWidget.__init__(self,parent) 39 QWidget.__init__(self, parent)
39 self.setupUi(self) 40 self.setupUi(self)
40 41
41 # initialize icons of the tool buttons 42 # initialize icons of the tool buttons
42 self.commentButton.setIcon(UI.PixmapCache.getIcon("comment.png")) 43 self.commentButton.setIcon(UI.PixmapCache.getIcon("comment.png"))
43 self.charButton.setIcon(UI.PixmapCache.getIcon("characters.png")) 44 self.charButton.setIcon(UI.PixmapCache.getIcon("characters.png"))
72 self.validateButton.setToolTip(self.trUtf8("Validate the regular expression")) 73 self.validateButton.setToolTip(self.trUtf8("Validate the regular expression"))
73 self.executeButton = \ 74 self.executeButton = \
74 self.buttonBox.addButton(self.trUtf8("Execute"), QDialogButtonBox.ActionRole) 75 self.buttonBox.addButton(self.trUtf8("Execute"), QDialogButtonBox.ActionRole)
75 self.executeButton.setToolTip(self.trUtf8("Execute the regular expression")) 76 self.executeButton.setToolTip(self.trUtf8("Execute the regular expression"))
76 self.nextButton = \ 77 self.nextButton = \
77 self.buttonBox.addButton(self.trUtf8("Next match"), 78 self.buttonBox.addButton(self.trUtf8("Next match"),
78 QDialogButtonBox.ActionRole) 79 QDialogButtonBox.ActionRole)
79 self.nextButton.setToolTip( 80 self.nextButton.setToolTip(
80 self.trUtf8("Show the next match of the regular expression")) 81 self.trUtf8("Show the next match of the regular expression"))
81 self.nextButton.setEnabled(False) 82 self.nextButton.setEnabled(False)
82 83
94 self.variableLineEdit.hide() 95 self.variableLineEdit.hide()
95 self.variableLine.hide() 96 self.variableLine.hide()
96 self.importCheckBox.hide() 97 self.importCheckBox.hide()
97 self.regexpTextEdit.setFocus() 98 self.regexpTextEdit.setFocus()
98 99
99 def __insertString(self, s, steps = 0): 100 def __insertString(self, s, steps=0):
100 """ 101 """
101 Private method to insert a string into line edit and move cursor. 102 Private method to insert a string into line edit and move cursor.
102 103
103 @param s string to be inserted into the regexp line edit 104 @param s string to be inserted into the regexp line edit
104 (string) 105 (string)
313 if QFileInfo(fname).exists(): 314 if QFileInfo(fname).exists():
314 res = E5MessageBox.yesNo(self, 315 res = E5MessageBox.yesNo(self,
315 self.trUtf8("Save regular expression"), 316 self.trUtf8("Save regular expression"),
316 self.trUtf8("<p>The file <b>{0}</b> already exists." 317 self.trUtf8("<p>The file <b>{0}</b> already exists."
317 " Overwrite it?</p>").format(fname), 318 " Overwrite it?</p>").format(fname),
318 icon = E5MessageBox.Warning) 319 icon=E5MessageBox.Warning)
319 if not res: 320 if not res:
320 return 321 return
321 322
322 try: 323 try:
323 f=open(Utilities.toNativeSeparators(fname), "w", encoding = "utf-8") 324 f = open(Utilities.toNativeSeparators(fname), "w", encoding="utf-8")
324 f.write(self.regexpTextEdit.toPlainText()) 325 f.write(self.regexpTextEdit.toPlainText())
325 f.close() 326 f.close()
326 except IOError as err: 327 except IOError as err:
327 E5MessageBox.information(self, 328 E5MessageBox.information(self,
328 self.trUtf8("Save regular expression"), 329 self.trUtf8("Save regular expression"),
339 self.trUtf8("Load regular expression"), 340 self.trUtf8("Load regular expression"),
340 "", 341 "",
341 self.trUtf8("RegExp Files (*.rx);;All Files (*)")) 342 self.trUtf8("RegExp Files (*.rx);;All Files (*)"))
342 if fname: 343 if fname:
343 try: 344 try:
344 f=open(Utilities.toNativeSeparators(fname), "r", encoding = "utf-8") 345 f = open(Utilities.toNativeSeparators(fname), "r", encoding="utf-8")
345 regexp = f.read() 346 regexp = f.read()
346 f.close() 347 f.close()
347 self.regexpTextEdit.setPlainText(regexp) 348 self.regexpTextEdit.setPlainText(regexp)
348 except IOError as err: 349 except IOError as err:
349 E5MessageBox.information(self, 350 E5MessageBox.information(self,
407 E5MessageBox.critical(self, 408 E5MessageBox.critical(self,
408 self.trUtf8("Error"), 409 self.trUtf8("Error"),
409 self.trUtf8("""A regular expression must be given.""")) 410 self.trUtf8("""A regular expression must be given."""))
410 411
411 @pyqtSlot() 412 @pyqtSlot()
412 def on_executeButton_clicked(self, startpos = 0): 413 def on_executeButton_clicked(self, startpos=0):
413 """ 414 """
414 Private slot to execute the entered regexp on the test text. 415 Private slot to execute the entered regexp on the test text.
415 416
416 This slot will execute the entered regexp on the entered test 417 This slot will execute the entered regexp on the entered test
417 data and will display the result in the table part of the dialog. 418 data and will display the result in the table part of the dialog.
457 if matchobj is not None: 458 if matchobj is not None:
458 offset = matchobj.start() 459 offset = matchobj.start()
459 self.lastMatchEnd = matchobj.end() 460 self.lastMatchEnd = matchobj.end()
460 self.nextButton.setEnabled(True) 461 self.nextButton.setEnabled(True)
461 row += 1 462 row += 1
462 self.resultTable.setItem(row, 0, 463 self.resultTable.setItem(row, 0,
463 QTableWidgetItem(self.trUtf8("Offset"))) 464 QTableWidgetItem(self.trUtf8("Offset")))
464 self.resultTable.setItem(row, 1, 465 self.resultTable.setItem(row, 1,
465 QTableWidgetItem("{0:d}".format(matchobj.start(0)))) 466 QTableWidgetItem("{0:d}".format(matchobj.start(0))))
466 467
467 row += 1 468 row += 1
468 self.resultTable.setItem(row, 0, 469 self.resultTable.setItem(row, 0,
469 QTableWidgetItem(self.trUtf8("Captures"))) 470 QTableWidgetItem(self.trUtf8("Captures")))
470 self.resultTable.setItem(row, 1, 471 self.resultTable.setItem(row, 1,
471 QTableWidgetItem("{0:d}".format(captures))) 472 QTableWidgetItem("{0:d}".format(captures)))
472 row += 1 473 row += 1
473 self.resultTable.setItem(row, 1, 474 self.resultTable.setItem(row, 1,
474 QTableWidgetItem(self.trUtf8("Text"))) 475 QTableWidgetItem(self.trUtf8("Text")))
475 self.resultTable.setItem(row, 2, 476 self.resultTable.setItem(row, 2,
476 QTableWidgetItem(self.trUtf8("Characters"))) 477 QTableWidgetItem(self.trUtf8("Characters")))
477 478
478 row += 1 479 row += 1
479 self.resultTable.setItem(row, 0, 480 self.resultTable.setItem(row, 0,
480 QTableWidgetItem(self.trUtf8("Match"))) 481 QTableWidgetItem(self.trUtf8("Match")))
481 self.resultTable.setItem(row, 1, 482 self.resultTable.setItem(row, 1,
482 QTableWidgetItem(matchobj.group(0))) 483 QTableWidgetItem(matchobj.group(0)))
483 self.resultTable.setItem(row, 2, 484 self.resultTable.setItem(row, 2,
484 QTableWidgetItem("{0:d}".format(len(matchobj.group(0))))) 485 QTableWidgetItem("{0:d}".format(len(matchobj.group(0)))))
485 486
486 for i in range(1, captures + 1): 487 for i in range(1, captures + 1):
487 if matchobj.group(i) is not None: 488 if matchobj.group(i) is not None:
488 row += 1 489 row += 1
489 self.resultTable.insertRow(row) 490 self.resultTable.insertRow(row)
490 self.resultTable.setItem(row, 0, 491 self.resultTable.setItem(row, 0,
491 QTableWidgetItem(self.trUtf8("Capture #{0}").format(i))) 492 QTableWidgetItem(self.trUtf8("Capture #{0}").format(i)))
492 self.resultTable.setItem(row, 1, 493 self.resultTable.setItem(row, 1,
493 QTableWidgetItem(matchobj.group(i))) 494 QTableWidgetItem(matchobj.group(i)))
494 self.resultTable.setItem(row, 2, 495 self.resultTable.setItem(row, 2,
495 QTableWidgetItem("{0:d}".format(len(matchobj.group(i))))) 496 QTableWidgetItem("{0:d}".format(len(matchobj.group(i)))))
496 497
497 # highlight the matched text 498 # highlight the matched text
498 tc = self.textTextEdit.textCursor() 499 tc = self.textTextEdit.textCursor()
499 tc.setPosition(offset) 500 tc.setPosition(offset)
502 else: 503 else:
503 self.nextButton.setEnabled(False) 504 self.nextButton.setEnabled(False)
504 self.resultTable.setRowCount(2) 505 self.resultTable.setRowCount(2)
505 row += 1 506 row += 1
506 if startpos > 0: 507 if startpos > 0:
507 self.resultTable.setItem(row, 0, 508 self.resultTable.setItem(row, 0,
508 QTableWidgetItem(self.trUtf8("No more matches"))) 509 QTableWidgetItem(self.trUtf8("No more matches")))
509 else: 510 else:
510 self.resultTable.setItem(row, 0, 511 self.resultTable.setItem(row, 0,
511 QTableWidgetItem(self.trUtf8("No matches"))) 512 QTableWidgetItem(self.trUtf8("No matches")))
512 513
513 # remove the highlight 514 # remove the highlight
514 tc = self.textTextEdit.textCursor() 515 tc = self.textTextEdit.textCursor()
515 tc.setPosition(0) 516 tc.setPosition(0)
621 if flags: 622 if flags:
622 code += ', {0}{1}{2}'.format(os.linesep, i1string, flags) 623 code += ', {0}{1}{2}'.format(os.linesep, i1string, flags)
623 code += '){0}'.format(estring) 624 code += '){0}'.format(estring)
624 return code 625 return code
625 626
627
626 class PyRegExpWizardDialog(QDialog): 628 class PyRegExpWizardDialog(QDialog):
627 """ 629 """
628 Class for the dialog variant. 630 Class for the dialog variant.
629 """ 631 """
630 def __init__(self, parent = None, fromEric = True): 632 def __init__(self, parent=None, fromEric=True):
631 """ 633 """
632 Constructor 634 Constructor
633 635
634 @param parent parent widget (QWidget) 636 @param parent parent widget (QWidget)
635 @param fromEric flag indicating a call from within eric5 637 @param fromEric flag indicating a call from within eric5
658 @param indString string used for indentation (space or tab) (string) 660 @param indString string used for indentation (space or tab) (string)
659 @return generated code (string) 661 @return generated code (string)
660 """ 662 """
661 return self.cw.getCode(indLevel, indString) 663 return self.cw.getCode(indLevel, indString)
662 664
665
663 class PyRegExpWizardWindow(QMainWindow): 666 class PyRegExpWizardWindow(QMainWindow):
664 """ 667 """
665 Main window class for the standalone dialog. 668 Main window class for the standalone dialog.
666 """ 669 """
667 def __init__(self, parent = None): 670 def __init__(self, parent=None):
668 """ 671 """
669 Constructor 672 Constructor
670 673
671 @param parent reference to the parent widget (QWidget) 674 @param parent reference to the parent widget (QWidget)
672 """ 675 """
673 QMainWindow.__init__(self, parent) 676 QMainWindow.__init__(self, parent)
674 self.cw = PyRegExpWizardWidget(self, fromEric = False) 677 self.cw = PyRegExpWizardWidget(self, fromEric=False)
675 size = self.cw.size() 678 size = self.cw.size()
676 self.setCentralWidget(self.cw) 679 self.setCentralWidget(self.cw)
677 self.resize(size) 680 self.resize(size)
678 681
679 self.cw.buttonBox.accepted[()].connect(self.close) 682 self.cw.buttonBox.accepted[()].connect(self.close)

eric ide

mercurial