diff --git a/src/gui/symbology/qgssvgselectorwidget.cpp b/src/gui/symbology/qgssvgselectorwidget.cpp index ff45c2c89bf7..c375ceaa85e7 100644 --- a/src/gui/symbology/qgssvgselectorwidget.cpp +++ b/src/gui/symbology/qgssvgselectorwidget.cpp @@ -288,8 +288,8 @@ QVariant QgsSvgSelectorListModel::data( const QModelIndex &index, int role ) con if ( role == Qt::DecorationRole ) // icon { - QPixmap *pixmap = nullptr; - if ( !QPixmapCache::find( entry, pixmap ) || !pixmap ) + QPixmap pixmap; + if ( !QPixmapCache::find( entry, &pixmap ) ) { QPixmap newPixmap = createPreview( entry ); QPixmapCache::insert( entry, newPixmap ); @@ -297,7 +297,7 @@ QVariant QgsSvgSelectorListModel::data( const QModelIndex &index, int role ) con } else { - return *pixmap; + return pixmap; } } else if ( role == Qt::UserRole || role == Qt::ToolTipRole ) @@ -771,5 +771,3 @@ void QgsSvgParameterValueDelegate::updateEditorGeometry( QWidget *editor, const } ///@endcond - - diff --git a/src/gui/symbology/qgssvgselectorwidget.h b/src/gui/symbology/qgssvgselectorwidget.h index c922df927b10..e82401654e08 100644 --- a/src/gui/symbology/qgssvgselectorwidget.h +++ b/src/gui/symbology/qgssvgselectorwidget.h @@ -343,6 +343,7 @@ class GUI_EXPORT QgsSvgSelectorListModel : public QAbstractListModel */ void addSvgs( const QStringList &svgs ); + friend class TestQgsSvgSelectorWidget; }; diff --git a/tests/src/gui/CMakeLists.txt b/tests/src/gui/CMakeLists.txt index a1d151f974e0..61ac05b31bed 100644 --- a/tests/src/gui/CMakeLists.txt +++ b/tests/src/gui/CMakeLists.txt @@ -77,6 +77,7 @@ set(TESTS testqgsqueryresultwidget.cpp testqgscompoundcolorwidget.cpp testqgsmaskingwidget.cpp + testqgssvgselectorwidget.cpp ) foreach(TESTSRC ${TESTS}) diff --git a/tests/src/gui/testqgssvgselectorwidget.cpp b/tests/src/gui/testqgssvgselectorwidget.cpp new file mode 100644 index 000000000000..280296c04d4c --- /dev/null +++ b/tests/src/gui/testqgssvgselectorwidget.cpp @@ -0,0 +1,78 @@ +/*************************************************************************** + testqgssvgselectorwidget.cpp + --------------------- + begin : 2024/09/25 + copyright : (C) 2024 by Julien Cabieces + email : julien dot cabieces at oslandia dot com + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include +#include + +#include "qgstest.h" +#include "qgssvgselectorwidget.h" + +class TestQgsSvgSelectorWidget : public QgsTest +{ + Q_OBJECT + + public: + + TestQgsSvgSelectorWidget() : QgsTest( QStringLiteral( "SVG Selector Widget Tests" ) ) {} + + private slots: + void initTestCase();// will be called before the first testfunction is executed. + void cleanupTestCase();// will be called after the last testfunction was executed. + void init();// will be called before each testfunction is executed. + void cleanup();// will be called after every testfunction. + void testPixmapCache(); +}; + +void TestQgsSvgSelectorWidget::initTestCase() +{ + QgsApplication::init(); + QgsApplication::initQgis(); +} + +void TestQgsSvgSelectorWidget::cleanupTestCase() +{ + QgsApplication::exitQgis(); +} + +void TestQgsSvgSelectorWidget::init() +{ +} + +void TestQgsSvgSelectorWidget::cleanup() +{ +} + +void TestQgsSvgSelectorWidget::testPixmapCache() +{ + // We want to check that QPixmapCache is correctly used + // So we force a cache image different from the one we set to the model to check that this is + // the cache image returned, not the model svg one + + const QPixmap cachePixmap( QStringLiteral( "%1/rgb256x256.png" ).arg( TEST_DATA_DIR ) ); + const QString sampleSvg = QStringLiteral( "%1/sample_svg.svg" ).arg( TEST_DATA_DIR ); + QPixmapCache::insert( sampleSvg, cachePixmap ); + + QgsSvgSelectorListModel model( nullptr ); + model.addSvgs( QStringList() << sampleSvg ); + + const QPixmap pixmap = model.data( model.index( 0, 0 ), Qt::DecorationRole ).value(); + QVERIFY( !pixmap.isNull() ); + + QCOMPARE( pixmap, cachePixmap ); +} + + +QGSTEST_MAIN( TestQgsSvgSelectorWidget ) +#include "testqgssvgselectorwidget.moc"