Skip to content

Commit

Permalink
fix(SvgSelectorWidget): Correctly use QPixmapCache
Browse files Browse the repository at this point in the history
With previous code, pixmap was never taken from cache
  • Loading branch information
troopa81 committed Sep 27, 2024
1 parent 4550bea commit b06984f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/gui/symbology/qgssvgselectorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,16 @@ 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 );
return newPixmap;
}
else
{
return *pixmap;
return pixmap;
}
}
else if ( role == Qt::UserRole || role == Qt::ToolTipRole )
Expand Down Expand Up @@ -771,5 +771,3 @@ void QgsSvgParameterValueDelegate::updateEditorGeometry( QWidget *editor, const
}

///@endcond


1 change: 1 addition & 0 deletions src/gui/symbology/qgssvgselectorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ class GUI_EXPORT QgsSvgSelectorListModel : public QAbstractListModel
*/
void addSvgs( const QStringList &svgs );

friend class TestQgsSvgSelectorWidget;
};


Expand Down
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ set(TESTS
testqgsqueryresultwidget.cpp
testqgscompoundcolorwidget.cpp
testqgsmaskingwidget.cpp
testqgssvgselectorwidget.cpp
)

foreach(TESTSRC ${TESTS})
Expand Down
78 changes: 78 additions & 0 deletions tests/src/gui/testqgssvgselectorwidget.cpp
Original file line number Diff line number Diff line change
@@ -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 <QPixmapCache>
#include <QTemporaryFile>

#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<QPixmap>();
QVERIFY( !pixmap.isNull() );

QCOMPARE( pixmap, cachePixmap );
}


QGSTEST_MAIN( TestQgsSvgSelectorWidget )
#include "testqgssvgselectorwidget.moc"

0 comments on commit b06984f

Please sign in to comment.