Fix #5516 (search forward AND backward in Ebook Viewer)

This commit is contained in:
Kovid Goyal 2010-05-13 12:38:19 -06:00
parent 5318d73056
commit 0c18cf92e3
5 changed files with 44 additions and 10 deletions

View File

@ -92,10 +92,10 @@ class EbookIterator(object):
ext = re.sub(r'(x{0,1})htm(l{0,1})', 'html', ext) ext = re.sub(r'(x{0,1})htm(l{0,1})', 'html', ext)
self.ebook_ext = ext self.ebook_ext = ext
def search(self, text, index): def search(self, text, index, backwards=False):
text = text.lower() text = text.lower()
for i, path in enumerate(self.spine): for i, path in enumerate(self.spine):
if i > index: if (backwards and i < index) or (not backwards and i > index):
if text in open(path, 'rb').read().decode(path.encoding).lower(): if text in open(path, 'rb').read().decode(path.encoding).lower():
return i return i

View File

@ -137,6 +137,13 @@ class SearchBox2(QComboBox):
if event.timerId() == self.timer: if event.timerId() == self.timer:
self.do_search() self.do_search()
@property
def smart_text(self):
text = unicode(self.currentText()).strip()
if not text or text == self.help_text:
return ''
return text
def do_search(self): def do_search(self):
text = unicode(self.currentText()).strip() text = unicode(self.currentText()).strip()
if not text or text == self.help_text: if not text or text == self.help_text:

View File

@ -585,7 +585,9 @@ class DocumentView(QWebView):
def fset(self, val): self.document.current_language = val def fset(self, val): self.document.current_language = val
return property(fget=fget, fset=fset) return property(fget=fget, fset=fset)
def search(self, text): def search(self, text, backwards=False):
if backwards:
return self.findText(text, self.document.FindBackwards)
return self.findText(text) return self.findText(text)
def path(self): def path(self):

View File

@ -229,7 +229,11 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.connect(self.action_previous_page, SIGNAL('triggered(bool)'), self.connect(self.action_previous_page, SIGNAL('triggered(bool)'),
lambda x:self.view.previous_page()) lambda x:self.view.previous_page())
self.connect(self.action_find_next, SIGNAL('triggered(bool)'), self.connect(self.action_find_next, SIGNAL('triggered(bool)'),
lambda x:self.find(unicode(self.search.text()), True, repeat=True)) lambda x:self.find(self.search.smart_text, True, repeat=True))
self.connect(self.action_find_previous, SIGNAL('triggered(bool)'),
lambda x:self.find(self.search.smart_text, True,
repeat=True, backwards=True))
self.connect(self.action_full_screen, SIGNAL('triggered(bool)'), self.connect(self.action_full_screen, SIGNAL('triggered(bool)'),
self.toggle_fullscreen) self.toggle_fullscreen)
self.action_full_screen.setShortcuts([Qt.Key_F11, Qt.CTRL+Qt.SHIFT+Qt.Key_F]) self.action_full_screen.setShortcuts([Qt.Key_F11, Qt.CTRL+Qt.SHIFT+Qt.Key_F])
@ -420,13 +424,15 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.set_bookmarks(self.iterator.bookmarks) self.set_bookmarks(self.iterator.bookmarks)
def find(self, text, refinement, repeat=False): def find(self, text, refinement, repeat=False, backwards=False):
if not text: if not text:
self.view.search('')
return self.search.search_done(False) return self.search.search_done(False)
if self.view.search(text): if self.view.search(text):
self.scrolled(self.view.scroll_fraction) self.scrolled(self.view.scroll_fraction)
return self.search.search_done(True) return self.search.search_done(True)
index = self.iterator.search(text, self.current_index) index = self.iterator.search(text, self.current_index,
backwards=backwards)
if index is None: if index is None:
if self.current_index > 0: if self.current_index > 0:
index = self.iterator.search(text, 0) index = self.iterator.search(text, 0)
@ -444,10 +450,7 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.scrolled(self.view.scroll_fraction) self.scrolled(self.view.scroll_fraction)
def keyPressEvent(self, event): def keyPressEvent(self, event):
if event.key() == Qt.Key_F3: if event.key() == Qt.Key_Slash:
text = unicode(self.search.text())
self.find(text, True, repeat=True)
elif event.key() == Qt.Key_Slash:
self.search.setFocus(Qt.OtherFocusReason) self.search.setFocus(Qt.OtherFocusReason)
else: else:
return MainWindow.keyPressEvent(self, event) return MainWindow.keyPressEvent(self, event)

View File

@ -142,6 +142,7 @@
<bool>false</bool> <bool>false</bool>
</attribute> </attribute>
<addaction name="action_find_next"/> <addaction name="action_find_next"/>
<addaction name="action_find_previous"/>
</widget> </widget>
<action name="action_back"> <action name="action_back">
<property name="icon"> <property name="icon">
@ -232,6 +233,12 @@
<property name="text"> <property name="text">
<string>Find next</string> <string>Find next</string>
</property> </property>
<property name="toolTip">
<string>Find next occurrence</string>
</property>
<property name="shortcut">
<string>F3</string>
</property>
</action> </action>
<action name="action_copy"> <action name="action_copy">
<property name="icon"> <property name="icon">
@ -287,6 +294,21 @@
<string>Print</string> <string>Print</string>
</property> </property>
</action> </action>
<action name="action_find_previous">
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/arrow-up.svg</normaloff>:/images/arrow-up.svg</iconset>
</property>
<property name="text">
<string>Find previous</string>
</property>
<property name="toolTip">
<string>Find previous occurrence</string>
</property>
<property name="shortcut">
<string>Shift+F3</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>