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