feat: Added search priority display.#591
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: wjyrich The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Reviewer's GuideThis PR replaces the previous boolean matching in the proxy model with a weight-based ranking: it computes a detailed match weight for each item (via a new calculateMatchWeight method), caches these weights, and overrides lessThan to sort by weight then launch frequency, while falling back to the base sort. Class diagram for updated SearchFilterProxyModel with weight-based rankingclassDiagram
class QSortFilterProxyModel {
<<Qt Base>>
+filterAcceptsRow(int, QModelIndex) : bool
+lessThan(QModelIndex, QModelIndex) : bool
}
class SearchFilterProxyModel {
+filterAcceptsRow(int, QModelIndex) : bool
+lessThan(QModelIndex, QModelIndex) : bool
-calculateMatchWeight(QString, QString, QString, QString, QString, QString) : int
-m_weightCache : QHash<QPersistentModelIndex, int>
}
QSortFilterProxyModel <|-- SearchFilterProxyModel
Flow diagram for search filtering and ranking processflowchart TD
A[User enters search query] --> B[SearchFilterProxyModel::filterAcceptsRow]
B --> C[calculateMatchWeight]
C --> D{Weight > 0?}
D -- Yes --> E[Item accepted]
D -- No --> F[Item filtered out]
E & F --> G[Weights cached in m_weightCache]
G --> H[SearchFilterProxyModel::lessThan]
H --> I[Sort by weight desc]
I --> J[If tie, sort by launch count]
J --> K[If still tie, fallback to base sort]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @wjyrich - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/models/searchfilterproxymodel.cpp:28` </location>
<code_context>
const QRegularExpression searchPattern = this->filterRegularExpression();
+ // 如果是新的搜索,清理权重缓存
+ static QString lastSearchPattern;
+ if (lastSearchPattern != searchPattern.pattern()) {
+ m_weightCache.clear();
+ lastSearchPattern = searchPattern.pattern();
</code_context>
<issue_to_address>
Use of static variable for lastSearchPattern may cause issues in multi-instance scenarios.
A static lastSearchPattern will be shared across all SearchFilterProxyModel instances, which can cause incorrect cache clearing. Make it an instance member to avoid this issue.
</issue_to_address>
### Comment 2
<location> `src/models/searchfilterproxymodel.cpp:90` </location>
<code_context>
+ QPersistentModelIndex leftPersistent(source_left);
+ QPersistentModelIndex rightPersistent(source_right);
+
+ int leftWeight = m_weightCache.value(leftPersistent, 0);
+ int rightWeight = m_weightCache.value(rightPersistent, 0);
+
+
</code_context>
<issue_to_address>
Defaulting to weight 0 for missing cache entries may cause inconsistent sorting.
Assigning a default weight of 0 for missing cache entries can lead to incorrect ordering. Make sure the cache is complete for all indices or recalculate weights when missing.
</issue_to_address>
### Comment 3
<location> `src/models/searchfilterproxymodel.cpp:66` </location>
<code_context>
+ }
+
+ // 处理英文搜索的特殊情况
+ QRegularExpression searchEnglishCheck("^[a-zA-Z0-9\\s\\-\\.]+$");
+ bool isEnglishSearch = searchEnglishCheck.match(searchPattern).hasMatch();
+
+ if (isEnglishSearch) {
</code_context>
<issue_to_address>
Redundant redefinition of searchEnglishCheck regular expression.
Please reuse the existing searchEnglishCheck definition or move it to a shared scope within the function to avoid redundancy.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| static QString lastSearchPattern; | ||
| if (lastSearchPattern != searchPattern.pattern()) { |
There was a problem hiding this comment.
issue (bug_risk): Use of static variable for lastSearchPattern may cause issues in multi-instance scenarios.
A static lastSearchPattern will be shared across all SearchFilterProxyModel instances, which can cause incorrect cache clearing. Make it an instance member to avoid this issue.
| int leftWeight = m_weightCache.value(leftPersistent, 0); | ||
| int rightWeight = m_weightCache.value(rightPersistent, 0); |
There was a problem hiding this comment.
issue (bug_risk): Defaulting to weight 0 for missing cache entries may cause inconsistent sorting.
Assigning a default weight of 0 for missing cache entries can lead to incorrect ordering. Make sure the cache is complete for all indices or recalculate weights when missing.
| QRegularExpression searchEnglishCheck("^[a-zA-Z0-9\\s\\-\\.]+$"); | ||
| bool isEnglishSearch = searchEnglishCheck.match(searchPattern.pattern()).hasMatch(); | ||
|
|
||
| QRegularExpression searchHasLetterCheck("[a-zA-Z]"); | ||
| bool hasLetter = searchHasLetterCheck.match(searchPattern.pattern()).hasMatch(); | ||
|
|
||
| if (isNumberSearch || isEnglishSearch) { | ||
| bool hasMatch = false; | ||
| // Handle number prefix matching eg: 360zip | ||
| if (isNumberSearch) { | ||
| QRegularExpression numberRegex("\\d+"); | ||
| QRegularExpressionMatchIterator matches = numberRegex.globalMatch(targetName); | ||
|
|
||
| while (matches.hasNext()) { | ||
| QRegularExpressionMatch match = matches.next(); | ||
| QString numberInDisplayName = match.captured(0); | ||
| hasMatch = true; | ||
| if (numberInDisplayName.startsWith(searchPatternDelBlank)) { | ||
| return true; | ||
| } | ||
| // 计算匹配权重并存储到缓存 | ||
| int weight = calculateMatchWeight(searchPatternDelBlank, displayName, targetName, | ||
| transliterated, jianpin, nameFirstLetters); | ||
|
|
There was a problem hiding this comment.
nitpick: Redundant redefinition of searchEnglishCheck regular expression.
Please reuse the existing searchEnglishCheck definition or move it to a shared scope within the function to avoid redundancy.
fe2404c to
2e04686
Compare
|
TAG Bot New tag: 2.0.2 |
| QString nameFirstLettersLower = nameFirstLetters.toLower(); | ||
|
|
||
| // 使用 QMap 存储权重计算函数,QString 表示匹配类型 | ||
| QMap<QString, std::function<int()>> weightCalculators; |
2e04686 to
e926bfe
Compare
deepin pr auto review关键摘要:
是否建议立即修改:
|
| if (hasMatch && isNumberSearch) { | ||
| return false; | ||
| // 对于以英文单词开头的应用名,在英文搜索时降低优先级(增加索引值) | ||
| if (isEnglishSearch && matchIndex < 10) { |
There was a problem hiding this comment.
保证英文名在开头的匹配的时候, 能够让他优先级靠后 。小于10是防止在最后索引在匹配英语的时候 他的优先级发生变化
| if (isEnglishSearch && matchIndex < 10) { | ||
| QRegularExpression startsWithEnglishCheck("^[a-zA-Z][a-zA-Z0-9]*"); | ||
| if (startsWithEnglishCheck.match(displayName).hasMatch()) { | ||
| matchIndex += 1; // 增加索引值来降低优先级 |
There was a problem hiding this comment.
这个条件能不能合入到一个条件,变成一个组合条件,可以来依赖其它的子条件的,
| })); | ||
|
|
||
| // 计算匹配索引(索引越小优先级越高) | ||
| int matchIndex = -1; |
|
|
||
| #include <QDebug> | ||
| #include <DPinyin> | ||
| #include <cmath> |
| int matchIndex = calculateWeight(modelIndex); | ||
|
|
||
| // 如果索引为0,表示不匹配 | ||
| return matchIndex > 0; |
f7738fa to
672c396
Compare
as title. pms:task-377379
672c396 to
cb77de7
Compare
|
|
||
| const int matchIndex = std::distance(matchTypes.begin(), it); | ||
|
|
||
| // 返回索引值+1,确保返回值大于0(0表示不匹配) |
|
/forcemerge |
|
This pr force merged! (status: behind) |

as title.
pms:task-377379
Summary by Sourcery
Implement priority-based search display by calculating and caching a relevance weight per item and sorting results by weight then launch times.
New Features:
Enhancements: