Allow showing icons in HTMLDisplay

This commit is contained in:
Kovid Goyal 2022-06-09 21:26:34 +05:30
parent 6111955dc6
commit 6c51e54d53
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 18 additions and 0 deletions

View File

@ -178,6 +178,21 @@ class IconResourceManager:
ans = q ans = q
return ans return ans
def icon_as_png(self, name, as_bytearray=False, compression_level=0):
ans = self(name)
ba = QByteArray()
if ans.availableSizes():
from qt.core import QImageWriter
pmap = ans.pixmap(ans.availableSizes()[0])
buf = QBuffer(ba)
buf.open(QIODevice.OpenModeFlag.WriteOnly)
w = QImageWriter(buf, b'PNG')
cl = min(9, max(0, compression_level))
w.setQuality(10 * (9-cl))
w.setQuality(90)
w.write(pmap.toImage())
return ba if as_bytearray else ba.data()
def set_theme(self): def set_theme(self):
current = QIcon.themeName() current = QIcon.themeName()
new = self.dark_theme_name if QApplication.instance().is_dark_theme else self.light_theme_name new = self.dark_theme_name if QApplication.instance().is_dark_theme else self.light_theme_name
@ -190,6 +205,7 @@ class IconResourceManager:
icon_resource_manager = IconResourceManager() icon_resource_manager = IconResourceManager()
QIcon.ic = icon_resource_manager QIcon.ic = icon_resource_manager
QIcon.icon_as_png = icon_resource_manager.icon_as_png
# Setup gprefs {{{ # Setup gprefs {{{

View File

@ -584,6 +584,8 @@ class HTMLDisplay(QTextBrowser):
'426082')) '426082'))
else: else:
return QByteArray(data) return QByteArray(data)
elif qurl.scheme() == 'calibre-icon':
return QIcon.icon_as_png(qurl.path().lstrip('/'), as_bytearray=True)
else: else:
return QTextBrowser.loadResource(self, rtype, qurl) return QTextBrowser.loadResource(self, rtype, qurl)