|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the QRegularExpression wizard dialog. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import re |
|
12 |
|
13 from PyQt4.QtCore import QFileInfo, pyqtSlot, qVersion |
|
14 try: |
|
15 from PyQt4.QtCore import QRegularExpression |
|
16 AVAILABLE = True |
|
17 except ImportError: |
|
18 AVAILABLE = False |
|
19 from PyQt4.QtGui import QWidget, QDialog, QInputDialog, QApplication, QClipboard, \ |
|
20 QTextCursor, QDialogButtonBox, QVBoxLayout, QTableWidgetItem |
|
21 |
|
22 from E5Gui import E5MessageBox, E5FileDialog |
|
23 from E5Gui.E5MainWindow import E5MainWindow |
|
24 |
|
25 from .Ui_QRegularExpressionWizardDialog import Ui_QRegularExpressionWizardDialog |
|
26 |
|
27 import UI.PixmapCache |
|
28 |
|
29 import Utilities |
|
30 import Preferences |
|
31 |
|
32 |
|
33 class QRegularExpressionWizardWidget(QWidget, Ui_QRegularExpressionWizardDialog): |
|
34 """ |
|
35 Class implementing the QRegularExpression wizard dialog. |
|
36 """ |
|
37 def __init__(self, parent=None, fromEric=True): |
|
38 """ |
|
39 Constructor |
|
40 |
|
41 @param parent parent widget (QWidget) |
|
42 @param fromEric flag indicating a call from within eric5 |
|
43 """ |
|
44 super().__init__(parent) |
|
45 self.setupUi(self) |
|
46 |
|
47 # initialize icons of the tool buttons |
|
48 self.commentButton.setIcon(UI.PixmapCache.getIcon("comment.png")) |
|
49 self.charButton.setIcon(UI.PixmapCache.getIcon("characters.png")) |
|
50 self.anycharButton.setIcon(UI.PixmapCache.getIcon("anychar.png")) |
|
51 self.repeatButton.setIcon(UI.PixmapCache.getIcon("repeat.png")) |
|
52 self.nonGroupButton.setIcon(UI.PixmapCache.getIcon("nongroup.png")) |
|
53 self.atomicGroupButton.setIcon(UI.PixmapCache.getIcon("atomicgroup.png")) |
|
54 self.groupButton.setIcon(UI.PixmapCache.getIcon("group.png")) |
|
55 self.namedGroupButton.setIcon(UI.PixmapCache.getIcon("namedgroup.png")) |
|
56 self.namedReferenceButton.setIcon(UI.PixmapCache.getIcon("namedreference.png")) |
|
57 self.altnButton.setIcon(UI.PixmapCache.getIcon("altn.png")) |
|
58 self.beglineButton.setIcon(UI.PixmapCache.getIcon("begline.png")) |
|
59 self.endlineButton.setIcon(UI.PixmapCache.getIcon("endline.png")) |
|
60 self.wordboundButton.setIcon(UI.PixmapCache.getIcon("wordboundary.png")) |
|
61 self.nonwordboundButton.setIcon(UI.PixmapCache.getIcon("nonwordboundary.png")) |
|
62 self.poslookaheadButton.setIcon(UI.PixmapCache.getIcon("poslookahead.png")) |
|
63 self.neglookaheadButton.setIcon(UI.PixmapCache.getIcon("neglookahead.png")) |
|
64 self.poslookbehindButton.setIcon(UI.PixmapCache.getIcon("poslookbehind.png")) |
|
65 self.neglookbehindButton.setIcon(UI.PixmapCache.getIcon("neglookbehind.png")) |
|
66 self.undoButton.setIcon(UI.PixmapCache.getIcon("editUndo.png")) |
|
67 self.redoButton.setIcon(UI.PixmapCache.getIcon("editRedo.png")) |
|
68 |
|
69 self.namedGroups = re.compile(r"""\(?P<([^>]+)>""").findall |
|
70 |
|
71 self.saveButton = \ |
|
72 self.buttonBox.addButton(self.trUtf8("Save"), QDialogButtonBox.ActionRole) |
|
73 self.saveButton.setToolTip(self.trUtf8("Save the regular expression to a file")) |
|
74 self.loadButton = \ |
|
75 self.buttonBox.addButton(self.trUtf8("Load"), QDialogButtonBox.ActionRole) |
|
76 self.loadButton.setToolTip(self.trUtf8("Load a regular expression from a file")) |
|
77 if qVersion() >= "5.0.0" and AVAILABLE: |
|
78 self.validateButton = self.buttonBox.addButton( |
|
79 self.trUtf8("Validate"), QDialogButtonBox.ActionRole) |
|
80 self.validateButton.setToolTip(self.trUtf8("Validate the regular expression")) |
|
81 self.executeButton = self.buttonBox.addButton( |
|
82 self.trUtf8("Execute"), QDialogButtonBox.ActionRole) |
|
83 self.executeButton.setToolTip(self.trUtf8("Execute the regular expression")) |
|
84 self.nextButton = self.buttonBox.addButton( |
|
85 self.trUtf8("Next match"), QDialogButtonBox.ActionRole) |
|
86 self.nextButton.setToolTip( |
|
87 self.trUtf8("Show the next match of the regular expression")) |
|
88 self.nextButton.setEnabled(False) |
|
89 else: |
|
90 self.validateButton = None |
|
91 self.executeButton = None |
|
92 self.nextButton = None |
|
93 |
|
94 if fromEric: |
|
95 self.buttonBox.button(QDialogButtonBox.Close).hide() |
|
96 self.copyButton = None |
|
97 else: |
|
98 self.copyButton = \ |
|
99 self.buttonBox.addButton(self.trUtf8("Copy"), QDialogButtonBox.ActionRole) |
|
100 self.copyButton.setToolTip( |
|
101 self.trUtf8("Copy the regular expression to the clipboard")) |
|
102 self.buttonBox.button(QDialogButtonBox.Ok).hide() |
|
103 self.buttonBox.button(QDialogButtonBox.Cancel).hide() |
|
104 self.variableLabel.hide() |
|
105 self.variableLineEdit.hide() |
|
106 self.variableLine.hide() |
|
107 self.importCheckBox.hide() |
|
108 self.regexpTextEdit.setFocus() |
|
109 |
|
110 def __insertString(self, s, steps=0): |
|
111 """ |
|
112 Private method to insert a string into line edit and move cursor. |
|
113 |
|
114 @param s string to be inserted into the regexp line edit |
|
115 (string) |
|
116 @param steps number of characters to move the cursor (integer). |
|
117 Negative steps moves cursor back, positives forward. |
|
118 """ |
|
119 self.regexpTextEdit.insertPlainText(s) |
|
120 tc = self.regexpTextEdit.textCursor() |
|
121 if steps != 0: |
|
122 if steps < 0: |
|
123 act = QTextCursor.Left |
|
124 steps = abs(steps) |
|
125 else: |
|
126 act = QTextCursor.Right |
|
127 for i in range(steps): |
|
128 tc.movePosition(act) |
|
129 self.regexpTextEdit.setTextCursor(tc) |
|
130 |
|
131 @pyqtSlot() |
|
132 def on_commentButton_clicked(self): |
|
133 """ |
|
134 Private slot to handle the comment toolbutton. |
|
135 """ |
|
136 self.__insertString("(?#)", -1) |
|
137 |
|
138 @pyqtSlot() |
|
139 def on_charButton_clicked(self): |
|
140 """ |
|
141 Private slot to handle the characters toolbutton. |
|
142 """ |
|
143 from .QRegularExpressionWizardCharactersDialog import \ |
|
144 QRegularExpressionWizardCharactersDialog |
|
145 dlg = QRegularExpressionWizardCharactersDialog(self) |
|
146 if dlg.exec_() == QDialog.Accepted: |
|
147 self.__insertString(dlg.getCharacters()) |
|
148 |
|
149 @pyqtSlot() |
|
150 def on_anycharButton_clicked(self): |
|
151 """ |
|
152 Private slot to handle the any character toolbutton. |
|
153 """ |
|
154 self.__insertString(".") |
|
155 |
|
156 @pyqtSlot() |
|
157 def on_repeatButton_clicked(self): |
|
158 """ |
|
159 Private slot to handle the repeat toolbutton. |
|
160 """ |
|
161 from .QRegularExpressionWizardRepeatDialog import \ |
|
162 QRegularExpressionWizardRepeatDialog |
|
163 dlg = QRegularExpressionWizardRepeatDialog(self) |
|
164 if dlg.exec_() == QDialog.Accepted: |
|
165 self.__insertString(dlg.getRepeat()) |
|
166 |
|
167 @pyqtSlot() |
|
168 def on_nonGroupButton_clicked(self): |
|
169 """ |
|
170 Private slot to handle the non group toolbutton. |
|
171 """ |
|
172 self.__insertString("(?:)", -1) |
|
173 |
|
174 @pyqtSlot() |
|
175 def on_atomicGroupButton_clicked(self): |
|
176 """ |
|
177 Private slot to handle the atomic non group toolbutton. |
|
178 """ |
|
179 self.__insertString("(?>)", -1) |
|
180 |
|
181 @pyqtSlot() |
|
182 def on_groupButton_clicked(self): |
|
183 """ |
|
184 Private slot to handle the group toolbutton. |
|
185 """ |
|
186 self.__insertString("()", -1) |
|
187 |
|
188 @pyqtSlot() |
|
189 def on_namedGroupButton_clicked(self): |
|
190 """ |
|
191 Private slot to handle the named group toolbutton. |
|
192 """ |
|
193 self.__insertString("(?P<>)", -2) |
|
194 |
|
195 @pyqtSlot() |
|
196 def on_namedReferenceButton_clicked(self): |
|
197 """ |
|
198 Private slot to handle the named reference toolbutton. |
|
199 """ |
|
200 # determine cursor position as length into text |
|
201 length = self.regexpTextEdit.textCursor().position() |
|
202 |
|
203 # only present group names that occur before the current cursor position |
|
204 regex = self.regexpTextEdit.toPlainText()[:length] |
|
205 names = self.namedGroups(regex) |
|
206 if not names: |
|
207 E5MessageBox.information(self, |
|
208 self.trUtf8("Named reference"), |
|
209 self.trUtf8("""No named groups have been defined yet.""")) |
|
210 return |
|
211 |
|
212 groupName, ok = QInputDialog.getItem( |
|
213 self, |
|
214 self.trUtf8("Named reference"), |
|
215 self.trUtf8("Select group name:"), |
|
216 names, |
|
217 0, True) |
|
218 if ok and groupName: |
|
219 self.__insertString("(?P={0})".format(groupName)) |
|
220 |
|
221 @pyqtSlot() |
|
222 def on_altnButton_clicked(self): |
|
223 """ |
|
224 Private slot to handle the alternatives toolbutton. |
|
225 """ |
|
226 self.__insertString("(|)", -2) |
|
227 |
|
228 @pyqtSlot() |
|
229 def on_beglineButton_clicked(self): |
|
230 """ |
|
231 Private slot to handle the begin line toolbutton. |
|
232 """ |
|
233 self.__insertString("^") |
|
234 |
|
235 @pyqtSlot() |
|
236 def on_endlineButton_clicked(self): |
|
237 """ |
|
238 Private slot to handle the end line toolbutton. |
|
239 """ |
|
240 self.__insertString("$") |
|
241 |
|
242 @pyqtSlot() |
|
243 def on_wordboundButton_clicked(self): |
|
244 """ |
|
245 Private slot to handle the word boundary toolbutton. |
|
246 """ |
|
247 self.__insertString("\\b") |
|
248 |
|
249 @pyqtSlot() |
|
250 def on_nonwordboundButton_clicked(self): |
|
251 """ |
|
252 Private slot to handle the non word boundary toolbutton. |
|
253 """ |
|
254 self.__insertString("\\B") |
|
255 |
|
256 @pyqtSlot() |
|
257 def on_poslookaheadButton_clicked(self): |
|
258 """ |
|
259 Private slot to handle the positive lookahead toolbutton. |
|
260 """ |
|
261 self.__insertString("(?=)", -1) |
|
262 |
|
263 @pyqtSlot() |
|
264 def on_neglookaheadButton_clicked(self): |
|
265 """ |
|
266 Private slot to handle the negative lookahead toolbutton. |
|
267 """ |
|
268 self.__insertString("(?!)", -1) |
|
269 |
|
270 @pyqtSlot() |
|
271 def on_poslookbehindButton_clicked(self): |
|
272 """ |
|
273 Private slot to handle the positive lookbehind toolbutton. |
|
274 """ |
|
275 self.__insertString("(?<=)", -1) |
|
276 |
|
277 @pyqtSlot() |
|
278 def on_neglookbehindButton_clicked(self): |
|
279 """ |
|
280 Private slot to handle the negative lookbehind toolbutton. |
|
281 """ |
|
282 self.__insertString("(?<!)", -1) |
|
283 |
|
284 @pyqtSlot() |
|
285 def on_undoButton_clicked(self): |
|
286 """ |
|
287 Private slot to handle the undo action. |
|
288 """ |
|
289 self.regexpTextEdit.document().undo() |
|
290 |
|
291 @pyqtSlot() |
|
292 def on_redoButton_clicked(self): |
|
293 """ |
|
294 Private slot to handle the redo action. |
|
295 """ |
|
296 self.regexpTextEdit.document().redo() |
|
297 |
|
298 def on_buttonBox_clicked(self, button): |
|
299 """ |
|
300 Private slot called by a button of the button box clicked. |
|
301 |
|
302 @param button button that was clicked (QAbstractButton) |
|
303 """ |
|
304 if button == self.validateButton: |
|
305 self.on_validateButton_clicked() |
|
306 elif button == self.executeButton: |
|
307 self.on_executeButton_clicked() |
|
308 elif button == self.saveButton: |
|
309 self.on_saveButton_clicked() |
|
310 elif button == self.loadButton: |
|
311 self.on_loadButton_clicked() |
|
312 elif button == self.nextButton: |
|
313 self.on_nextButton_clicked() |
|
314 elif self.copyButton and button == self.copyButton: |
|
315 self.on_copyButton_clicked() |
|
316 |
|
317 @pyqtSlot() |
|
318 def on_saveButton_clicked(self): |
|
319 """ |
|
320 Private slot to save the QRegularExpression to a file. |
|
321 """ |
|
322 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
|
323 self, |
|
324 self.trUtf8("Save regular expression"), |
|
325 "", |
|
326 self.trUtf8("RegExp Files (*.rx);;All Files (*)"), |
|
327 None, |
|
328 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
|
329 if fname: |
|
330 ext = QFileInfo(fname).suffix() |
|
331 if not ext: |
|
332 ex = selectedFilter.split("(*")[1].split(")")[0] |
|
333 if ex: |
|
334 fname += ex |
|
335 if QFileInfo(fname).exists(): |
|
336 res = E5MessageBox.yesNo(self, |
|
337 self.trUtf8("Save regular expression"), |
|
338 self.trUtf8("<p>The file <b>{0}</b> already exists." |
|
339 " Overwrite it?</p>").format(fname), |
|
340 icon=E5MessageBox.Warning) |
|
341 if not res: |
|
342 return |
|
343 |
|
344 try: |
|
345 f = open(Utilities.toNativeSeparators(fname), "w", encoding="utf-8") |
|
346 f.write(self.regexpTextEdit.toPlainText()) |
|
347 f.close() |
|
348 except IOError as err: |
|
349 E5MessageBox.information(self, |
|
350 self.trUtf8("Save regular expression"), |
|
351 self.trUtf8("""<p>The regular expression could not be saved.</p>""" |
|
352 """<p>Reason: {0}</p>""").format(str(err))) |
|
353 |
|
354 @pyqtSlot() |
|
355 def on_loadButton_clicked(self): |
|
356 """ |
|
357 Private slot to load a QRegularExpression from a file. |
|
358 """ |
|
359 fname = E5FileDialog.getOpenFileName( |
|
360 self, |
|
361 self.trUtf8("Load regular expression"), |
|
362 "", |
|
363 self.trUtf8("RegExp Files (*.rx);;All Files (*)")) |
|
364 if fname: |
|
365 try: |
|
366 f = open(Utilities.toNativeSeparators(fname), "r", encoding="utf-8") |
|
367 regexp = f.read() |
|
368 f.close() |
|
369 self.regexpTextEdit.setPlainText(regexp) |
|
370 except IOError as err: |
|
371 E5MessageBox.information(self, |
|
372 self.trUtf8("Save regular expression"), |
|
373 self.trUtf8("""<p>The regular expression could not be saved.</p>""" |
|
374 """<p>Reason: {0}</p>""").format(str(err))) |
|
375 |
|
376 @pyqtSlot() |
|
377 def on_copyButton_clicked(self): |
|
378 """ |
|
379 Private slot to copy the QRegularExpression string into the clipboard. |
|
380 |
|
381 This slot is only available, if not called from within eric5. |
|
382 """ |
|
383 escaped = self.regexpTextEdit.toPlainText() |
|
384 if escaped: |
|
385 escaped = escaped.replace("\\", "\\\\") |
|
386 cb = QApplication.clipboard() |
|
387 cb.setText(escaped, QClipboard.Clipboard) |
|
388 if cb.supportsSelection(): |
|
389 cb.setText(escaped, QClipboard.Selection) |
|
390 |
|
391 @pyqtSlot() |
|
392 def on_validateButton_clicked(self): |
|
393 """ |
|
394 Private slot to validate the entered QRegularExpression. |
|
395 """ |
|
396 if qVersion() < "5.0.0" or not AVAILABLE: |
|
397 # only available for Qt5 |
|
398 return |
|
399 |
|
400 regex = self.regexpTextEdit.toPlainText() |
|
401 if regex: |
|
402 options = QRegularExpression.NoPatternOption |
|
403 if self.caseInsensitiveCheckBox.isChecked(): |
|
404 options |= QRegularExpression.CaseInsensitiveOption |
|
405 if self.multilineCheckBox.isChecked(): |
|
406 options |= QRegularExpression.MultilineOption |
|
407 if self.dotallCheckBox.isChecked(): |
|
408 options |= QRegularExpression.DotMatchesEverythingOption |
|
409 if self.extendedCheckBox.isChecked(): |
|
410 options |= QRegularExpression.ExtendedPatternSyntaxOption |
|
411 if self.greedinessCheckBox.isChecked(): |
|
412 options |= QRegularExpression.InvertedGreedinessOption |
|
413 if self.unicodeCheckBox.isChecked(): |
|
414 options |= QRegularExpression.UseUnicodePropertiesOption |
|
415 if self.captureCheckBox.isChecked(): |
|
416 options |= QRegularExpression.DontCaptureOption |
|
417 |
|
418 re = QRegularExpression(regex, options) |
|
419 if re.isValid(): |
|
420 E5MessageBox.information(self, |
|
421 self.trUtf8("Validation"), |
|
422 self.trUtf8("""The regular expression is valid.""")) |
|
423 else: |
|
424 E5MessageBox.critical(self, |
|
425 self.trUtf8("Error"), |
|
426 self.trUtf8("""Invalid regular expression: {0}""") |
|
427 .format(re.errorString())) |
|
428 # move cursor to error offset |
|
429 offset = re.errorPatternOffset() |
|
430 tc = self.regexpTextEdit.textCursor() |
|
431 tc.setPosition(offset) |
|
432 self.regexpTextEdit.setTextCursor(tc) |
|
433 return |
|
434 else: |
|
435 E5MessageBox.critical(self, |
|
436 self.trUtf8("Error"), |
|
437 self.trUtf8("""A regular expression must be given.""")) |
|
438 |
|
439 @pyqtSlot() |
|
440 def on_executeButton_clicked(self, startpos=0): |
|
441 """ |
|
442 Private slot to execute the entered QRegularExpression on the test text. |
|
443 |
|
444 This slot will execute the entered QRegularExpression on the entered test |
|
445 data and will display the result in the table part of the dialog. |
|
446 |
|
447 @param startpos starting position for the QRegularExpression matching |
|
448 """ |
|
449 if qVersion() < "5.0.0" or not AVAILABLE: |
|
450 # only available for Qt5 |
|
451 return |
|
452 |
|
453 regex = self.regexpTextEdit.toPlainText() |
|
454 text = self.textTextEdit.toPlainText() |
|
455 if regex and text: |
|
456 options = QRegularExpression.NoPatternOption |
|
457 if self.caseInsensitiveCheckBox.isChecked(): |
|
458 options |= QRegularExpression.CaseInsensitiveOption |
|
459 if self.multilineCheckBox.isChecked(): |
|
460 options |= QRegularExpression.MultilineOption |
|
461 if self.dotallCheckBox.isChecked(): |
|
462 options |= QRegularExpression.DotMatchesEverythingOption |
|
463 if self.extendedCheckBox.isChecked(): |
|
464 options |= QRegularExpression.ExtendedPatternSyntaxOption |
|
465 if self.greedinessCheckBox.isChecked(): |
|
466 options |= QRegularExpression.InvertedGreedinessOption |
|
467 if self.unicodeCheckBox.isChecked(): |
|
468 options |= QRegularExpression.UseUnicodePropertiesOption |
|
469 if self.captureCheckBox.isChecked(): |
|
470 options |= QRegularExpression.DontCaptureOption |
|
471 |
|
472 re = QRegularExpression(regex, options) |
|
473 if not re.isValid(): |
|
474 E5MessageBox.critical(self, |
|
475 self.trUtf8("Error"), |
|
476 self.trUtf8("""Invalid regular expression: {0}""") |
|
477 .format(re.errorString())) |
|
478 # move cursor to error offset |
|
479 offset = re.errorPatternOffset() |
|
480 tc = self.regexpTextEdit.textCursor() |
|
481 tc.setPosition(offset) |
|
482 self.regexpTextEdit.setTextCursor(tc) |
|
483 return |
|
484 |
|
485 match = re.match(text, startpos) |
|
486 if match.hasMatch(): |
|
487 captures = match.lastCapturedIndex() |
|
488 else: |
|
489 captures = 0 |
|
490 row = 0 |
|
491 OFFSET = 5 |
|
492 |
|
493 self.resultTable.setColumnCount(0) |
|
494 self.resultTable.setColumnCount(3) |
|
495 self.resultTable.setRowCount(0) |
|
496 self.resultTable.setRowCount(OFFSET) |
|
497 self.resultTable.setItem(row, 0, QTableWidgetItem(self.trUtf8("Regexp"))) |
|
498 self.resultTable.setItem(row, 1, QTableWidgetItem(regex)) |
|
499 |
|
500 if match.hasMatch(): |
|
501 # index 0 is the complete match |
|
502 offset = match.capturedStart(0) |
|
503 self.lastMatchEnd = match.capturedEnd(0) |
|
504 self.nextButton.setEnabled(True) |
|
505 row += 1 |
|
506 self.resultTable.setItem(row, 0, |
|
507 QTableWidgetItem(self.trUtf8("Offset"))) |
|
508 self.resultTable.setItem(row, 1, |
|
509 QTableWidgetItem("{0:d}".format(match.capturedStart(0)))) |
|
510 |
|
511 row += 1 |
|
512 self.resultTable.setItem(row, 0, |
|
513 QTableWidgetItem(self.trUtf8("Captures"))) |
|
514 self.resultTable.setItem(row, 1, |
|
515 QTableWidgetItem("{0:d}".format(captures))) |
|
516 row += 1 |
|
517 self.resultTable.setItem(row, 1, |
|
518 QTableWidgetItem(self.trUtf8("Text"))) |
|
519 self.resultTable.setItem(row, 2, |
|
520 QTableWidgetItem(self.trUtf8("Characters"))) |
|
521 |
|
522 row += 1 |
|
523 self.resultTable.setItem(row, 0, |
|
524 QTableWidgetItem(self.trUtf8("Match"))) |
|
525 self.resultTable.setItem(row, 1, |
|
526 QTableWidgetItem(match.captured(0))) |
|
527 self.resultTable.setItem(row, 2, |
|
528 QTableWidgetItem("{0:d}".format(match.capturedLength(0)))) |
|
529 |
|
530 for i in range(1, captures + 1): |
|
531 if match.captured(i): |
|
532 row += 1 |
|
533 self.resultTable.insertRow(row) |
|
534 self.resultTable.setItem(row, 0, |
|
535 QTableWidgetItem(self.trUtf8("Capture #{0}").format(i))) |
|
536 self.resultTable.setItem(row, 1, |
|
537 QTableWidgetItem(match.captured(i))) |
|
538 self.resultTable.setItem(row, 2, |
|
539 QTableWidgetItem("{0:d}".format(match.capturedLength(i)))) |
|
540 |
|
541 # highlight the matched text |
|
542 tc = self.textTextEdit.textCursor() |
|
543 tc.setPosition(offset) |
|
544 tc.setPosition(self.lastMatchEnd, QTextCursor.KeepAnchor) |
|
545 self.textTextEdit.setTextCursor(tc) |
|
546 else: |
|
547 self.nextButton.setEnabled(False) |
|
548 self.resultTable.setRowCount(2) |
|
549 row += 1 |
|
550 if startpos > 0: |
|
551 self.resultTable.setItem(row, 0, |
|
552 QTableWidgetItem(self.trUtf8("No more matches"))) |
|
553 else: |
|
554 self.resultTable.setItem(row, 0, |
|
555 QTableWidgetItem(self.trUtf8("No matches"))) |
|
556 |
|
557 # remove the highlight |
|
558 tc = self.textTextEdit.textCursor() |
|
559 tc.setPosition(0) |
|
560 self.textTextEdit.setTextCursor(tc) |
|
561 |
|
562 self.resultTable.resizeColumnsToContents() |
|
563 self.resultTable.resizeRowsToContents() |
|
564 self.resultTable.verticalHeader().hide() |
|
565 self.resultTable.horizontalHeader().hide() |
|
566 else: |
|
567 E5MessageBox.critical(self, |
|
568 self.trUtf8("Error"), |
|
569 self.trUtf8("""A regular expression and a text must be given.""")) |
|
570 |
|
571 @pyqtSlot() |
|
572 def on_nextButton_clicked(self): |
|
573 """ |
|
574 Private slot to find the next match. |
|
575 """ |
|
576 self.on_executeButton_clicked(self.lastMatchEnd) |
|
577 |
|
578 @pyqtSlot() |
|
579 def on_regexpTextEdit_textChanged(self): |
|
580 """ |
|
581 Private slot called when the regexp changes. |
|
582 """ |
|
583 if self.nextButton: |
|
584 self.nextButton.setEnabled(False) |
|
585 |
|
586 def getCode(self, indLevel, indString): |
|
587 """ |
|
588 Public method to get the source code. |
|
589 |
|
590 @param indLevel indentation level (int) |
|
591 @param indString string used for indentation (space or tab) (string) |
|
592 @return generated code (string) |
|
593 """ |
|
594 # calculate the indentation string |
|
595 i1string = (indLevel + 1) * indString |
|
596 estring = os.linesep + indLevel * indString |
|
597 |
|
598 # now generate the code |
|
599 reVar = self.variableLineEdit.text() |
|
600 if not reVar: |
|
601 reVar = "regexp" |
|
602 |
|
603 regexp = self.regexpTextEdit.toPlainText() |
|
604 |
|
605 options = [] |
|
606 if self.caseInsensitiveCheckBox.isChecked(): |
|
607 options.append("QRegularExpression.CaseInsensitiveOption") |
|
608 if self.multilineCheckBox.isChecked(): |
|
609 options.append("QRegularExpression.MultilineOption") |
|
610 if self.dotallCheckBox.isChecked(): |
|
611 options.append("QRegularExpression.DotMatchesEverythingOption") |
|
612 if self.extendedCheckBox.isChecked(): |
|
613 options.append("QRegularExpression.ExtendedPatternSyntaxOption") |
|
614 if self.greedinessCheckBox.isChecked(): |
|
615 options.append("QRegularExpression.InvertedGreedinessOption") |
|
616 if self.unicodeCheckBox.isChecked(): |
|
617 options.append("QRegularExpression.UseUnicodePropertiesOption") |
|
618 if self.captureCheckBox.isChecked(): |
|
619 options.append("QRegularExpression.DontCaptureOption") |
|
620 options = " | \\{0}{1}".format(os.linesep, i1string).join(options) |
|
621 |
|
622 code = '{0} = QRegularExpression(r"""{1}"""'.format( |
|
623 reVar, regexp.replace('"', '\\"')) |
|
624 if options: |
|
625 code += ', {0}{1}{2}'.format(os.linesep, i1string, options) |
|
626 code += '){0}'.format(estring) |
|
627 return code |
|
628 |
|
629 |
|
630 class QRegularExpressionWizardDialog(QDialog): |
|
631 """ |
|
632 Class for the dialog variant. |
|
633 """ |
|
634 def __init__(self, parent=None, fromEric=True): |
|
635 """ |
|
636 Constructor |
|
637 |
|
638 @param parent parent widget (QWidget) |
|
639 @param fromEric flag indicating a call from within eric5 |
|
640 """ |
|
641 super().__init__(parent) |
|
642 self.setModal(fromEric) |
|
643 self.setSizeGripEnabled(True) |
|
644 |
|
645 self.__layout = QVBoxLayout(self) |
|
646 self.__layout.setMargin(0) |
|
647 self.setLayout(self.__layout) |
|
648 |
|
649 self.cw = QRegularExpressionWizardWidget(self, fromEric) |
|
650 size = self.cw.size() |
|
651 self.__layout.addWidget(self.cw) |
|
652 self.resize(size) |
|
653 self.setWindowTitle(self.cw.windowTitle()) |
|
654 |
|
655 self.cw.buttonBox.accepted[()].connect(self.accept) |
|
656 self.cw.buttonBox.rejected[()].connect(self.reject) |
|
657 |
|
658 def getCode(self, indLevel, indString): |
|
659 """ |
|
660 Public method to get the source code. |
|
661 |
|
662 @param indLevel indentation level (int) |
|
663 @param indString string used for indentation (space or tab) (string) |
|
664 @return generated code (string) |
|
665 """ |
|
666 return self.cw.getCode(indLevel, indString) |
|
667 |
|
668 |
|
669 class QRegularExpressionWizardWindow(E5MainWindow): |
|
670 """ |
|
671 Main window class for the standalone dialog. |
|
672 """ |
|
673 def __init__(self, parent=None): |
|
674 """ |
|
675 Constructor |
|
676 |
|
677 @param parent reference to the parent widget (QWidget) |
|
678 """ |
|
679 super().__init__(parent) |
|
680 self.cw = QRegularExpressionWizardWidget(self, fromEric=False) |
|
681 size = self.cw.size() |
|
682 self.setCentralWidget(self.cw) |
|
683 self.resize(size) |
|
684 self.setWindowTitle(self.cw.windowTitle()) |
|
685 |
|
686 self.setStyle(Preferences.getUI("Style"), Preferences.getUI("StyleSheet")) |
|
687 |
|
688 self.cw.buttonBox.accepted[()].connect(self.close) |
|
689 self.cw.buttonBox.rejected[()].connect(self.close) |