E-book viewer: When searching the Table of Contents allow holding the Shift key to search backwards. Fixes #1925038 [[Enhancement] E-book viewer: keyboard shortcut and mouse action modifier for searching ToC backwards](https://bugs.launchpad.net/calibre/+bug/1925038)

This commit is contained in:
Kovid Goyal 2021-04-25 20:15:39 +05:30
parent 8965b8a940
commit 281ff268f8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -148,7 +148,8 @@ class TOCSearch(QWidget):
def do_search(self, text):
if not text or not text.strip():
return
index = self.toc_view.model().search(text)
delta = -1 if QApplication.instance().keyboardModifiers() & Qt.KeyboardModifier.ShiftModifier else 1
index = self.toc_view.model().search(text, delta=delta)
if index.isValid():
self.toc_view.scrollTo(index)
self.toc_view.searched.emit(index)
@ -244,15 +245,16 @@ class TOC(QStandardItemModel):
if (exact and query == href) or (not exact and query in href):
return item.node_id
def search(self, query):
def search(self, query, delta=1):
cq = self.current_query
if cq['items'] and -1 < cq['index'] < len(cq['items']):
cq['items'][cq['index']].set_current_search_result(False)
if cq['text'] != query:
items = tuple(self.find_items(query))
cq.update({'text':query, 'items':items, 'index':-1})
if len(cq['items']) > 0:
cq['index'] = (cq['index'] + 1) % len(cq['items'])
num = len(cq['items'])
if num > 0:
cq['index'] = (cq['index'] + delta + num) % num
item = cq['items'][cq['index']]
item.set_current_search_result(True)
index = self.indexFromItem(item)