From 67ebd40a705bf5d3c1717443f498459314c852a3 Mon Sep 17 00:00:00 2001 From: Yue-Lan Date: Thu, 25 Apr 2024 10:59:18 +0800 Subject: [PATCH] =?UTF-8?q?UkuiSearchTask=E5=A2=9E=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AAsetMatchAllIfNoKeyword()=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E8=AE=BE=E7=BD=AE=E4=B8=BAtrue=E5=88=99?= =?UTF-8?q?=E5=9C=A8=E6=9C=AA=E6=B7=BB=E5=8A=A0=E5=85=B3=E9=94=AE=E8=AF=8D?= =?UTF-8?q?=E6=97=B6=E5=8C=B9=E9=85=8D=E6=89=80=E6=9C=89=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E5=B9=B6=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../searchinterface/search-controller.cpp | 11 +++++ libsearch/searchinterface/search-controller.h | 2 + .../searchtasks/file-search-task.cpp | 46 +++++++++++-------- .../searchinterface/ukui-search-task.cpp | 5 ++ libsearch/searchinterface/ukui-search-task.h | 6 +++ 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/libsearch/searchinterface/search-controller.cpp b/libsearch/searchinterface/search-controller.cpp index 6851eda..b91042a 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -36,6 +36,7 @@ public: QStringList m_searchDirs; QStringList m_FileLabels; bool m_recurse = true; + bool m_matchAllIfNoKeyword = false; bool m_activeKeywordSegmentation = false; bool m_onlySearchFile = false; bool m_onlySearchDir = false; @@ -127,6 +128,11 @@ void SearchController::addKeyword(const QString &keyword) d->m_keywords.append(keyword); } +void SearchController::setMatchAllIfNoKeyword(bool matchAll) +{ + d->m_matchAllIfNoKeyword = matchAll; +} + size_t SearchController::getCurrentSearchId() { d->m_searchIdMutex.lock(); @@ -208,6 +214,11 @@ QStringList SearchController::getKeyword() return d->m_keywords; } +bool SearchController::getMatchAllIfNoKeyword() +{ + return d->m_matchAllIfNoKeyword; +} + bool SearchController::isKeywordSegmentationActived() { return d->m_activeKeywordSegmentation; diff --git a/libsearch/searchinterface/search-controller.h b/libsearch/searchinterface/search-controller.h index 64e4d37..719073f 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -47,6 +47,7 @@ public: void addSearchDir(const QString &path); void setRecurse(bool recurse = true); void addKeyword(const QString &keyword); + void setMatchAllIfNoKeyword(bool matchAll); void setActiveKeywordSegmentation(bool active); void addFileLabel(const QString &label); void setOnlySearchFile(bool onlySearchFile); @@ -68,6 +69,7 @@ public: QStringList getSearchDir(); bool isRecurse(); QStringList getKeyword(); + bool getMatchAllIfNoKeyword(); bool isKeywordSegmentationActived(); QStringList getFileLabel(); bool isSearchFileOnly(); diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index a17e11f..af8bfdf 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -176,20 +176,24 @@ Xapian::Query FileSearchWorker::creatQueryForFileSearch() { } std::vector queries; - for (QString &keyword : m_searchController->getKeyword()) { - if (!keyword.isEmpty()) { - std::vector queryOfKeyword; - QString inputKeyWord = keyword.toLower(); - QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, inputKeyWord.toLower()); - int start = 0; - for(; bf.position() != -1; bf.toNextBoundary()) { - int end = bf.position(); - if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) { - queryOfKeyword.emplace_back(QUrl::toPercentEncoding(inputKeyWord.toLower().mid(start, end - start)).toStdString()); + if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) { + queries.emplace_back(Xapian::Query::MatchAll); + } else { + for (QString &keyword : m_searchController->getKeyword()) { + if (!keyword.isEmpty()) { + std::vector queryOfKeyword; + QString inputKeyWord = keyword.toLower(); + QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, inputKeyWord.toLower()); + int start = 0; + for(; bf.position() != -1; bf.toNextBoundary()) { + int end = bf.position(); + if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) { + queryOfKeyword.emplace_back(QUrl::toPercentEncoding(inputKeyWord.toLower().mid(start, end - start)).toStdString()); + } + start = end; } - start = end; + queries.emplace_back(Xapian::Query::OP_PHRASE, queryOfKeyword.begin(), queryOfKeyword.end()); } - queries.emplace_back(Xapian::Query::OP_PHRASE, queryOfKeyword.begin(), queryOfKeyword.end()); } } @@ -303,13 +307,17 @@ bool FileSearchWorker::directSearch() } bool matched = false; - //同时包含几个key为成功匹配 - for (const QString &keyword: m_searchController->getKeyword()) { - if (!keyword.isEmpty()) { - //TODO 修改匹配方式,对结果进行排序 - if (fileInfo.fileName().contains(keyword, Qt::CaseInsensitive)) { - matched = true; - break; + if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) { + matched = true; + } else { + //同时包含几个key为成功匹配 + for (const QString &keyword: m_searchController->getKeyword()) { + if (!keyword.isEmpty()) { + //TODO 修改匹配方式,对结果进行排序 + if (fileInfo.fileName().contains(keyword, Qt::CaseInsensitive)) { + matched = true; + break; + } } } } diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index de15d97..642461e 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -63,6 +63,11 @@ void UkuiSearchTask::addKeyword(const QString &keyword) d->m_searchCotroller.addKeyword(keyword); } +void UkuiSearchTask::setMatchAllIfNoKeyword(bool matchAll) +{ + d->m_searchCotroller.setMatchAllIfNoKeyword(matchAll); +} + void UkuiSearchTask::addFileLabel(const QString &label) { d->m_searchCotroller.addFileLabel(label); diff --git a/libsearch/searchinterface/ukui-search-task.h b/libsearch/searchinterface/ukui-search-task.h index b45eb42..bcbeb29 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -35,6 +35,12 @@ public: void addSearchDir(const QString &path); void setRecurse(bool recurse = true); void addKeyword(const QString &keyword); + /** + * 是否在未添加关键词的情况下进行所有文件名匹配 + * @brief setMatchAllIfNoKeyword + * @param matchAll + */ + void setMatchAllIfNoKeyword(bool matchAll); void addFileLabel(const QString &label); void setOnlySearchFile(bool onlySearchFile); void setOnlySearchDir(bool onlySearchDir);