allow the user to paste with formatting by holding down; don't clobber clipboard

This commit is contained in:
Damien Elmes 2011-04-21 07:56:18 +09:00
parent d0c9beddfd
commit 2b326e5773

View file

@ -4,7 +4,7 @@
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
from PyQt4.QtWebKit import QWebView from PyQt4.QtWebKit import QWebView, QWebPage
import re, os, sys, urllib2, ctypes, simplejson, traceback import re, os, sys, urllib2, ctypes, simplejson, traceback
from anki.utils import stripHTML, parseTags, isWin, namedtmp from anki.utils import stripHTML, parseTags, isWin, namedtmp
from anki.sound import play from anki.sound import play
@ -777,14 +777,22 @@ class EditorWebView(AnkiWebView):
def keyPressEvent(self, evt): def keyPressEvent(self, evt):
self._curKey = True self._curKey = True
if evt.matches(QKeySequence.Paste): self.origClip = None
self.onPaste() shiftPaste = (evt.modifiers() == (Qt.ShiftModifier | Qt.ControlModifier)
return QWebView.keyPressEvent(self, evt) and evt.key() == Qt.Key_V)
if evt.matches(QKeySequence.Paste) or shiftPaste:
self.prepareClip(shiftPaste)
if shiftPaste:
self.triggerPageAction(QWebPage.Paste)
QWebView.keyPressEvent(self, evt)
if self.origClip:
self.restoreClip()
def contextMenuEvent(self, evt): def contextMenuEvent(self, evt):
# adjust in case the user is going to paste # adjust in case the user is going to paste
self.onPaste() self.prepareClip()
QWebView.contextMenuEvent(self, evt) QWebView.contextMenuEvent(self, evt)
self.restoreClip()
def dropEvent(self, evt): def dropEvent(self, evt):
oldmime = evt.mimeData() oldmime = evt.mimeData()
@ -809,12 +817,39 @@ class EditorWebView(AnkiWebView):
evt.accept() evt.accept()
QWebView.dropEvent(self, new) QWebView.dropEvent(self, new)
def onPaste(self): def prepareClip(self, keep=False):
clip = self.editor.mw.app.clipboard() clip = self.editor.mw.app.clipboard()
mime = clip.mimeData() mime = clip.mimeData()
mime = self._processMime(mime) self.saveClip()
if keep:
new = QMimeData()
if mime.hasHtml():
new.setHtml(mime.html())
else:
new.setText(mime.text())
mime = new
else:
mime = self._processMime(mime)
clip.setMimeData(mime) clip.setMimeData(mime)
def restoreClip(self):
clip = self.editor.mw.app.clipboard()
clip.setMimeData(self.origClip)
def saveClip(self):
# we don't own the clipboard object, so we need to copy it
mime = self.editor.mw.app.clipboard().mimeData()
n = QMimeData()
if mime.hasText():
n.setText(mime.text())
if mime.hasHtml():
n.setHtml(mime.html())
if mime.hasUrls():
n.setUrls(mime.urls())
if mime.hasImage():
n.setImageData(mime.imageData())
self.origClip = n
def _processMime(self, mime): def _processMime(self, mime):
# print "html=%s image=%s urls=%s txt=%s" % ( # print "html=%s image=%s urls=%s txt=%s" % (
# mime.hasHtml(), mime.hasImage(), mime.hasUrls(), mime.hasText()) # mime.hasHtml(), mime.hasImage(), mime.hasUrls(), mime.hasText())