From 524a43787b6b15ae62d2803e0d0c475fad83dfa3 Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Sun, 8 Oct 2023 16:48:24 +0800 Subject: [PATCH] =?UTF-8?q?perf(settingsearch):=E4=BF=AE=E6=94=B9=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=90=9C=E7=B4=A2=E7=9A=84=E5=8C=B9=E9=85=8D=E7=AD=96?= =?UTF-8?q?=E7=95=A5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../settingsearch/settings-search-plugin.cpp | 56 ++++++++++++++----- .../settingsearch/settings-search-plugin.h | 3 +- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/libsearch/settingsearch/settings-search-plugin.cpp b/libsearch/settingsearch/settings-search-plugin.cpp index 65c7419..70c6a9c 100644 --- a/libsearch/settingsearch/settings-search-plugin.cpp +++ b/libsearch/settingsearch/settings-search-plugin.cpp @@ -382,6 +382,7 @@ void SettingsMatch::startSearch(QString &keyword, size_t uniqueSymbol, DataQueue return; } //根据searchMap的对应关系匹配dataMap中的数据信息 + QStringList thirdLvKeys; for (auto i = m_searchMap.constBegin(); i != m_searchMap.constEnd(); ++i) { QMap regMatch = *i; QString moduleName = i.key(); @@ -389,14 +390,33 @@ void SettingsMatch::startSearch(QString &keyword, size_t uniqueSymbol, DataQueue //匹配二级菜单信息 QString pluginName = t.key(); QString keyOfSecondLv =QString("%1/%2").arg(moduleName, pluginName); - matchDataMap(keyOfSecondLv, keyword, uniqueSymbol, searchResult); + bool isSecondLv = matchDataMap(keyOfSecondLv, keyword); + //匹配对应三级菜单信息 - for (QString funcName: t.value()) { + for (const QString& funcName: t.value()) { QString keyOfThirdLv = keyOfSecondLv + QString("/%1").arg(funcName); - matchDataMap(keyOfThirdLv, keyword, uniqueSymbol, searchResult); + if (isSecondLv) { + SearchPluginIface::ResultInfo resultInfo; + resultInfo.type = 0; + createResultInfo(resultInfo, m_dataMap.value(keyOfThirdLv), keyOfThirdLv); + if (!enqueueResultInfo(resultInfo, uniqueSymbol, searchResult)) { + return; + } + } else if (matchDataMap(keyOfThirdLv, keyword)) { + thirdLvKeys.append(keyOfThirdLv); + } } } } + //单独处理三级 + for (const QString& key : thirdLvKeys) { + SearchPluginIface::ResultInfo resultInfo; + resultInfo.type = 0; + createResultInfo(resultInfo, m_dataMap.value(key), key); + if (!enqueueResultInfo(resultInfo, uniqueSymbol, searchResult)) { + return; + } + } } void SettingsMatch::addItem(QVariantMap replyMap) @@ -409,8 +429,9 @@ void SettingsMatch::removeItem(QVariantMap replyMap) this->parsingArgsOfDbus(replyMap, HandleType::Delete); } -void SettingsMatch::matchDataMap(QString &key, QString &keyword, size_t uniqueSymbol, DataQueue *searchResult) +bool SettingsMatch::matchDataMap(QString &key, QString &keyword) { + bool res(false); SearchPluginIface::ResultInfo resultInfo; resultInfo.type = 0; for (int counter = 0; counter < m_dataMap.value(key).size(); ++counter) { @@ -420,19 +441,11 @@ void SettingsMatch::matchDataMap(QString &key, QString &keyword, size_t uniqueSy continue; } if (data.contains(keyword, Qt::CaseInsensitive)) { - createResultInfo(resultInfo, m_dataMap.value(key), key); - //判断是否为同一次搜索 - SettingsSearchPlugin::m_mutex.lock(); - if (uniqueSymbol == SettingsSearchPlugin::m_uniqueSymbolForSettings) { - searchResult->enqueue(resultInfo); - SettingsSearchPlugin::m_mutex.unlock(); - break; - } else { - SettingsSearchPlugin::m_mutex.unlock(); - return; - } + res = true; + break; } } + return res; } void SettingsMatch::createResultInfo(SearchPluginIface::ResultInfo &resultInfo, const QStringList &itemInfo, const QString &path) @@ -448,3 +461,16 @@ void SettingsMatch::createResultInfo(SearchPluginIface::ResultInfo &resultInfo, resultInfo.icon = FileUtils::getSettingIcon(); resultInfo.actionKey = path.section("/", 1, 1); } + +bool SettingsMatch::enqueueResultInfo(SearchPluginIface::ResultInfo &resultInfo, size_t uniqueSymbol, DataQueue *searchResult) { + bool res(false); + SettingsSearchPlugin::m_mutex.lock(); + if (uniqueSymbol == SettingsSearchPlugin::m_uniqueSymbolForSettings) { + res = true; + searchResult->enqueue(resultInfo); + SettingsSearchPlugin::m_mutex.unlock(); + } else { + SettingsSearchPlugin::m_mutex.unlock(); + } + return res; +} diff --git a/libsearch/settingsearch/settings-search-plugin.h b/libsearch/settingsearch/settings-search-plugin.h index a4af278..61721ab 100644 --- a/libsearch/settingsearch/settings-search-plugin.h +++ b/libsearch/settingsearch/settings-search-plugin.h @@ -109,8 +109,9 @@ private: void matchNodes(QDomNode node); //search - void matchDataMap(QString &key, QString &keyword, size_t uniqueSymbol, DataQueue *searchResult); + bool matchDataMap(QString &key, QString &keyword); void createResultInfo(SearchPluginIface::ResultInfo &resultInfo, const QStringList &itemInfo, const QString &path); + bool enqueueResultInfo(SearchPluginIface::ResultInfo &resultInfo, size_t uniqueSymbol, DataQueue *searchResult); QMap> m_searchMap; QMap m_dataMap; QDBusInterface *m_interface = nullptr;