Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/models/searchfilterproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ SearchFilterProxyModel::SearchFilterProxyModel(QObject *parent)
sort(0, Qt::DescendingOrder);
}

bool SearchFilterProxyModel::fuzzyMatch(const QString &modelData, const QString &pattern) const
{
if (modelData.contains(pattern, Qt::CaseInsensitive)) {
return true;
}
QString processedText = modelData.toLower().simplified();
int textLen = processedText.length();
int patternLen = pattern.length();
std::vector<std::vector<int>> dp(textLen + 1, std::vector<int>(patternLen + 1, 0));
for (int i = 1; i <= textLen; i++) {
for (int j = 1; j <= patternLen; j++) {
if (processedText[i - 1] == pattern[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

float matchScore = static_cast<float>(dp[textLen][patternLen]) / patternLen;
return matchScore >= m_fuzzyThreshold;
}

bool SearchFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex modelIndex = this->sourceModel()->index(sourceRow, 0, sourceParent);
Expand All @@ -26,11 +49,8 @@ bool SearchFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &
const QString & displayName = modelIndex.data(Qt::DisplayRole).toString();
const QString & name = modelIndex.data(AppsModel::NameRole).toString();
const QString & transliterated = modelIndex.data(AppsModel::AllTransliteratedRole).toString();
const QString & jianpin = Dtk::Core::firstLetters(displayName).join(',');

auto nameCopy = name;
nameCopy = nameCopy.toLower();
nameCopy.replace(" ", "");
QString pattern = searchPattern.pattern().toLower().remove(" ");
Comment thread
sourcery-ai[bot] marked this conversation as resolved.

return displayName.contains(searchPattern) || nameCopy.contains(searchPattern) || transliterated.contains(searchPattern) || jianpin.contains(searchPattern);
return fuzzyMatch(displayName, pattern) || fuzzyMatch(name, pattern) || fuzzyMatch(transliterated, pattern);
}
2 changes: 2 additions & 0 deletions src/models/searchfilterproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ class SearchFilterProxyModel : public QSortFilterProxyModel
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
private:
explicit SearchFilterProxyModel(QObject *parent = nullptr);
bool fuzzyMatch(const QString &modelData, const QString &pattern) const;
float m_fuzzyThreshold = 0.8f;
};