mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
Add method to convert resource to data uri, add button image with this
This commit is contained in:
parent
81ed10faee
commit
67246f8c80
1 changed files with 11 additions and 1 deletions
|
@ -8,6 +8,8 @@ import ctypes
|
||||||
import urllib.request, urllib.parse, urllib.error
|
import urllib.request, urllib.parse, urllib.error
|
||||||
import warnings
|
import warnings
|
||||||
import html
|
import html
|
||||||
|
import mimetypes
|
||||||
|
import base64
|
||||||
|
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
|
@ -476,9 +478,17 @@ class Editor(object):
|
||||||
# Top buttons
|
# Top buttons
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
def resourceToData(self, path):
|
||||||
|
"""Convert a file (specified by a path) into a data URI."""
|
||||||
|
mime, _ = mimetypes.guess_type(path)
|
||||||
|
with open(path, 'rb') as fp:
|
||||||
|
data = fp.read()
|
||||||
|
data64 = b''.join(base64.encodestring(data).splitlines())
|
||||||
|
return 'data:%s;base64,%s' % (mime, data64.decode('ascii'))
|
||||||
|
|
||||||
def _addButton(self, icon, cmd, tip="", id=None):
|
def _addButton(self, icon, cmd, tip="", id=None):
|
||||||
if os.path.isabs(icon):
|
if os.path.isabs(icon):
|
||||||
iconstr = icon
|
iconstr = self.resourceToData(icon)
|
||||||
else:
|
else:
|
||||||
iconstr = "qrc:/icons/{}.png".format(icon)
|
iconstr = "qrc:/icons/{}.png".format(icon)
|
||||||
if id:
|
if id:
|
||||||
|
|
Loading…
Reference in a new issue