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