20 from .QRegExpWizardCharactersDialog import QRegExpWizardCharactersDialog |
20 from .QRegExpWizardCharactersDialog import QRegExpWizardCharactersDialog |
21 |
21 |
22 import UI.PixmapCache |
22 import UI.PixmapCache |
23 |
23 |
24 import Utilities |
24 import Utilities |
|
25 |
25 |
26 |
26 class QRegExpWizardWidget(QWidget, Ui_QRegExpWizardDialog): |
27 class QRegExpWizardWidget(QWidget, Ui_QRegExpWizardDialog): |
27 """ |
28 """ |
28 Class implementing the QRegExp wizard dialog. |
29 Class implementing the QRegExp wizard dialog. |
29 """ |
30 """ |
30 def __init__(self, parent = None, fromEric = True): |
31 def __init__(self, parent=None, fromEric=True): |
31 """ |
32 """ |
32 Constructor |
33 Constructor |
33 |
34 |
34 @param parent parent widget (QWidget) |
35 @param parent parent widget (QWidget) |
35 @param fromEric flag indicating a call from within eric5 |
36 @param fromEric flag indicating a call from within eric5 |
36 """ |
37 """ |
37 QWidget.__init__(self,parent) |
38 QWidget.__init__(self, parent) |
38 self.setupUi(self) |
39 self.setupUi(self) |
39 |
40 |
40 # initialize icons of the tool buttons |
41 # initialize icons of the tool buttons |
41 self.charButton.setIcon(UI.PixmapCache.getIcon("characters.png")) |
42 self.charButton.setIcon(UI.PixmapCache.getIcon("characters.png")) |
42 self.anycharButton.setIcon(UI.PixmapCache.getIcon("anychar.png")) |
43 self.anycharButton.setIcon(UI.PixmapCache.getIcon("anychar.png")) |
64 self.validateButton.setToolTip(self.trUtf8("Validate the regular expression")) |
65 self.validateButton.setToolTip(self.trUtf8("Validate the regular expression")) |
65 self.executeButton = \ |
66 self.executeButton = \ |
66 self.buttonBox.addButton(self.trUtf8("Execute"), QDialogButtonBox.ActionRole) |
67 self.buttonBox.addButton(self.trUtf8("Execute"), QDialogButtonBox.ActionRole) |
67 self.executeButton.setToolTip(self.trUtf8("Execute the regular expression")) |
68 self.executeButton.setToolTip(self.trUtf8("Execute the regular expression")) |
68 self.nextButton = \ |
69 self.nextButton = \ |
69 self.buttonBox.addButton(self.trUtf8("Next match"), |
70 self.buttonBox.addButton(self.trUtf8("Next match"), |
70 QDialogButtonBox.ActionRole) |
71 QDialogButtonBox.ActionRole) |
71 self.nextButton.setToolTip( |
72 self.nextButton.setToolTip( |
72 self.trUtf8("Show the next match of the regular expression")) |
73 self.trUtf8("Show the next match of the regular expression")) |
73 self.nextButton.setEnabled(False) |
74 self.nextButton.setEnabled(False) |
74 |
75 |
227 if QFileInfo(fname).exists(): |
228 if QFileInfo(fname).exists(): |
228 res = E5MessageBox.yesNo(self, |
229 res = E5MessageBox.yesNo(self, |
229 self.trUtf8("Save regular expression"), |
230 self.trUtf8("Save regular expression"), |
230 self.trUtf8("<p>The file <b>{0}</b> already exists." |
231 self.trUtf8("<p>The file <b>{0}</b> already exists." |
231 " Overwrite it?</p>").format(fname), |
232 " Overwrite it?</p>").format(fname), |
232 icon = E5MessageBox.Warning) |
233 icon=E5MessageBox.Warning) |
233 if not res: |
234 if not res: |
234 return |
235 return |
235 |
236 |
236 try: |
237 try: |
237 f=open(Utilities.toNativeSeparators(fname), "w", encoding = "utf-8") |
238 f = open(Utilities.toNativeSeparators(fname), "w", encoding="utf-8") |
238 f.write(self.regexpLineEdit.text()) |
239 f.write(self.regexpLineEdit.text()) |
239 f.close() |
240 f.close() |
240 except IOError as err: |
241 except IOError as err: |
241 E5MessageBox.information(self, |
242 E5MessageBox.information(self, |
242 self.trUtf8("Save regular expression"), |
243 self.trUtf8("Save regular expression"), |
253 self.trUtf8("Load regular expression"), |
254 self.trUtf8("Load regular expression"), |
254 "", |
255 "", |
255 self.trUtf8("RegExp Files (*.rx);;All Files (*)")) |
256 self.trUtf8("RegExp Files (*.rx);;All Files (*)")) |
256 if fname: |
257 if fname: |
257 try: |
258 try: |
258 f=open(Utilities.toNativeSeparators(fname), "r", encoding = "utf-8") |
259 f = open(Utilities.toNativeSeparators(fname), "r", encoding="utf-8") |
259 regexp = f.read() |
260 regexp = f.read() |
260 f.close() |
261 f.close() |
261 self.regexpLineEdit.setText(regexp) |
262 self.regexpLineEdit.setText(regexp) |
262 except IOError as err: |
263 except IOError as err: |
263 E5MessageBox.information(self, |
264 E5MessageBox.information(self, |
311 E5MessageBox.critical(self, |
312 E5MessageBox.critical(self, |
312 self.trUtf8("Error"), |
313 self.trUtf8("Error"), |
313 self.trUtf8("""A regular expression must be given.""")) |
314 self.trUtf8("""A regular expression must be given.""")) |
314 |
315 |
315 @pyqtSlot() |
316 @pyqtSlot() |
316 def on_executeButton_clicked(self, startpos = 0): |
317 def on_executeButton_clicked(self, startpos=0): |
317 """ |
318 """ |
318 Private slot to execute the entered regexp on the test text. |
319 Private slot to execute the entered regexp on the test text. |
319 |
320 |
320 This slot will execute the entered regexp on the entered test |
321 This slot will execute the entered regexp on the entered test |
321 data and will display the result in the table part of the dialog. |
322 data and will display the result in the table part of the dialog. |
361 self.resultTable.setItem(row, 0, QTableWidgetItem(self.trUtf8("Offset"))) |
362 self.resultTable.setItem(row, 0, QTableWidgetItem(self.trUtf8("Offset"))) |
362 self.resultTable.setItem(row, 1, QTableWidgetItem("{0:d}".format(offset))) |
363 self.resultTable.setItem(row, 1, QTableWidgetItem("{0:d}".format(offset))) |
363 |
364 |
364 if not wildcard: |
365 if not wildcard: |
365 row += 1 |
366 row += 1 |
366 self.resultTable.setItem(row, 0, |
367 self.resultTable.setItem(row, 0, |
367 QTableWidgetItem(self.trUtf8("Captures"))) |
368 QTableWidgetItem(self.trUtf8("Captures"))) |
368 self.resultTable.setItem(row, 1, |
369 self.resultTable.setItem(row, 1, |
369 QTableWidgetItem("{0:d}".format(captures))) |
370 QTableWidgetItem("{0:d}".format(captures))) |
370 row += 1 |
371 row += 1 |
371 self.resultTable.setItem(row, 1, |
372 self.resultTable.setItem(row, 1, |
372 QTableWidgetItem(self.trUtf8("Text"))) |
373 QTableWidgetItem(self.trUtf8("Text"))) |
373 self.resultTable.setItem(row, 2, |
374 self.resultTable.setItem(row, 2, |
374 QTableWidgetItem(self.trUtf8("Characters"))) |
375 QTableWidgetItem(self.trUtf8("Characters"))) |
375 |
376 |
376 row += 1 |
377 row += 1 |
377 self.resultTable.setItem(row, 0, |
378 self.resultTable.setItem(row, 0, |
378 QTableWidgetItem(self.trUtf8("Match"))) |
379 QTableWidgetItem(self.trUtf8("Match"))) |
379 self.resultTable.setItem(row, 1, |
380 self.resultTable.setItem(row, 1, |
380 QTableWidgetItem(re.cap(0))) |
381 QTableWidgetItem(re.cap(0))) |
381 self.resultTable.setItem(row, 2, |
382 self.resultTable.setItem(row, 2, |
382 QTableWidgetItem("{0:d}".format(re.matchedLength()))) |
383 QTableWidgetItem("{0:d}".format(re.matchedLength()))) |
383 |
384 |
384 if not wildcard: |
385 if not wildcard: |
385 for i in range(1, captures + 1): |
386 for i in range(1, captures + 1): |
386 if len(re.cap(i)) > 0: |
387 if len(re.cap(i)) > 0: |
387 row += 1 |
388 row += 1 |
388 self.resultTable.insertRow(row) |
389 self.resultTable.insertRow(row) |
389 self.resultTable.setItem(row, 0, |
390 self.resultTable.setItem(row, 0, |
390 QTableWidgetItem(self.trUtf8("Capture #{0}").format(i))) |
391 QTableWidgetItem(self.trUtf8("Capture #{0}").format(i))) |
391 self.resultTable.setItem(row, 1, |
392 self.resultTable.setItem(row, 1, |
392 QTableWidgetItem(re.cap(i))) |
393 QTableWidgetItem(re.cap(i))) |
393 self.resultTable.setItem(row, 2, |
394 self.resultTable.setItem(row, 2, |
394 QTableWidgetItem("{0:d}".format(len(re.cap(i))))) |
395 QTableWidgetItem("{0:d}".format(len(re.cap(i))))) |
395 else: |
396 else: |
396 self.resultTable.setRowCount(3) |
397 self.resultTable.setRowCount(3) |
397 |
398 |
398 # highlight the matched text |
399 # highlight the matched text |
403 else: |
404 else: |
404 self.nextButton.setEnabled(False) |
405 self.nextButton.setEnabled(False) |
405 self.resultTable.setRowCount(2) |
406 self.resultTable.setRowCount(2) |
406 row += 1 |
407 row += 1 |
407 if startpos > 0: |
408 if startpos > 0: |
408 self.resultTable.setItem(row, 0, |
409 self.resultTable.setItem(row, 0, |
409 QTableWidgetItem(self.trUtf8("No more matches"))) |
410 QTableWidgetItem(self.trUtf8("No more matches"))) |
410 else: |
411 else: |
411 self.resultTable.setItem(row, 0, |
412 self.resultTable.setItem(row, 0, |
412 QTableWidgetItem(self.trUtf8("No matches"))) |
413 QTableWidgetItem(self.trUtf8("No matches"))) |
413 |
414 |
414 # remove the highlight |
415 # remove the highlight |
415 tc = self.textTextEdit.textCursor() |
416 tc = self.textTextEdit.textCursor() |
416 tc.setPosition(0) |
417 tc.setPosition(0) |
469 if self.wildcardCheckBox.isChecked(): |
470 if self.wildcardCheckBox.isChecked(): |
470 code += '{0}{1}.setPatternSyntax(QRegExp.Wildcard){2}'.format( |
471 code += '{0}{1}.setPatternSyntax(QRegExp.Wildcard){2}'.format( |
471 istring, reVar, estring) |
472 istring, reVar, estring) |
472 return code |
473 return code |
473 |
474 |
|
475 |
474 class QRegExpWizardDialog(QDialog): |
476 class QRegExpWizardDialog(QDialog): |
475 """ |
477 """ |
476 Class for the dialog variant. |
478 Class for the dialog variant. |
477 """ |
479 """ |
478 def __init__(self, parent = None, fromEric = True): |
480 def __init__(self, parent=None, fromEric=True): |
479 """ |
481 """ |
480 Constructor |
482 Constructor |
481 |
483 |
482 @param parent parent widget (QWidget) |
484 @param parent parent widget (QWidget) |
483 @param fromEric flag indicating a call from within eric5 |
485 @param fromEric flag indicating a call from within eric5 |
506 @param indString string used for indentation (space or tab) (string) |
508 @param indString string used for indentation (space or tab) (string) |
507 @return generated code (string) |
509 @return generated code (string) |
508 """ |
510 """ |
509 return self.cw.getCode(indLevel, indString) |
511 return self.cw.getCode(indLevel, indString) |
510 |
512 |
|
513 |
511 class QRegExpWizardWindow(QMainWindow): |
514 class QRegExpWizardWindow(QMainWindow): |
512 """ |
515 """ |
513 Main window class for the standalone dialog. |
516 Main window class for the standalone dialog. |
514 """ |
517 """ |
515 def __init__(self, parent = None): |
518 def __init__(self, parent=None): |
516 """ |
519 """ |
517 Constructor |
520 Constructor |
518 |
521 |
519 @param parent reference to the parent widget (QWidget) |
522 @param parent reference to the parent widget (QWidget) |
520 """ |
523 """ |
521 QMainWindow.__init__(self, parent) |
524 QMainWindow.__init__(self, parent) |
522 self.cw = QRegExpWizardWidget(self, fromEric = False) |
525 self.cw = QRegExpWizardWidget(self, fromEric=False) |
523 size = self.cw.size() |
526 size = self.cw.size() |
524 self.setCentralWidget(self.cw) |
527 self.setCentralWidget(self.cw) |
525 self.resize(size) |
528 self.resize(size) |
526 |
529 |
527 self.cw.buttonBox.accepted[()].connect(self.close) |
530 self.cw.buttonBox.accepted[()].connect(self.close) |