Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/private/dquickdciiconimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void DQuickDciIconImageItemPrivate::updatePlayer()
player->setPalette(palette);

// treeland下拿qApp的缩放不准确,初始化的时候尝试去更新一次缩放
updateDevicePixelRatio(1.0);
refreshDtkDevicePixelRatio();

player->setDevicePixelRatio(devicePixelRatio);
}
Expand Down
19 changes: 15 additions & 4 deletions src/private/dquickiconimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
DGUI_USE_NAMESPACE
DQUICK_BEGIN_NAMESPACE

bool DQuickIconImagePrivate::refreshDtkDevicePixelRatio()
{
if (iconType == FileUrl)
return false;

return updateDevicePixelRatio(1.0);
}

bool DQuickIconImagePrivate::updateDevicePixelRatio(qreal targetDevicePixelRatio)
{
if (iconType == FileUrl)
return QQuickImagePrivate::updateDevicePixelRatio(targetDevicePixelRatio);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
Comment thread
18202781743 marked this conversation as resolved.
Resolved

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if (!qApp->testAttribute(Qt::AA_UseHighDpiPixmaps)) {
devicePixelRatio = 1.0;
Expand Down Expand Up @@ -67,7 +78,7 @@ void DQuickIconImagePrivate::init()
D_Q(DQuickIconImage);

// 强制确保图标的缩放比例正确
updateDevicePixelRatio(1.0);
refreshDtkDevicePixelRatio();

if (iconType == ThemeIconName) {
QObject::connect(DGuiApplicationHelper::instance()->applicationTheme(), SIGNAL(iconThemeNameChanged(QByteArray)),
Expand Down Expand Up @@ -173,7 +184,7 @@ void DQuickIconImage::componentComplete()

connect(this, &QQuickItem::windowChanged, this, [this](){
D_D(DQuickIconImage);
if (d->updateDevicePixelRatio(1.0)) {
if (d->refreshDtkDevicePixelRatio()) {
d->maybeUpdateUrl();
}
});
Expand Down Expand Up @@ -356,7 +367,7 @@ void DQuickIconImage::itemChange(ItemChange change, const ItemChangeData &value)

switch (change) {
case ItemDevicePixelRatioHasChanged: {
if (d->updateDevicePixelRatio(1.0)) {
if (d->refreshDtkDevicePixelRatio()) {
d->maybeUpdateUrl();
}
}
Expand Down Expand Up @@ -384,7 +395,7 @@ void DQuickIconImage::pixmapChange()
// QQuickImage中只会在设置了souceSize的前提下才会计算图片自身的缩放比例
// 此处强制确保图标的缩放比例正确
D_D(DQuickIconImage);
if (d->updateDevicePixelRatio(1.0)) {
if (d->refreshDtkDevicePixelRatio()) {
d->maybeUpdateUrl();
}

Expand Down
3 changes: 2 additions & 1 deletion src/private/dquickiconimage_p_p.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2020 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand Down Expand Up @@ -28,6 +28,7 @@ class DQuickIconImagePrivate : public QQuickImagePrivate
DQuickIconImage::Mode getIconMode() const;

qreal calculateDevicePixelRatio() const;
bool refreshDtkDevicePixelRatio();
bool updateDevicePixelRatio(qreal targetDevicePixelRatio) override;

void updateBase64Image();
Expand Down
24 changes: 22 additions & 2 deletions tests/ut_dquickiconimage.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>

Check warning on line 5 in tests/ut_dquickiconimage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "test_helper.hpp"
#include "dquickiconimage_p.h"
#include "dquickiconimage_p_p.h"

#include <QQuickItem>
#include <QSignalSpy>
Expand Down Expand Up @@ -59,3 +59,23 @@
auto windowImange = helper.object->window()->grabWindow();
EXPECT_EQ(windowImange.pixelColor(QPoint(50, 50)), Qt::red);
}

TEST(ut_DQuickIconImage, fileUrlIgnoresWindowDprRefresh)
{
DQuickIconImage icon;
icon.setName(QStringLiteral("qrc:/dpr-test/from-name.png"));

const qreal decodedDpr = 2.0;
icon.d_func()->devicePixelRatio = decodedDpr;

EXPECT_FALSE(icon.d_func()->refreshDtkDevicePixelRatio());
EXPECT_DOUBLE_EQ(icon.d_func()->devicePixelRatio, decodedDpr);

// A raster file lets Qt Quick continue to its @Nx lookup.
EXPECT_FALSE(icon.d_func()->updateDevicePixelRatio(1.25));
EXPECT_DOUBLE_EQ(icon.d_func()->devicePixelRatio, decodedDpr);
Comment thread
18202781743 marked this conversation as resolved.
Resolved

icon.setName(QStringLiteral("qrc:/icon/red.svg"));
Comment thread
18202781743 marked this conversation as resolved.
Resolved
EXPECT_TRUE(icon.d_func()->updateDevicePixelRatio(1.0));
EXPECT_DOUBLE_EQ(icon.d_func()->devicePixelRatio, 1.0);
}
Loading