Skip to content

Commit

Permalink
Edit book: Allow selecting multiple books to edit at once, opening al…
Browse files Browse the repository at this point in the history
…l selected books in separate editor instances
  • Loading branch information
kovidgoyal committed Feb 12, 2024
1 parent 2acb96a commit 1ff9bf9
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/calibre/gui2/actions/tweak_epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

class Choose(QDialog):

def __init__(self, fmts, parent=None):
def __init__(self, title, fmts, parent=None):
QDialog.__init__(self, parent)
self.l = l = QVBoxLayout(self)
self.setLayout(l)
self.setWindowTitle(_('Choose format to edit'))

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)

self.rem = QCheckBox(_('Always ask when more than one format is available'))
Expand Down Expand Up @@ -91,13 +91,16 @@ def genesis(self):
self.qaction.triggered.connect(self.tweak_book)

def tweak_book(self):
row = self.gui.library_view.currentIndex()
if not row.isValid():
ids = self.gui.library_view.get_selected_ids()
if not ids:
return error_dialog(self.gui, _('Cannot Edit book'),
_('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)
self.do_tweak(book_id)
for book_id in ids:
self.do_tweak(book_id)

def do_tweak(self, book_id):
if self.gui.current_view() is not self.gui.library_view:
Expand All @@ -108,10 +111,11 @@ def do_tweak(self, book_id):
fmts = db.formats(book_id, index_is_id=True) or ''
fmts = [x.upper().strip() for x in fmts.split(',') if x]
tweakable_fmts = set(fmts).intersection(SUPPORTED)
title = db.new_api.field_for('title', book_id)
if not tweakable_fmts:
if not fmts:
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
tweakable_fmts = {'EPUB'}
self.gui.iactions['Add Books'].add_empty_format_to_book(book_id, 'EPUB')
Expand All @@ -120,14 +124,14 @@ def do_tweak(self, book_id):
self.gui.library_view.model().current_changed(current_idx, current_idx)
else:
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.'
) % (_(' or ').join(SUPPORTED)), show=True)
).format(title, _(' or ').join(SUPPORTED)), show=True)
from calibre.gui2.tweak_book import tprefs
tprefs.refresh() # In case they were changed in a Tweak Book process
if len(tweakable_fmts) > 1:
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:
return
tweakable_fmts = {d.fmt}
Expand Down

0 comments on commit 1ff9bf9

Please sign in to comment.