UkuiSearchTask增加一个setMatchAllIfNoKeyword()方法,如果设置为true则在未添加关键词时匹配所有文件名并返回

This commit is contained in:
Yue-Lan 2024-04-25 10:59:18 +08:00 committed by iaom
parent 39709ebc3a
commit 67ebd40a70
5 changed files with 51 additions and 19 deletions

View File

@ -36,6 +36,7 @@ public:
QStringList m_searchDirs; QStringList m_searchDirs;
QStringList m_FileLabels; QStringList m_FileLabels;
bool m_recurse = true; bool m_recurse = true;
bool m_matchAllIfNoKeyword = false;
bool m_activeKeywordSegmentation = false; bool m_activeKeywordSegmentation = false;
bool m_onlySearchFile = false; bool m_onlySearchFile = false;
bool m_onlySearchDir = false; bool m_onlySearchDir = false;
@ -127,6 +128,11 @@ void SearchController::addKeyword(const QString &keyword)
d->m_keywords.append(keyword); d->m_keywords.append(keyword);
} }
void SearchController::setMatchAllIfNoKeyword(bool matchAll)
{
d->m_matchAllIfNoKeyword = matchAll;
}
size_t SearchController::getCurrentSearchId() size_t SearchController::getCurrentSearchId()
{ {
d->m_searchIdMutex.lock(); d->m_searchIdMutex.lock();
@ -208,6 +214,11 @@ QStringList SearchController::getKeyword()
return d->m_keywords; return d->m_keywords;
} }
bool SearchController::getMatchAllIfNoKeyword()
{
return d->m_matchAllIfNoKeyword;
}
bool SearchController::isKeywordSegmentationActived() bool SearchController::isKeywordSegmentationActived()
{ {
return d->m_activeKeywordSegmentation; return d->m_activeKeywordSegmentation;

View File

@ -47,6 +47,7 @@ public:
void addSearchDir(const QString &path); void addSearchDir(const QString &path);
void setRecurse(bool recurse = true); void setRecurse(bool recurse = true);
void addKeyword(const QString &keyword); void addKeyword(const QString &keyword);
void setMatchAllIfNoKeyword(bool matchAll);
void setActiveKeywordSegmentation(bool active); void setActiveKeywordSegmentation(bool active);
void addFileLabel(const QString &label); void addFileLabel(const QString &label);
void setOnlySearchFile(bool onlySearchFile); void setOnlySearchFile(bool onlySearchFile);
@ -68,6 +69,7 @@ public:
QStringList getSearchDir(); QStringList getSearchDir();
bool isRecurse(); bool isRecurse();
QStringList getKeyword(); QStringList getKeyword();
bool getMatchAllIfNoKeyword();
bool isKeywordSegmentationActived(); bool isKeywordSegmentationActived();
QStringList getFileLabel(); QStringList getFileLabel();
bool isSearchFileOnly(); bool isSearchFileOnly();

View File

@ -176,20 +176,24 @@ Xapian::Query FileSearchWorker::creatQueryForFileSearch() {
} }
std::vector<Xapian::Query> queries; std::vector<Xapian::Query> queries;
for (QString &keyword : m_searchController->getKeyword()) { if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) {
if (!keyword.isEmpty()) { queries.emplace_back(Xapian::Query::MatchAll);
std::vector<Xapian::Query> queryOfKeyword; } else {
QString inputKeyWord = keyword.toLower(); for (QString &keyword : m_searchController->getKeyword()) {
QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, inputKeyWord.toLower()); if (!keyword.isEmpty()) {
int start = 0; std::vector<Xapian::Query> queryOfKeyword;
for(; bf.position() != -1; bf.toNextBoundary()) { QString inputKeyWord = keyword.toLower();
int end = bf.position(); QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, inputKeyWord.toLower());
if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) { int start = 0;
queryOfKeyword.emplace_back(QUrl::toPercentEncoding(inputKeyWord.toLower().mid(start, end - start)).toStdString()); 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; bool matched = false;
//同时包含几个key为成功匹配 if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) {
for (const QString &keyword: m_searchController->getKeyword()) { matched = true;
if (!keyword.isEmpty()) { } else {
//TODO 修改匹配方式,对结果进行排序 //同时包含几个key为成功匹配
if (fileInfo.fileName().contains(keyword, Qt::CaseInsensitive)) { for (const QString &keyword: m_searchController->getKeyword()) {
matched = true; if (!keyword.isEmpty()) {
break; //TODO 修改匹配方式,对结果进行排序
if (fileInfo.fileName().contains(keyword, Qt::CaseInsensitive)) {
matched = true;
break;
}
} }
} }
} }

View File

@ -63,6 +63,11 @@ void UkuiSearchTask::addKeyword(const QString &keyword)
d->m_searchCotroller.addKeyword(keyword); d->m_searchCotroller.addKeyword(keyword);
} }
void UkuiSearchTask::setMatchAllIfNoKeyword(bool matchAll)
{
d->m_searchCotroller.setMatchAllIfNoKeyword(matchAll);
}
void UkuiSearchTask::addFileLabel(const QString &label) void UkuiSearchTask::addFileLabel(const QString &label)
{ {
d->m_searchCotroller.addFileLabel(label); d->m_searchCotroller.addFileLabel(label);

View File

@ -35,6 +35,12 @@ public:
void addSearchDir(const QString &path); void addSearchDir(const QString &path);
void setRecurse(bool recurse = true); void setRecurse(bool recurse = true);
void addKeyword(const QString &keyword); void addKeyword(const QString &keyword);
/**
*
* @brief setMatchAllIfNoKeyword
* @param matchAll
*/
void setMatchAllIfNoKeyword(bool matchAll);
void addFileLabel(const QString &label); void addFileLabel(const QString &label);
void setOnlySearchFile(bool onlySearchFile); void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir); void setOnlySearchDir(bool onlySearchDir);