Fix the problem that the right side of the content search's detail page was blocked.
This commit is contained in:
parent
2c1cce5309
commit
9d62a24662
|
@ -8,7 +8,7 @@ namespace UkuiSearch {
|
|||
#define CONTENT_DATABASE_PATH_SLOT 1
|
||||
#define CONTENT_DATABASE_SUFFIX_SLOT 2
|
||||
|
||||
static const int LABEL_MAX_WIDTH = 300;
|
||||
static const int LABEL_MAX_WIDTH = 320;
|
||||
|
||||
static const QString HOME_PATH = QDir::homePath();
|
||||
static const QString INDEX_PATH = HOME_PATH + QStringLiteral("/.config/org.ukui/ukui-search/index_data");
|
||||
|
|
|
@ -782,48 +782,44 @@ QString FileUtils::escapeHtml(const QString &str)
|
|||
return temp;
|
||||
}
|
||||
|
||||
QString FileUtils::chineseSubString(const std::string &myStr, uint start, uint length)
|
||||
QString FileUtils::chineseSubString(const std::string &myStr, uint start, uint length, const QString &keyword)
|
||||
{
|
||||
std::string afterSub = "";
|
||||
QString sub = QString::fromStdString(myStr);
|
||||
QFont ft(QApplication::font().family(),QApplication::font().pointSize());
|
||||
QFontMetrics fm (ft);
|
||||
|
||||
if (length >= myStr.length()) {
|
||||
afterSub = myStr.substr(start,length); //截取;
|
||||
if (fm.horizontalAdvance(QString::fromStdString(afterSub)) >= 2*LABEL_MAX_WIDTH) {
|
||||
sub = fm.elidedText(sub, Qt::ElideRight, 2*LABEL_MAX_WIDTH); //超过两行则省略
|
||||
} else {
|
||||
sub = fm.elidedText(sub, Qt::ElideLeft, 2*LABEL_MAX_WIDTH); //超过两行则省略
|
||||
if (horizontalAdvanceContainsKeyword(QString::fromStdString(afterSub), keyword) >= 2*LABEL_MAX_WIDTH) {
|
||||
sub = QString::fromStdString(afterSub);
|
||||
}
|
||||
return sub;
|
||||
return wrapData(sub, keyword);
|
||||
}
|
||||
|
||||
//从关键字截length个字,文本内容长度够截
|
||||
if (start + length <= myStr.length()) {
|
||||
afterSub = myStr.substr(start,length); //截取
|
||||
sub = QString::fromStdString(afterSub); //转QString
|
||||
|
||||
if(start + length < myStr.length()){
|
||||
sub.replace(sub.length()-3,3,"…"); //替换后三位
|
||||
} else{
|
||||
sub.append("…"); //直接加
|
||||
sub.replace(sub.length() - 1, 1, "…"); //最后一位可能为乱码,替换掉
|
||||
}
|
||||
sub = fm.elidedText(sub, Qt::ElideRight, 2*LABEL_MAX_WIDTH); //超过两行则省略
|
||||
sub = wrapData(sub, keyword);
|
||||
} else {
|
||||
uint newStart = myStr.length()-length; //更新截取位置
|
||||
|
||||
uint newStart = myStr.length() - length; //从start截到末尾长度不够length,更新截取位置到末尾前length个字的位置
|
||||
afterSub = myStr.substr(newStart, length);
|
||||
sub=QString::fromStdString(afterSub);
|
||||
if (fm.horizontalAdvance(QString::fromStdString(myStr.substr(newStart, start))) >= 2*LABEL_MAX_WIDTH) {
|
||||
sub = fm.elidedText(sub, Qt::ElideLeft, 2*LABEL_MAX_WIDTH);
|
||||
sub = QString::fromStdString(afterSub);
|
||||
|
||||
if (horizontalAdvanceContainsKeyword(QString::fromStdString(myStr.substr(newStart, start)), keyword) >= 2*LABEL_MAX_WIDTH) {
|
||||
sub = wrapData(sub.replace(0, 1, "…"), keyword, true);
|
||||
} else {
|
||||
if (newStart + 3 < start) {
|
||||
sub.replace(0,3,"…").append("…");
|
||||
sub.replace(0, 1, "…")/*.append("…")*/; //第一个字有可能乱码,直接替换
|
||||
} else {
|
||||
afterSub = myStr.substr(start, length);
|
||||
afterSub = myStr.substr(start, length); //需要往前补三位以内说明:补的全是乱码,直接从start截就完了
|
||||
sub = "…" + QString::fromStdString(afterSub);
|
||||
sub.append("…");
|
||||
// sub.append("…");
|
||||
}
|
||||
sub = fm.elidedText(sub, Qt::ElideRight, 2*LABEL_MAX_WIDTH);
|
||||
sub = wrapData(sub, keyword);
|
||||
}
|
||||
}
|
||||
return sub;
|
||||
|
@ -929,19 +925,24 @@ bool FileUtils::isOcrSupportSize(QString path)
|
|||
|
||||
QString FileUtils::getHtmlText(const QString &text, const QString &keyword)
|
||||
{
|
||||
QString htmlString;
|
||||
QString htmlString = QString("<style>"
|
||||
"span {"
|
||||
"font-size:%0pt;"
|
||||
"font-weight:bold;"
|
||||
"}"
|
||||
"</style>").arg(qApp->font().pointSizeF() + 2);
|
||||
bool boldOpenned = false;
|
||||
for(int i = 0; i < text.length(); i++) {
|
||||
if((keyword.toUpper()).contains(QString(text.at(i)).toUpper())) {
|
||||
if(! boldOpenned) {
|
||||
boldOpenned = true;
|
||||
htmlString.append(QString("<b><font size=\"4\">"));
|
||||
htmlString.append(QString("<span>"));
|
||||
}
|
||||
htmlString.append(FileUtils::escapeHtml(QString(text.at(i))));
|
||||
} else {
|
||||
if(boldOpenned) {
|
||||
boldOpenned = false;
|
||||
htmlString.append(QString("</font></b>"));
|
||||
htmlString.append(QString("</span>"));
|
||||
}
|
||||
htmlString.append(FileUtils::escapeHtml(QString(text.at(i))));
|
||||
}
|
||||
|
@ -989,6 +990,213 @@ QString FileUtils::wrapData(QLabel *p_label, const QString &text)
|
|||
return wrapText;
|
||||
}
|
||||
|
||||
QString FileUtils::wrapData(const QString &text, const QString &keyword, bool elideLeft)
|
||||
{
|
||||
QString wrapText = text;
|
||||
|
||||
QFont boldFont(qApp->font().family());
|
||||
boldFont.setPointSizeF(qApp->font().pointSizeF() + 2);
|
||||
boldFont.setWeight(QFont::Bold);
|
||||
QFontMetricsF boldMetricsF(boldFont);
|
||||
|
||||
QFont font(qApp->font().family());
|
||||
font.setPointSizeF(qApp->font().pointSizeF());
|
||||
QFontMetricsF fontMetricsF(font);
|
||||
qreal blockLength = 0;
|
||||
qreal total = 0;
|
||||
int lineCount = 0;
|
||||
int normalLength = 0;
|
||||
int boldLength = 0;
|
||||
|
||||
if (elideLeft) {
|
||||
for (int i = text.length() - 1; i >= 0; i--) {
|
||||
if (keyword.toUpper().contains(text.at(i).toUpper())) {
|
||||
if (normalLength) {
|
||||
total += fontMetricsF.horizontalAdvance(text.mid(i + 1, normalLength));
|
||||
normalLength = 0;
|
||||
blockLength = 0;
|
||||
}
|
||||
if (boldLength) {
|
||||
blockLength = boldMetricsF.horizontalAdvance(text.mid(i + 1, boldLength));
|
||||
}
|
||||
boldLength++;
|
||||
} else {
|
||||
if (boldLength) {
|
||||
total += boldMetricsF.horizontalAdvance(text.mid(i + 1, boldLength));
|
||||
boldLength = 0;
|
||||
blockLength = 0;
|
||||
}
|
||||
|
||||
if (normalLength) {
|
||||
blockLength = fontMetricsF.horizontalAdvance(text.mid(i + 1, normalLength));
|
||||
}
|
||||
normalLength++;
|
||||
}
|
||||
|
||||
if (!i) {
|
||||
if (normalLength) {
|
||||
blockLength = fontMetricsF.horizontalAdvance(text.left(normalLength));
|
||||
}
|
||||
if (boldLength) {
|
||||
blockLength = boldMetricsF.horizontalAdvance(text.left(boldLength));
|
||||
}
|
||||
}
|
||||
|
||||
if (total + blockLength >= LABEL_MAX_WIDTH) {
|
||||
i++;
|
||||
if (total + blockLength > LABEL_MAX_WIDTH) {
|
||||
if (normalLength) {
|
||||
normalLength = 1;
|
||||
} else {
|
||||
boldLength = 1;
|
||||
}
|
||||
} else {
|
||||
normalLength = 0;
|
||||
boldLength = 0;
|
||||
}
|
||||
wrapText.insert(i + 1, '\n');
|
||||
lineCount++;
|
||||
total = 0;
|
||||
blockLength = 0;
|
||||
}
|
||||
|
||||
if (lineCount == 2) {
|
||||
QString leftWord = text.left(i + 1);
|
||||
if (!leftWord.isEmpty()) {
|
||||
qreal distance = 2;//2是换行符加上要换第一个字
|
||||
qreal wordSize = 0;
|
||||
for (int index = i + 1; index < text.length(); index++) {
|
||||
wordSize += keyword.toUpper().contains(text.at(index).toUpper()) ?
|
||||
boldMetricsF.horizontalAdvance(text.at(index)) : fontMetricsF.horizontalAdvance(text.at(index));
|
||||
if (wordSize < fontMetricsF.horizontalAdvance("…")) {
|
||||
distance++;//字长度比省略号要小,可能会挡上,所以再截一个字
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
wrapText = wrapText.right(wrapText.size() - leftWord.size() - distance);
|
||||
wrapText.prepend("…");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i <= text.length(); i++) {
|
||||
if (i == text.length()) {
|
||||
if (normalLength) {
|
||||
blockLength = fontMetricsF.horizontalAdvance(text.right(normalLength));
|
||||
}
|
||||
if (boldLength) {
|
||||
blockLength = boldMetricsF.horizontalAdvance(text.right(boldLength));
|
||||
}
|
||||
} else {
|
||||
if (keyword.toUpper().contains(text.at(i).toUpper())) {
|
||||
if (normalLength) {
|
||||
total += fontMetricsF.horizontalAdvance(text.mid(i - normalLength, normalLength));
|
||||
normalLength = 0;
|
||||
blockLength = 0;
|
||||
}
|
||||
if (boldLength) {
|
||||
blockLength = boldMetricsF.horizontalAdvance(text.mid(i - boldLength, boldLength));
|
||||
}
|
||||
boldLength++;
|
||||
} else {
|
||||
if (boldLength) {
|
||||
total += boldMetricsF.horizontalAdvance(text.mid(i - boldLength, boldLength));
|
||||
boldLength = 0;
|
||||
blockLength = 0;
|
||||
}
|
||||
|
||||
if (normalLength) {
|
||||
blockLength = fontMetricsF.horizontalAdvance(text.mid(i - normalLength, normalLength));
|
||||
}
|
||||
normalLength++;
|
||||
}
|
||||
}
|
||||
|
||||
if (total + blockLength >= LABEL_MAX_WIDTH) {
|
||||
i--;
|
||||
if (total + blockLength > LABEL_MAX_WIDTH) {
|
||||
wrapText.insert(i + lineCount, '\n');
|
||||
if (normalLength) {
|
||||
normalLength = 1;
|
||||
} else {
|
||||
boldLength = 1;
|
||||
}
|
||||
} else {
|
||||
wrapText.insert(i + 1 + lineCount, '\n');
|
||||
normalLength = 0;
|
||||
boldLength = 0;
|
||||
}
|
||||
|
||||
lineCount++;
|
||||
total = 0;
|
||||
blockLength = 0;
|
||||
}
|
||||
|
||||
if (lineCount == 2) {
|
||||
QString leftWord = text.mid(i);
|
||||
if (!leftWord.isEmpty()) {
|
||||
qreal distance = 2;
|
||||
qreal wordSize = 0;
|
||||
for (int index = i; index > 0; index--) {
|
||||
wordSize += keyword.toUpper().contains(text.at(index).toUpper()) ?
|
||||
boldMetricsF.horizontalAdvance(text.at(index)) : fontMetricsF.horizontalAdvance(text.at(index));
|
||||
if (wordSize < fontMetricsF.horizontalAdvance("…")) {
|
||||
distance++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
wrapText = wrapText.left(wrapText.size() - leftWord.size() - distance);
|
||||
wrapText.append("…");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return wrapText;
|
||||
}
|
||||
|
||||
qreal FileUtils::horizontalAdvanceContainsKeyword(const QString &content, const QString &keyword)
|
||||
{
|
||||
QFont boldFont(qApp->font().family());
|
||||
boldFont.setPointSizeF(qApp->font().pointSizeF() + 2);
|
||||
boldFont.setWeight(QFont::Bold);
|
||||
QFontMetricsF boldMetricsF(boldFont);
|
||||
|
||||
QFont font(qApp->font().family());
|
||||
font.setPointSizeF(qApp->font().pointSizeF());
|
||||
QFontMetricsF fontMetricsF(font);
|
||||
qreal contentSize = 0;
|
||||
int boldLength = 0;
|
||||
int normalLength = 0;
|
||||
|
||||
for (int i = 0; i < content.length(); i++) {
|
||||
if (keyword.toUpper().contains(content.at(i).toUpper())) {
|
||||
boldLength++;
|
||||
if (normalLength) {
|
||||
contentSize += boldMetricsF.horizontalAdvance(content.mid(i - normalLength, normalLength));
|
||||
normalLength = 0;
|
||||
}
|
||||
} else {
|
||||
normalLength++;
|
||||
if (boldLength) {
|
||||
contentSize += boldMetricsF.horizontalAdvance(content.mid(i - boldLength, boldLength));
|
||||
boldLength = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (boldLength) {
|
||||
contentSize += boldMetricsF.horizontalAdvance(content.right(boldLength));
|
||||
}
|
||||
if (normalLength) {
|
||||
contentSize += fontMetricsF.horizontalAdvance(content.right(normalLength));
|
||||
}
|
||||
return contentSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* uof1.0解析
|
||||
* 参考规范:GB/T 20916-2007
|
||||
|
|
|
@ -34,6 +34,8 @@ public:
|
|||
static QString getHtmlText(const QString &text, const QString &keyword);
|
||||
static QString setAllTextBold(const QString &name);
|
||||
static QString wrapData(QLabel *p_label, const QString &text);
|
||||
static QString wrapData(const QString &text, const QString &keyword, bool elideLeft = false);
|
||||
static qreal horizontalAdvanceContainsKeyword(const QString &content, const QString &keyword);
|
||||
static std::string makeDocUterm(QString path);
|
||||
static QIcon getFileIcon(const QString &uri, bool checkValid = true);
|
||||
static QIcon getAppIcon(const QString &path);
|
||||
|
@ -62,7 +64,7 @@ public:
|
|||
static int openFile(QString &path, bool openInDir = false);
|
||||
static bool copyPath(QString &path);
|
||||
static QString escapeHtml(const QString &str);
|
||||
static QString chineseSubString(const std::string &myStr, uint start, uint length);
|
||||
static QString chineseSubString(const std::string &myStr, uint start, uint length, const QString &keyword);
|
||||
static QIcon iconFromTheme(const QString &name, const QIcon &iconDefault);
|
||||
static bool isOpenXMLFileEncrypted(const QString &path);
|
||||
/**
|
||||
|
|
|
@ -534,7 +534,7 @@ QWidget *FileContengSearchPlugin::detailPage(const ResultInfo &ri)
|
|||
} else {
|
||||
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
|
||||
m_pluginLabel->setText(tr("File"));
|
||||
m_snippetLabel->setText(getHtmlText(wrapData(m_snippetLabel,ri.description.at(0).value), m_keyWord));
|
||||
m_snippetLabel->setText(ri.description.at(0).value);
|
||||
m_snippetLabel->show();
|
||||
m_detailLyt->setContentsMargins(8, 50, 16, 0);
|
||||
}
|
||||
|
|
|
@ -350,9 +350,9 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
|
|||
}
|
||||
}
|
||||
auto pos = termIterator.positionlist_begin();
|
||||
QString snippet = FileUtils::chineseSubString(data,*pos,120);
|
||||
QString snippet = FileUtils::chineseSubString(data, *pos, 120, QString::fromStdString(keyWord).remove(" "));
|
||||
|
||||
ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet});
|
||||
ri.description.prepend(SearchPluginIface::DescriptionInfo{"",FileUtils::getHtmlText(snippet, QString::fromStdString(keyWord).remove(" "))});
|
||||
QString().swap(snippet);
|
||||
std::string().swap(data);
|
||||
|
||||
|
@ -467,9 +467,9 @@ int OcrSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
|
|||
term.skip_to(wordTobeFound);
|
||||
//fix me: make a snippet without cut cjk char.
|
||||
auto pos = term.positionlist_begin();
|
||||
QString snippet = FileUtils::chineseSubString(data,*pos,120);
|
||||
QString snippet = FileUtils::chineseSubString(data, *pos, 120, QString::fromStdString(keyWord).remove(" "));
|
||||
|
||||
ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet});
|
||||
ri.description.prepend(SearchPluginIface::DescriptionInfo{"", FileUtils::getHtmlText(snippet, QString::fromStdString(keyWord).remove(" "))});
|
||||
QString().swap(snippet);
|
||||
std::string().swap(data);
|
||||
SearchManager::m_mutexOcr.lock();
|
||||
|
|
Loading…
Reference in New Issue