@@ -18,6 +18,29 @@ SearchFilterProxyModel::SearchFilterProxyModel(QObject *parent)
1818 sort (0 , Qt::DescendingOrder);
1919}
2020
21+ bool SearchFilterProxyModel::fuzzyMatch (const QString &modelData, const QString &pattern) const
22+ {
23+ if (modelData.contains (pattern, Qt::CaseInsensitive)) {
24+ return true ;
25+ }
26+ QString processedText = modelData.toLower ().simplified ();
27+ int textLen = processedText.length ();
28+ int patternLen = pattern.length ();
29+ std::vector<std::vector<int >> dp (textLen + 1 , std::vector<int >(patternLen + 1 , 0 ));
30+ for (int i = 1 ; i <= textLen; i++) {
31+ for (int j = 1 ; j <= patternLen; j++) {
32+ if (processedText[i - 1 ] == pattern[j - 1 ]) {
33+ dp[i][j] = dp[i - 1 ][j - 1 ] + 1 ;
34+ } else {
35+ dp[i][j] = std::max (dp[i - 1 ][j], dp[i][j - 1 ]);
36+ }
37+ }
38+ }
39+
40+ float matchScore = static_cast <float >(dp[textLen][patternLen]) / patternLen;
41+ return matchScore >= m_fuzzyThreshold;
42+ }
43+
2144bool SearchFilterProxyModel::filterAcceptsRow (int sourceRow, const QModelIndex &sourceParent) const
2245{
2346 QModelIndex modelIndex = this ->sourceModel ()->index (sourceRow, 0 , sourceParent);
@@ -26,11 +49,8 @@ bool SearchFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &
2649 const QString & displayName = modelIndex.data (Qt::DisplayRole).toString ();
2750 const QString & name = modelIndex.data (AppsModel::NameRole).toString ();
2851 const QString & transliterated = modelIndex.data (AppsModel::AllTransliteratedRole).toString ();
29- const QString & jianpin = Dtk::Core::firstLetters (displayName).join (' ,' );
3052
31- auto nameCopy = name;
32- nameCopy = nameCopy.toLower ();
33- nameCopy.replace (" " , " " );
53+ QString pattern = searchPattern.pattern ().toLower ().remove (" " );
3454
35- return displayName. contains (searchPattern) || nameCopy. contains (searchPattern ) || transliterated. contains (searchPattern ) || jianpin. contains (searchPattern );
55+ return fuzzyMatch (displayName, pattern ) || fuzzyMatch (name, pattern ) || fuzzyMatch (transliterated, pattern );
3656}
0 commit comments