Fix #4486 (xpath and RegEx HELPER needs "search" feature)

This commit is contained in:
Kovid Goyal 2011-01-21 18:49:23 -07:00
commit 2e8b03cecc
2 changed files with 157 additions and 30 deletions

View File

@ -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.occurrences.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.occurrences.setText(str(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

View File

@ -6,15 +6,102 @@
<rect>
<x>0</x>
<y>0</y>
<width>662</width>
<height>505</height>
<width>580</width>
<height>503</height>
</rect>
</property>
<property name="windowTitle">
<string>Regex Builder</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="5">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Regex:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="regex"/>
</item>
<item>
<widget class="QPushButton" name="test">
<property name="text">
<string>Test</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Occurences:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="occurances">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>298</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Goto:</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="previous">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Previous</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="next">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Next</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Preview</string>
@ -36,32 +123,28 @@
</layout>
</widget>
</item>
<item row="2" column="4">
<widget class="QDialogButtonBox" name="button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Regex:</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="4">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLineEdit" name="regex"/>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>328</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="test">
<property name="text">
<string>Test</string>
<widget class="QDialogButtonBox" name="button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>