Skip to content

Commit

Permalink
Right click context menu on images not useful (ankitects#3362)
Browse files Browse the repository at this point in the history
* right click and copy on image works

* renamed helper fn

* separated functionality of copy and copy image

* Update CONTRIBUTORS

* snake case

* Update CONTRIBUTORS
  • Loading branch information
bpnguyen107 authored and jeankhawand committed Aug 17, 2024
1 parent 91e24a9 commit 880ea7c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions ftl/core/editing.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 28 additions & 4 deletions qt/aqt/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,16 @@ def onCut(self) -> None:
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
Expand Down Expand Up @@ -1575,15 +1585,29 @@ def _processImage(self, mime: QMimeData, extended: bool = False) -> str | None:

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
Expand Down

0 comments on commit 880ea7c

Please sign in to comment.