From 501ee2252c179045d695fe00545ace367983b8f8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 12 May 2020 17:07:39 +0530 Subject: [PATCH] Removing of highlights works --- src/calibre/gui2/viewer/highlights.py | 18 ++++++++++++++++-- src/calibre/gui2/viewer/ui.py | 1 + src/pyj/read_book/create_annotation.pyj | 11 +++++++++++ src/pyj/read_book/iframe.pyj | 5 +++++ src/pyj/read_book/view.pyj | 2 +- 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/viewer/highlights.py b/src/calibre/gui2/viewer/highlights.py index 018b249b15..8406c2bf23 100644 --- a/src/calibre/gui2/viewer/highlights.py +++ b/src/calibre/gui2/viewer/highlights.py @@ -44,12 +44,25 @@ class Highlights(QListWidget): pi = plugins['progress_indicator'][0] pi.set_no_activate_on_click(self) self.itemActivated.connect(self.item_activated) + self.uuid_map = {} def load(self, highlights): self.clear() + self.uuid_map = {} for h in highlights or (): - i = QListWidgetItem(h['highlighted_text'], self) - i.setData(Qt.UserRole, h) + txt = h.get('highlighted_text') + if not h.get('removed') and txt: + i = QListWidgetItem(txt, self) + i.setData(Qt.UserRole, h) + self.uuid_map[h['uuid']] = self.count() - 1 + + def refresh(self, highlights): + h = self.current_highlight + self.load(highlights) + if h is not None: + idx = self.uuid_map.get(h['uuid']) + if idx is not None: + self.set_current_row(idx) def find_query(self, query): cr = self.currentRow() @@ -106,6 +119,7 @@ class HighlightsPanel(QWidget): l.addWidget(h) h.jump_to_highlight.connect(self.jump_to_highlight) self.load = h.load + self.refresh = h.refresh self.h = h = QHBoxLayout() l.addLayout(h) diff --git a/src/calibre/gui2/viewer/ui.py b/src/calibre/gui2/viewer/ui.py index 02dbcb506c..2060fad674 100644 --- a/src/calibre/gui2/viewer/ui.py +++ b/src/calibre/gui2/viewer/ui.py @@ -595,6 +595,7 @@ class EbookViewer(MainWindow): h['timestamp'] = parse_iso8601(h['timestamp'], assume_utc=True) amap = self.current_book_data['annotations_map'] amap['highlight'] = highlights + self.highlights_widget.refresh(highlights) self.save_annotations() def save_state(self): diff --git a/src/pyj/read_book/create_annotation.pyj b/src/pyj/read_book/create_annotation.pyj index 1c7c7a42fa..5053640715 100644 --- a/src/pyj/read_book/create_annotation.pyj +++ b/src/pyj/read_book/create_annotation.pyj @@ -36,6 +36,12 @@ class AnnotationsManager: v'delete h.notes' v'delete h.spine_name' v'delete h.spine_index' + return True + + def delete_highlight(self, uuid): + if self.remove_highlight(uuid): + if ui_operations.highlights_changed: + ui_operations.highlights_changed(Object.values(self.highlights)) def notes_for_highlight(self, uuid): h = self.highlights[uuid] @@ -552,6 +558,11 @@ class CreateAnnotation: self.send_message('edit-highlight', uuid=uuid) self.show() + def remove_highlight(self, uuid): + self.send_message('remove-highlight', uuid=uuid) + self.annotations_manager.delete_highlight(uuid) + self.hide() + def handle_message(self, msg): if msg.type is 'create-annotation': self.editing_annot_uuid = None diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 29c08a26a4..c385e87431 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -704,6 +704,11 @@ class IframeBoss: select_crw(crw_) self.ensure_selection_visible() self.initiate_creation_of_annotation(data.uuid) + elif data.type is 'remove-highlight': + crw_ = {v: k for k, v in Object.entries(annot_id_uuid_map)}[data.uuid] + if crw_: + unwrap_crw(crw_) + v'delete annot_id_uuid_map[crw_]' elif data.type is 'apply-highlight': sel = window.getSelection() text = sel.toString() diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 5a0463bea6..9d61991519 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -1198,4 +1198,4 @@ class View: name = self.book.manifest.spine[spine_index] self.show_name(name, initial_position={'type':'edit_annotation', 'uuid': uuid, 'replace_history':True}) elif which is 'delete': - pass + self.create_annotation.remove_highlight(uuid)