From 8a58428a84e15c7e396503a34743d29eb4a25fa1 Mon Sep 17 00:00:00 2001 From: wjyrich Date: Fri, 4 Jul 2025 15:57:20 +0800 Subject: [PATCH] feat: add package name search configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Added new DConfig setting "searchPackage" to control package name search functionality 2. Implemented package name search in SearchFilterProxyModel when enabled 3. Added DConfig monitoring to dynamically update search behavior when setting changes 4. Package name search includes exact match, starts with, and contains matching 5. Added desktopId comparison in calculateWeight method for package name matching The changes allow users to enable/disable package name searching through DConfig, providing more flexible search options. This is particularly useful for technical users who may want to search applications by their package names. feat: 添加包名搜索配置功能 1. 新增 DConfig 设置项 "searchPackage" 用于控制包名搜索功能 2. 在 SearchFilterProxyModel 中实现包名搜索功能(当启用时) 3. 添加 DConfig 监听以动态更新搜索行为 4. 包名搜索支持精确匹配、开头匹配和包含匹配 5. 在 calculateWeight 方法中添加 desktopId 比较用于包名匹配 这些改动允许用户通过 DConfig 启用/禁用包名搜索功能,提供更灵活的搜索选 项。对于技术用户来说特别有用,他们可能希望通过包名来搜索应用程序。 --- src/models/org.deepin.ds.launchpad.json | 11 ++++++++++ src/models/searchfilterproxymodel.cpp | 27 ++++++++++++++++++++++++- src/models/searchfilterproxymodel.h | 7 +++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/models/org.deepin.ds.launchpad.json b/src/models/org.deepin.ds.launchpad.json index 0560b2b2..c7b19781 100644 --- a/src/models/org.deepin.ds.launchpad.json +++ b/src/models/org.deepin.ds.launchpad.json @@ -75,6 +75,17 @@ "description[zh_CN]": "在窗口模式下排序应用列表的类别。0: 首字母分组排序,1: DDE 风格分类,2: 自由排序", "permissions": "readwrite", "visibility": "private" + }, + "searchByDesktopId": { + "value": false, + "serial": 0, + "flags": [], + "name": "searchByDesktopId", + "name[zh_CN]": "按桌面条目 ID 搜索", + "description": "Whether to allow the search for Desktop ID during the search.", + "description[zh_CN]": "在搜索时候是否允许搜索Desktop ID。", + "permissions": "readwrite", + "visibility": "private" } } } diff --git a/src/models/searchfilterproxymodel.cpp b/src/models/searchfilterproxymodel.cpp index c2989f06..521b4a54 100644 --- a/src/models/searchfilterproxymodel.cpp +++ b/src/models/searchfilterproxymodel.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -14,11 +15,25 @@ DCORE_USE_NAMESPACE SearchFilterProxyModel::SearchFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) + , m_dconfig(DConfig::create("org.deepin.dde.shell", "org.deepin.ds.launchpad")) + , m_searchPackageEnabled(false) { setFilterCaseSensitivity(Qt::CaseInsensitive); setSourceModel(&AppsModel::instance()); sort(0, Qt::DescendingOrder); + + Q_ASSERT_X(m_dconfig->isValid(), "DConfig", "DConfig file is missing or invalid"); + + m_searchPackageEnabled = m_dconfig->value("searchByDesktopId", false).toBool(); + QObject::connect(m_dconfig, &DConfig::valueChanged, this, [this](const QString &key) { + if (key == "searchByDesktopId") { + m_searchPackageEnabled = m_dconfig->value("searchByDesktopId", false).toBool(); + qDebug() << "searchByDesktopId配置已更新:" << m_searchPackageEnabled; + // 触发重新过滤以应用新的搜索配置 + invalidateFilter(); + } + }); } bool SearchFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const @@ -29,7 +44,6 @@ bool SearchFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & // 计算匹配索引 int matchIndex = calculateWeight(modelIndex); - // 如果索引为0,表示不匹配 return matchIndex >= 0; } @@ -70,6 +84,9 @@ int SearchFilterProxyModel::calculateWeight(const QModelIndex &modelIndex) const const QString & transliterated = modelIndex.data(AppsModel::AllTransliteratedRole).toString(); const QString & jianpin = Dtk::Core::firstLetters(displayName).join(','); + //包名搜索使用 + const QString & desktopId = modelIndex.data(AppItem::DesktopIdRole).toString(); + QString searchPatternDelBlank = searchPattern.pattern().toLower().remove(" "); // Choose which name to use for matching based on search input and vendor @@ -105,6 +122,7 @@ int SearchFilterProxyModel::calculateWeight(const QModelIndex &modelIndex) const QString transliteratedLower = transliterated.toLower(); QString jianpinLower = jianpin.toLower(); QString nameFirstLettersLower = nameFirstLetters.toLower(); + QString desktopIdLower = desktopId.toLower().remove(" "); // 使用 QVector 存储匹配类型和对应的函数,按优先级顺序插入 QVector>> matchTypes; @@ -248,6 +266,13 @@ int SearchFilterProxyModel::calculateWeight(const QModelIndex &modelIndex) const return getCapitalizedWords().contains(searchPatternLower); })); + // 包名搜索(仅在配置启用时生效) + if (m_searchPackageEnabled) { + matchTypes.push_back(qMakePair(QString("desktopId"), [&]() -> bool { + return desktopIdLower.contains(searchPatternLower); + })); + } + // 计算匹配索引(索引越小优先级越高) auto it = std::find_if(matchTypes.begin(), matchTypes.end(), [](const auto& matchType) { return matchType.second(); }); diff --git a/src/models/searchfilterproxymodel.h b/src/models/searchfilterproxymodel.h index 4e3c3d0a..ff4cb8eb 100644 --- a/src/models/searchfilterproxymodel.h +++ b/src/models/searchfilterproxymodel.h @@ -7,6 +7,10 @@ #include #include +namespace Dtk::Core { +class DConfig; +} + class SearchFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT @@ -35,4 +39,7 @@ class SearchFilterProxyModel : public QSortFilterProxyModel explicit SearchFilterProxyModel(QObject *parent = nullptr); int calculateWeight(const QModelIndex &modelIndex) const; + + Dtk::Core::DConfig *m_dconfig; + bool m_searchPackageEnabled; };