forked from linuxdeepin/dde-launchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchfilterproxymodel.cpp
More file actions
56 lines (46 loc) · 1.99 KB
/
searchfilterproxymodel.cpp
File metadata and controls
56 lines (46 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "appsmodel.h"
#include "searchfilterproxymodel.h"
#include <QDebug>
#include <DPinyin>
DCORE_USE_NAMESPACE
SearchFilterProxyModel::SearchFilterProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSourceModel(&AppsModel::instance());
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);
const QRegularExpression searchPattern = this->filterRegularExpression();
const QString & displayName = modelIndex.data(Qt::DisplayRole).toString();
const QString & name = modelIndex.data(AppsModel::NameRole).toString();
const QString & transliterated = modelIndex.data(AppsModel::AllTransliteratedRole).toString();
QString pattern = searchPattern.pattern().toLower().remove(" ");
return fuzzyMatch(displayName, pattern) || fuzzyMatch(name, pattern) || fuzzyMatch(transliterated, pattern);
}