From f259e86cb34636d6c5889b8a8080a58a29ce6d80 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 12 May 2020 19:20:18 +0530 Subject: [PATCH] Use the same code path for jumping to highlights as for editing/removing --- src/calibre/gui2/viewer/highlights.py | 28 ++++--------------------- src/calibre/gui2/viewer/ui.py | 1 - src/calibre/gui2/viewer/web_view.py | 4 ++-- src/pyj/read_book/create_annotation.pyj | 18 ++++++++++++++++ src/pyj/read_book/view.pyj | 13 +++++++++--- src/pyj/viewer-main.pyj | 4 ++-- 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/calibre/gui2/viewer/highlights.py b/src/calibre/gui2/viewer/highlights.py index 8406c2bf23..65891af73d 100644 --- a/src/calibre/gui2/viewer/highlights.py +++ b/src/calibre/gui2/viewer/highlights.py @@ -14,26 +14,9 @@ from PyQt5.Qt import ( from calibre.constants import plugins from calibre.gui2 import error_dialog from calibre.gui2.viewer.search import SearchInput -from calibre.gui2.viewer.web_view import get_manifest from polyglot.builtins import range -def spine_index_for_highlight(highlight): - ans = highlight['spine_index'] - manifest = get_manifest() - if manifest is not None: - spine = manifest['spine'] - name = highlight.get('spine_name') - if name: - try: - idx = spine.index(name) - except Exception: - pass - else: - ans = idx - return ans - - class Highlights(QListWidget): jump_to_highlight = pyqtSignal(object) @@ -100,7 +83,7 @@ class HighlightsPanel(QWidget): jump_to_cfi = pyqtSignal(object) add_highlight = pyqtSignal() - request_highlight_action = pyqtSignal(object, object, object) + request_highlight_action = pyqtSignal(object, object) def __init__(self, parent=None): QWidget.__init__(self, parent) @@ -145,10 +128,7 @@ class HighlightsPanel(QWidget): self.highlights.setFocus(Qt.OtherFocusReason) def jump_to_highlight(self, highlight): - cfi = highlight['start_cfi'] - idx = spine_index_for_highlight(highlight) - cfi = 'epubcfi(/{}{})'.format(2*(idx + 1), cfi) - self.jump_to_cfi.emit(cfi) + self.request_highlight_action.emit(highlight['uuid'], 'goto') def no_selected_highlight(self): error_dialog(self, _('No selected highlight'), _( @@ -158,10 +138,10 @@ class HighlightsPanel(QWidget): h = self.highlights.current_highlight if h is None: return self.no_selected_highlight() - self.request_highlight_action.emit(h['uuid'], spine_index_for_highlight(h), 'edit') + self.request_highlight_action.emit(h['uuid'], 'edit') def remove_highlight(self): h = self.highlights.current_highlight if h is None: return self.no_selected_highlight() - self.request_highlight_action.emit(h['uuid'], spine_index_for_highlight(h), 'delete') + self.request_highlight_action.emit(h['uuid'], 'delete') diff --git a/src/calibre/gui2/viewer/ui.py b/src/calibre/gui2/viewer/ui.py index 2060fad674..074f4409c8 100644 --- a/src/calibre/gui2/viewer/ui.py +++ b/src/calibre/gui2/viewer/ui.py @@ -193,7 +193,6 @@ class EbookViewer(MainWindow): self.restore_state() self.actions_toolbar.update_visibility() self.dock_visibility_changed() - self.highlights_widget.jump_to_cfi.connect(self.web_view.goto_cfi) self.highlights_widget.request_highlight_action.connect(self.web_view.highlight_action) if continue_reading: self.continue_reading() diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index df873d2220..371df57b61 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -695,8 +695,8 @@ class WebView(RestartingWebEngineView): def prepare_for_close(self): self.execute_when_ready('prepare_for_close') - def highlight_action(self, uuid, spine_index, which): - self.execute_when_ready('highlight_action', uuid, spine_index, which) + def highlight_action(self, uuid, which): + self.execute_when_ready('highlight_action', uuid, which) def contextMenuEvent(self, ev): ev.accept() diff --git a/src/pyj/read_book/create_annotation.pyj b/src/pyj/read_book/create_annotation.pyj index 5053640715..0c7dde9eb7 100644 --- a/src/pyj/read_book/create_annotation.pyj +++ b/src/pyj/read_book/create_annotation.pyj @@ -56,6 +56,24 @@ class AnnotationsManager: def data_for_highlight(self, uuid): return self.highlights[uuid] + def spine_index_for_highlight(self, uuid, spine): + h = self.highlights[uuid] + if not h: + return -1 + ans = h.spine_index + name = h.spine_name + if name: + idx = spine.indexOf(name) + if idx > -1: + ans = idx + return ans + + def cfi_for_highlight(self, uuid, spine_index): + h = self.highlights[uuid] + if h: + x = 2 * (spine_index + 1) + return f'epubcfi(/{x}{h.start_cfi})' + def add_highlight(self, msg, style, notes): now = Date().toISOString() for uuid in msg.removed_highlights: diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 9d61991519..1757384e48 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -1190,12 +1190,19 @@ class View: else: self.show_name(sr.file_name, initial_position={'type':'search_result', 'search_result':sr, 'replace_history':True}) - def highlight_action(self, uuid, spine_index, which): + def highlight_action(self, uuid, which): + spine = self.book.manifest.spine + spine_index = self.annotations_manager.spine_index_for_highlight(uuid, spine) + if spine_index < 0 or spine_index >= spine.length: + return if which is 'edit': if self.currently_showing.spine_index is spine_index: self.create_annotation.edit_highlight(uuid) else: - name = self.book.manifest.spine[spine_index] - self.show_name(name, initial_position={'type':'edit_annotation', 'uuid': uuid, 'replace_history':True}) + self.show_name(spine[spine_index], initial_position={'type':'edit_annotation', 'uuid': uuid, 'replace_history':True}) elif which is 'delete': self.create_annotation.remove_highlight(uuid) + elif which is 'goto': + cfi = self.annotations_manager.cfi_for_highlight(uuid, spine_index) + if cfi: + self.goto_cfi(cfi) diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 617283e07b..9c510dd48f 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -241,9 +241,9 @@ def set_system_palette(system_colors): @from_python -def highlight_action(uuid, spine_index, which): +def highlight_action(uuid, which): if view: - view.highlight_action(uuid, spine_index, which) + view.highlight_action(uuid, which) @from_python