Edit metadata dialog: Allow right clicking on the cover to edit it with an external program. Fixes #2103833 [[Enhancement] Add 'open with' to image viewer](https://bugs.launchpad.net/calibre/+bug/2103833)

This commit is contained in:
Kovid Goyal 2025-03-25 12:09:16 +05:30
parent cf47ee5eb7
commit c11e6511a5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 31 additions and 3 deletions

View File

@ -744,16 +744,16 @@ def details_context_menu_event(view, ev, book_info, add_popup_action=False, edit
# }}}
def create_open_cover_with_menu(self, parent_menu):
def create_open_cover_with_menu(self, parent_menu, text=''):
from calibre.gui2.open_with import edit_programs, populate_menu
m = QMenu(_('Open cover with...'))
m = QMenu(text or _('Open cover with...'))
def connect_action(ac, entry):
connect_lambda(ac.triggered, self, lambda self: self.open_with(entry))
populate_menu(m, connect_action, 'cover_image')
if len(m.actions()) == 0:
parent_menu.addAction(_('Open cover with...'), self.choose_open_with)
parent_menu.addAction(text or _('Open cover with...'), self.choose_open_with)
else:
m.addSeparator()
m.addAction(_('Add another application to open cover with...'), self.choose_open_with)

View File

@ -1232,6 +1232,8 @@ class Cover(ImageView): # {{{
m = super().build_context_menu()
m.addSeparator()
m.addAction(QIcon.ic('view-image'), _('View image in popup window'), self.view_image)
from calibre.gui2.book_details import create_open_cover_with_menu
create_open_cover_with_menu(self, m, _('Edit cover with...'))
return m
def mouseDoubleClickEvent(self, event):
@ -1249,6 +1251,32 @@ class Cover(ImageView): # {{{
from calibre.utils.img import image_to_data
self.current_val = image_to_data(d.current_img.toImage(), fmt='png')
def open_with(self, entry):
from calibre.gui2 import info_dialog
from calibre.gui2.open_with import run_program
from calibre.utils.img import image_from_data, save_image
cdata = self.current_val
img = image_from_data(cdata)
pt = PersistentTemporaryFile(suffix='.png')
pt.close()
try:
save_image(img, pt.name)
run_program(entry, pt.name, self)
info_dialog(self, _('Cover opened in {}').format(entry.get('name') or _('external editor')), _(
'Close this popup when you are done making changes to the cover'), show=True, show_copy_button=False)
finally:
with open(pt.name, 'rb') as f:
ncdata = f.read()
os.remove(pt.name)
if ncdata and ncdata != cdata:
self.current_val = ncdata
def choose_open_with(self):
from calibre.gui2.open_with import choose_program
entry = choose_program('cover_image', self)
if entry is not None:
self.open_with(entry)
def undo_trim(self):
if self.cdata_before_trim:
self.current_val = self.cdata_before_trim