@@ -31,7 +31,9 @@ QgsStyleModel::QgsStyleModel( QgsStyle *style, QObject *parent )
31
31
connect ( mStyle , &QgsStyle::symbolSaved, this , &QgsStyleModel::onSymbolAdded );
32
32
connect ( mStyle , &QgsStyle::symbolRemoved, this , &QgsStyleModel::onSymbolRemoved );
33
33
connect ( mStyle , &QgsStyle::symbolRenamed, this , &QgsStyleModel::onSymbolRename );
34
+ connect ( mStyle , &QgsStyle::symbolChanged, this , &QgsStyleModel::onSymbolChanged );
34
35
connect ( mStyle , &QgsStyle::rampAdded, this , &QgsStyleModel::onRampAdded );
36
+ connect ( mStyle , &QgsStyle::rampChanged, this , &QgsStyleModel::onRampChanged );
35
37
connect ( mStyle , &QgsStyle::rampRemoved, this , &QgsStyleModel::onRampRemoved );
36
38
connect ( mStyle , &QgsStyle::rampRenamed, this , &QgsStyleModel::onRampRename );
37
39
@@ -78,8 +80,12 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
78
80
case Name:
79
81
if ( !isColorRamp )
80
82
{
83
+ // use cached icon if possible
84
+ QIcon icon = mSymbolIconCache .value ( name );
85
+ if ( !icon.isNull () )
86
+ return icon;
87
+
81
88
std::unique_ptr< QgsSymbol > symbol ( mStyle ->symbol ( name ) );
82
- QIcon icon;
83
89
icon.addPixmap ( QgsSymbolLayerUtils::symbolPreviewPixmap ( symbol.get (), QSize ( 24 , 24 ), 1 ) );
84
90
85
91
for ( const QVariant &size : mAdditionalSizes )
@@ -88,18 +94,25 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
88
94
icon.addPixmap ( QgsSymbolLayerUtils::symbolPreviewPixmap ( symbol.get (), s, static_cast < int >( s.width () * ICON_PADDING_FACTOR ) ) );
89
95
}
90
96
97
+ mSymbolIconCache .insert ( name, icon );
91
98
return icon;
92
99
}
93
100
else
94
101
{
102
+ // use cached icon if possible
103
+ QIcon icon = mColorRampIconCache .value ( name );
104
+ if ( !icon.isNull () )
105
+ return icon;
106
+
95
107
std::unique_ptr< QgsColorRamp > ramp ( mStyle ->colorRamp ( name ) );
96
- QIcon icon;
97
108
icon.addPixmap ( QgsSymbolLayerUtils::colorRampPreviewPixmap ( ramp.get (), QSize ( 24 , 24 ), 1 ) );
98
109
for ( const QVariant &size : mAdditionalSizes )
99
110
{
100
111
QSize s = size.toSize ();
101
112
icon.addPixmap ( QgsSymbolLayerUtils::colorRampPreviewPixmap ( ramp.get (), s, static_cast < int >( s.width () * ICON_PADDING_FACTOR ) ) );
102
113
}
114
+
115
+ mColorRampIconCache .insert ( name, icon );
103
116
return icon;
104
117
}
105
118
case Tags:
@@ -235,10 +248,13 @@ int QgsStyleModel::columnCount( const QModelIndex & ) const
235
248
void QgsStyleModel::addDesiredIconSize ( QSize size )
236
249
{
237
250
mAdditionalSizes << size;
251
+ mSymbolIconCache .clear ();
252
+ mColorRampIconCache .clear ();
238
253
}
239
254
240
255
void QgsStyleModel::onSymbolAdded ( const QString &name, QgsSymbol * )
241
256
{
257
+ mSymbolIconCache .remove ( name );
242
258
const QStringList oldSymbolNames = mSymbolNames ;
243
259
const QStringList newSymbolNames = mStyle ->symbolNames ();
244
260
@@ -254,6 +270,7 @@ void QgsStyleModel::onSymbolAdded( const QString &name, QgsSymbol * )
254
270
255
271
void QgsStyleModel::onSymbolRemoved ( const QString &name )
256
272
{
273
+ mSymbolIconCache .remove ( name );
257
274
const QStringList oldSymbolNames = mSymbolNames ;
258
275
const QStringList newSymbolNames = mStyle ->symbolNames ();
259
276
@@ -267,8 +284,17 @@ void QgsStyleModel::onSymbolRemoved( const QString &name )
267
284
endRemoveRows ();
268
285
}
269
286
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
+
270
295
void QgsStyleModel::onSymbolRename ( const QString &oldName, const QString &newName )
271
296
{
297
+ mSymbolIconCache .remove ( oldName );
272
298
const QStringList oldSymbolNames = mSymbolNames ;
273
299
const QStringList newSymbolNames = mStyle ->symbolNames ();
274
300
@@ -295,6 +321,7 @@ void QgsStyleModel::onSymbolRename( const QString &oldName, const QString &newNa
295
321
296
322
void QgsStyleModel::onRampAdded ( const QString &name )
297
323
{
324
+ mColorRampIconCache .remove ( name );
298
325
const QStringList oldRampNames = mRampNames ;
299
326
const QStringList newRampNames = mStyle ->colorRampNames ();
300
327
@@ -310,6 +337,7 @@ void QgsStyleModel::onRampAdded( const QString &name )
310
337
311
338
void QgsStyleModel::onRampRemoved ( const QString &name )
312
339
{
340
+ mColorRampIconCache .remove ( name );
313
341
const QStringList oldRampNames = mRampNames ;
314
342
const QStringList newRampNames = mStyle ->colorRampNames ();
315
343
@@ -323,8 +351,17 @@ void QgsStyleModel::onRampRemoved( const QString &name )
323
351
endRemoveRows ();
324
352
}
325
353
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
+
326
362
void QgsStyleModel::onRampRename ( const QString &oldName, const QString &newName )
327
363
{
364
+ mColorRampIconCache .remove ( oldName );
328
365
const QStringList oldRampNames = mRampNames ;
329
366
const QStringList newRampNames = mStyle ->colorRampNames ();
330
367
0 commit comments