88 response = self.__receiveResponse() |
88 response = self.__receiveResponse() |
89 if response and response["available"]: |
89 if response and response["available"]: |
90 self.__pyqt5Available = True |
90 self.__pyqt5Available = True |
91 |
91 |
92 self.saveButton = self.buttonBox.addButton( |
92 self.saveButton = self.buttonBox.addButton( |
93 self.trUtf8("Save"), QDialogButtonBox.ActionRole) |
93 self.tr("Save"), QDialogButtonBox.ActionRole) |
94 self.saveButton.setToolTip( |
94 self.saveButton.setToolTip( |
95 self.trUtf8("Save the regular expression to a file")) |
95 self.tr("Save the regular expression to a file")) |
96 self.loadButton = self.buttonBox.addButton( |
96 self.loadButton = self.buttonBox.addButton( |
97 self.trUtf8("Load"), QDialogButtonBox.ActionRole) |
97 self.tr("Load"), QDialogButtonBox.ActionRole) |
98 self.loadButton.setToolTip( |
98 self.loadButton.setToolTip( |
99 self.trUtf8("Load a regular expression from a file")) |
99 self.tr("Load a regular expression from a file")) |
100 if qVersion() >= "5.0.0" and self.__pyqt5Available: |
100 if qVersion() >= "5.0.0" and self.__pyqt5Available: |
101 self.validateButton = self.buttonBox.addButton( |
101 self.validateButton = self.buttonBox.addButton( |
102 self.trUtf8("Validate"), QDialogButtonBox.ActionRole) |
102 self.tr("Validate"), QDialogButtonBox.ActionRole) |
103 self.validateButton.setToolTip( |
103 self.validateButton.setToolTip( |
104 self.trUtf8("Validate the regular expression")) |
104 self.tr("Validate the regular expression")) |
105 self.executeButton = self.buttonBox.addButton( |
105 self.executeButton = self.buttonBox.addButton( |
106 self.trUtf8("Execute"), QDialogButtonBox.ActionRole) |
106 self.tr("Execute"), QDialogButtonBox.ActionRole) |
107 self.executeButton.setToolTip( |
107 self.executeButton.setToolTip( |
108 self.trUtf8("Execute the regular expression")) |
108 self.tr("Execute the regular expression")) |
109 self.nextButton = self.buttonBox.addButton( |
109 self.nextButton = self.buttonBox.addButton( |
110 self.trUtf8("Next match"), QDialogButtonBox.ActionRole) |
110 self.tr("Next match"), QDialogButtonBox.ActionRole) |
111 self.nextButton.setToolTip( |
111 self.nextButton.setToolTip( |
112 self.trUtf8("Show the next match of the regular expression")) |
112 self.tr("Show the next match of the regular expression")) |
113 self.nextButton.setEnabled(False) |
113 self.nextButton.setEnabled(False) |
114 else: |
114 else: |
115 self.validateButton = None |
115 self.validateButton = None |
116 self.executeButton = None |
116 self.executeButton = None |
117 self.nextButton = None |
117 self.nextButton = None |
120 self.buttonBox.setStandardButtons( |
120 self.buttonBox.setStandardButtons( |
121 QDialogButtonBox.Cancel | QDialogButtonBox.Ok) |
121 QDialogButtonBox.Cancel | QDialogButtonBox.Ok) |
122 self.copyButton = None |
122 self.copyButton = None |
123 else: |
123 else: |
124 self.copyButton = self.buttonBox.addButton( |
124 self.copyButton = self.buttonBox.addButton( |
125 self.trUtf8("Copy"), QDialogButtonBox.ActionRole) |
125 self.tr("Copy"), QDialogButtonBox.ActionRole) |
126 self.copyButton.setToolTip( |
126 self.copyButton.setToolTip( |
127 self.trUtf8("Copy the regular expression to the clipboard")) |
127 self.tr("Copy the regular expression to the clipboard")) |
128 self.buttonBox.setStandardButtons(QDialogButtonBox.Close) |
128 self.buttonBox.setStandardButtons(QDialogButtonBox.Close) |
129 self.variableLabel.hide() |
129 self.variableLabel.hide() |
130 self.variableLineEdit.hide() |
130 self.variableLineEdit.hide() |
131 self.variableLine.hide() |
131 self.variableLine.hide() |
132 self.regexpTextEdit.setFocus() |
132 self.regexpTextEdit.setFocus() |
162 responseStr = data.decode("utf-8") |
162 responseStr = data.decode("utf-8") |
163 responseDict = json.loads(responseStr) |
163 responseDict = json.loads(responseStr) |
164 if responseDict["error"]: |
164 if responseDict["error"]: |
165 E5MessageBox.critical( |
165 E5MessageBox.critical( |
166 self, |
166 self, |
167 self.trUtf8("Communication Error"), |
167 self.tr("Communication Error"), |
168 self.trUtf8("""<p>The PyQt5 backend reported""" |
168 self.tr("""<p>The PyQt5 backend reported""" |
169 """ an error.</p><p>{0}</p>""") |
169 """ an error.</p><p>{0}</p>""") |
170 .format(responseDict["error"])) |
170 .format(responseDict["error"])) |
171 responseDict = {} |
171 responseDict = {} |
172 |
172 |
173 return responseDict |
173 return responseDict |
174 |
174 |
277 regex = self.regexpTextEdit.toPlainText()[:length] |
277 regex = self.regexpTextEdit.toPlainText()[:length] |
278 names = self.namedGroups(regex) |
278 names = self.namedGroups(regex) |
279 if not names: |
279 if not names: |
280 E5MessageBox.information( |
280 E5MessageBox.information( |
281 self, |
281 self, |
282 self.trUtf8("Named reference"), |
282 self.tr("Named reference"), |
283 self.trUtf8("""No named groups have been defined yet.""")) |
283 self.tr("""No named groups have been defined yet.""")) |
284 return |
284 return |
285 |
285 |
286 groupName, ok = QInputDialog.getItem( |
286 groupName, ok = QInputDialog.getItem( |
287 self, |
287 self, |
288 self.trUtf8("Named reference"), |
288 self.tr("Named reference"), |
289 self.trUtf8("Select group name:"), |
289 self.tr("Select group name:"), |
290 names, |
290 names, |
291 0, True) |
291 0, True) |
292 if ok and groupName: |
292 if ok and groupName: |
293 self.__insertString("(?P={0})".format(groupName)) |
293 self.__insertString("(?P={0})".format(groupName)) |
294 |
294 |
393 """ |
393 """ |
394 Private slot to save the QRegularExpression to a file. |
394 Private slot to save the QRegularExpression to a file. |
395 """ |
395 """ |
396 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
396 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
397 self, |
397 self, |
398 self.trUtf8("Save regular expression"), |
398 self.tr("Save regular expression"), |
399 "", |
399 "", |
400 self.trUtf8("RegExp Files (*.rx);;All Files (*)"), |
400 self.tr("RegExp Files (*.rx);;All Files (*)"), |
401 None, |
401 None, |
402 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
402 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
403 if fname: |
403 if fname: |
404 ext = QFileInfo(fname).suffix() |
404 ext = QFileInfo(fname).suffix() |
405 if not ext: |
405 if not ext: |
407 if ex: |
407 if ex: |
408 fname += ex |
408 fname += ex |
409 if QFileInfo(fname).exists(): |
409 if QFileInfo(fname).exists(): |
410 res = E5MessageBox.yesNo( |
410 res = E5MessageBox.yesNo( |
411 self, |
411 self, |
412 self.trUtf8("Save regular expression"), |
412 self.tr("Save regular expression"), |
413 self.trUtf8("<p>The file <b>{0}</b> already exists." |
413 self.tr("<p>The file <b>{0}</b> already exists." |
414 " Overwrite it?</p>").format(fname), |
414 " Overwrite it?</p>").format(fname), |
415 icon=E5MessageBox.Warning) |
415 icon=E5MessageBox.Warning) |
416 if not res: |
416 if not res: |
417 return |
417 return |
418 |
418 |
419 try: |
419 try: |
422 f.write(self.regexpTextEdit.toPlainText()) |
422 f.write(self.regexpTextEdit.toPlainText()) |
423 f.close() |
423 f.close() |
424 except IOError as err: |
424 except IOError as err: |
425 E5MessageBox.information( |
425 E5MessageBox.information( |
426 self, |
426 self, |
427 self.trUtf8("Save regular expression"), |
427 self.tr("Save regular expression"), |
428 self.trUtf8("""<p>The regular expression could not""" |
428 self.tr("""<p>The regular expression could not""" |
429 """ be saved.</p><p>Reason: {0}</p>""") |
429 """ be saved.</p><p>Reason: {0}</p>""") |
430 .format(str(err))) |
430 .format(str(err))) |
431 |
431 |
432 @pyqtSlot() |
432 @pyqtSlot() |
433 def on_loadButton_clicked(self): |
433 def on_loadButton_clicked(self): |
434 """ |
434 """ |
435 Private slot to load a QRegularExpression from a file. |
435 Private slot to load a QRegularExpression from a file. |
436 """ |
436 """ |
437 fname = E5FileDialog.getOpenFileName( |
437 fname = E5FileDialog.getOpenFileName( |
438 self, |
438 self, |
439 self.trUtf8("Load regular expression"), |
439 self.tr("Load regular expression"), |
440 "", |
440 "", |
441 self.trUtf8("RegExp Files (*.rx);;All Files (*)")) |
441 self.tr("RegExp Files (*.rx);;All Files (*)")) |
442 if fname: |
442 if fname: |
443 try: |
443 try: |
444 f = open( |
444 f = open( |
445 Utilities.toNativeSeparators(fname), "r", encoding="utf-8") |
445 Utilities.toNativeSeparators(fname), "r", encoding="utf-8") |
446 regexp = f.read() |
446 regexp = f.read() |
447 f.close() |
447 f.close() |
448 self.regexpTextEdit.setPlainText(regexp) |
448 self.regexpTextEdit.setPlainText(regexp) |
449 except IOError as err: |
449 except IOError as err: |
450 E5MessageBox.information( |
450 E5MessageBox.information( |
451 self, |
451 self, |
452 self.trUtf8("Save regular expression"), |
452 self.tr("Save regular expression"), |
453 self.trUtf8("""<p>The regular expression could not""" |
453 self.tr("""<p>The regular expression could not""" |
454 """ be saved.</p><p>Reason: {0}</p>""") |
454 """ be saved.</p><p>Reason: {0}</p>""") |
455 .format(str(err))) |
455 .format(str(err))) |
456 |
456 |
457 @pyqtSlot() |
457 @pyqtSlot() |
458 def on_copyButton_clicked(self): |
458 def on_copyButton_clicked(self): |
459 """ |
459 """ |
500 response = self.__receiveResponse() |
500 response = self.__receiveResponse() |
501 if response and "valid" in response: |
501 if response and "valid" in response: |
502 if response["valid"]: |
502 if response["valid"]: |
503 E5MessageBox.information( |
503 E5MessageBox.information( |
504 self, |
504 self, |
505 self.trUtf8("Validation"), |
505 self.tr("Validation"), |
506 self.trUtf8( |
506 self.tr( |
507 """The regular expression is valid.""")) |
507 """The regular expression is valid.""")) |
508 else: |
508 else: |
509 E5MessageBox.critical( |
509 E5MessageBox.critical( |
510 self, |
510 self, |
511 self.trUtf8("Error"), |
511 self.tr("Error"), |
512 self.trUtf8("""Invalid regular expression: {0}""") |
512 self.tr("""Invalid regular expression: {0}""") |
513 .format(response["errorMessage"])) |
513 .format(response["errorMessage"])) |
514 # move cursor to error offset |
514 # move cursor to error offset |
515 offset = response["errorOffset"] |
515 offset = response["errorOffset"] |
516 tc = self.regexpTextEdit.textCursor() |
516 tc = self.regexpTextEdit.textCursor() |
517 tc.setPosition(offset) |
517 tc.setPosition(offset) |
519 self.regexpTextEdit.setFocus() |
519 self.regexpTextEdit.setFocus() |
520 return |
520 return |
521 else: |
521 else: |
522 E5MessageBox.critical( |
522 E5MessageBox.critical( |
523 self, |
523 self, |
524 self.trUtf8("Communication Error"), |
524 self.tr("Communication Error"), |
525 self.trUtf8("""Invalid response received from""" |
525 self.tr("""Invalid response received from""" |
526 """ PyQt5 backend.""")) |
526 """ PyQt5 backend.""")) |
527 else: |
527 else: |
528 E5MessageBox.critical( |
528 E5MessageBox.critical( |
529 self, |
529 self, |
530 self.trUtf8("Communication Error"), |
530 self.tr("Communication Error"), |
531 self.trUtf8("""Communication with PyQt5 backend""" |
531 self.tr("""Communication with PyQt5 backend""" |
532 """ failed.""")) |
532 """ failed.""")) |
533 else: |
533 else: |
534 E5MessageBox.critical( |
534 E5MessageBox.critical( |
535 self, |
535 self, |
536 self.trUtf8("Error"), |
536 self.tr("Error"), |
537 self.trUtf8("""A regular expression must be given.""")) |
537 self.tr("""A regular expression must be given.""")) |
538 |
538 |
539 @pyqtSlot() |
539 @pyqtSlot() |
540 def on_executeButton_clicked(self, startpos=0): |
540 def on_executeButton_clicked(self, startpos=0): |
541 """ |
541 """ |
542 Private slot to execute the entered QRegularExpression on the test |
542 Private slot to execute the entered QRegularExpression on the test |
575 response = self.__receiveResponse() |
575 response = self.__receiveResponse() |
576 if response and ("valid" in response or "matched" in response): |
576 if response and ("valid" in response or "matched" in response): |
577 if "valid" in response: |
577 if "valid" in response: |
578 E5MessageBox.critical( |
578 E5MessageBox.critical( |
579 self, |
579 self, |
580 self.trUtf8("Error"), |
580 self.tr("Error"), |
581 self.trUtf8("""Invalid regular expression: {0}""") |
581 self.tr("""Invalid regular expression: {0}""") |
582 .format(response["errorMessage"])) |
582 .format(response["errorMessage"])) |
583 # move cursor to error offset |
583 # move cursor to error offset |
584 offset = response["errorOffset"] |
584 offset = response["errorOffset"] |
585 tc = self.regexpTextEdit.textCursor() |
585 tc = self.regexpTextEdit.textCursor() |
586 tc.setPosition(offset) |
586 tc.setPosition(offset) |
594 self.resultTable.setColumnCount(0) |
594 self.resultTable.setColumnCount(0) |
595 self.resultTable.setColumnCount(3) |
595 self.resultTable.setColumnCount(3) |
596 self.resultTable.setRowCount(0) |
596 self.resultTable.setRowCount(0) |
597 self.resultTable.setRowCount(OFFSET) |
597 self.resultTable.setRowCount(OFFSET) |
598 self.resultTable.setItem( |
598 self.resultTable.setItem( |
599 row, 0, QTableWidgetItem(self.trUtf8("Regexp"))) |
599 row, 0, QTableWidgetItem(self.tr("Regexp"))) |
600 self.resultTable.setItem( |
600 self.resultTable.setItem( |
601 row, 1, QTableWidgetItem(regexp)) |
601 row, 1, QTableWidgetItem(regexp)) |
602 if response["matched"]: |
602 if response["matched"]: |
603 captures = response["captures"] |
603 captures = response["captures"] |
604 # index 0 is the complete match |
604 # index 0 is the complete match |
606 self.lastMatchEnd = captures[0][2] |
606 self.lastMatchEnd = captures[0][2] |
607 self.nextButton.setEnabled(True) |
607 self.nextButton.setEnabled(True) |
608 row += 1 |
608 row += 1 |
609 self.resultTable.setItem( |
609 self.resultTable.setItem( |
610 row, 0, |
610 row, 0, |
611 QTableWidgetItem(self.trUtf8("Offset"))) |
611 QTableWidgetItem(self.tr("Offset"))) |
612 self.resultTable.setItem( |
612 self.resultTable.setItem( |
613 row, 1, |
613 row, 1, |
614 QTableWidgetItem("{0:d}".format(offset))) |
614 QTableWidgetItem("{0:d}".format(offset))) |
615 |
615 |
616 row += 1 |
616 row += 1 |
617 self.resultTable.setItem( |
617 self.resultTable.setItem( |
618 row, 0, |
618 row, 0, |
619 QTableWidgetItem(self.trUtf8("Captures"))) |
619 QTableWidgetItem(self.tr("Captures"))) |
620 self.resultTable.setItem( |
620 self.resultTable.setItem( |
621 row, 1, |
621 row, 1, |
622 QTableWidgetItem( |
622 QTableWidgetItem( |
623 "{0:d}".format(len(captures) - 1))) |
623 "{0:d}".format(len(captures) - 1))) |
624 row += 1 |
624 row += 1 |
625 self.resultTable.setItem( |
625 self.resultTable.setItem( |
626 row, 1, |
626 row, 1, |
627 QTableWidgetItem(self.trUtf8("Text"))) |
627 QTableWidgetItem(self.tr("Text"))) |
628 self.resultTable.setItem( |
628 self.resultTable.setItem( |
629 row, 2, |
629 row, 2, |
630 QTableWidgetItem(self.trUtf8("Characters"))) |
630 QTableWidgetItem(self.tr("Characters"))) |
631 |
631 |
632 row += 1 |
632 row += 1 |
633 self.resultTable.setItem( |
633 self.resultTable.setItem( |
634 row, 0, |
634 row, 0, |
635 QTableWidgetItem(self.trUtf8("Match"))) |
635 QTableWidgetItem(self.tr("Match"))) |
636 self.resultTable.setItem( |
636 self.resultTable.setItem( |
637 row, 1, |
637 row, 1, |
638 QTableWidgetItem(captures[0][0])) |
638 QTableWidgetItem(captures[0][0])) |
639 self.resultTable.setItem( |
639 self.resultTable.setItem( |
640 row, 2, |
640 row, 2, |
670 row += 1 |
670 row += 1 |
671 if startpos > 0: |
671 if startpos > 0: |
672 self.resultTable.setItem( |
672 self.resultTable.setItem( |
673 row, 0, |
673 row, 0, |
674 QTableWidgetItem( |
674 QTableWidgetItem( |
675 self.trUtf8("No more matches"))) |
675 self.tr("No more matches"))) |
676 else: |
676 else: |
677 self.resultTable.setItem( |
677 self.resultTable.setItem( |
678 row, 0, |
678 row, 0, |
679 QTableWidgetItem( |
679 QTableWidgetItem( |
680 self.trUtf8("No matches"))) |
680 self.tr("No matches"))) |
681 |
681 |
682 # remove the highlight |
682 # remove the highlight |
683 tc = self.textTextEdit.textCursor() |
683 tc = self.textTextEdit.textCursor() |
684 tc.setPosition(0) |
684 tc.setPosition(0) |
685 self.textTextEdit.setTextCursor(tc) |
685 self.textTextEdit.setTextCursor(tc) |
689 self.resultTable.verticalHeader().hide() |
689 self.resultTable.verticalHeader().hide() |
690 self.resultTable.horizontalHeader().hide() |
690 self.resultTable.horizontalHeader().hide() |
691 else: |
691 else: |
692 E5MessageBox.critical( |
692 E5MessageBox.critical( |
693 self, |
693 self, |
694 self.trUtf8("Communication Error"), |
694 self.tr("Communication Error"), |
695 self.trUtf8("""Invalid response received from""" |
695 self.tr("""Invalid response received from""" |
696 """ PyQt5 backend.""")) |
696 """ PyQt5 backend.""")) |
697 else: |
697 else: |
698 E5MessageBox.critical( |
698 E5MessageBox.critical( |
699 self, |
699 self, |
700 self.trUtf8("Communication Error"), |
700 self.tr("Communication Error"), |
701 self.trUtf8("""Communication with PyQt5""" |
701 self.tr("""Communication with PyQt5""" |
702 """ backend failed.""")) |
702 """ backend failed.""")) |
703 else: |
703 else: |
704 E5MessageBox.critical( |
704 E5MessageBox.critical( |
705 self, |
705 self, |
706 self.trUtf8("Error"), |
706 self.tr("Error"), |
707 self.trUtf8("""A regular expression and a text must""" |
707 self.tr("""A regular expression and a text must""" |
708 """ be given.""")) |
708 """ be given.""")) |
709 |
709 |
710 @pyqtSlot() |
710 @pyqtSlot() |
711 def on_nextButton_clicked(self): |
711 def on_nextButton_clicked(self): |
712 """ |
712 """ |
713 Private slot to find the next match. |
713 Private slot to find the next match. |