17 from PyQt6.QtWidgets import ( |
17 from PyQt6.QtWidgets import ( |
18 QWidget, QDialog, QInputDialog, QApplication, QDialogButtonBox, |
18 QWidget, QDialog, QInputDialog, QApplication, QDialogButtonBox, |
19 QVBoxLayout, QTableWidgetItem |
19 QVBoxLayout, QTableWidgetItem |
20 ) |
20 ) |
21 |
21 |
22 from E5Gui import E5MessageBox, E5FileDialog |
22 from E5Gui import EricMessageBox, EricFileDialog |
23 from E5Gui.E5MainWindow import E5MainWindow |
23 from E5Gui.EricMainWindow import EricMainWindow |
24 |
24 |
25 from .Ui_QRegularExpressionWizardDialog import ( |
25 from .Ui_QRegularExpressionWizardDialog import ( |
26 Ui_QRegularExpressionWizardDialog |
26 Ui_QRegularExpressionWizardDialog |
27 ) |
27 ) |
28 |
28 |
167 if self.__pyqt5Server.waitForReadyRead(10000): |
167 if self.__pyqt5Server.waitForReadyRead(10000): |
168 data = bytes(self.__pyqt5Server.readAllStandardOutput()) |
168 data = bytes(self.__pyqt5Server.readAllStandardOutput()) |
169 responseStr = data.decode("utf-8") |
169 responseStr = data.decode("utf-8") |
170 responseDict = json.loads(responseStr) |
170 responseDict = json.loads(responseStr) |
171 if responseDict["error"]: |
171 if responseDict["error"]: |
172 E5MessageBox.critical( |
172 EricMessageBox.critical( |
173 self, |
173 self, |
174 self.tr("Communication Error"), |
174 self.tr("Communication Error"), |
175 self.tr("""<p>The PyQt5 backend reported""" |
175 self.tr("""<p>The PyQt5 backend reported""" |
176 """ an error.</p><p>{0}</p>""") |
176 """ an error.</p><p>{0}</p>""") |
177 .format(responseDict["error"])) |
177 .format(responseDict["error"])) |
284 # only present group names that occur before the current |
284 # only present group names that occur before the current |
285 # cursor position |
285 # cursor position |
286 regex = self.regexpTextEdit.toPlainText()[:length] |
286 regex = self.regexpTextEdit.toPlainText()[:length] |
287 names = self.namedGroups(regex) |
287 names = self.namedGroups(regex) |
288 if not names: |
288 if not names: |
289 E5MessageBox.information( |
289 EricMessageBox.information( |
290 self, |
290 self, |
291 self.tr("Named reference"), |
291 self.tr("Named reference"), |
292 self.tr("""No named groups have been defined yet.""")) |
292 self.tr("""No named groups have been defined yet.""")) |
293 return |
293 return |
294 |
294 |
400 @pyqtSlot() |
400 @pyqtSlot() |
401 def on_saveButton_clicked(self): |
401 def on_saveButton_clicked(self): |
402 """ |
402 """ |
403 Private slot to save the QRegularExpression to a file. |
403 Private slot to save the QRegularExpression to a file. |
404 """ |
404 """ |
405 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
405 fname, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( |
406 self, |
406 self, |
407 self.tr("Save regular expression"), |
407 self.tr("Save regular expression"), |
408 "", |
408 "", |
409 self.tr("RegExp Files (*.rx);;All Files (*)"), |
409 self.tr("RegExp Files (*.rx);;All Files (*)"), |
410 None, |
410 None, |
411 E5FileDialog.DontConfirmOverwrite) |
411 EricFileDialog.DontConfirmOverwrite) |
412 if fname: |
412 if fname: |
413 ext = QFileInfo(fname).suffix() |
413 ext = QFileInfo(fname).suffix() |
414 if not ext: |
414 if not ext: |
415 ex = selectedFilter.split("(*")[1].split(")")[0] |
415 ex = selectedFilter.split("(*")[1].split(")")[0] |
416 if ex: |
416 if ex: |
417 fname += ex |
417 fname += ex |
418 if QFileInfo(fname).exists(): |
418 if QFileInfo(fname).exists(): |
419 res = E5MessageBox.yesNo( |
419 res = EricMessageBox.yesNo( |
420 self, |
420 self, |
421 self.tr("Save regular expression"), |
421 self.tr("Save regular expression"), |
422 self.tr("<p>The file <b>{0}</b> already exists." |
422 self.tr("<p>The file <b>{0}</b> already exists." |
423 " Overwrite it?</p>").format(fname), |
423 " Overwrite it?</p>").format(fname), |
424 icon=E5MessageBox.Warning) |
424 icon=EricMessageBox.Warning) |
425 if not res: |
425 if not res: |
426 return |
426 return |
427 |
427 |
428 fname = Utilities.toNativeSeparators(fname) |
428 fname = Utilities.toNativeSeparators(fname) |
429 try: |
429 try: |
430 with open(fname, "w", encoding="utf-8") as f: |
430 with open(fname, "w", encoding="utf-8") as f: |
431 f.write(self.regexpTextEdit.toPlainText()) |
431 f.write(self.regexpTextEdit.toPlainText()) |
432 except OSError as err: |
432 except OSError as err: |
433 E5MessageBox.information( |
433 EricMessageBox.information( |
434 self, |
434 self, |
435 self.tr("Save regular expression"), |
435 self.tr("Save regular expression"), |
436 self.tr("""<p>The regular expression could not""" |
436 self.tr("""<p>The regular expression could not""" |
437 """ be saved.</p><p>Reason: {0}</p>""") |
437 """ be saved.</p><p>Reason: {0}</p>""") |
438 .format(str(err))) |
438 .format(str(err))) |
440 @pyqtSlot() |
440 @pyqtSlot() |
441 def on_loadButton_clicked(self): |
441 def on_loadButton_clicked(self): |
442 """ |
442 """ |
443 Private slot to load a QRegularExpression from a file. |
443 Private slot to load a QRegularExpression from a file. |
444 """ |
444 """ |
445 fname = E5FileDialog.getOpenFileName( |
445 fname = EricFileDialog.getOpenFileName( |
446 self, |
446 self, |
447 self.tr("Load regular expression"), |
447 self.tr("Load regular expression"), |
448 "", |
448 "", |
449 self.tr("RegExp Files (*.rx);;All Files (*)")) |
449 self.tr("RegExp Files (*.rx);;All Files (*)")) |
450 if fname: |
450 if fname: |
452 try: |
452 try: |
453 with open(fname, "r", encoding="utf-8") as f: |
453 with open(fname, "r", encoding="utf-8") as f: |
454 regexp = f.read() |
454 regexp = f.read() |
455 self.regexpTextEdit.setPlainText(regexp) |
455 self.regexpTextEdit.setPlainText(regexp) |
456 except OSError as err: |
456 except OSError as err: |
457 E5MessageBox.information( |
457 EricMessageBox.information( |
458 self, |
458 self, |
459 self.tr("Save regular expression"), |
459 self.tr("Save regular expression"), |
460 self.tr("""<p>The regular expression could not""" |
460 self.tr("""<p>The regular expression could not""" |
461 """ be saved.</p><p>Reason: {0}</p>""") |
461 """ be saved.</p><p>Reason: {0}</p>""") |
462 .format(str(err))) |
462 .format(str(err))) |
505 |
505 |
506 if self.__sendCommand("validate", options=options, regexp=regexp): |
506 if self.__sendCommand("validate", options=options, regexp=regexp): |
507 response = self.__receiveResponse() |
507 response = self.__receiveResponse() |
508 if response and "valid" in response: |
508 if response and "valid" in response: |
509 if response["valid"]: |
509 if response["valid"]: |
510 E5MessageBox.information( |
510 EricMessageBox.information( |
511 self, |
511 self, |
512 self.tr("Validation"), |
512 self.tr("Validation"), |
513 self.tr( |
513 self.tr( |
514 """The regular expression is valid.""")) |
514 """The regular expression is valid.""")) |
515 else: |
515 else: |
516 E5MessageBox.critical( |
516 EricMessageBox.critical( |
517 self, |
517 self, |
518 self.tr("Error"), |
518 self.tr("Error"), |
519 self.tr("""Invalid regular expression: {0}""") |
519 self.tr("""Invalid regular expression: {0}""") |
520 .format(response["errorMessage"])) |
520 .format(response["errorMessage"])) |
521 # move cursor to error offset |
521 # move cursor to error offset |
524 tc.setPosition(offset) |
524 tc.setPosition(offset) |
525 self.regexpTextEdit.setTextCursor(tc) |
525 self.regexpTextEdit.setTextCursor(tc) |
526 self.regexpTextEdit.setFocus() |
526 self.regexpTextEdit.setFocus() |
527 return |
527 return |
528 else: |
528 else: |
529 E5MessageBox.critical( |
529 EricMessageBox.critical( |
530 self, |
530 self, |
531 self.tr("Communication Error"), |
531 self.tr("Communication Error"), |
532 self.tr("""Invalid response received from""" |
532 self.tr("""Invalid response received from""" |
533 """ PyQt5 backend.""")) |
533 """ PyQt5 backend.""")) |
534 else: |
534 else: |
535 E5MessageBox.critical( |
535 EricMessageBox.critical( |
536 self, |
536 self, |
537 self.tr("Communication Error"), |
537 self.tr("Communication Error"), |
538 self.tr("""Communication with PyQt5 backend""" |
538 self.tr("""Communication with PyQt5 backend""" |
539 """ failed.""")) |
539 """ failed.""")) |
540 else: |
540 else: |
541 E5MessageBox.critical( |
541 EricMessageBox.critical( |
542 self, |
542 self, |
543 self.tr("Error"), |
543 self.tr("Error"), |
544 self.tr("""A regular expression must be given.""")) |
544 self.tr("""A regular expression must be given.""")) |
545 |
545 |
546 @pyqtSlot() |
546 @pyqtSlot() |
580 if self.__sendCommand("execute", options=options, regexp=regexp, |
580 if self.__sendCommand("execute", options=options, regexp=regexp, |
581 text=text, startpos=startpos): |
581 text=text, startpos=startpos): |
582 response = self.__receiveResponse() |
582 response = self.__receiveResponse() |
583 if response and ("valid" in response or "matched" in response): |
583 if response and ("valid" in response or "matched" in response): |
584 if "valid" in response: |
584 if "valid" in response: |
585 E5MessageBox.critical( |
585 EricMessageBox.critical( |
586 self, |
586 self, |
587 self.tr("Error"), |
587 self.tr("Error"), |
588 self.tr("""Invalid regular expression: {0}""") |
588 self.tr("""Invalid regular expression: {0}""") |
589 .format(response["errorMessage"])) |
589 .format(response["errorMessage"])) |
590 # move cursor to error offset |
590 # move cursor to error offset |
695 self.resultTable.resizeColumnsToContents() |
695 self.resultTable.resizeColumnsToContents() |
696 self.resultTable.resizeRowsToContents() |
696 self.resultTable.resizeRowsToContents() |
697 self.resultTable.verticalHeader().hide() |
697 self.resultTable.verticalHeader().hide() |
698 self.resultTable.horizontalHeader().hide() |
698 self.resultTable.horizontalHeader().hide() |
699 else: |
699 else: |
700 E5MessageBox.critical( |
700 EricMessageBox.critical( |
701 self, |
701 self, |
702 self.tr("Communication Error"), |
702 self.tr("Communication Error"), |
703 self.tr("""Invalid response received from""" |
703 self.tr("""Invalid response received from""" |
704 """ PyQt5 backend.""")) |
704 """ PyQt5 backend.""")) |
705 else: |
705 else: |
706 E5MessageBox.critical( |
706 EricMessageBox.critical( |
707 self, |
707 self, |
708 self.tr("Communication Error"), |
708 self.tr("Communication Error"), |
709 self.tr("""Communication with PyQt5""" |
709 self.tr("""Communication with PyQt5""" |
710 """ backend failed.""")) |
710 """ backend failed.""")) |
711 else: |
711 else: |
712 E5MessageBox.critical( |
712 EricMessageBox.critical( |
713 self, |
713 self, |
714 self.tr("Error"), |
714 self.tr("Error"), |
715 self.tr("""A regular expression and a text must""" |
715 self.tr("""A regular expression and a text must""" |
716 """ be given.""")) |
716 """ be given.""")) |
717 |
717 |