Use the same code path for jumping to highlights as for editing/removing

This commit is contained in:
Kovid Goyal 2020-05-12 19:20:18 +05:30
parent 501ee2252c
commit f259e86cb3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
6 changed files with 36 additions and 32 deletions

View File

@ -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')

View File

@ -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()

View File

@ -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()

View File

@ -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:

View File

@ -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)

View File

@ -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