From f4d63adc0781485482e7ee0a3e044cf48a3f1960 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 10 Oct 2022 10:55:24 +0530 Subject: [PATCH] Fix #1992273 [[Enhancement] Remove format from book by right-clicking on the format in the Tag browser](https://bugs.launchpad.net/calibre/+bug/1992273) --- src/calibre/gui2/actions/delete.py | 33 +++++++++++++++++++++++----- src/calibre/gui2/tag_browser/view.py | 9 +++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/calibre/gui2/actions/delete.py b/src/calibre/gui2/actions/delete.py index 8b44164617..eb9fff5349 100644 --- a/src/calibre/gui2/actions/delete.py +++ b/src/calibre/gui2/actions/delete.py @@ -173,6 +173,12 @@ class DeleteAction(InterfaceAction): return set() return set(map(self.gui.library_view.model().id, rows)) + def _remove_formats_from_ids(self, fmts, ids): + self.gui.library_view.model().db.new_api.remove_formats({bid: fmts for bid in ids}) + self.gui.library_view.model().refresh_ids(ids) + self.gui.library_view.model().current_changed(self.gui.library_view.currentIndex(), + self.gui.library_view.currentIndex()) + def remove_format_by_id(self, book_id, fmt): title = self.gui.current_db.title(book_id, index_is_id=True) if not confirm('

'+(_( @@ -180,13 +186,28 @@ class DeleteAction(InterfaceAction): '%(title)s. Are you sure?')%dict(fmt=fmt, title=title)) + '

', 'library_delete_specific_format', self.gui): return + self._remove_format_from_ids((fmt,), (book_id,)) - self.gui.library_view.model().db.remove_format(book_id, fmt, - index_is_id=True, notify=False) - self.gui.library_view.model().refresh_ids([book_id]) - self.gui.library_view.model().current_changed(self.gui.library_view.currentIndex(), - self.gui.library_view.currentIndex()) - self.gui.tags_view.recount_with_position_based_index() + def remove_format_from_selected_books(self, fmt): + ids = self._get_selected_ids() + if not ids: + return + db = self.gui.current_db.new_api + fmt = fmt.upper() + for bid in ids: + if fmt in db.formats(bid): + break + else: + return error_dialog(self.gui, _('Format not found'), _('The {} format is not present in the selected books.').format(fmt), show=True) + if not confirm( + '

'+ ngettext( + _('The {fmt} format will be permanently deleted from {title}.'), + _('The {fmt} format will be permanently deleted from all {num} selected books.'), + len(ids)).format(fmt=fmt.upper(), num=len(ids), title=self.gui.current_db.title(next(iter(ids)), index_is_id=True) + ) + ' ' + _('Are you sure?'), 'library_delete_specific_format_from_selected', self.gui + ): + return + self._remove_formats_from_ids((fmt,), ids) def restore_format(self, book_id, original_fmt): self.gui.current_db.restore_original_format(book_id, original_fmt) diff --git a/src/calibre/gui2/tag_browser/view.py b/src/calibre/gui2/tag_browser/view.py index 4cf85a0d9a..5173c889df 100644 --- a/src/calibre/gui2/tag_browser/view.py +++ b/src/calibre/gui2/tag_browser/view.py @@ -507,6 +507,7 @@ class TagsView(QTreeView): # {{{ is_first_letter=False, ignore_vl=False): if not action: return + from calibre.gui2.ui import get_gui try: if action == 'set_icon': try: @@ -585,7 +586,6 @@ class TagsView(QTreeView): # {{{ self._toggle(index, set_to=search_state) return if action == "raw_search": - from calibre.gui2.ui import get_gui get_gui().get_saved_search_text(search_name='search:' + key) return if action == 'add_to_category': @@ -638,6 +638,10 @@ class TagsView(QTreeView): # {{{ if action == 'edit_author_link': self.author_sort_edit.emit(self, index, False, True, False) return + if action == 'remove_format': + gui = get_gui() + gui.iactions['Remove Books'].remove_format_from_selected_books(key) + return reset_filter_categories = True if action == 'hide': @@ -973,6 +977,9 @@ class TagsView(QTreeView): # {{{ self.context_menu.addAction(_('Manage Saved searches'), partial(self.context_menu_handler, action='manage_searches', category=tag.name if tag else None)) + elif key == 'formats': + self.context_menu.addAction(_('Remove the {} format from selected books').format(tag.name), partial( + self.context_menu_handler, action='remove_format', key=tag.name)) # Hide/Show/Restore categories self.context_menu.addSeparator()