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
124 lines (103 loc) · 4.68 KB
/
searchfilterproxymodel.cpp
File metadata and controls
124 lines (103 loc) · 4.68 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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::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();
const QString & jianpin = Dtk::Core::firstLetters(displayName).join(',');
auto nameCopy = name;
nameCopy = nameCopy.toLower();
nameCopy.replace(" ", "");
QString searchPatternDelBlank = searchPattern.pattern().toLower().remove(" ");
// Get first letters of each word in displayName
QStringList words = displayName.split(" ", Qt::SkipEmptyParts);
QString nameFirstLetters;
for (const QString &word : words) {
if (!word.isEmpty()) {
QChar firstChar = word[0];
if (firstChar.isLetter()) {
nameFirstLetters += firstChar.toLower();
}
}
}
// Check for number or English prefix matches only
QRegularExpression searchNumberCheck("\\d+$");
QRegularExpression searchEnglishCheck("^[a-zA-Z\\s]+$");
bool isNumberSearch = searchNumberCheck.match(searchPatternDelBlank).hasMatch();
bool isEnglishSearch = searchEnglishCheck.match(searchPattern.pattern()).hasMatch();
if (isNumberSearch || isEnglishSearch) {
bool hasMatch = false;
// Handle number prefix matching
if (isNumberSearch) {
QRegularExpression numberRegex("\\d+");
QRegularExpressionMatchIterator matches = numberRegex.globalMatch(displayName);
while (matches.hasNext()) {
QRegularExpressionMatch match = matches.next();
QString numberInDisplayName = match.captured(0);
hasMatch = true;
if (numberInDisplayName.startsWith(searchPatternDelBlank)) {
return true;
}
}
}
// Handle English prefix matching
if (isEnglishSearch) {
// Remove spaces and convert to lowercase for comparison
QString displayNameLower = displayName.toLower().remove(" ");
// Check prefix matching for various name formats
if (displayNameLower.startsWith(searchPatternDelBlank) ||
nameCopy.startsWith(searchPatternDelBlank) ||
transliterated.startsWith(searchPatternDelBlank) ||
jianpin.startsWith(searchPatternDelBlank) ||
nameFirstLetters.startsWith(searchPatternDelBlank)) {
return true;
}
// Also check if search pattern matches the prefix of any word in displayName
for (const QString &word : words) {
if (word.toLower().startsWith(searchPatternDelBlank)) {
return true;
}
}
// Also check if search pattern matches the prefix of any word in name
QStringList nameWords = name.split(" ", Qt::SkipEmptyParts);
for (const QString &word : nameWords) {
if (word.toLower().startsWith(searchPatternDelBlank)) {
return true;
}
}
// Also check if search pattern matches the prefix of any word in transliterated
QStringList transliteratedWords = transliterated.split(" ", Qt::SkipEmptyParts);
for (const QString &word : transliteratedWords) {
if (word.toLower().startsWith(searchPatternDelBlank)) {
return true;
}
}
hasMatch = true; // English content was found
}
// If we had matches but none were prefix matches, return false
if (hasMatch) {
return false;
}
}
return displayName.contains(searchPatternDelBlank) ||
nameCopy.contains(searchPatternDelBlank) ||
transliterated.contains(searchPatternDelBlank) ||
jianpin.contains(searchPatternDelBlank) ||
nameFirstLetters.contains(searchPatternDelBlank);
}