Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/private/dquickiconimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ void DQuickIconImagePrivate::updateBase64Image()
setImage(image);
}

void DQuickIconImagePrivate::updateFileUrlImage()
{
Q_ASSERT(iconType == FileUrl);

D_Q(DQuickIconImage);

// 处理本地PNG文件
QUrl url(name);
if (url.isValid()) {
QImage image(url.toLocalFile());
if (!image.isNull()) {
QSize iconSize = q->sourceSize();
if (iconSize.isEmpty()) {
iconSize = image.size();
}
// 应用devicePixelRatio进行缩放
image = image.scaled(iconSize * devicePixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QImage image(icon_size * devicePixelRatio, QImage::Format_ARGB32);

是不是走的这里呀,

setImage(image);
}
}
}

QImage DQuickIconImagePrivate::requestImageFromBase64(const QString &name, const QSize &requestedSize, qreal devicePixelRatio)
{
const QString flag("base64,");
Expand Down Expand Up @@ -94,6 +116,8 @@ void DQuickIconImagePrivate::maybeUpdateUrl()
if (iconType != ThemeIconName) {
if (iconType == Base64Data)
updateBase64Image();
else if (iconType == FileUrl)
updateFileUrlImage();
return;
}

Expand Down Expand Up @@ -216,10 +240,10 @@ void DQuickIconImage::setName(const QString &name)
} else if (QQmlFile::isLocalFile(name)) {
QUrl url(name);

// 如果name指定的是一个url,则直接将其当作url使用
// 如果name指定的是一个url,则将其标记为FileUrl类型
// 不直接设置source,而是通过maybeUpdateUrl统一处理以支持缩放
if (url.isValid()) {
d->iconType = DQuickIconImagePrivate::FileUrl;
setSource(url);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/private/dquickiconimage_p_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DQuickIconImagePrivate : public QQuickImagePrivate
bool updateDevicePixelRatio(qreal targetDevicePixelRatio) override;

void updateBase64Image();
void updateFileUrlImage();

static QImage requestImageFromBase64(const QString &name, const QSize &requestedSize, qreal devicePixelRatio);

Expand All @@ -45,7 +46,7 @@ class DQuickIconImagePrivate : public QQuickImagePrivate
enum IconType : qint8 {
ThemeIconName, // 图标名称
Base64Data, // base64编码的图标图片数据
FileUrl // 图标文件的url地址
FileUrl, // 图标文件的url地址
};

// 记录此图标是否应该从图标主题中获取。
Expand Down