forked from openkylin/ukui-search
Merge branch '0818dev' into 'dev-unity'
fix the question that the keyword will be cut off in the resultPage of the file content search. See merge request kylin-desktop/ukui-search!99
This commit is contained in:
commit
87eccf6dc4
|
@ -773,3 +773,69 @@ void FileUtils::getTxtContent(QString &path, QString &textcontent) {
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
#include <QMap>
|
||||
#include <QDomDocument>
|
||||
#include <QQueue>
|
||||
#include <QApplication>
|
||||
#include <QFontMetrics>
|
||||
|
||||
#include <quazip/quazipfile.h>
|
||||
#include <stdio.h>
|
||||
|
@ -83,6 +85,7 @@ public:
|
|||
static void getXlsxTextContent(QString &path, QString &textcontent);
|
||||
static void getPdfTextContent(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 _current_index_count; //this one has been Abandoned,do not use it.
|
||||
static unsigned short _index_status;
|
||||
|
|
|
@ -338,22 +338,26 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
|
|||
// qWarning()<<QString::fromStdString(s);
|
||||
auto term = doc.termlist_begin();
|
||||
std::string wordTobeFound = QString::fromStdString(keyWord).section(" ", 0, 0).toStdString();
|
||||
int size = wordTobeFound.length();
|
||||
// int size = wordTobeFound.length();
|
||||
term.skip_to(wordTobeFound);
|
||||
int count = 0;
|
||||
for(auto pos = term.positionlist_begin(); pos != term.positionlist_end() && count < 6; ++pos) {
|
||||
std::string s = data.substr((*pos < 60) ? 0 : (*pos - 60), size + 120);
|
||||
QString snippet = QString::fromStdString(s);
|
||||
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;
|
||||
}
|
||||
auto pos = term.positionlist_begin();
|
||||
int length = 120;
|
||||
QString snippet = FileUtils::chineseSubString(data,*pos,length);
|
||||
// int count = 0;
|
||||
// for(auto pos = term.positionlist_begin(); pos != term.positionlist_end() && count < 6; ++pos) {
|
||||
// std::string s = data.substr((*pos < 60) ? 0 : (*pos - 60), size + 120);
|
||||
// QString snippet = QString::fromStdString(s);
|
||||
// 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);
|
||||
std::string().swap(data);
|
||||
|
||||
// for(QString i : QString::fromStdString(keyWord).split(" ",QString::SkipEmptyParts))
|
||||
|
|
Loading…
Reference in New Issue