From 0a45fa19afedea870b5f6bfd6278b0686c357886 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 28 Jan 2018 17:17:21 +0530 Subject: [PATCH] E-book viewer: Fix copy keyboard shortcut/context menu action not working on some windows systems Now all copying happens via the current selection. This means that HTML copying no longer works. But it is likely needed for systems with buggy clipboard implementations. See #1477696 (Copy-Paste does not work) --- src/calibre/gui2/viewer/documentview.py | 28 +++++++------------------ src/calibre/gui2/viewer/main.py | 4 ++-- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 574dd6c769..1ac8f9cb85 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -12,7 +12,7 @@ from future_builtins import map from PyQt5.Qt import ( QSize, QSizePolicy, QUrl, Qt, QPainter, QPalette, QBrush, QDialog, QColor, QPoint, QImage, QRegion, QIcon, QAction, QMenu, - pyqtSignal, QApplication, pyqtSlot, QKeySequence, QMimeData) + pyqtSignal, QApplication, pyqtSlot, QKeySequence) from PyQt5.QtWebKitWidgets import QWebPage, QWebView from PyQt5.QtWebKit import QWebSettings, QWebElement @@ -553,13 +553,10 @@ class DocumentView(QWebView): # {{{ self.document.selectionChanged[()].connect(self.selection_changed) self.document.animated_scroll_done_signal.connect(self.animated_scroll_done, type=Qt.QueuedConnection) self.document.page_turn.connect(self.page_turn_requested) - copy_action = self.copy_action - copy_action.setIcon(QIcon(I('edit-copy.png'))) - copy_action.triggered.connect(self.copy, Qt.QueuedConnection) d = self.document self.unimplemented_actions = list(map(self.pageAction, [d.DownloadImageToDisk, d.OpenLinkInNewWindow, d.DownloadLinkToDisk, d.CopyImageUrlToClipboard, - d.OpenImageInNewWindow, d.OpenLink, d.Reload, d.InspectElement])) + d.OpenImageInNewWindow, d.OpenLink, d.Reload, d.InspectElement, d.Copy])) self.search_online_action = QAction(QIcon(I('search.png')), '', self) self.search_online_action.triggered.connect(self.search_online) @@ -628,10 +625,6 @@ class DocumentView(QWebView): # {{{ if self.manager is not None: self.manager.goto_end() - @property - def copy_action(self): - return self.pageAction(self.document.Copy) - def animated_scroll_done(self): if self.manager is not None: self.manager.scrolled(self.document.scroll_fraction) @@ -668,17 +661,6 @@ class DocumentView(QWebView): # {{{ def selected_text(self): return self.document.selectedText().replace(u'\u00ad', u'').strip() - def copy(self): - self.document.triggerAction(self.document.Copy) - c = QApplication.clipboard() - md = c.mimeData() - if iswindows: - nmd = QMimeData() - nmd.setHtml(md.html().replace(u'\u00ad', '')) - md = nmd - md.setText(self.selected_text) - QApplication.clipboard().setMimeData(md) - def selection_changed(self): if self.manager is not None: self.manager.selection_changed(self.selected_text) @@ -724,6 +706,9 @@ class DocumentView(QWebView): # {{{ for action in self.unimplemented_actions: menu.removeAction(action) + if self.manager is not None and self.manager.action_copy.isEnabled(): + menu.addAction(self.manager.action_copy) + if not img.isNull(): cia = self.pageAction(self.document.CopyImageToClipboard) for action in menu.actions(): @@ -1383,7 +1368,8 @@ class DocumentView(QWebView): # {{{ if self.manager is not None: self.manager.forward(None) elif event.matches(QKeySequence.Copy): - self.copy() + if self.manager is not None: + self.manager.copy() else: handled = False return handled diff --git a/src/calibre/gui2/viewer/main.py b/src/calibre/gui2/viewer/main.py index 0a8c30135a..a903ae4ba9 100644 --- a/src/calibre/gui2/viewer/main.py +++ b/src/calibre/gui2/viewer/main.py @@ -574,7 +574,7 @@ class EbookViewer(MainWindow): self.selected_text = selected_text.strip() self.action_copy.setEnabled(bool(self.selected_text)) - def copy(self, x): + def copy(self, x=False): if self.selected_text: QApplication.clipboard().setText(self.selected_text) @@ -1098,7 +1098,7 @@ class EbookViewer(MainWindow): action = { 'Quit':self.action_quit, 'Show metadata':self.action_metadata, - 'Copy':self.view.copy_action, + 'Copy':self.action_copy, 'Font larger': self.action_font_size_larger, 'Font smaller': self.action_font_size_smaller, 'Fullscreen': self.action_full_screen,