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

This commit is contained in:
Yue-Lan 2024-04-25 10:59:18 +08:00 committed by iaom
parent 0ad53563e3
commit 6045517aa4
5 changed files with 51 additions and 19 deletions

View File

@ -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;

View File

@ -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();

View File

@ -176,20 +176,24 @@ Xapian::Query FileSearchWorker::creatQueryForFileSearch() {
}
std::vector<Xapian::Query> queries;
for (QString &keyword : m_searchController->getKeyword()) {
if (!keyword.isEmpty()) {
std::vector<Xapian::Query> 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<Xapian::Query> 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;
}
}
}
}

View File

@ -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);

View File

@ -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);