Skip to content

Commit 554d23b

Browse files
committedJun 21, 2019
Improve handling of favorites in QgsStyleModel and proxy model
Fixes favorite entities sometimes incorrectly filtered out
1 parent 7757a5a commit 554d23b

File tree

8 files changed

+279
-27
lines changed

8 files changed

+279
-27
lines changed
 

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ takes responsibility for deleting the returned object.
182182
Returns count of color ramps
183183
%End
184184

185-
QStringList colorRampNames();
185+
QStringList colorRampNames() const;
186186
%Docstring
187187
Returns a list of names of color ramps
188188
%End
@@ -298,7 +298,7 @@ Returns a const pointer to a symbol (doesn't create new instance)
298298
Returns count of symbols in style
299299
%End
300300

301-
QStringList symbolNames();
301+
QStringList symbolNames() const;
302302
%Docstring
303303
Returns a list of names of symbols
304304
%End
@@ -315,6 +315,13 @@ Returns the DB id for the given tag name
315315
int smartgroupId( const QString &smartgroup );
316316
%Docstring
317317
Returns the DB id for the given smartgroup name
318+
%End
319+
320+
QStringList allNames( StyleEntity type ) const;
321+
%Docstring
322+
Returns a list of the names of all existing entities of the specified ``type``.
323+
324+
.. versionadded:: 3.10
318325
%End
319326

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

520527
:return: A QStringList of the tags that have been applied to that symbol/colorramp
528+
%End
529+
530+
bool isFavorite( StyleEntity type, const QString &name );
531+
%Docstring
532+
Returns ``True`` if the symbol with matching ``type`` and \name is
533+
marked as a favorite.
534+
535+
.. versionadded:: 3.10
521536
%End
522537

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

544-
QStringList smartgroupNames();
559+
QStringList smartgroupNames() const;
545560
%Docstring
546561
Returns the smart groups list
547562
%End

‎python/core/auto_generated/symbology/qgsstylemodel.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ within a QgsStyle database.
3737
TypeRole,
3838
TagRole,
3939
SymbolTypeRole,
40+
IsFavoriteRole,
4041
};
4142

4243
explicit QgsStyleModel( QgsStyle *style, QObject *parent /TransferThis/ = 0 );

‎src/core/symbology/qgsstyle.cpp

Lines changed: 146 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ void QgsStyle::clear()
8585
mCachedColorRampTags.clear();
8686
mCachedSymbolTags.clear();
8787
mCachedTextFormatTags.clear();
88+
89+
mCachedSymbolFavorites.clear();
90+
mCachedColorRampFavorites.clear();
91+
mCachedTextFormatFavorites.clear();
8892
}
8993

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

142+
mCachedSymbolFavorites[ name ] = favorite;
143+
138144
tagSymbol( SymbolEntity, name, tags );
139145

140146
emit symbolSaved( name, symbol );
@@ -169,6 +175,7 @@ bool QgsStyle::removeSymbol( const QString &name )
169175
if ( result )
170176
{
171177
mCachedSymbolTags.remove( name );
178+
mCachedSymbolFavorites.remove( name );
172179
emit symbolRemoved( name );
173180
}
174181
return result;
@@ -190,7 +197,7 @@ int QgsStyle::symbolCount()
190197
return mSymbols.count();
191198
}
192199

193-
QStringList QgsStyle::symbolNames()
200+
QStringList QgsStyle::symbolNames() const
194201
{
195202
return mSymbols.keys();
196203
}
@@ -265,6 +272,8 @@ bool QgsStyle::saveColorRamp( const QString &name, QgsColorRamp *ramp, bool favo
265272
return false;
266273
}
267274

275+
mCachedColorRampFavorites[ name ] = favorite;
276+
268277
tagSymbol( ColorrampEntity, name, tags );
269278

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

288297
mCachedColorRampTags.remove( name );
298+
mCachedColorRampFavorites.remove( name );
289299

290300
emit rampRemoved( name );
291301

@@ -308,7 +318,7 @@ int QgsStyle::colorRampCount()
308318
return mColorRamps.count();
309319
}
310320

311-
QStringList QgsStyle::colorRampNames()
321+
QStringList QgsStyle::colorRampNames() const
312322
{
313323
return mColorRamps.keys();
314324
}
@@ -565,6 +575,7 @@ bool QgsStyle::renameSymbol( const QString &oldName, const QString &newName )
565575
}
566576

567577
mCachedSymbolTags.remove( oldName );
578+
mCachedSymbolFavorites.remove( oldName );
568579

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

588599
mColorRamps.insert( newName, ramp );
589600
mCachedColorRampTags.remove( oldName );
601+
mCachedColorRampFavorites.remove( oldName );
590602

591603
int rampid = 0;
592604
sqlite3_statement_unique_ptr statement;
@@ -628,6 +640,8 @@ bool QgsStyle::saveTextFormat( const QString &name, const QgsTextFormat &format,
628640
return false;
629641
}
630642

643+
mCachedTextFormatFavorites[ name ] = favorite;
644+
631645
tagSymbol( TextFormatEntity, name, tags );
632646

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

652666
mCachedTextFormatTags.remove( name );
667+
mCachedTextFormatFavorites.remove( name );
653668

654669
emit textFormatRemoved( name );
655670

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

672687
mTextFormats.insert( newName, format );
673688
mCachedTextFormatTags.remove( oldName );
689+
mCachedTextFormatFavorites.remove( oldName );
674690

675691
int textFormatId = 0;
676692
sqlite3_statement_unique_ptr statement;
@@ -872,6 +888,9 @@ bool QgsStyle::rename( StyleEntity type, int id, const QString &newName )
872888
mCachedColorRampTags.clear();
873889
mCachedSymbolTags.clear();
874890
mCachedTextFormatTags.clear();
891+
mCachedSymbolFavorites.clear();
892+
mCachedColorRampFavorites.clear();
893+
mCachedTextFormatFavorites.clear();
875894

876895
switch ( type )
877896
{
@@ -931,6 +950,9 @@ bool QgsStyle::remove( StyleEntity type, int id )
931950
mCachedColorRampTags.clear();
932951
mCachedSymbolTags.clear();
933952
mCachedTextFormatTags.clear();
953+
mCachedSymbolFavorites.clear();
954+
mCachedColorRampFavorites.clear();
955+
mCachedTextFormatFavorites.clear();
934956

935957
if ( groupRemoved )
936958
{
@@ -985,7 +1007,24 @@ bool QgsStyle::addFavorite( StyleEntity type, const QString &name )
9851007

9861008
const bool res = runEmptyQuery( query );
9871009
if ( res )
1010+
{
1011+
switch ( type )
1012+
{
1013+
case SymbolEntity:
1014+
mCachedSymbolFavorites[ name ] = true;
1015+
break;
1016+
case ColorrampEntity:
1017+
mCachedColorRampFavorites[ name ] = true;
1018+
break;
1019+
case TextFormatEntity:
1020+
mCachedTextFormatFavorites[ name ] = true;
1021+
break;
1022+
case TagEntity:
1023+
case SmartgroupEntity:
1024+
break;
1025+
}
9881026
emit favoritedChanged( type, name, true );
1027+
}
9891028

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

10151054
const bool res = runEmptyQuery( query );
10161055
if ( res )
1056+
{
1057+
switch ( type )
1058+
{
1059+
case SymbolEntity:
1060+
mCachedSymbolFavorites[ name ] = false;
1061+
break;
1062+
case ColorrampEntity:
1063+
mCachedColorRampFavorites[ name ] = false;
1064+
break;
1065+
case TextFormatEntity:
1066+
mCachedTextFormatFavorites[ name ] = false;
1067+
break;
1068+
case TagEntity:
1069+
case SmartgroupEntity:
1070+
break;
1071+
}
10171072
emit favoritedChanged( type, name, false );
1073+
}
10181074

10191075
return res;
10201076
}
@@ -1482,6 +1538,71 @@ QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol )
14821538
return tagList;
14831539
}
14841540

1541+
bool QgsStyle::isFavorite( QgsStyle::StyleEntity type, const QString &name )
1542+
{
1543+
if ( !mCurrentDB )
1544+
{
1545+
QgsDebugMsg( QStringLiteral( "Sorry! Cannot open database for getting the tags." ) );
1546+
return false;
1547+
}
1548+
1549+
switch ( type )
1550+
{
1551+
case SymbolEntity:
1552+
if ( mCachedSymbolFavorites.contains( name ) )
1553+
return mCachedSymbolFavorites.value( name );
1554+
break;
1555+
1556+
case ColorrampEntity:
1557+
if ( mCachedColorRampFavorites.contains( name ) )
1558+
return mCachedColorRampFavorites.value( name );
1559+
break;
1560+
1561+
case TextFormatEntity:
1562+
if ( mCachedTextFormatFavorites.contains( name ) )
1563+
return mCachedTextFormatFavorites.value( name );
1564+
break;
1565+
1566+
case TagEntity:
1567+
case SmartgroupEntity:
1568+
return false;
1569+
}
1570+
1571+
const QStringList names = allNames( type );
1572+
if ( !names.contains( name ) )
1573+
return false; // entity doesn't exist
1574+
1575+
// for efficiency, retrieve names of all favorited symbols and store them in cache
1576+
const QStringList favorites = symbolsOfFavorite( type );
1577+
bool res = false;
1578+
for ( const QString &n : names )
1579+
{
1580+
const bool isFav = favorites.contains( n );
1581+
if ( n == name )
1582+
res = isFav;
1583+
1584+
switch ( type )
1585+
{
1586+
case SymbolEntity:
1587+
mCachedSymbolFavorites[n] = isFav;
1588+
break;
1589+
1590+
case ColorrampEntity:
1591+
mCachedColorRampFavorites[ n ] = isFav;
1592+
break;
1593+
1594+
case TextFormatEntity:
1595+
mCachedTextFormatFavorites[ n ] = isFav;
1596+
break;
1597+
1598+
case TagEntity:
1599+
case SmartgroupEntity:
1600+
return false;
1601+
}
1602+
}
1603+
return res;
1604+
}
1605+
14851606
bool QgsStyle::symbolHasTag( StyleEntity type, const QString &symbol, const QString &tag )
14861607
{
14871608
if ( !mCurrentDB )
@@ -1652,6 +1773,28 @@ int QgsStyle::smartgroupId( const QString &name )
16521773
return getId( QStringLiteral( "smartgroup" ), name );
16531774
}
16541775

1776+
QStringList QgsStyle::allNames( QgsStyle::StyleEntity type ) const
1777+
{
1778+
switch ( type )
1779+
{
1780+
case SymbolEntity:
1781+
return symbolNames();
1782+
1783+
case ColorrampEntity:
1784+
return colorRampNames();
1785+
1786+
case TextFormatEntity:
1787+
return textFormatNames();
1788+
1789+
case TagEntity:
1790+
return tags();
1791+
1792+
case SmartgroupEntity:
1793+
return smartgroupNames();
1794+
}
1795+
return QStringList();
1796+
}
1797+
16551798
int QgsStyle::addSmartgroup( const QString &name, const QString &op, const QgsSmartConditionMap &conditions )
16561799
{
16571800
return addSmartgroup( name, op, conditions.values( QStringLiteral( "tag" ) ),
@@ -1729,7 +1872,7 @@ QgsSymbolGroupMap QgsStyle::smartgroupsListMap()
17291872
return groupNames;
17301873
}
17311874

1732-
QStringList QgsStyle::smartgroupNames()
1875+
QStringList QgsStyle::smartgroupNames() const
17331876
{
17341877
if ( !mCurrentDB )
17351878
{

‎src/core/symbology/qgsstyle.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class CORE_EXPORT QgsStyle : public QObject
252252
int colorRampCount();
253253

254254
//! Returns a list of names of color ramps
255-
QStringList colorRampNames();
255+
QStringList colorRampNames() const;
256256

257257
//! Returns a const pointer to a symbol (doesn't create new instance)
258258
const QgsColorRamp *colorRampRef( const QString &name ) const;
@@ -348,7 +348,7 @@ class CORE_EXPORT QgsStyle : public QObject
348348
int symbolCount();
349349

350350
//! Returns a list of names of symbols
351-
QStringList symbolNames();
351+
QStringList symbolNames() const;
352352

353353
/**
354354
* Returns the id in the style database for the given symbol name
@@ -360,6 +360,12 @@ class CORE_EXPORT QgsStyle : public QObject
360360
//! Returns the DB id for the given smartgroup name
361361
int smartgroupId( const QString &smartgroup );
362362

363+
/**
364+
* Returns a list of the names of all existing entities of the specified \a type.
365+
* \since QGIS 3.10
366+
*/
367+
QStringList allNames( StyleEntity type ) const;
368+
363369
/**
364370
* Returns the symbol names which are flagged as favorite
365371
*
@@ -531,6 +537,14 @@ class CORE_EXPORT QgsStyle : public QObject
531537
*/
532538
QStringList tagsOfSymbol( StyleEntity type, const QString &symbol );
533539

540+
/**
541+
* Returns TRUE if the symbol with matching \a type and \name is
542+
* marked as a favorite.
543+
*
544+
* \since QGIS 3.10
545+
*/
546+
bool isFavorite( StyleEntity type, const QString &name );
547+
534548
/**
535549
* Returns whether a given tag is associated with the symbol
536550
*
@@ -548,7 +562,7 @@ class CORE_EXPORT QgsStyle : public QObject
548562
QgsSymbolGroupMap smartgroupsListMap();
549563

550564
//! Returns the smart groups list
551-
QStringList smartgroupNames();
565+
QStringList smartgroupNames() const;
552566

553567
//! Returns the QgsSmartConditionMap for the given id
554568
QgsSmartConditionMap smartgroup( int id );
@@ -714,6 +728,10 @@ class CORE_EXPORT QgsStyle : public QObject
714728
QHash< QString, QStringList > mCachedColorRampTags;
715729
QHash< QString, QStringList > mCachedTextFormatTags;
716730

731+
QHash< QString, bool > mCachedSymbolFavorites;
732+
QHash< QString, bool > mCachedColorRampFavorites;
733+
QHash< QString, bool > mCachedTextFormatFavorites;
734+
717735
QString mErrorString;
718736
QString mFileName;
719737

‎src/core/symbology/qgsstylemodel.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
250250
case TagRole:
251251
return mStyle->tagsOfSymbol( entityType, name );
252252

253+
case IsFavoriteRole:
254+
return mStyle->isFavorite( entityType, name );
255+
253256
case SymbolTypeRole:
254257
{
255258
if ( entityType != QgsStyle::SymbolEntity )
@@ -720,7 +723,7 @@ bool QgsStyleProxyModel::filterAcceptsRow( int source_row, const QModelIndex &so
720723
if ( mSmartGroupId >= 0 && !mSmartGroupSymbolNames.contains( name ) )
721724
return false;
722725

723-
if ( mFavoritesOnly && !mFavoritedSymbolNames.contains( name ) )
726+
if ( mFavoritesOnly && !sourceModel()->data( index, QgsStyleModel::IsFavoriteRole ).toBool() )
724727
return false;
725728

726729
if ( !mFilterString.isEmpty() )
@@ -768,17 +771,6 @@ bool QgsStyleProxyModel::favoritesOnly() const
768771
void QgsStyleProxyModel::setFavoritesOnly( bool favoritesOnly )
769772
{
770773
mFavoritesOnly = favoritesOnly;
771-
772-
if ( mFavoritesOnly )
773-
{
774-
mFavoritedSymbolNames = mStyle->symbolsOfFavorite( QgsStyle::SymbolEntity );
775-
mFavoritedSymbolNames.append( mStyle->symbolsOfFavorite( QgsStyle::ColorrampEntity ) );
776-
mFavoritedSymbolNames.append( mStyle->symbolsOfFavorite( QgsStyle::TextFormatEntity ) );
777-
}
778-
else
779-
{
780-
mFavoritedSymbolNames.clear();
781-
}
782774
invalidateFilter();
783775
}
784776

‎src/core/symbology/qgsstylemodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
5757
TypeRole = Qt::UserRole + 1, //!< Style entity type, see QgsStyle::StyleEntity
5858
TagRole, //!< String list of tags
5959
SymbolTypeRole, //!< Symbol type (for symbol entities)
60+
IsFavoriteRole, //!< Whether entity is flagged as a favorite
6061
};
6162

6263
/**
@@ -311,7 +312,6 @@ class CORE_EXPORT QgsStyleProxyModel: public QSortFilterProxyModel
311312
QStringList mSmartGroupSymbolNames;
312313

313314
bool mFavoritesOnly = false;
314-
QStringList mFavoritedSymbolNames;
315315

316316
bool mEntityFilterEnabled = false;
317317
QgsStyle::StyleEntity mEntityFilter = QgsStyle::SymbolEntity;

‎tests/src/core/testqgsstyle.cpp

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class TestStyle : public QObject
6262
void cleanup() {}// will be called after every testfunction.
6363
// void initStyles();
6464

65+
void testCreateSymbols();
6566
void testCreateColorRamps();
6667
void testCreateTextFormats();
6768
void testLoadColorRamps();
@@ -122,6 +123,25 @@ void TestStyle::cleanupTestCase()
122123
}
123124
}
124125

126+
void TestStyle::testCreateSymbols()
127+
{
128+
// add some symbols to favorites
129+
QgsStyle s;
130+
s.createMemoryDatabase();
131+
132+
std::unique_ptr< QgsMarkerSymbol > sym1( QgsMarkerSymbol::createSimple( QgsStringMap() ) );
133+
std::unique_ptr< QgsMarkerSymbol > sym2( QgsMarkerSymbol::createSimple( QgsStringMap() ) );
134+
std::unique_ptr< QgsMarkerSymbol > sym3( QgsMarkerSymbol::createSimple( QgsStringMap() ) );
135+
s.addSymbol( QStringLiteral( "symbolA" ), sym1.release(), true );
136+
s.addSymbol( QStringLiteral( "symbolB" ), sym2.release(), true );
137+
s.addSymbol( QStringLiteral( "symbolC" ), sym3.release(), true );
138+
139+
QCOMPARE( s.allNames( QgsStyle::SymbolEntity ),
140+
QStringList() << QStringLiteral( "symbolA" )
141+
<< QStringLiteral( "symbolB" )
142+
<< QStringLiteral( "symbolC" ) );
143+
}
144+
125145
bool TestStyle::imageCheck( QgsMapSettings &ms, const QString &testName )
126146
{
127147
QgsMultiRenderChecker checker;
@@ -175,6 +195,14 @@ void TestStyle::testCreateColorRamps()
175195
// continuous ramp
176196
QgsCptCityColorRamp *cc3Ramp = new QgsCptCityColorRamp( QStringLiteral( "grass/byr" ), QString() );
177197
QVERIFY( mStyle->addColorRamp( "test_cc3", cc3Ramp, true ) );
198+
199+
QCOMPARE( mStyle->allNames( QgsStyle::ColorrampEntity ), QStringList() << QStringLiteral( "test_cb1" )
200+
<< QStringLiteral( "test_cb2" )
201+
<< QStringLiteral( "test_cc1" )
202+
<< QStringLiteral( "test_cc2" )
203+
<< QStringLiteral( "test_cc3" )
204+
<< QStringLiteral( "test_gradient" )
205+
<< QStringLiteral( "test_random" ) );
178206
}
179207

180208
void TestStyle::testCreateTextFormats()
@@ -226,7 +254,8 @@ void TestStyle::testCreateTextFormats()
226254
QCOMPARE( style2.textFormat( QString( "test_format" ) ).color().name(), QStringLiteral( "#ffff00" ) );
227255
QCOMPARE( style2.textFormat( QString( "test_format2" ) ).color().name(), QStringLiteral( "#ffffff" ) );
228256

229-
257+
QCOMPARE( mStyle->allNames( QgsStyle::TextFormatEntity ), QStringList() << QStringLiteral( "test_format" )
258+
<< QStringLiteral( "test_format2" ) );
230259
}
231260

232261
void TestStyle::testLoadColorRamps()
@@ -306,6 +335,10 @@ void TestStyle::testFavorites()
306335
favorites = mStyle->symbolsOfFavorite( QgsStyle::SymbolEntity );
307336
int count = favorites.count();
308337

338+
QVERIFY( !mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "AaaaaaaaaA" ) ) );
339+
QVERIFY( !mStyle->isFavorite( QgsStyle::TextFormatEntity, QStringLiteral( "AaaaaaaaaA" ) ) );
340+
QVERIFY( !mStyle->isFavorite( QgsStyle::ColorrampEntity, QStringLiteral( "AaaaaaaaaA" ) ) );
341+
309342
// add some symbols to favorites
310343
std::unique_ptr< QgsMarkerSymbol > sym1( QgsMarkerSymbol::createSimple( QgsStringMap() ) );
311344
std::unique_ptr< QgsMarkerSymbol > sym2( QgsMarkerSymbol::createSimple( QgsStringMap() ) );
@@ -314,11 +347,15 @@ void TestStyle::testFavorites()
314347
mStyle->saveSymbol( QStringLiteral( "symbolB" ), sym2.get(), false, QStringList() );
315348
mStyle->saveSymbol( QStringLiteral( "symbolC" ), sym3.get(), true, QStringList() );
316349

350+
QVERIFY( mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolA" ) ) );
351+
QVERIFY( !mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolB" ) ) );
352+
QVERIFY( mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolC" ) ) );
353+
317354
// check for added symbols to favorites
318355
favorites = mStyle->symbolsOfFavorite( QgsStyle::SymbolEntity );
319356
QCOMPARE( favorites.count(), count + 2 );
320-
QVERIFY( favorites.contains( "symbolA" ) );
321-
QVERIFY( favorites.contains( "symbolC" ) );
357+
QVERIFY( favorites.contains( QStringLiteral( "symbolA" ) ) );
358+
QVERIFY( favorites.contains( QStringLiteral( "symbolC" ) ) );
322359

323360
QSignalSpy favoriteChangedSpy( mStyle, &QgsStyle::favoritedChanged );
324361

@@ -329,6 +366,10 @@ void TestStyle::testFavorites()
329366
QCOMPARE( favoriteChangedSpy.at( 0 ).at( 1 ).toString(), QStringLiteral( "symbolA" ) );
330367
QCOMPARE( favoriteChangedSpy.at( 0 ).at( 2 ).toBool(), false );
331368

369+
QVERIFY( !mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolA" ) ) );
370+
QVERIFY( !mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolB" ) ) );
371+
QVERIFY( mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolC" ) ) );
372+
332373
// insure favorites updated after removal
333374
favorites = mStyle->symbolsOfFavorite( QgsStyle::SymbolEntity );
334375
QCOMPARE( favorites.count(), count + 1 );
@@ -343,11 +384,17 @@ void TestStyle::testFavorites()
343384
QCOMPARE( favorites.count(), count + 2 );
344385
QVERIFY( favorites.contains( "symbolA" ) );
345386

387+
QVERIFY( mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolA" ) ) );
388+
QVERIFY( !mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolB" ) ) );
389+
QVERIFY( mStyle->isFavorite( QgsStyle::SymbolEntity, QStringLiteral( "symbolC" ) ) );
390+
346391
QgsGradientColorRamp *gradientRamp = new QgsGradientColorRamp( QColor( Qt::red ), QColor( Qt::blue ) );
347392
QVERIFY( mStyle->addColorRamp( "gradient_1", gradientRamp, true ) );
348393
favorites = mStyle->symbolsOfFavorite( QgsStyle::ColorrampEntity );
349394
QCOMPARE( favorites.count(), 0 );
350395

396+
QVERIFY( !mStyle->isFavorite( QgsStyle::ColorrampEntity, QStringLiteral( "gradient_1" ) ) );
397+
351398
mStyle->addFavorite( QgsStyle::ColorrampEntity, QStringLiteral( "gradient_1" ) );
352399
QCOMPARE( favoriteChangedSpy.count(), 3 );
353400
QCOMPARE( favoriteChangedSpy.at( 2 ).at( 0 ).toInt(), static_cast< int >( QgsStyle::ColorrampEntity ) );
@@ -356,6 +403,7 @@ void TestStyle::testFavorites()
356403
favorites = mStyle->symbolsOfFavorite( QgsStyle::ColorrampEntity );
357404
QCOMPARE( favorites.count(), 1 );
358405
QVERIFY( favorites.contains( "gradient_1" ) );
406+
QVERIFY( mStyle->isFavorite( QgsStyle::ColorrampEntity, QStringLiteral( "gradient_1" ) ) );
359407

360408
mStyle->removeFavorite( QgsStyle::ColorrampEntity, QStringLiteral( "gradient_1" ) );
361409
QCOMPARE( favoriteChangedSpy.count(), 4 );
@@ -364,12 +412,14 @@ void TestStyle::testFavorites()
364412
QCOMPARE( favoriteChangedSpy.at( 3 ).at( 2 ).toBool(), false );
365413
favorites = mStyle->symbolsOfFavorite( QgsStyle::ColorrampEntity );
366414
QCOMPARE( favorites.count(), 0 );
415+
QVERIFY( !mStyle->isFavorite( QgsStyle::ColorrampEntity, QStringLiteral( "gradient_1" ) ) );
367416

368417
// text formats
369418
QgsTextFormat format1;
370-
QVERIFY( mStyle->addTextFormat( "format_1", format1, true ) );
419+
QVERIFY( mStyle->addTextFormat( QStringLiteral( "format_1" ), format1, true ) );
371420
favorites = mStyle->symbolsOfFavorite( QgsStyle::TextFormatEntity );
372421
QCOMPARE( favorites.count(), 0 );
422+
QVERIFY( !mStyle->isFavorite( QgsStyle::TextFormatEntity, QStringLiteral( "format_1" ) ) );
373423

374424
mStyle->addFavorite( QgsStyle::TextFormatEntity, QStringLiteral( "format_1" ) );
375425
QCOMPARE( favoriteChangedSpy.count(), 5 );
@@ -378,7 +428,8 @@ void TestStyle::testFavorites()
378428
QCOMPARE( favoriteChangedSpy.at( 4 ).at( 2 ).toBool(), true );
379429
favorites = mStyle->symbolsOfFavorite( QgsStyle::TextFormatEntity );
380430
QCOMPARE( favorites.count(), 1 );
381-
QVERIFY( favorites.contains( "format_1" ) );
431+
QVERIFY( favorites.contains( QStringLiteral( "format_1" ) ) );
432+
QVERIFY( mStyle->isFavorite( QgsStyle::TextFormatEntity, QStringLiteral( "format_1" ) ) );
382433

383434
mStyle->removeFavorite( QgsStyle::TextFormatEntity, QStringLiteral( "format_1" ) );
384435
QCOMPARE( favoriteChangedSpy.count(), 6 );
@@ -387,6 +438,7 @@ void TestStyle::testFavorites()
387438
QCOMPARE( favoriteChangedSpy.at( 5 ).at( 2 ).toBool(), false );
388439
favorites = mStyle->symbolsOfFavorite( QgsStyle::TextFormatEntity );
389440
QCOMPARE( favorites.count(), 0 );
441+
QVERIFY( !mStyle->isFavorite( QgsStyle::TextFormatEntity, QStringLiteral( "format_1" ) ) );
390442
}
391443

392444
void TestStyle::testTags()
@@ -407,6 +459,13 @@ void TestStyle::testTags()
407459
QCOMPARE( id, mStyle->tagId( "purple" ) );
408460
QCOMPARE( QStringLiteral( "purple" ), mStyle->tag( id ) );
409461

462+
QCOMPARE( mStyle->allNames( QgsStyle::TagEntity ),
463+
QStringList() << QStringLiteral( "red" )
464+
<< QStringLiteral( "starry" )
465+
<< QStringLiteral( "circle" )
466+
<< QStringLiteral( "blue" )
467+
<< QStringLiteral( "purple" ) );
468+
410469
// Cyrillic
411470
id = mStyle->addTag( QStringLiteral( "МЕТЕОР" ) );
412471
QCOMPARE( id, mStyle->tagId( "МЕТЕОР" ) );
@@ -726,6 +785,9 @@ void TestStyle::testSmartGroup()
726785
QCOMPARE( style.smartgroupId( QStringLiteral( "mine" ) ), 1 );
727786
QCOMPARE( groupModifiedSpy.count(), 1 );
728787

788+
QCOMPARE( style.allNames( QgsStyle::SmartgroupEntity ),
789+
QStringList() << QStringLiteral( "mine" ) );
790+
729791
QCOMPARE( style.symbolsOfSmartgroup( QgsStyle::SymbolEntity, 1 ), QStringList() << QStringLiteral( "symbolA" ) );
730792
QCOMPARE( style.symbolsOfSmartgroup( QgsStyle::ColorrampEntity, 1 ), QStringList() << QStringLiteral( "ramp a" ) );
731793
QCOMPARE( style.symbolsOfSmartgroup( QgsStyle::TextFormatEntity, 1 ), QStringList() << QStringLiteral( "format a" ) );

‎tests/src/python/test_qgsstylemodel.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_style_with_symbols(self):
7474
symbol_C.setColor(QColor(10, 255, 10))
7575
self.assertTrue(style.addSymbol('C', symbol_C, True))
7676
style.tagSymbol(QgsStyle.SymbolEntity, 'C', ['tag 3'])
77+
style.addFavorite(QgsStyle.SymbolEntity, 'C')
7778
symbol_C = createMarkerSymbol()
7879
symbol_C.setColor(QColor(10, 255, 10))
7980
self.assertTrue(style.addSymbol(' ----c/- ', symbol_C, True))
@@ -125,6 +126,12 @@ def test_style_with_symbols(self):
125126
self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity)
126127
self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity)
127128

129+
self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.IsFavoriteRole), False)
130+
self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.IsFavoriteRole), False)
131+
self.assertEqual(model.data(model.index(2, 0), QgsStyleModel.IsFavoriteRole), True)
132+
self.assertEqual(model.data(model.index(3, 0), QgsStyleModel.IsFavoriteRole), False)
133+
self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.IsFavoriteRole), False)
134+
128135
def test_style_with_ramps(self):
129136
style = QgsStyle()
130137
style.createMemoryDatabase()
@@ -141,6 +148,7 @@ def test_style_with_ramps(self):
141148
symbol_C = QgsLimitedRandomColorRamp()
142149
self.assertTrue(style.addColorRamp('C', symbol_C, True))
143150
style.tagSymbol(QgsStyle.ColorrampEntity, 'C', ['tag 3'])
151+
style.addFavorite(QgsStyle.ColorrampEntity, 'C')
144152
symbol_C = QgsLimitedRandomColorRamp()
145153
self.assertTrue(style.addColorRamp(' ----c/- ', symbol_C, True))
146154

@@ -182,6 +190,12 @@ def test_style_with_ramps(self):
182190
self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity)
183191
self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity)
184192

193+
self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.IsFavoriteRole), False)
194+
self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.IsFavoriteRole), False)
195+
self.assertEqual(model.data(model.index(2, 0), QgsStyleModel.IsFavoriteRole), True)
196+
self.assertEqual(model.data(model.index(3, 0), QgsStyleModel.IsFavoriteRole), False)
197+
self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.IsFavoriteRole), False)
198+
185199
def test_style_with_text_formats(self):
186200
style = QgsStyle()
187201
style.createMemoryDatabase()
@@ -198,6 +212,7 @@ def test_style_with_text_formats(self):
198212
format_C = QgsTextFormat()
199213
self.assertTrue(style.addTextFormat('C', format_C, True))
200214
style.tagSymbol(QgsStyle.TextFormatEntity, 'C', ['tag 3'])
215+
style.addFavorite(QgsStyle.TextFormatEntity, 'C')
201216
format_C = QgsTextFormat()
202217
self.assertTrue(style.addTextFormat(' ----c/- ', format_C, True))
203218

@@ -239,6 +254,12 @@ def test_style_with_text_formats(self):
239254
self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.TextFormatEntity)
240255
self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.TextFormatEntity)
241256

257+
self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.IsFavoriteRole), False)
258+
self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.IsFavoriteRole), False)
259+
self.assertEqual(model.data(model.index(2, 0), QgsStyleModel.IsFavoriteRole), True)
260+
self.assertEqual(model.data(model.index(3, 0), QgsStyleModel.IsFavoriteRole), False)
261+
self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.IsFavoriteRole), False)
262+
242263
def test_mixed_style(self):
243264
"""
244265
Test style with both symbols and ramps

0 commit comments

Comments
 (0)
Please sign in to comment.