Cache icons in style model for efficiency

This commit is contained in:
Nyall Dawson 2018-09-13 16:39:00 +10:00
parent 940624705b
commit 26885e52b5
5 changed files with 106 additions and 2 deletions

View File

@ -472,6 +472,18 @@ has been updated as a result.
.. seealso:: :py:func:`symbolRemoved`
.. seealso:: :py:func:`rampAdded`
.. seealso:: :py:func:`symbolChanged`
%End
void symbolChanged( const QString &name );
%Docstring
Emitted whenever a symbol's definition is changed. This does not include
name or tag changes.
.. seealso:: :py:func:`symbolSaved`
.. versionadded:: 3.4
%End
void groupsModified();
@ -544,6 +556,16 @@ has been updated as a result.
.. seealso:: :py:func:`symbolRemoved`
.. versionadded:: 3.4
%End
void rampChanged( const QString &name );
%Docstring
Emitted whenever a color ramp's definition is changed. This does not include
name or tag changes.
.. seealso:: :py:func:`rampAdded`
.. versionadded:: 3.4
%End

View File

@ -1760,6 +1760,23 @@ bool QgsStyle::updateSymbol( StyleEntity type, const QString &name )
QgsDebugMsg( QStringLiteral( "Couldn't insert symbol into the database!" ) );
return false;
}
else
{
switch ( type )
{
case SymbolEntity:
emit symbolChanged( name );
break;
case ColorrampEntity:
emit rampChanged( name );
break;
case TagEntity:
case SmartgroupEntity:
break;
}
}
return true;
}

View File

@ -449,9 +449,20 @@ class CORE_EXPORT QgsStyle : public QObject
* has been updated as a result.
* \see symbolRemoved()
* \see rampAdded()
* \see symbolChanged()
*/
void symbolSaved( const QString &name, QgsSymbol *symbol );
/**
* Emitted whenever a symbol's definition is changed. This does not include
* name or tag changes.
*
* \see symbolSaved()
*
* \since QGIS 3.4
*/
void symbolChanged( const QString &name );
//! Is emitted every time a tag or smartgroup has been added, removed, or renamed
void groupsModified();
@ -510,6 +521,16 @@ class CORE_EXPORT QgsStyle : public QObject
*/
void rampRemoved( const QString &name );
/**
* Emitted whenever a color ramp's definition is changed. This does not include
* name or tag changes.
*
* \see rampAdded()
*
* \since QGIS 3.4
*/
void rampChanged( const QString &name );
private:
QgsSymbolMap mSymbols;

View File

@ -31,7 +31,9 @@ QgsStyleModel::QgsStyleModel( QgsStyle *style, QObject *parent )
connect( mStyle, &QgsStyle::symbolSaved, this, &QgsStyleModel::onSymbolAdded );
connect( mStyle, &QgsStyle::symbolRemoved, this, &QgsStyleModel::onSymbolRemoved );
connect( mStyle, &QgsStyle::symbolRenamed, this, &QgsStyleModel::onSymbolRename );
connect( mStyle, &QgsStyle::symbolChanged, this, &QgsStyleModel::onSymbolChanged );
connect( mStyle, &QgsStyle::rampAdded, this, &QgsStyleModel::onRampAdded );
connect( mStyle, &QgsStyle::rampChanged, this, &QgsStyleModel::onRampChanged );
connect( mStyle, &QgsStyle::rampRemoved, this, &QgsStyleModel::onRampRemoved );
connect( mStyle, &QgsStyle::rampRenamed, this, &QgsStyleModel::onRampRename );
@ -78,8 +80,12 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
case Name:
if ( !isColorRamp )
{
// use cached icon if possible
QIcon icon = mSymbolIconCache.value( name );
if ( !icon.isNull() )
return icon;
std::unique_ptr< QgsSymbol > symbol( mStyle->symbol( name ) );
QIcon icon;
icon.addPixmap( QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), QSize( 24, 24 ), 1 ) );
for ( const QVariant &size : mAdditionalSizes )
@ -88,18 +94,25 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
icon.addPixmap( QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), s, static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) );
}
mSymbolIconCache.insert( name, icon );
return icon;
}
else
{
// use cached icon if possible
QIcon icon = mColorRampIconCache.value( name );
if ( !icon.isNull() )
return icon;
std::unique_ptr< QgsColorRamp > ramp( mStyle->colorRamp( name ) );
QIcon icon;
icon.addPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( ramp.get(), QSize( 24, 24 ), 1 ) );
for ( const QVariant &size : mAdditionalSizes )
{
QSize s = size.toSize();
icon.addPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( ramp.get(), s, static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) );
}
mColorRampIconCache.insert( name, icon );
return icon;
}
case Tags:
@ -235,10 +248,13 @@ int QgsStyleModel::columnCount( const QModelIndex & ) const
void QgsStyleModel::addDesiredIconSize( QSize size )
{
mAdditionalSizes << size;
mSymbolIconCache.clear();
mColorRampIconCache.clear();
}
void QgsStyleModel::onSymbolAdded( const QString &name, QgsSymbol * )
{
mSymbolIconCache.remove( name );
const QStringList oldSymbolNames = mSymbolNames;
const QStringList newSymbolNames = mStyle->symbolNames();
@ -254,6 +270,7 @@ void QgsStyleModel::onSymbolAdded( const QString &name, QgsSymbol * )
void QgsStyleModel::onSymbolRemoved( const QString &name )
{
mSymbolIconCache.remove( name );
const QStringList oldSymbolNames = mSymbolNames;
const QStringList newSymbolNames = mStyle->symbolNames();
@ -267,8 +284,17 @@ void QgsStyleModel::onSymbolRemoved( const QString &name )
endRemoveRows();
}
void QgsStyleModel::onSymbolChanged( const QString &name )
{
mSymbolIconCache.remove( name );
QModelIndex i = index( mSymbolNames.indexOf( name ), Tags );
emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
}
void QgsStyleModel::onSymbolRename( const QString &oldName, const QString &newName )
{
mSymbolIconCache.remove( oldName );
const QStringList oldSymbolNames = mSymbolNames;
const QStringList newSymbolNames = mStyle->symbolNames();
@ -295,6 +321,7 @@ void QgsStyleModel::onSymbolRename( const QString &oldName, const QString &newNa
void QgsStyleModel::onRampAdded( const QString &name )
{
mColorRampIconCache.remove( name );
const QStringList oldRampNames = mRampNames;
const QStringList newRampNames = mStyle->colorRampNames();
@ -310,6 +337,7 @@ void QgsStyleModel::onRampAdded( const QString &name )
void QgsStyleModel::onRampRemoved( const QString &name )
{
mColorRampIconCache.remove( name );
const QStringList oldRampNames = mRampNames;
const QStringList newRampNames = mStyle->colorRampNames();
@ -323,8 +351,17 @@ void QgsStyleModel::onRampRemoved( const QString &name )
endRemoveRows();
}
void QgsStyleModel::onRampChanged( const QString &name )
{
mColorRampIconCache.remove( name );
QModelIndex i = index( mSymbolNames.count() + mRampNames.indexOf( name ), Tags );
emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
}
void QgsStyleModel::onRampRename( const QString &oldName, const QString &newName )
{
mColorRampIconCache.remove( oldName );
const QStringList oldRampNames = mRampNames;
const QStringList newRampNames = mStyle->colorRampNames();

View File

@ -22,6 +22,8 @@
#include "qgssymbol.h"
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <QIcon>
#include <QHash>
class QgsSymbol;
@ -87,9 +89,11 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
void onSymbolAdded( const QString &name, QgsSymbol *symbol );
void onSymbolRemoved( const QString &name );
void onSymbolChanged( const QString &name );
void onSymbolRename( const QString &oldName, const QString &newName );
void onRampAdded( const QString &name );
void onRampRemoved( const QString &name );
void onRampChanged( const QString &name );
void onRampRename( const QString &oldName, const QString &newName );
void onTagsChanged( int entity, const QString &name, const QStringList &tags );
@ -100,6 +104,9 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
QStringList mRampNames;
QList< QSize > mAdditionalSizes;
mutable QHash< QString, QIcon > mSymbolIconCache;
mutable QHash< QString, QIcon > mColorRampIconCache;
};
/**