Skip to content

Commit

Permalink
Sort filter results so closer matches appear higher
Browse files Browse the repository at this point in the history
A closer match means more of the text is matched, ie
a short string inside a long string is penalised
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent a53516d commit 6649d2b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/gui/locator/qgslocatorfilter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class QgsLocatorResult
Icon for result.
%End

double score = 0.5;
%Docstring
Match score, from 0 - 1, where 1 represents a perfect match.
%End

};

class QgsLocatorFilter : QObject
Expand Down
4 changes: 4 additions & 0 deletions python/plugins/processing/gui/AlgorithmLocatorFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def fetchResults(self,string,context,feedback):
result.displayString = a.displayName()
result.icon = a.icon()
result.userData = a.id()
if string.lower() in a.displayName().lower():
result.score = float(len(string)) / len(a.displayName())
else:
result.score = 0
self.resultFetched.emit(result)

def triggerResult(self, result):
Expand Down
3 changes: 3 additions & 0 deletions src/app/locator/qgsinbuiltlocatorfilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void QgsLayerTreeLocatorFilter::fetchResults( const QString &string, const QgsLo
result.displayString = layer->layer()->name();
result.userData = layer->layerId();
result.icon = QgsMapLayerModel::iconForLayer( layer->layer() );
result.score = static_cast< double >( string.length() ) / layer->layer()->name().length();
emit resultFetched( result );
}
}
Expand Down Expand Up @@ -81,6 +82,7 @@ void QgsLayoutLocatorFilter::fetchResults( const QString &string, const QgsLocat
result.displayString = composition->name();
result.userData = composition->name();
//result.icon = QgsMapLayerModel::iconForLayer( layer->layer() );
result.score = static_cast< double >( string.length() ) / composition->name().length();
emit resultFetched( result );
}
}
Expand Down Expand Up @@ -155,6 +157,7 @@ void QgsActionLocatorFilter::searchActions( const QString &string, QWidget *pare
result.displayString = searchText;
result.userData = QVariant::fromValue( action );
result.icon = action->icon();
result.score = static_cast< double >( string.length() ) / searchText.length();
emit resultFetched( result );
found << action;
}
Expand Down
5 changes: 5 additions & 0 deletions src/gui/locator/qgslocatorfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class GUI_EXPORT QgsLocatorResult
*/
QIcon icon;

/**
* Match score, from 0 - 1, where 1 represents a perfect match.
*/
double score = 0.5;

};

/**
Expand Down
12 changes: 12 additions & 0 deletions src/gui/locator/qgslocatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
else
return 1;

case ResultScoreRole:
if ( mResults.at( index.row() ).filter )
return 0;
else
return ( mResults.at( index.row() ).result.score );

case ResultFilterPriorityRole:
if ( !mResults.at( index.row() ).filter )
return mResults.at( index.row() ).result.filter->priority();
Expand Down Expand Up @@ -443,6 +449,12 @@ bool QgsLocatorProxyModel::lessThan( const QModelIndex &left, const QModelIndex
if ( leftTypeRole != rightTypeRole )
return leftTypeRole < rightTypeRole;

// sort filter's results by score
double leftScore = sourceModel()->data( left, QgsLocatorModel::ResultScoreRole ).toDouble();
double rightScore = sourceModel()->data( right, QgsLocatorModel::ResultScoreRole ).toDouble();
if ( !qgsDoubleNear( leftScore, rightScore ) )
return leftScore > rightScore;

// lastly sort filter's results by string
leftFilter = sourceModel()->data( left, Qt::DisplayRole ).toString();
rightFilter = sourceModel()->data( right, Qt::DisplayRole ).toString();
Expand Down
1 change: 1 addition & 0 deletions src/gui/locator/qgslocatorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class QgsLocatorModel : public QAbstractListModel
ResultDataRole = Qt::UserRole + 1, //!< QgsLocatorResult data
ResultTypeRole,
ResultFilterPriorityRole,
ResultScoreRole,
ResultFilterNameRole,
};

Expand Down

0 comments on commit 6649d2b

Please sign in to comment.