Skip to content

Commit 0e9e9fd

Browse files
wjyrichBLumia
authored andcommitted
feat: Add fuzzy search support for applications
add fuzzy search. PMS-BUG-288459
1 parent a9c8f52 commit 0e9e9fd

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/models/searchfilterproxymodel.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2144
bool 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
}

src/models/searchfilterproxymodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ class SearchFilterProxyModel : public QSortFilterProxyModel
3131
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
3232
private:
3333
explicit SearchFilterProxyModel(QObject *parent = nullptr);
34+
bool fuzzyMatch(const QString &modelData, const QString &pattern) const;
35+
float m_fuzzyThreshold = 0.8f;
3436
};

0 commit comments

Comments
 (0)