Fix #1992273 [[Enhancement] Remove format from book by right-clicking on the format in the Tag browser](https://bugs.launchpad.net/calibre/+bug/1992273)

This commit is contained in:
Kovid Goyal 2022-10-10 10:55:24 +05:30
parent 69eb9ee126
commit f4d63adc07
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 35 additions and 7 deletions

View File

@ -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('<p>'+(_(
@ -180,13 +186,28 @@ class DeleteAction(InterfaceAction):
'%(title)s. Are you sure?')%dict(fmt=fmt, title=title)) +
'</p>', '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(
'<p>'+ ngettext(
_('The {fmt} format will be <b>permanently deleted</b> from {title}.'),
_('The {fmt} format will be <b>permanently deleted</b> 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)

View File

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