mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
mime images; use cacheKey for duplicate pastes
This commit is contained in:
parent
5f2eb6e5fa
commit
7b31fd6785
1 changed files with 56 additions and 45 deletions
|
@ -781,6 +781,7 @@ class EditorWebView(AnkiWebView):
|
||||||
self.editor = editor
|
self.editor = editor
|
||||||
self.__tmpDir = None
|
self.__tmpDir = None
|
||||||
self.errtxt = _("An error occured while opening %s")
|
self.errtxt = _("An error occured while opening %s")
|
||||||
|
self.strip = self.editor.mw.config['stripHTML']
|
||||||
# if sys.platform.startswith("win32"):
|
# if sys.platform.startswith("win32"):
|
||||||
# self._ownLayout = None
|
# self._ownLayout = None
|
||||||
|
|
||||||
|
@ -832,8 +833,25 @@ class EditorWebView(AnkiWebView):
|
||||||
print "text", mime.text()
|
print "text", mime.text()
|
||||||
if mime.hasUrls():
|
if mime.hasUrls():
|
||||||
return self._processUrls(mime)
|
return self._processUrls(mime)
|
||||||
if mime.hasText() and (self.mw.config['stripHTML'] or
|
if mime.hasText() and (self.strip or not mime.hasHtml()):
|
||||||
not mime.hasHtml()):
|
return self._processText(mime)
|
||||||
|
if mime.hasHtml():
|
||||||
|
return self._processHtml(mime)
|
||||||
|
if mime.hasImage():
|
||||||
|
return self._processImage(mime)
|
||||||
|
|
||||||
|
def _processUrls(self, mime):
|
||||||
|
links = []
|
||||||
|
for url in mime.urls():
|
||||||
|
url = unicode(url.toString())
|
||||||
|
link = self._retrieveURL(url)
|
||||||
|
if link:
|
||||||
|
links.append(link)
|
||||||
|
mime = QMimeData()
|
||||||
|
mime.setHtml("".join(links))
|
||||||
|
return mime
|
||||||
|
|
||||||
|
def _processText(self, mime):
|
||||||
txt = unicode(mime.text())
|
txt = unicode(mime.text())
|
||||||
l = txt.lower()
|
l = txt.lower()
|
||||||
if l.startswith("http://") or l.startswith("file://"):
|
if l.startswith("http://") or l.startswith("file://"):
|
||||||
|
@ -863,31 +881,21 @@ class EditorWebView(AnkiWebView):
|
||||||
else:
|
else:
|
||||||
self.insertPlainText(mime.text())
|
self.insertPlainText(mime.text())
|
||||||
return True
|
return True
|
||||||
if mime.hasImage():
|
|
||||||
im = QImage(mime.imageData())
|
def _processHtml(self, mime):
|
||||||
if im.hasAlphaChannel():
|
|
||||||
(fd, name) = tempfile.mkstemp(prefix="paste", suffix=".png")
|
|
||||||
uname = unicode(name, sys.getfilesystemencoding())
|
|
||||||
im.save(uname)
|
|
||||||
else:
|
|
||||||
(fd, name) = tempfile.mkstemp(prefix="paste", suffix=".jpg")
|
|
||||||
uname = unicode(name, sys.getfilesystemencoding())
|
|
||||||
im.save(uname, None, 95)
|
|
||||||
self.parent._addPicture(uname, widget=self)
|
|
||||||
return True
|
|
||||||
if mime.hasHtml():
|
|
||||||
self.insertHtml(self.simplifyHTML(unicode(mime.html())))
|
self.insertHtml(self.simplifyHTML(unicode(mime.html())))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _processUrls(self, mime):
|
def _processImage(self, mime):
|
||||||
links = []
|
im = QImage(mime.imageData())
|
||||||
for url in mime.urls():
|
name = os.path.join(self._tmpDir(), "paste-%d.png" % im.cacheKey())
|
||||||
url = unicode(url.toString())
|
uname = unicode(name, sys.getfilesystemencoding())
|
||||||
link = self._retrieveURL(url)
|
if im.hasAlphaChannel():
|
||||||
if link:
|
im.save(uname)
|
||||||
links.append(link)
|
else:
|
||||||
|
im.save(uname, None, 95)
|
||||||
mime = QMimeData()
|
mime = QMimeData()
|
||||||
mime.setHtml("".join(links))
|
mime.setHtml(self._addMedia(uname))
|
||||||
return mime
|
return mime
|
||||||
|
|
||||||
def _retrieveURL(self, url):
|
def _retrieveURL(self, url):
|
||||||
|
@ -903,6 +911,9 @@ class EditorWebView(AnkiWebView):
|
||||||
file = open(path, "wb")
|
file = open(path, "wb")
|
||||||
file.write(filecontents)
|
file.write(filecontents)
|
||||||
file.close()
|
file.close()
|
||||||
|
self._addMedia(path)
|
||||||
|
|
||||||
|
def _addMedia(self, path):
|
||||||
# copy to media folder
|
# copy to media folder
|
||||||
name = self.editor.mw.deck.media.addFile(path)
|
name = self.editor.mw.deck.media.addFile(path)
|
||||||
print "name was", name
|
print "name was", name
|
||||||
|
|
Loading…
Reference in a new issue