Skip to content

Commit

Permalink
Make style filter model search by word, not phrase
Browse files Browse the repository at this point in the history
This allows matching of a filter string "hash line" to the symbol
 "hashed red lines"
  • Loading branch information
nyalldawson committed Sep 14, 2018
1 parent 31daa82 commit 57f89c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/core/symbology/qgsstylemodel.cpp
Expand Up @@ -472,8 +472,33 @@ bool QgsStyleProxyModel::filterAcceptsRow( int source_row, const QModelIndex &so
if ( mFavoritesOnly && !mFavoritedSymbolNames.contains( name ) )
return false;

if ( !name.contains( mFilterString, Qt::CaseInsensitive ) && !tags.join( ';' ).contains( mFilterString, Qt::CaseInsensitive ) )
return false;
if ( !mFilterString.isEmpty() )
{
// filter by word, in both filter string and style entity name/tags
// this allows matching of a filter string "hash line" to the symbol "hashed red lines"
const QStringList partsToMatch = mFilterString.trimmed().split( ' ' );

QStringList partsToSearch = name.split( ' ' );
for ( const QString &tag : tags )
{
partsToSearch.append( tag.split( ' ' ) );
}

for ( const QString &part : partsToMatch )
{
bool found = false;
for ( const QString &partToSearch : qgis::as_const( partsToSearch ) )
{
if ( partToSearch.contains( part, Qt::CaseInsensitive ) )
{
found = true;
break;
}
}
if ( !found )
return false; // couldn't find a match for this word, so hide entity
}
}

return true;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/src/python/test_qgsstylemodel.py
Expand Up @@ -548,6 +548,12 @@ def test_filter_proxy(self):
self.assertEqual(model.rowCount(), 2)
self.assertEqual(model.data(model.index(0, 0)), 'a')
self.assertEqual(model.data(model.index(1, 0)), 'ramp a')
model.setFilterString('ram b')
self.assertEqual(model.rowCount(), 1)
self.assertEqual(model.data(model.index(0, 0)), 'ramp BB')
model.setFilterString('ta ram') # match ta -> "tag 1", ram -> "ramp a"
self.assertEqual(model.rowCount(), 1)
self.assertEqual(model.data(model.index(0, 0)), 'ramp a')
model.setFilterString('')
self.assertEqual(model.rowCount(), 8)

Expand Down

0 comments on commit 57f89c9

Please sign in to comment.