From e24d08acd14059934e38f59f353c046b804afcf6 Mon Sep 17 00:00:00 2001 From: John Schember Date: Fri, 21 Jan 2011 20:36:29 -0500 Subject: [PATCH] Implement all of ticket #4486: Search next and preious in Regex helper. --- src/calibre/gui2/convert/regex_builder.py | 50 +++++++- src/calibre/gui2/convert/regex_builder.ui | 137 +++++++++++++++++----- 2 files changed, 157 insertions(+), 30 deletions(-) diff --git a/src/calibre/gui2/convert/regex_builder.py b/src/calibre/gui2/convert/regex_builder.py index ca6e429176..8e75fb9691 100644 --- a/src/calibre/gui2/convert/regex_builder.py +++ b/src/calibre/gui2/convert/regex_builder.py @@ -35,6 +35,10 @@ class RegexBuilder(QDialog, Ui_RegexBuilder): self.connect(self.button_box, SIGNAL('clicked(QAbstractButton*)'), self.button_clicked) self.connect(self.regex, SIGNAL('textChanged(QString)'), self.regex_valid) self.connect(self.test, SIGNAL('clicked()'), self.do_test) + self.connect(self.previous, SIGNAL('clicked()'), self.goto_previous) + self.connect(self.next, SIGNAL('clicked()'), self.goto_next) + + self.match_locs = [] def regex_valid(self): regex = unicode(self.regex.text()) @@ -42,17 +46,23 @@ class RegexBuilder(QDialog, Ui_RegexBuilder): try: re.compile(regex) self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }') + return True except: self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgb(255,0,0,20%); }') - return False else: self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }') self.preview.setExtraSelections([]) - return False - return True + + self.match_locs = [] + self.next.setEnabled(False) + self.previous.setEnabled(False) + self.occurances.setText('0') + + return False def do_test(self): selections = [] + self.match_locs = [] if self.regex_valid(): text = unicode(self.preview.toPlainText()) regex = unicode(self.regex.text()) @@ -66,9 +76,43 @@ class RegexBuilder(QDialog, Ui_RegexBuilder): es.cursor.setPosition(match.start(), QTextCursor.MoveAnchor) es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor) selections.append(es) + self.match_locs.append((match.start(), match.end())) except: pass self.preview.setExtraSelections(selections) + if self.match_locs: + self.next.setEnabled(True) + self.previous.setEnabled(True) + self.occurances.setText('%s' % len(self.match_locs)) + + def goto_previous(self): + pos = self.preview.textCursor().position() + if self.match_locs: + match_loc = len(self.match_locs) - 1 + for i in xrange(len(self.match_locs) - 1, -1, -1): + loc = self.match_locs[i][1] + if pos > loc: + match_loc = i + break + self.goto_loc(self.match_locs[match_loc][1], operation=QTextCursor.Left, n=self.match_locs[match_loc][1] - self.match_locs[match_loc][0]) + + def goto_next(self): + pos = self.preview.textCursor().position() + if self.match_locs: + match_loc = 0 + for i in xrange(len(self.match_locs)): + loc = self.match_locs[i][0] + if pos < loc: + match_loc = i + break + self.goto_loc(self.match_locs[match_loc][0], n=self.match_locs[match_loc][1] - self.match_locs[match_loc][0]) + + def goto_loc(self, loc, operation=QTextCursor.Right, mode=QTextCursor.KeepAnchor, n=0): + cursor = QTextCursor(self.preview.document()) + cursor.setPosition(loc) + if n: + cursor.movePosition(operation, mode, n) + self.preview.setTextCursor(cursor) def select_format(self, db, book_id): format = None diff --git a/src/calibre/gui2/convert/regex_builder.ui b/src/calibre/gui2/convert/regex_builder.ui index af17917676..bbb08454e9 100644 --- a/src/calibre/gui2/convert/regex_builder.ui +++ b/src/calibre/gui2/convert/regex_builder.ui @@ -6,15 +6,102 @@ 0 0 - 662 - 505 + 580 + 503 Regex Builder - - + + + + + + + Regex: + + + + + + + + + + + + Test + + + + + + + + + + + + + Occurances: + + + + + + + 0 + + + + + + + Qt::Horizontal + + + + 298 + 20 + + + + + + + + + + Goto: + + + + + + + false + + + Previous + + + + + + + false + + + Next + + + + + + + + Preview @@ -36,32 +123,28 @@ - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - Regex: - - - - - + + - + + + Qt::Horizontal + + + + 328 + 20 + + + - - - Test + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok