From c99b50c82f42ba01c7a8727f1659d0c66cc61d34 Mon Sep 17 00:00:00 2001 From: bpnguyen107 <105088397+bpnguyen107@users.noreply.github.com> Date: Fri, 16 Aug 2024 23:18:07 -0700 Subject: [PATCH] Right click context menu on images not useful (#3362) * right click and copy on image works * renamed helper fn * separated functionality of copy and copy image * Update CONTRIBUTORS * snake case * Update CONTRIBUTORS --- CONTRIBUTORS | 2 +- ftl/core/editing.ftl | 1 + qt/aqt/editor.py | 32 ++++++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 481b5eb63..dca1a11c3 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -187,7 +187,7 @@ Christian Donat Asuka Minato Dillon Baldwin Voczi -Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com> +Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com> ******************** The text of the 3 clause BSD license follows: diff --git a/ftl/core/editing.ftl b/ftl/core/editing.ftl index 692a9216c..5c9b766f5 100644 --- a/ftl/core/editing.ftl +++ b/ftl/core/editing.ftl @@ -10,6 +10,7 @@ editing-center = Center editing-change-color = Change color editing-cloze-deletion = Cloze deletion (new card) editing-cloze-deletion-repeat = Cloze deletion (same card) +editing-copy-image = Copy image editing-couldnt-record-audio-have-you-installed = Couldn't record audio. Have you installed 'lame'? editing-customize-card-templates = Customize Card Templates editing-customize-fields = Customize Fields diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index b87f9c62a..6f0a994d7 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -1433,6 +1433,16 @@ class EditorWebView(AnkiWebView): def onCopy(self) -> None: self.triggerPageAction(QWebEnginePage.WebAction.Copy) + def on_copy_image(self) -> None: + self.triggerPageAction(QWebEnginePage.WebAction.CopyImageToClipboard) + + def _opened_context_menu_on_image(self) -> bool: + context_menu_request = self.lastContextMenuRequest() + return ( + context_menu_request.mediaType() + == context_menu_request.MediaType.MediaTypeImage + ) + def _wantsExtendedPaste(self) -> bool: strip_html = self.editor.mw.col.get_config_bool( Config.Bool.PASTE_STRIPS_FORMATTING @@ -1575,15 +1585,29 @@ class EditorWebView(AnkiWebView): def contextMenuEvent(self, evt: QContextMenuEvent) -> None: m = QMenu(self) - a = m.addAction(tr.editing_cut()) - qconnect(a.triggered, self.onCut) - a = m.addAction(tr.actions_copy()) - qconnect(a.triggered, self.onCopy) + self._maybe_add_cut_action(m) + self._maybe_add_copy_action(m) a = m.addAction(tr.editing_paste()) qconnect(a.triggered, self.onPaste) + self._maybe_add_copy_image_action(m) gui_hooks.editor_will_show_context_menu(self, m) m.popup(QCursor.pos()) + def _maybe_add_cut_action(self, menu: QMenu) -> None: + if self.hasSelection(): + a = menu.addAction(tr.editing_cut()) + qconnect(a.triggered, self.onCut) + + def _maybe_add_copy_action(self, menu: QMenu) -> None: + if self.hasSelection(): + a = menu.addAction(tr.actions_copy()) + qconnect(a.triggered, self.onCopy) + + def _maybe_add_copy_image_action(self, menu: QMenu) -> None: + if self._opened_context_menu_on_image(): + a = menu.addAction(tr.editing_copy_image()) + qconnect(a.triggered, self.on_copy_image) + # QFont returns "Kozuka Gothic Pro L" but WebEngine expects "Kozuka Gothic Pro Light" # - there may be other cases like a trailing 'Bold' that need fixing, but will