Edit book: Allow selecting multiple books to edit at once, opening all selected books in separate editor instances

This commit is contained in:
Kovid Goyal 2024-02-12 14:34:37 +05:30
parent 2acb96a081
commit 1ff9bf9e24
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -17,14 +17,14 @@ from calibre.startup import connect_lambda
class Choose(QDialog): class Choose(QDialog):
def __init__(self, fmts, parent=None): def __init__(self, title, fmts, parent=None):
QDialog.__init__(self, parent) QDialog.__init__(self, parent)
self.l = l = QVBoxLayout(self) self.l = l = QVBoxLayout(self)
self.setLayout(l) self.setLayout(l)
self.setWindowTitle(_('Choose format to edit')) self.setWindowTitle(_('Choose format to edit'))
self.la = la = QLabel(_( self.la = la = QLabel(_(
'This book has multiple formats that can be edited. Choose the format you want to edit.')) 'The book "{}" has multiple formats that can be edited. Choose the format you want to edit.').format(title))
l.addWidget(la) l.addWidget(la)
self.rem = QCheckBox(_('Always ask when more than one format is available')) self.rem = QCheckBox(_('Always ask when more than one format is available'))
@ -91,13 +91,16 @@ class TweakEpubAction(InterfaceAction):
self.qaction.triggered.connect(self.tweak_book) self.qaction.triggered.connect(self.tweak_book)
def tweak_book(self): def tweak_book(self):
row = self.gui.library_view.currentIndex() ids = self.gui.library_view.get_selected_ids()
if not row.isValid(): if not ids:
return error_dialog(self.gui, _('Cannot Edit book'), return error_dialog(self.gui, _('Cannot Edit book'),
_('No book selected'), show=True) _('No book selected'), show=True)
if len(ids) > 10 and not question_dialog(self.gui, _('Are you sure?'), _(
'You are trying to edit {} books at once. Are you sure?').format(len(ids))):
return
book_id = self.gui.library_view.model().id(row) for book_id in ids:
self.do_tweak(book_id) self.do_tweak(book_id)
def do_tweak(self, book_id): def do_tweak(self, book_id):
if self.gui.current_view() is not self.gui.library_view: if self.gui.current_view() is not self.gui.library_view:
@ -108,10 +111,11 @@ class TweakEpubAction(InterfaceAction):
fmts = db.formats(book_id, index_is_id=True) or '' fmts = db.formats(book_id, index_is_id=True) or ''
fmts = [x.upper().strip() for x in fmts.split(',') if x] fmts = [x.upper().strip() for x in fmts.split(',') if x]
tweakable_fmts = set(fmts).intersection(SUPPORTED) tweakable_fmts = set(fmts).intersection(SUPPORTED)
title = db.new_api.field_for('title', book_id)
if not tweakable_fmts: if not tweakable_fmts:
if not fmts: if not fmts:
if not question_dialog(self.gui, _('No editable formats'), if not question_dialog(self.gui, _('No editable formats'),
_('Do you want to create an empty EPUB file to edit?')): _('Do you want to create an empty EPUB file in the book "{}" to edit?').format(title)):
return return
tweakable_fmts = {'EPUB'} tweakable_fmts = {'EPUB'}
self.gui.iactions['Add Books'].add_empty_format_to_book(book_id, 'EPUB') self.gui.iactions['Add Books'].add_empty_format_to_book(book_id, 'EPUB')
@ -120,14 +124,14 @@ class TweakEpubAction(InterfaceAction):
self.gui.library_view.model().current_changed(current_idx, current_idx) self.gui.library_view.model().current_changed(current_idx, current_idx)
else: else:
return error_dialog(self.gui, _('Cannot edit book'), _( return error_dialog(self.gui, _('Cannot edit book'), _(
'The book must be in the %s formats to edit.' 'The book "{0}" must be in the {1} formats to edit.'
'\n\nFirst convert the book to one of these formats.' '\n\nFirst convert the book to one of these formats.'
) % (_(' or ').join(SUPPORTED)), show=True) ).format(title, _(' or ').join(SUPPORTED)), show=True)
from calibre.gui2.tweak_book import tprefs from calibre.gui2.tweak_book import tprefs
tprefs.refresh() # In case they were changed in a Tweak Book process tprefs.refresh() # In case they were changed in a Tweak Book process
if len(tweakable_fmts) > 1: if len(tweakable_fmts) > 1:
if tprefs['choose_tweak_fmt']: if tprefs['choose_tweak_fmt']:
d = Choose(sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui) d = Choose(title, sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui)
if d.exec() != QDialog.DialogCode.Accepted: if d.exec() != QDialog.DialogCode.Accepted:
return return
tweakable_fmts = {d.fmt} tweakable_fmts = {d.fmt}