Skip to content

Commit 26885e5

Browse files
committed
Cache icons in style model for efficiency
1 parent 9406247 commit 26885e5

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed

python/core/auto_generated/symbology/qgsstyle.sip.in

+22
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,18 @@ has been updated as a result.
472472
.. seealso:: :py:func:`symbolRemoved`
473473

474474
.. seealso:: :py:func:`rampAdded`
475+
476+
.. seealso:: :py:func:`symbolChanged`
477+
%End
478+
479+
void symbolChanged( const QString &name );
480+
%Docstring
481+
Emitted whenever a symbol's definition is changed. This does not include
482+
name or tag changes.
483+
484+
.. seealso:: :py:func:`symbolSaved`
485+
486+
.. versionadded:: 3.4
475487
%End
476488

477489
void groupsModified();
@@ -544,6 +556,16 @@ has been updated as a result.
544556

545557
.. seealso:: :py:func:`symbolRemoved`
546558

559+
.. versionadded:: 3.4
560+
%End
561+
562+
void rampChanged( const QString &name );
563+
%Docstring
564+
Emitted whenever a color ramp's definition is changed. This does not include
565+
name or tag changes.
566+
567+
.. seealso:: :py:func:`rampAdded`
568+
547569
.. versionadded:: 3.4
548570
%End
549571

src/core/symbology/qgsstyle.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,23 @@ bool QgsStyle::updateSymbol( StyleEntity type, const QString &name )
17601760
QgsDebugMsg( QStringLiteral( "Couldn't insert symbol into the database!" ) );
17611761
return false;
17621762
}
1763+
else
1764+
{
1765+
switch ( type )
1766+
{
1767+
case SymbolEntity:
1768+
emit symbolChanged( name );
1769+
break;
1770+
1771+
case ColorrampEntity:
1772+
emit rampChanged( name );
1773+
break;
1774+
1775+
case TagEntity:
1776+
case SmartgroupEntity:
1777+
break;
1778+
}
1779+
}
17631780
return true;
17641781
}
17651782

src/core/symbology/qgsstyle.h

+21
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,20 @@ class CORE_EXPORT QgsStyle : public QObject
449449
* has been updated as a result.
450450
* \see symbolRemoved()
451451
* \see rampAdded()
452+
* \see symbolChanged()
452453
*/
453454
void symbolSaved( const QString &name, QgsSymbol *symbol );
454455

456+
/**
457+
* Emitted whenever a symbol's definition is changed. This does not include
458+
* name or tag changes.
459+
*
460+
* \see symbolSaved()
461+
*
462+
* \since QGIS 3.4
463+
*/
464+
void symbolChanged( const QString &name );
465+
455466
//! Is emitted every time a tag or smartgroup has been added, removed, or renamed
456467
void groupsModified();
457468

@@ -510,6 +521,16 @@ class CORE_EXPORT QgsStyle : public QObject
510521
*/
511522
void rampRemoved( const QString &name );
512523

524+
/**
525+
* Emitted whenever a color ramp's definition is changed. This does not include
526+
* name or tag changes.
527+
*
528+
* \see rampAdded()
529+
*
530+
* \since QGIS 3.4
531+
*/
532+
void rampChanged( const QString &name );
533+
513534
private:
514535

515536
QgsSymbolMap mSymbols;

src/core/symbology/qgsstylemodel.cpp

+39-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ QgsStyleModel::QgsStyleModel( QgsStyle *style, QObject *parent )
3131
connect( mStyle, &QgsStyle::symbolSaved, this, &QgsStyleModel::onSymbolAdded );
3232
connect( mStyle, &QgsStyle::symbolRemoved, this, &QgsStyleModel::onSymbolRemoved );
3333
connect( mStyle, &QgsStyle::symbolRenamed, this, &QgsStyleModel::onSymbolRename );
34+
connect( mStyle, &QgsStyle::symbolChanged, this, &QgsStyleModel::onSymbolChanged );
3435
connect( mStyle, &QgsStyle::rampAdded, this, &QgsStyleModel::onRampAdded );
36+
connect( mStyle, &QgsStyle::rampChanged, this, &QgsStyleModel::onRampChanged );
3537
connect( mStyle, &QgsStyle::rampRemoved, this, &QgsStyleModel::onRampRemoved );
3638
connect( mStyle, &QgsStyle::rampRenamed, this, &QgsStyleModel::onRampRename );
3739

@@ -78,8 +80,12 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
7880
case Name:
7981
if ( !isColorRamp )
8082
{
83+
// use cached icon if possible
84+
QIcon icon = mSymbolIconCache.value( name );
85+
if ( !icon.isNull() )
86+
return icon;
87+
8188
std::unique_ptr< QgsSymbol > symbol( mStyle->symbol( name ) );
82-
QIcon icon;
8389
icon.addPixmap( QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), QSize( 24, 24 ), 1 ) );
8490

8591
for ( const QVariant &size : mAdditionalSizes )
@@ -88,18 +94,25 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
8894
icon.addPixmap( QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), s, static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) );
8995
}
9096

97+
mSymbolIconCache.insert( name, icon );
9198
return icon;
9299
}
93100
else
94101
{
102+
// use cached icon if possible
103+
QIcon icon = mColorRampIconCache.value( name );
104+
if ( !icon.isNull() )
105+
return icon;
106+
95107
std::unique_ptr< QgsColorRamp > ramp( mStyle->colorRamp( name ) );
96-
QIcon icon;
97108
icon.addPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( ramp.get(), QSize( 24, 24 ), 1 ) );
98109
for ( const QVariant &size : mAdditionalSizes )
99110
{
100111
QSize s = size.toSize();
101112
icon.addPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( ramp.get(), s, static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) );
102113
}
114+
115+
mColorRampIconCache.insert( name, icon );
103116
return icon;
104117
}
105118
case Tags:
@@ -235,10 +248,13 @@ int QgsStyleModel::columnCount( const QModelIndex & ) const
235248
void QgsStyleModel::addDesiredIconSize( QSize size )
236249
{
237250
mAdditionalSizes << size;
251+
mSymbolIconCache.clear();
252+
mColorRampIconCache.clear();
238253
}
239254

240255
void QgsStyleModel::onSymbolAdded( const QString &name, QgsSymbol * )
241256
{
257+
mSymbolIconCache.remove( name );
242258
const QStringList oldSymbolNames = mSymbolNames;
243259
const QStringList newSymbolNames = mStyle->symbolNames();
244260

@@ -254,6 +270,7 @@ void QgsStyleModel::onSymbolAdded( const QString &name, QgsSymbol * )
254270

255271
void QgsStyleModel::onSymbolRemoved( const QString &name )
256272
{
273+
mSymbolIconCache.remove( name );
257274
const QStringList oldSymbolNames = mSymbolNames;
258275
const QStringList newSymbolNames = mStyle->symbolNames();
259276

@@ -267,8 +284,17 @@ void QgsStyleModel::onSymbolRemoved( const QString &name )
267284
endRemoveRows();
268285
}
269286

287+
void QgsStyleModel::onSymbolChanged( const QString &name )
288+
{
289+
mSymbolIconCache.remove( name );
290+
291+
QModelIndex i = index( mSymbolNames.indexOf( name ), Tags );
292+
emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
293+
}
294+
270295
void QgsStyleModel::onSymbolRename( const QString &oldName, const QString &newName )
271296
{
297+
mSymbolIconCache.remove( oldName );
272298
const QStringList oldSymbolNames = mSymbolNames;
273299
const QStringList newSymbolNames = mStyle->symbolNames();
274300

@@ -295,6 +321,7 @@ void QgsStyleModel::onSymbolRename( const QString &oldName, const QString &newNa
295321

296322
void QgsStyleModel::onRampAdded( const QString &name )
297323
{
324+
mColorRampIconCache.remove( name );
298325
const QStringList oldRampNames = mRampNames;
299326
const QStringList newRampNames = mStyle->colorRampNames();
300327

@@ -310,6 +337,7 @@ void QgsStyleModel::onRampAdded( const QString &name )
310337

311338
void QgsStyleModel::onRampRemoved( const QString &name )
312339
{
340+
mColorRampIconCache.remove( name );
313341
const QStringList oldRampNames = mRampNames;
314342
const QStringList newRampNames = mStyle->colorRampNames();
315343

@@ -323,8 +351,17 @@ void QgsStyleModel::onRampRemoved( const QString &name )
323351
endRemoveRows();
324352
}
325353

354+
void QgsStyleModel::onRampChanged( const QString &name )
355+
{
356+
mColorRampIconCache.remove( name );
357+
358+
QModelIndex i = index( mSymbolNames.count() + mRampNames.indexOf( name ), Tags );
359+
emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
360+
}
361+
326362
void QgsStyleModel::onRampRename( const QString &oldName, const QString &newName )
327363
{
364+
mColorRampIconCache.remove( oldName );
328365
const QStringList oldRampNames = mRampNames;
329366
const QStringList newRampNames = mStyle->colorRampNames();
330367

src/core/symbology/qgsstylemodel.h

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "qgssymbol.h"
2323
#include <QAbstractItemModel>
2424
#include <QSortFilterProxyModel>
25+
#include <QIcon>
26+
#include <QHash>
2527

2628
class QgsSymbol;
2729

@@ -87,9 +89,11 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
8789

8890
void onSymbolAdded( const QString &name, QgsSymbol *symbol );
8991
void onSymbolRemoved( const QString &name );
92+
void onSymbolChanged( const QString &name );
9093
void onSymbolRename( const QString &oldName, const QString &newName );
9194
void onRampAdded( const QString &name );
9295
void onRampRemoved( const QString &name );
96+
void onRampChanged( const QString &name );
9397
void onRampRename( const QString &oldName, const QString &newName );
9498
void onTagsChanged( int entity, const QString &name, const QStringList &tags );
9599

@@ -100,6 +104,9 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
100104
QStringList mRampNames;
101105
QList< QSize > mAdditionalSizes;
102106

107+
mutable QHash< QString, QIcon > mSymbolIconCache;
108+
mutable QHash< QString, QIcon > mColorRampIconCache;
109+
103110
};
104111

105112
/**

0 commit comments

Comments
 (0)