From 6ac03f52f943129404d2be5d00d8aff68e72ca8c Mon Sep 17 00:00:00 2001 From: Tomi Korpipaa Date: Mon, 18 Nov 2024 09:41:49 +0200 Subject: [PATCH] Give a warning if a file is missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit setHeighMapFile and setMeshFile did not check whether the filename given pointed to an existing file. Pick-to: 6.8 Fixes: QTBUG-129824 Change-Id: I2326d839e27a612733b4edcda08c97c87bbd7ec4 Reviewed-by: Sakaria Pouke Reviewed-by: Tomi Korpipää Reviewed-by: Sami Varanka --- src/graphs3d/data/qcustom3ditem.cpp | 6 ++++++ .../data/qheightmapsurfacedataproxy.cpp | 16 +++++++++++++--- tests/auto/cpptest/qgcustom/CMakeLists.txt | 7 +++++++ tests/auto/cpptest/qgcustom/customitem.mesh | Bin 0 -> 1220 bytes tests/auto/cpptest/qgcustom/tst_custom.cpp | 12 ++++++++++++ .../cpptest/qgsurface-heightproxy/tst_proxy.cpp | 6 ++++++ 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 tests/auto/cpptest/qgcustom/customitem.mesh diff --git a/src/graphs3d/data/qcustom3ditem.cpp b/src/graphs3d/data/qcustom3ditem.cpp index 71feeda3..be2e6a10 100644 --- a/src/graphs3d/data/qcustom3ditem.cpp +++ b/src/graphs3d/data/qcustom3ditem.cpp @@ -1,6 +1,7 @@ // Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only +#include #include "qcustom3ditem_p.h" QT_BEGIN_NAMESPACE @@ -236,6 +237,11 @@ QCustom3DItem::~QCustom3DItem() {} void QCustom3DItem::setMeshFile(const QString &meshFile) { Q_D(QCustom3DItem); + QFileInfo validfile(meshFile); + if (!validfile.exists() || !validfile.isFile()) { + qWarning("Mesh file %ls does not exist.", qUtf16Printable(meshFile)); + return; + } if (d->m_meshFile != meshFile) { d->m_meshFile = meshFile; d->m_dirtyBits.meshDirty = true; diff --git a/src/graphs3d/data/qheightmapsurfacedataproxy.cpp b/src/graphs3d/data/qheightmapsurfacedataproxy.cpp index e4444de8..89bc0987 100644 --- a/src/graphs3d/data/qheightmapsurfacedataproxy.cpp +++ b/src/graphs3d/data/qheightmapsurfacedataproxy.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include "qheightmapsurfacedataproxy_p.h" #include "qsurface3dseries_p.h" @@ -296,9 +297,18 @@ QImage QHeightMapSurfaceDataProxy::heightMap() const void QHeightMapSurfaceDataProxy::setHeightMapFile(const QString &filename) { Q_D(QHeightMapSurfaceDataProxy); - d->m_heightMapFile = filename; - setHeightMap(QImage(filename)); - emit heightMapFileChanged(filename); + QFileInfo validfile(filename); + // Check if the filename is empty, in which case we should clear the height map, + // or if not, it's an actual file that can be found + if (!filename.isEmpty() && (!validfile.exists() || !validfile.isFile())) { + qWarning("Height map file %ls does not exist.", qUtf16Printable(filename)); + return; + } + if (d->m_heightMapFile != filename) { + d->m_heightMapFile = filename; + setHeightMap(QImage(filename)); + emit heightMapFileChanged(filename); + } } QString QHeightMapSurfaceDataProxy::heightMapFile() const diff --git a/tests/auto/cpptest/qgcustom/CMakeLists.txt b/tests/auto/cpptest/qgcustom/CMakeLists.txt index a784e22c..bb243675 100644 --- a/tests/auto/cpptest/qgcustom/CMakeLists.txt +++ b/tests/auto/cpptest/qgcustom/CMakeLists.txt @@ -8,3 +8,10 @@ qt_internal_add_test(tst_qgcustom Qt::Gui Qt::Graphs ) + +qt_internal_add_resource(tst_qgcustom "qgcustom" + PREFIX + "/" + FILES + "customitem.mesh" +) diff --git a/tests/auto/cpptest/qgcustom/customitem.mesh b/tests/auto/cpptest/qgcustom/customitem.mesh new file mode 100644 index 0000000000000000000000000000000000000000..ab1c85383ee90b8ea4df371b25fb4f2c45e90bb5 GIT binary patch literal 1220 zcmZ9MxlRK?42I2p-)9*l5E2zN1!-t0cmhNL2#HG}AR3fyQbS1x1yyL^0Z>}r0Pzsi zwEW+WXLeajj_vvV+v8a|yRGNXAv1gI*N>!6TEjsU3}-1fM|!0YvRR2FWO7?1k_ zF}cegkH{W0n~nX;iz|-6^Z5LtaV9_E*EgHsW?5HSca|Uh8a^aPktqxB0sL z)JZs4y^MbyZzx7m{lw+0{odYkKD>Y2Sce?vi(B-@hwJw%@u_1OUBSa0(rlg=@ZbCP z7Beax?Yfm8aB47Pah})aSd!JjoL}Y!SUCNi5AHb*3+HZ?`ROeQeZX#i(*1eBMasv4(8H54Cj?W^;bLV|f1xUpY~-ZuoY@ zsXjHlhZ%U~eqb(HKD&Qk=Y@Ci8s%>X?m5N%$t&()#{RKK*je}4yM8IVik)+RP?jAX zmd;Cw@!2i*ZA`l0F+RIGA)S;idW=6MotDl>mpsNtR)f)R{C07M{JrwGfLoOvJG3J^ huxmTDrtQjW>ZD;O@=mMC9>@Ql@4MOx{)>ji{sEr4&Ncu5 literal 0 HcmV?d00001 diff --git a/tests/auto/cpptest/qgcustom/tst_custom.cpp b/tests/auto/cpptest/qgcustom/tst_custom.cpp index 1ae4f1b9..55e3f712 100644 --- a/tests/auto/cpptest/qgcustom/tst_custom.cpp +++ b/tests/auto/cpptest/qgcustom/tst_custom.cpp @@ -19,6 +19,7 @@ private slots: void initialProperties(); void initializeProperties(); + void invalidProperties(); private: QCustom3DItem *m_custom; @@ -129,5 +130,16 @@ void tst_custom::initializeProperties() QCOMPARE(updateSpy.size(), 10); } +void tst_custom::invalidProperties() +{ + QVERIFY(m_custom); + + // Verify we're getting this warning + QTest::ignoreMessage(QtWarningMsg, "Mesh file :/nonexistentitem.mesh does not exist."); + m_custom->setMeshFile(":/nonexistentitem.mesh"); + QEXPECT_FAIL("", "Nonexistent file given", Continue); + QCOMPARE(m_custom->meshFile(), QString(":/nonexistentitem.mesh")); +} + QTEST_MAIN(tst_custom) #include "tst_custom.moc" diff --git a/tests/auto/cpptest/qgsurface-heightproxy/tst_proxy.cpp b/tests/auto/cpptest/qgsurface-heightproxy/tst_proxy.cpp index cb479c48..44db636d 100644 --- a/tests/auto/cpptest/qgsurface-heightproxy/tst_proxy.cpp +++ b/tests/auto/cpptest/qgsurface-heightproxy/tst_proxy.cpp @@ -160,6 +160,12 @@ void tst_proxy::initializeProperties() void tst_proxy::invalidProperties() { + // Verify we're getting this warning + QTest::ignoreMessage(QtWarningMsg, "Height map file :/nonexistenttexture.jpg does not exist."); + m_proxy->setHeightMapFile(":/nonexistenttexture.jpg"); + QEXPECT_FAIL("", "Nonexistent file given", Continue); + QCOMPARE(m_proxy->heightMapFile(), QString(":/nonexistenttexture.jpg")); + m_proxy->setMaxXValue(-10.0f); m_proxy->setMaxZValue(-10.0f); QCOMPARE(m_proxy->maxXValue(), -10.0f);