E-book viewer: Fix broken backwards searching

This commit is contained in:
Kovid Goyal 2010-12-03 09:51:35 -07:00
parent 3be66119d0
commit a172785529
3 changed files with 14 additions and 6 deletions

View File

@ -96,7 +96,10 @@ class EbookIterator(object):
def search(self, text, index, backwards=False): def search(self, text, index, backwards=False):
text = text.lower() text = text.lower()
for i, path in enumerate(self.spine): pmap = [(i, path) for i, path in enumerate(self.spine)]
if backwards:
pmap.reverse()
for i, path in pmap:
if (backwards and i < index) or (not backwards and 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

@ -614,7 +614,7 @@ class DocumentView(QWebView):
def search(self, text, backwards=False): def search(self, text, backwards=False):
if backwards: if backwards:
return self.findText(text, self.document.FindBackwards) return self.findText(text, self.document.FindBackward)
return self.findText(text) return self.findText(text)
def path(self): def path(self):

View File

@ -172,6 +172,7 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.iterator = None self.iterator = None
self.current_page = None self.current_page = None
self.pending_search = None self.pending_search = None
self.pending_search_dir= None
self.pending_anchor = None self.pending_anchor = None
self.pending_reference = None self.pending_reference = None
self.pending_bookmark = None self.pending_bookmark = None
@ -435,7 +436,7 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
if not text: if not text:
self.view.search('') 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, backwards=backwards):
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,
@ -449,11 +450,13 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
return self.search.search_done(True) return self.search.search_done(True)
return self.search.search_done(True) return self.search.search_done(True)
self.pending_search = text self.pending_search = text
self.pending_search_dir = 'backwards' if backwards else 'forwards'
self.load_path(self.iterator.spine[index]) self.load_path(self.iterator.spine[index])
def do_search(self, text): def do_search(self, text, backwards):
self.pending_search = None self.pending_search = None
if self.view.search(text): self.pending_search_dir = None
if self.view.search(text, backwards=backwards):
self.scrolled(self.view.scroll_fraction) self.scrolled(self.view.scroll_fraction)
def keyPressEvent(self, event): def keyPressEvent(self, event):
@ -499,8 +502,10 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.current_index = index self.current_index = index
self.set_page_number(self.view.scroll_fraction) self.set_page_number(self.view.scroll_fraction)
if self.pending_search is not None: if self.pending_search is not None:
self.do_search(self.pending_search) self.do_search(self.pending_search,
self.pending_search_dir=='backwards')
self.pending_search = None self.pending_search = None
self.pending_search_dir = None
if self.pending_anchor is not None: if self.pending_anchor is not None:
self.view.scroll_to(self.pending_anchor) self.view.scroll_to(self.pending_anchor)
self.pending_anchor = None self.pending_anchor = None