From a12e6f6b653b97dc3e68d64d5ed9a6003d592487 Mon Sep 17 00:00:00 2001 From: Houssam Salem Date: Sat, 15 Feb 2014 17:26:03 +1100 Subject: [PATCH] Issue #998: Allow copy/paste and context menu in all card windows. --- aqt/browser.py | 3 +-- aqt/clayout.py | 4 ++-- aqt/main.py | 2 ++ aqt/webview.py | 17 +++++++++++++---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/aqt/browser.py b/aqt/browser.py index 7622fe64b..a267f2841 100644 --- a/aqt/browser.py +++ b/aqt/browser.py @@ -986,8 +986,7 @@ where id in %s""" % ids2str(sf)) c(self._previewWindow, SIGNAL("finished(int)"), self._onPreviewFinished) vbox = QVBoxLayout() vbox.setMargin(0) - self._previewWeb = AnkiWebView() - self._previewWeb.setFocusPolicy(Qt.NoFocus) + self._previewWeb = AnkiWebView(True) vbox.addWidget(self._previewWeb) bbox = QDialogButtonBox() self._previewPrev = bbox.addButton("<", QDialogButtonBox.ActionRole) diff --git a/aqt/clayout.py b/aqt/clayout.py index 2d3b858e1..aa5f423e6 100644 --- a/aqt/clayout.py +++ b/aqt/clayout.py @@ -120,9 +120,9 @@ class CardLayout(QDialog): self.model, joinFields(self.note.fields))) for g in pform.groupBox, pform.groupBox_2: g.setTitle(g.title() + _(" (1 of %d)") % max(cnt, 1)) - pform.frontWeb = AnkiWebView() + pform.frontWeb = AnkiWebView(True) pform.frontPrevBox.addWidget(pform.frontWeb) - pform.backWeb = AnkiWebView() + pform.backWeb = AnkiWebView(True) pform.backPrevBox.addWidget(pform.backWeb) for wig in pform.frontWeb, pform.backWeb: wig.page().setLinkDelegationPolicy( diff --git a/aqt/main.py b/aqt/main.py index 5fe6bf3f5..273f2b066 100644 --- a/aqt/main.py +++ b/aqt/main.py @@ -409,10 +409,12 @@ the manual for information on how to restore from an automatic backup.")) def _reviewState(self, oldState): self.reviewer.show() + self.web.setCardViewer(True) def _reviewCleanup(self, newState): if newState != "resetRequired" and newState != "review": self.reviewer.cleanup() + self.web.setCardViewer(False) def noteChanged(self, nid): "Called when a card or note is edited (but not deleted)." diff --git a/aqt/webview.py b/aqt/webview.py index 278cfec15..3dd3ad990 100644 --- a/aqt/webview.py +++ b/aqt/webview.py @@ -40,7 +40,7 @@ class AnkiWebPage(QWebPage): class AnkiWebView(QWebView): - def __init__(self): + def __init__(self, isCardViewer=False): QWebView.__init__(self) self.setRenderHints( QPainter.TextAntialiasing | @@ -59,6 +59,7 @@ class AnkiWebView(QWebView): self.allowDrops = False # reset each time new html is set; used to detect if still in same state self.key = None + self.setCardViewer(isCardViewer) def keyPressEvent(self, evt): if evt.matches(QKeySequence.Copy): @@ -78,9 +79,7 @@ class AnkiWebView(QWebView): QWebView.keyReleaseEvent(self, evt) def contextMenuEvent(self, evt): - # lazy: only run in reviewer - import aqt - if aqt.mw.state != "review": + if not self.isCardViewer: return m = QMenu(self) a = m.addAction(_("Copy")) @@ -131,6 +130,16 @@ button { def setBridge(self, bridge): self._bridge.setBridge(bridge) + def setCardViewer(self, isCardViewer=False): + """Set flag to denote if this WebView should follow rules specific to + card display (e.g., allow context menu, copy/paste)""" + + self.isCardViewer = isCardViewer + if self.isCardViewer: + self.setFocusPolicy(Qt.WheelFocus) + else: + self.setFocusPolicy(Qt.NoFocus) + def eval(self, js): self.page().mainFrame().evaluateJavaScript(js)