fix the question that the keyword will be cut off in the resultPage of the file content search.

This commit is contained in:
baijunjie 2021-08-20 10:10:46 +08:00
parent 7e942b1f43
commit a7e296ca0c
3 changed files with 88 additions and 15 deletions

View File

@ -773,3 +773,69 @@ void FileUtils::getTxtContent(QString &path, QString &textcontent) {
return; return;
} }
QString FileUtils::chineseSubString(const std::string &data, int start, int length)
{
std::string afterSub = "";
//越界保护
if(start < 0 || length < 0){
return " ";
}
if (length >= data.length()) {
return QString::fromStdString(data);
}
QString snippet = "";
QFont ft(QApplication::font().family(),QApplication::font().pointSize());
QFontMetrics fm (ft);
if (start + length <= data.length()) {
afterSub = data.substr(start,length); //截取
snippet = QString::fromStdString(afterSub); //转QString
if(start + length < data.length()){
snippet.replace(snippet.length()-3,3,"..."); //替换后三位
}
snippet = fm.elidedText(snippet, Qt::ElideRight, 2*366); //超过两行则省略
} else {
int newStart = data.length()-length; //更新截取位置
int bodyByteCount = 0;
int letterCount = 0;
for(int i = newStart;i > 0;i--){
if(data[i] & 0x80) {
char tmp = data[i] << 1;
if(tmp & 0x80){
break;
} else {
bodyByteCount++;
}
} else {
letterCount++;
if(letterCount == 3) {
break;
}
}
}
int correctionValue = letterCount + (bodyByteCount);
afterSub = data.substr(newStart - correctionValue, length + correctionValue);
snippet=QString::fromStdString(afterSub);
switch(letterCount)
{
case 0:
snippet.replace(0,1,".").prepend("..");
break;
case 1:
snippet.replace(0,2,"..").prepend(".");
break;
default:
snippet.replace(0,3,"...");
}
snippet = fm.elidedText(snippet, Qt::ElideLeft, 2*500);
}
return snippet;
}

View File

@ -35,6 +35,8 @@
#include <QMap> #include <QMap>
#include <QDomDocument> #include <QDomDocument>
#include <QQueue> #include <QQueue>
#include <QApplication>
#include <QFontMetrics>
#include <quazip/quazipfile.h> #include <quazip/quazipfile.h>
#include <stdio.h> #include <stdio.h>
@ -83,6 +85,7 @@ public:
static void getXlsxTextContent(QString &path, QString &textcontent); static void getXlsxTextContent(QString &path, QString &textcontent);
static void getPdfTextContent(QString &path, QString &textcontent); static void getPdfTextContent(QString &path, QString &textcontent);
static void getTxtContent(QString &path, QString &textcontent); static void getTxtContent(QString &path, QString &textcontent);
static QString chineseSubString(const std::string &data, int start, int length);
static size_t _max_index_count; static size_t _max_index_count;
static size_t _current_index_count; //this one has been Abandoned,do not use it. static size_t _current_index_count; //this one has been Abandoned,do not use it.
static unsigned short _index_status; static unsigned short _index_status;

View File

@ -338,22 +338,26 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
// qWarning()<<QString::fromStdString(s); // qWarning()<<QString::fromStdString(s);
auto term = doc.termlist_begin(); auto term = doc.termlist_begin();
std::string wordTobeFound = QString::fromStdString(keyWord).section(" ", 0, 0).toStdString(); std::string wordTobeFound = QString::fromStdString(keyWord).section(" ", 0, 0).toStdString();
int size = wordTobeFound.length(); // int size = wordTobeFound.length();
term.skip_to(wordTobeFound); term.skip_to(wordTobeFound);
int count = 0; auto pos = term.positionlist_begin();
for(auto pos = term.positionlist_begin(); pos != term.positionlist_end() && count < 6; ++pos) { int length = 120;
std::string s = data.substr((*pos < 60) ? 0 : (*pos - 60), size + 120); QString snippet = FileUtils::chineseSubString(data,*pos,length);
QString snippet = QString::fromStdString(s); // int count = 0;
if(snippet.size() > 6 + QString::fromStdString(keyWord).size()) { // for(auto pos = term.positionlist_begin(); pos != term.positionlist_end() && count < 6; ++pos) {
snippet.replace(0, 3, "...").replace(snippet.size() - 3, 3, "..."); // std::string s = data.substr((*pos < 60) ? 0 : (*pos - 60), size + 120);
} else { // QString snippet = QString::fromStdString(s);
snippet.append("...").prepend("..."); // if(snippet.size() > 6 + QString::fromStdString(keyWord).size()) {
} // snippet.replace(0, 3, "...").replace(snippet.size() - 3, 3, "...");
// } else {
// snippet.append("...").prepend("...");
// }
// snippets.append(snippet);
// QString().swap(snippet);
// std::string().swap(s);
// ++count;
// }
snippets.append(snippet); snippets.append(snippet);
QString().swap(snippet);
std::string().swap(s);
++count;
}
std::string().swap(data); std::string().swap(data);
// for(QString i : QString::fromStdString(keyWord).split(" ",QString::SkipEmptyParts)) // for(QString i : QString::fromStdString(keyWord).split(" ",QString::SkipEmptyParts))