mirror of
git://github.com/kovidgoyal/calibre.git
synced 2025-12-23 22:35:30 +01:00
Search for a highlight
This commit is contained in:
parent
b957e43faf
commit
e00b12ca70
1 changed files with 32 additions and 2 deletions
|
|
@ -4,9 +4,15 @@
|
|||
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
from PyQt5.Qt import QListWidget, QListWidgetItem, Qt, QVBoxLayout, QWidget
|
||||
from itertools import chain
|
||||
|
||||
from PyQt5.Qt import (
|
||||
QItemSelectionModel, QListWidget, QListWidgetItem, Qt, QVBoxLayout, QWidget
|
||||
)
|
||||
|
||||
from calibre.gui2 import error_dialog
|
||||
from calibre.gui2.viewer.search import SearchInput
|
||||
from polyglot.builtins import range
|
||||
|
||||
|
||||
class Highlights(QListWidget):
|
||||
|
|
@ -22,6 +28,28 @@ def load(self, highlights):
|
|||
i = QListWidgetItem(h['highlighted_text'], self)
|
||||
i.setData(Qt.UserRole, h)
|
||||
|
||||
def find_query(self, query):
|
||||
cr = self.currentRow()
|
||||
pat = query.regex
|
||||
if query.backwards:
|
||||
if cr < 0:
|
||||
cr = self.count()
|
||||
indices = chain(range(cr - 1, -1, -1), range(self.count() - 1, cr, -1))
|
||||
else:
|
||||
if cr < 0:
|
||||
cr = -1
|
||||
indices = chain(range(cr + 1, self.count()), range(0, cr + 1))
|
||||
for i in indices:
|
||||
item = self.item(i)
|
||||
h = item.data(Qt.UserRole)
|
||||
if pat.search(h['highlighted_text']) is not None or pat.search(h.get('notes') or '') is not None:
|
||||
self.set_current_row(i)
|
||||
return True
|
||||
return False
|
||||
|
||||
def set_current_row(self, row):
|
||||
self.setCurrentRow(row, QItemSelectionModel.ClearAndSelect)
|
||||
|
||||
|
||||
class HighlightsPanel(QWidget):
|
||||
|
||||
|
|
@ -38,7 +66,9 @@ def __init__(self, parent=None):
|
|||
self.load = h.load
|
||||
|
||||
def search_requested(self, query):
|
||||
pass
|
||||
if not self.highlights.find_query(query):
|
||||
error_dialog(self, _('No matches'), _(
|
||||
'No highlights match the search: {}').format(query.text), show=True)
|
||||
|
||||
def focus(self):
|
||||
self.highlights_list.setFocus(Qt.OtherFocusReason)
|
||||
|
|
|
|||
Loading…
Reference in a new issue