Utils: Add support for icons and embedded resources

Adds support for Icons, Theme Icons and resources to the
MarkdownBrowser Image handling.

Change-Id: I58702bec8290dfc1518d9fa564815b65d3750a2c
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Marcus Tillmanns
2025-02-27 08:46:05 +01:00
parent bb673a66f2
commit ded71a05e3
2 changed files with 42 additions and 3 deletions

View File

@@ -22,6 +22,11 @@ int main() {
* [Is this a link to the Qt website?](https://www.qt.io)
* [Is this an anchor link that scrolls up to the top?](#markdown-browser-test)
## Icons
* Is the Home icon visible? ![You should see the home icon here](icon://HOME "Home Icon")
* Is this seek forward theme icon visible? ![You should see a seek forward icon here](theme://media-seek-forward "Seek Forward Icon")
]]
local mb = G.MarkdownBrowser {

View File

@@ -332,12 +332,39 @@ public:
Q_UNUSED(document);
Q_UNUSED(posInDocument);
Entry::Pointer *entryPtr = m_entries.object(format.toImageFormat().name());
const QString name = format.toImageFormat().name();
Entry::Pointer *entryPtr = m_entries.object(name);
if (!entryPtr) {
constexpr QStringView themeScheme(u"theme://");
constexpr QStringView iconScheme(u"icon://");
QVariant resource = document->resource(QTextDocument::ImageResource, name);
if (resource.isValid()) {
const QImage img = qvariant_cast<QImage>(resource);
if (!img.isNull()) {
painter->drawImage(rect, img);
return;
}
} else if (name.startsWith(themeScheme)) {
const QIcon icon = QIcon::fromTheme(name.mid(themeScheme.length()));
if (!icon.isNull()) {
painter->drawPixmap(
rect.toRect(),
icon.pixmap(rect.size().toSize(), painter->device()->devicePixelRatioF()));
return;
}
} else if (name.startsWith(iconScheme)) {
std::optional<Icon> icon = Icons::fromString(name.mid(iconScheme.length()));
if (icon) {
painter->drawPixmap(rect.toRect(), icon->pixmap());
return;
}
}
if (!entryPtr)
painter->drawPixmap(
rect.toRect(), Utils::Icons::UNKNOWN_FILE.icon().pixmap(rect.size().toSize()));
else if (!(*entryPtr)->movie.isValid())
} else if (!(*entryPtr)->movie.isValid())
painter->drawPixmap(rect.toRect(), m_brokenImage.pixmap(rect.size().toSize()));
else
painter->drawImage(rect, (*entryPtr)->movie.currentImage());
@@ -418,9 +445,16 @@ public:
};
const auto isLocalUrl = [this, isRemoteUrl](const QUrl &url) {
QVariant res = this->resource(QTextDocument::ImageResource, url);
if (res.isValid())
return false;
if (url.scheme() == "qrc")
return true;
if (!url.scheme().isEmpty())
return false;
if (!m_basePath.isEmpty() && !isRemoteUrl(url))
return true;