allow drag&drop of sounds, fix unicode issues

This commit is contained in:
Damien Elmes 2009-01-05 08:24:45 +09:00
parent 3d994b6359
commit 744e46f7fb

View file

@ -4,7 +4,7 @@
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
import re, os, sys, tempfile, urllib import re, os, sys, tempfile, urllib2
from anki.utils import stripHTML, tidyHTML, canonifyTags from anki.utils import stripHTML, tidyHTML, canonifyTags
from anki.sound import playFromText from anki.sound import playFromText
import anki.sound import anki.sound
@ -567,8 +567,15 @@ class FactEditor(object):
file = ui.utils.getFile(self.parent, _("Add audio"), "audio", key) file = ui.utils.getFile(self.parent, _("Add audio"), "audio", key)
if not file: if not file:
return return
anki.sound.play(file) self._addSound(file, widget=w)
def _addSound(self, file, widget=None):
if widget:
w = widget
else:
w = self.focusedEdit()
path = self.deck.addMedia(file) path = self.deck.addMedia(file)
anki.sound.play(path)
w.insertHtml('[sound:%s]' % path) w.insertHtml('[sound:%s]' % path)
class FactEdit(QTextEdit): class FactEdit(QTextEdit):
@ -603,15 +610,23 @@ class FactEdit(QTextEdit):
if source.hasUrls(): if source.hasUrls():
for url in source.urls(): for url in source.urls():
url = unicode(url.toString()) url = unicode(url.toString())
ext = url.split(".")[-1] ext = url.split(".")[-1].lower()
if ext in ("jpg", "jpeg", "png", "tif", "tiff", "gif"): if ext in ("jpg", "jpeg", "png", "tif", "tiff", "gif"):
url = url.encode(sys.getfilesystemencoding()) name = self._retrieveURL(url, ext)
(file, headers) = urllib.urlretrieve(url) self.parent._addPicture(name, widget=self)
self.parent._addPicture( elif ext in ("wav", "mp3", "ogg", "flac"):
unicode(file, sys.getfilesystemencoding()), name = self._retrieveURL(url, ext)
widget=self) self.parent._addSound(name, widget=self)
return return
def _retrieveURL(self, url, ext):
filecontents = urllib2.urlopen(url).read()
(fd, name) = tempfile.mkstemp(suffix=".%s" % ext)
file = os.fdopen(fd, "wb")
file.write(filecontents)
file.flush()
return name
def simplifyHTML(self, html): def simplifyHTML(self, html):
"Remove all style information and P tags." "Remove all style information and P tags."
html = re.sub("\n", " ", html) html = re.sub("\n", " ", html)