Skip to content

Commit

Permalink
Improve handling of favorites in QgsStyleModel and proxy model
Browse files Browse the repository at this point in the history
Fixes favorite entities sometimes incorrectly filtered out
  • Loading branch information
nyalldawson committed Jun 21, 2019
1 parent 7757a5a commit 554d23b
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 27 deletions.
21 changes: 18 additions & 3 deletions python/core/auto_generated/symbology/qgsstyle.sip.in
Expand Up @@ -182,7 +182,7 @@ takes responsibility for deleting the returned object.
Returns count of color ramps
%End

QStringList colorRampNames();
QStringList colorRampNames() const;
%Docstring
Returns a list of names of color ramps
%End
Expand Down Expand Up @@ -298,7 +298,7 @@ Returns a const pointer to a symbol (doesn't create new instance)
Returns count of symbols in style
%End

QStringList symbolNames();
QStringList symbolNames() const;
%Docstring
Returns a list of names of symbols
%End
Expand All @@ -315,6 +315,13 @@ Returns the DB id for the given tag name
int smartgroupId( const QString &smartgroup );
%Docstring
Returns the DB id for the given smartgroup name
%End

QStringList allNames( StyleEntity type ) const;
%Docstring
Returns a list of the names of all existing entities of the specified ``type``.

.. versionadded:: 3.10
%End

QStringList symbolsOfFavorite( StyleEntity type ) const;
Expand Down Expand Up @@ -518,6 +525,14 @@ Returns the tags associated with the symbol
:param symbol: is the name of the symbol or color ramp

:return: A QStringList of the tags that have been applied to that symbol/colorramp
%End

bool isFavorite( StyleEntity type, const QString &name );
%Docstring
Returns ``True`` if the symbol with matching ``type`` and \name is
marked as a favorite.

.. versionadded:: 3.10
%End

bool symbolHasTag( StyleEntity type, const QString &symbol, const QString &tag );
Expand All @@ -541,7 +556,7 @@ Returns the tag name for the given id
Returns the smart groups map with id as key and name as value
%End

QStringList smartgroupNames();
QStringList smartgroupNames() const;
%Docstring
Returns the smart groups list
%End
Expand Down
1 change: 1 addition & 0 deletions python/core/auto_generated/symbology/qgsstylemodel.sip.in
Expand Up @@ -37,6 +37,7 @@ within a QgsStyle database.
TypeRole,
TagRole,
SymbolTypeRole,
IsFavoriteRole,
};

explicit QgsStyleModel( QgsStyle *style, QObject *parent /TransferThis/ = 0 );
Expand Down
149 changes: 146 additions & 3 deletions src/core/symbology/qgsstyle.cpp
Expand Up @@ -85,6 +85,10 @@ void QgsStyle::clear()
mCachedColorRampTags.clear();
mCachedSymbolTags.clear();
mCachedTextFormatTags.clear();

mCachedSymbolFavorites.clear();
mCachedColorRampFavorites.clear();
mCachedTextFormatFavorites.clear();
}

bool QgsStyle::addSymbol( const QString &name, QgsSymbol *symbol, bool update )
Expand Down Expand Up @@ -135,6 +139,8 @@ bool QgsStyle::saveSymbol( const QString &name, QgsSymbol *symbol, bool favorite
return false;
}

mCachedSymbolFavorites[ name ] = favorite;

tagSymbol( SymbolEntity, name, tags );

emit symbolSaved( name, symbol );
Expand Down Expand Up @@ -169,6 +175,7 @@ bool QgsStyle::removeSymbol( const QString &name )
if ( result )
{
mCachedSymbolTags.remove( name );
mCachedSymbolFavorites.remove( name );
emit symbolRemoved( name );
}
return result;
Expand All @@ -190,7 +197,7 @@ int QgsStyle::symbolCount()
return mSymbols.count();
}

QStringList QgsStyle::symbolNames()
QStringList QgsStyle::symbolNames() const
{
return mSymbols.keys();
}
Expand Down Expand Up @@ -265,6 +272,8 @@ bool QgsStyle::saveColorRamp( const QString &name, QgsColorRamp *ramp, bool favo
return false;
}

mCachedColorRampFavorites[ name ] = favorite;

tagSymbol( ColorrampEntity, name, tags );

emit rampAdded( name );
Expand All @@ -286,6 +295,7 @@ bool QgsStyle::removeColorRamp( const QString &name )
}

mCachedColorRampTags.remove( name );
mCachedColorRampFavorites.remove( name );

emit rampRemoved( name );

Expand All @@ -308,7 +318,7 @@ int QgsStyle::colorRampCount()
return mColorRamps.count();
}

QStringList QgsStyle::colorRampNames()
QStringList QgsStyle::colorRampNames() const
{
return mColorRamps.keys();
}
Expand Down Expand Up @@ -565,6 +575,7 @@ bool QgsStyle::renameSymbol( const QString &oldName, const QString &newName )
}

mCachedSymbolTags.remove( oldName );
mCachedSymbolFavorites.remove( oldName );

const bool result = rename( SymbolEntity, symbolid, newName );
if ( result )
Expand All @@ -587,6 +598,7 @@ bool QgsStyle::renameColorRamp( const QString &oldName, const QString &newName )

mColorRamps.insert( newName, ramp );
mCachedColorRampTags.remove( oldName );
mCachedColorRampFavorites.remove( oldName );

int rampid = 0;
sqlite3_statement_unique_ptr statement;
Expand Down Expand Up @@ -628,6 +640,8 @@ bool QgsStyle::saveTextFormat( const QString &name, const QgsTextFormat &format,
return false;
}

mCachedTextFormatFavorites[ name ] = favorite;

tagSymbol( TextFormatEntity, name, tags );

emit textFormatAdded( name );
Expand All @@ -650,6 +664,7 @@ bool QgsStyle::removeTextFormat( const QString &name )
}

mCachedTextFormatTags.remove( name );
mCachedTextFormatFavorites.remove( name );

emit textFormatRemoved( name );

Expand All @@ -671,6 +686,7 @@ bool QgsStyle::renameTextFormat( const QString &oldName, const QString &newName

mTextFormats.insert( newName, format );
mCachedTextFormatTags.remove( oldName );
mCachedTextFormatFavorites.remove( oldName );

int textFormatId = 0;
sqlite3_statement_unique_ptr statement;
Expand Down Expand Up @@ -872,6 +888,9 @@ bool QgsStyle::rename( StyleEntity type, int id, const QString &newName )
mCachedColorRampTags.clear();
mCachedSymbolTags.clear();
mCachedTextFormatTags.clear();
mCachedSymbolFavorites.clear();
mCachedColorRampFavorites.clear();
mCachedTextFormatFavorites.clear();

switch ( type )
{
Expand Down Expand Up @@ -931,6 +950,9 @@ bool QgsStyle::remove( StyleEntity type, int id )
mCachedColorRampTags.clear();
mCachedSymbolTags.clear();
mCachedTextFormatTags.clear();
mCachedSymbolFavorites.clear();
mCachedColorRampFavorites.clear();
mCachedTextFormatFavorites.clear();

if ( groupRemoved )
{
Expand Down Expand Up @@ -985,7 +1007,24 @@ bool QgsStyle::addFavorite( StyleEntity type, const QString &name )

const bool res = runEmptyQuery( query );
if ( res )
{
switch ( type )
{
case SymbolEntity:
mCachedSymbolFavorites[ name ] = true;
break;
case ColorrampEntity:
mCachedColorRampFavorites[ name ] = true;
break;
case TextFormatEntity:
mCachedTextFormatFavorites[ name ] = true;
break;
case TagEntity:
case SmartgroupEntity:
break;
}
emit favoritedChanged( type, name, true );
}

return res;
}
Expand Down Expand Up @@ -1014,7 +1053,24 @@ bool QgsStyle::removeFavorite( StyleEntity type, const QString &name )

const bool res = runEmptyQuery( query );
if ( res )
{
switch ( type )
{
case SymbolEntity:
mCachedSymbolFavorites[ name ] = false;
break;
case ColorrampEntity:
mCachedColorRampFavorites[ name ] = false;
break;
case TextFormatEntity:
mCachedTextFormatFavorites[ name ] = false;
break;
case TagEntity:
case SmartgroupEntity:
break;
}
emit favoritedChanged( type, name, false );
}

return res;
}
Expand Down Expand Up @@ -1482,6 +1538,71 @@ QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol )
return tagList;
}

bool QgsStyle::isFavorite( QgsStyle::StyleEntity type, const QString &name )
{
if ( !mCurrentDB )
{
QgsDebugMsg( QStringLiteral( "Sorry! Cannot open database for getting the tags." ) );
return false;
}

switch ( type )
{
case SymbolEntity:
if ( mCachedSymbolFavorites.contains( name ) )
return mCachedSymbolFavorites.value( name );
break;

case ColorrampEntity:
if ( mCachedColorRampFavorites.contains( name ) )
return mCachedColorRampFavorites.value( name );
break;

case TextFormatEntity:
if ( mCachedTextFormatFavorites.contains( name ) )
return mCachedTextFormatFavorites.value( name );
break;

case TagEntity:
case SmartgroupEntity:
return false;
}

const QStringList names = allNames( type );
if ( !names.contains( name ) )
return false; // entity doesn't exist

// for efficiency, retrieve names of all favorited symbols and store them in cache
const QStringList favorites = symbolsOfFavorite( type );
bool res = false;
for ( const QString &n : names )
{
const bool isFav = favorites.contains( n );
if ( n == name )
res = isFav;

switch ( type )
{
case SymbolEntity:
mCachedSymbolFavorites[n] = isFav;
break;

case ColorrampEntity:
mCachedColorRampFavorites[ n ] = isFav;
break;

case TextFormatEntity:
mCachedTextFormatFavorites[ n ] = isFav;
break;

case TagEntity:
case SmartgroupEntity:
return false;
}
}
return res;
}

bool QgsStyle::symbolHasTag( StyleEntity type, const QString &symbol, const QString &tag )
{
if ( !mCurrentDB )
Expand Down Expand Up @@ -1652,6 +1773,28 @@ int QgsStyle::smartgroupId( const QString &name )
return getId( QStringLiteral( "smartgroup" ), name );
}

QStringList QgsStyle::allNames( QgsStyle::StyleEntity type ) const
{
switch ( type )
{
case SymbolEntity:
return symbolNames();

case ColorrampEntity:
return colorRampNames();

case TextFormatEntity:
return textFormatNames();

case TagEntity:
return tags();

case SmartgroupEntity:
return smartgroupNames();
}
return QStringList();
}

int QgsStyle::addSmartgroup( const QString &name, const QString &op, const QgsSmartConditionMap &conditions )
{
return addSmartgroup( name, op, conditions.values( QStringLiteral( "tag" ) ),
Expand Down Expand Up @@ -1729,7 +1872,7 @@ QgsSymbolGroupMap QgsStyle::smartgroupsListMap()
return groupNames;
}

QStringList QgsStyle::smartgroupNames()
QStringList QgsStyle::smartgroupNames() const
{
if ( !mCurrentDB )
{
Expand Down

0 comments on commit 554d23b

Please sign in to comment.