Skip to content

Commit

Permalink
Cache icons in style model for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 13, 2018
1 parent 9406247 commit 26885e5
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
22 changes: 22 additions & 0 deletions python/core/auto_generated/symbology/qgsstyle.sip.in
Expand Up @@ -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();
Expand Down Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/core/symbology/qgsstyle.cpp
Expand Up @@ -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;
}

Expand Down
21 changes: 21 additions & 0 deletions src/core/symbology/qgsstyle.h
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
41 changes: 39 additions & 2 deletions src/core/symbology/qgsstylemodel.cpp
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 )
Expand All @@ -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:
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions src/core/symbology/qgsstylemodel.h
Expand Up @@ -22,6 +22,8 @@
#include "qgssymbol.h"
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <QIcon>
#include <QHash>

class QgsSymbol;

Expand Down Expand Up @@ -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 );

Expand All @@ -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;

};

/**
Expand Down

0 comments on commit 26885e5

Please sign in to comment.