mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 18:54:09 -04:00
Fix #4486 (xpath and RegEx HELPER needs "search" feature)
This commit is contained in:
commit
2e8b03cecc
@ -35,6 +35,10 @@ class RegexBuilder(QDialog, Ui_RegexBuilder):
|
|||||||
self.connect(self.button_box, SIGNAL('clicked(QAbstractButton*)'), self.button_clicked)
|
self.connect(self.button_box, SIGNAL('clicked(QAbstractButton*)'), self.button_clicked)
|
||||||
self.connect(self.regex, SIGNAL('textChanged(QString)'), self.regex_valid)
|
self.connect(self.regex, SIGNAL('textChanged(QString)'), self.regex_valid)
|
||||||
self.connect(self.test, SIGNAL('clicked()'), self.do_test)
|
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):
|
def regex_valid(self):
|
||||||
regex = unicode(self.regex.text())
|
regex = unicode(self.regex.text())
|
||||||
@ -42,17 +46,23 @@ class RegexBuilder(QDialog, Ui_RegexBuilder):
|
|||||||
try:
|
try:
|
||||||
re.compile(regex)
|
re.compile(regex)
|
||||||
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }')
|
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }')
|
||||||
|
return True
|
||||||
except:
|
except:
|
||||||
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgb(255,0,0,20%); }')
|
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgb(255,0,0,20%); }')
|
||||||
return False
|
|
||||||
else:
|
else:
|
||||||
self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }')
|
self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }')
|
||||||
self.preview.setExtraSelections([])
|
self.preview.setExtraSelections([])
|
||||||
|
|
||||||
|
self.match_locs = []
|
||||||
|
self.next.setEnabled(False)
|
||||||
|
self.previous.setEnabled(False)
|
||||||
|
self.occurrences.setText('0')
|
||||||
|
|
||||||
return False
|
return False
|
||||||
return True
|
|
||||||
|
|
||||||
def do_test(self):
|
def do_test(self):
|
||||||
selections = []
|
selections = []
|
||||||
|
self.match_locs = []
|
||||||
if self.regex_valid():
|
if self.regex_valid():
|
||||||
text = unicode(self.preview.toPlainText())
|
text = unicode(self.preview.toPlainText())
|
||||||
regex = unicode(self.regex.text())
|
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.start(), QTextCursor.MoveAnchor)
|
||||||
es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor)
|
es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor)
|
||||||
selections.append(es)
|
selections.append(es)
|
||||||
|
self.match_locs.append((match.start(), match.end()))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
self.preview.setExtraSelections(selections)
|
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):
|
def select_format(self, db, book_id):
|
||||||
format = None
|
format = None
|
||||||
|
@ -6,15 +6,102 @@
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>662</width>
|
<width>580</width>
|
||||||
<height>505</height>
|
<height>503</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
<string>Regex Builder</string>
|
<string>Regex Builder</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||||
<item row="1" column="0" colspan="5">
|
<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>&Previous</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="next">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>&Next</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
<widget class="QGroupBox" name="groupBox">
|
<widget class="QGroupBox" name="groupBox">
|
||||||
<property name="title">
|
<property name="title">
|
||||||
<string>Preview</string>
|
<string>Preview</string>
|
||||||
@ -36,7 +123,22 @@
|
|||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="4">
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||||
|
<item>
|
||||||
|
<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="QDialogButtonBox" name="button_box">
|
<widget class="QDialogButtonBox" name="button_box">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
<enum>Qt::Horizontal</enum>
|
<enum>Qt::Horizontal</enum>
|
||||||
@ -46,25 +148,6 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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>
|
|
||||||
<widget class="QLineEdit" name="regex"/>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QPushButton" name="test">
|
|
||||||
<property name="text">
|
|
||||||
<string>Test</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user