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: 5 additions & 25 deletions src/models/searchfilterproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,6 @@ 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 @@ -49,8 +26,11 @@ 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(',');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Using join(',') inserts commas between initials and may break substring matching

Use join("") instead of join(",") to concatenate initials without commas so contains() works as expected.

Suggested change
const QString & jianpin = Dtk::Core::firstLetters(displayName).join(',');
// concatenate initials without commas so contains() works correctly
const QString jianpin = Dtk::Core::firstLetters(displayName).join(QString());


QString pattern = searchPattern.pattern().toLower().remove(" ");
auto nameCopy = name;
nameCopy = nameCopy.toLower();
nameCopy.replace(" ", "");

return fuzzyMatch(displayName, pattern) || fuzzyMatch(name, pattern) || fuzzyMatch(transliterated, pattern);
return displayName.contains(searchPattern) || nameCopy.contains(searchPattern) || transliterated.contains(searchPattern) || jianpin.contains(searchPattern);
}
2 changes: 0 additions & 2 deletions src/models/searchfilterproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ 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;
};